board.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-10-26 ChenYong first version
  9. * 2020-01-08 xiangxistu add HSI configuration
  10. */
  11. #include <board.h>
  12. #include <rtthread.h>
  13. #define DBG_TAG "board"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. TIM_HandleTypeDef htim3;
  17. ADC_HandleTypeDef hadc1;
  18. SPI_HandleTypeDef hspi1;
  19. void SystemClock_Config(void)
  20. {
  21. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  22. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  23. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  24. /**Initializes the CPU, AHB and APB busses clocks
  25. */
  26. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  27. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  28. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  29. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  30. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  31. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  32. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  33. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  34. {
  35. Error_Handler();
  36. }
  37. /**Initializes the CPU, AHB and APB busses clocks
  38. */
  39. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  40. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  41. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  42. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  43. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  44. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  45. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  46. {
  47. Error_Handler();
  48. }
  49. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_ADC;
  50. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  51. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  52. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  53. {
  54. Error_Handler();
  55. }
  56. /**Enables the Clock Security System
  57. */
  58. HAL_RCC_EnableCSS();
  59. }
  60. /**
  61. * @brief TIM_Base MSP Initialization
  62. * This function configures the hardware resources used in this example
  63. * @param htim_base: TIM_Base handle pointer
  64. * @retval None
  65. */
  66. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
  67. {
  68. if(htim_base->Instance==TIM3)
  69. {
  70. /* USER CODE BEGIN TIM3_MspInit 0 */
  71. /* USER CODE END TIM3_MspInit 0 */
  72. /* Peripheral clock enable */
  73. __HAL_RCC_TIM3_CLK_ENABLE();
  74. /* USER CODE BEGIN TIM3_MspInit 1 */
  75. /* USER CODE END TIM3_MspInit 1 */
  76. }
  77. }
  78. void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
  79. {
  80. GPIO_InitTypeDef GPIO_InitStruct = {0};
  81. if(htim->Instance==TIM3)
  82. {
  83. /* USER CODE BEGIN TIM3_MspPostInit 0 */
  84. /* USER CODE END TIM3_MspPostInit 0 */
  85. __HAL_RCC_GPIOC_CLK_ENABLE();
  86. /**TIM3 GPIO Configuration
  87. PC6 ------> TIM3_CH1
  88. */
  89. GPIO_InitStruct.Pin = GPIO_PIN_6;
  90. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  91. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  92. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  93. __HAL_AFIO_REMAP_TIM3_ENABLE();
  94. /* USER CODE BEGIN TIM3_MspPostInit 1 */
  95. /* USER CODE END TIM3_MspPostInit 1 */
  96. }
  97. }
  98. static void MX_TIM3_Init(void)
  99. {
  100. /* USER CODE BEGIN TIM3_Init 0 */
  101. /* USER CODE END TIM3_Init 0 */
  102. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  103. TIM_MasterConfigTypeDef sMasterConfig = {0};
  104. TIM_OC_InitTypeDef sConfigOC = {0};
  105. /* USER CODE BEGIN TIM3_Init 1 */
  106. /* USER CODE END TIM3_Init 1 */
  107. htim3.Instance = TIM3;
  108. htim3.Init.Prescaler = 72-1;
  109. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  110. htim3.Init.Period = 500-1;
  111. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  112. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  113. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  114. {
  115. Error_Handler();
  116. }
  117. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  118. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  119. {
  120. Error_Handler();
  121. }
  122. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  123. {
  124. Error_Handler();
  125. }
  126. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  127. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  128. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  129. {
  130. Error_Handler();
  131. }
  132. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  133. sConfigOC.Pulse = 0;
  134. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  135. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  136. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  137. {
  138. Error_Handler();
  139. }
  140. /* USER CODE BEGIN TIM3_Init 2 */
  141. /* USER CODE END TIM3_Init 2 */
  142. HAL_TIM_MspPostInit(&htim3);
  143. }
  144. /**
  145. * @brief ADC MSP Initialization
  146. * This function configures the hardware resources used in this example
  147. * @param hadc: ADC handle pointer
  148. * @retval None
  149. */
  150. void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  151. {
  152. GPIO_InitTypeDef GPIO_InitStruct = {0};
  153. if(hadc->Instance==ADC1)
  154. {
  155. /* USER CODE BEGIN ADC1_MspInit 0 */
  156. /* USER CODE END ADC1_MspInit 0 */
  157. /* Peripheral clock enable */
  158. __HAL_RCC_ADC1_CLK_ENABLE();
  159. __HAL_RCC_GPIOC_CLK_ENABLE();
  160. __HAL_RCC_GPIOA_CLK_ENABLE();
  161. /**ADC1 GPIO Configuration
  162. PC0 ------> ADC1_IN10
  163. PA0-WKUP ------> ADC1_IN0
  164. PA1 ------> ADC1_IN1
  165. */
  166. GPIO_InitStruct.Pin = GPIO_PIN_0;
  167. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  168. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  169. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  170. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  171. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  172. /* USER CODE BEGIN ADC1_MspInit 1 */
  173. /* USER CODE END ADC1_MspInit 1 */
  174. }
  175. }
  176. /**
  177. * @brief SPI MSP Initialization
  178. * This function configures the hardware resources used in this example
  179. * @param hspi: SPI handle pointer
  180. * @retval None
  181. */
  182. void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
  183. {
  184. GPIO_InitTypeDef GPIO_InitStruct = {0};
  185. if(hspi->Instance==SPI1)
  186. {
  187. /* USER CODE BEGIN SPI1_MspInit 0 */
  188. /* USER CODE END SPI1_MspInit 0 */
  189. /* Peripheral clock enable */
  190. __HAL_RCC_SPI1_CLK_ENABLE();
  191. __HAL_RCC_GPIOA_CLK_ENABLE();
  192. /**SPI1 GPIO Configuration
  193. PA5 ------> SPI1_SCK
  194. PA6 ------> SPI1_MISO
  195. PA7 ------> SPI1_MOSI
  196. */
  197. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
  198. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  199. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  200. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  201. GPIO_InitStruct.Pin = GPIO_PIN_6;
  202. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  203. GPIO_InitStruct.Pull = GPIO_NOPULL;
  204. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  205. /* USER CODE BEGIN SPI1_MspInit 1 */
  206. /* USER CODE END SPI1_MspInit 1 */
  207. }
  208. }
  209. /*禁用JTAG*/
  210. static int REMAP_SWJ_NOJTAG(void)
  211. {
  212. __HAL_RCC_AFIO_CLK_ENABLE();
  213. /**NOJTAG: JTAG-DP Disabled and SW-DP Enabled
  214. */
  215. __HAL_AFIO_REMAP_SWJ_NOJTAG();
  216. return RT_EOK;
  217. }
  218. /*初始化ADC*/
  219. static int ADC1_INIT(void)
  220. {
  221. HAL_ADC_MspInit(&hadc1);
  222. return RT_EOK;
  223. }
  224. /*初始化SPI*/
  225. static int SPI1_INIT(void)
  226. {
  227. HAL_SPI_MspInit(&hspi1);
  228. return RT_EOK;
  229. }
  230. /*初始化定时器*/
  231. static int PWM3_INIT(void)
  232. {
  233. MX_TIM3_Init();
  234. return RT_EOK;
  235. }
  236. /**
  237. * Function ota_app_vtor_reconfig
  238. * Description Set Vector Table base location to the start addr of app(RT_APP_PART_ADDR).
  239. */
  240. static int ota_app_vtor_reconfig(void)
  241. {
  242. #define NVIC_VTOR_MASK 0x3FFFFF80
  243. /* Set the Vector Table base location by user application firmware definition */
  244. SCB->VTOR = RT_APP_PART_ADDR & NVIC_VTOR_MASK;
  245. return 0;
  246. }
  247. INIT_BOARD_EXPORT(ota_app_vtor_reconfig);
  248. INIT_BOARD_EXPORT(REMAP_SWJ_NOJTAG);
  249. INIT_BOARD_EXPORT(ADC1_INIT);
  250. INIT_BOARD_EXPORT(SPI1_INIT);
  251. INIT_BOARD_EXPORT(PWM3_INIT);