board.c 7.7 KB

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