board.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. TIM_HandleTypeDef htim8;
  18. ADC_HandleTypeDef hadc1;
  19. SPI_HandleTypeDef hspi1;
  20. void SystemClock_Config(void)
  21. {
  22. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  23. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  24. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  25. //Initializes the CPU, AHB and APB busses clocks
  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. 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. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_ADC;
  49. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  50. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  51. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  52. {
  53. Error_Handler();
  54. }
  55. //Enables the Clock Security System
  56. HAL_RCC_EnableCSS();
  57. ///////////////////////////////////////////////////////////////////////////////////
  58. /*
  59. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  60. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  61. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  62. //Initializes the CPU, AHB and APB busses clocks
  63. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  64. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  65. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  66. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  67. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  68. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  69. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  70. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  71. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  72. {
  73. Error_Handler();
  74. }
  75. //Initializes the CPU, AHB and APB busses clocks
  76. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  77. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  78. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  79. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  80. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  81. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  82. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  83. {
  84. Error_Handler();
  85. }
  86. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_ADC;
  87. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  88. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  89. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  90. {
  91. Error_Handler();
  92. }
  93. //Enables the Clock Security System
  94. HAL_RCC_EnableCSS();
  95. */
  96. }
  97. /**
  98. * @brief TIM_Base MSP Initialization
  99. * This function configures the hardware resources used in this example
  100. * @param htim_base: TIM_Base handle pointer
  101. * @retval None
  102. */
  103. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
  104. {
  105. if(htim_base->Instance==TIM3)
  106. {
  107. /* USER CODE BEGIN TIM3_MspInit 0 */
  108. /* USER CODE END TIM3_MspInit 0 */
  109. /* Peripheral clock enable */
  110. __HAL_RCC_TIM3_CLK_ENABLE();
  111. /* USER CODE BEGIN TIM3_MspInit 1 */
  112. /* USER CODE END TIM3_MspInit 1 */
  113. }
  114. else if (htim_base->Instance==TIM8)
  115. {
  116. __HAL_RCC_TIM8_CLK_ENABLE();
  117. }
  118. }
  119. void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
  120. {
  121. GPIO_InitTypeDef GPIO_InitStruct = {0};
  122. if(htim->Instance==TIM3)
  123. {
  124. /* USER CODE BEGIN TIM3_MspPostInit 0 */
  125. /* USER CODE END TIM3_MspPostInit 0 */
  126. __HAL_RCC_GPIOB_CLK_ENABLE();
  127. /**TIM3 GPIO Configuration
  128. PB0 ------> TIM3_CH3
  129. PB1 ------> TIM3_CH4
  130. PB4 ------> TIM3_CH1
  131. */
  132. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4;
  133. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  134. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  135. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  136. __HAL_AFIO_REMAP_TIM3_PARTIAL();
  137. /* USER CODE BEGIN TIM3_MspPostInit 1 */
  138. /* USER CODE END TIM3_MspPostInit 1 */
  139. }
  140. else if(htim->Instance==TIM8)
  141. {
  142. /* USER CODE BEGIN TIM8_MspPostInit 0 */
  143. /* USER CODE END TIM8_MspPostInit 0 */
  144. __HAL_RCC_GPIOC_CLK_ENABLE();
  145. /**TIM8 GPIO Configuration
  146. PC7 ------> TIM8_CH2
  147. PC8 ------> TIM8_CH3
  148. */
  149. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8;
  150. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  151. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  152. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  153. /* USER CODE BEGIN TIM8_MspPostInit 1 */
  154. /* USER CODE END TIM8_MspPostInit 1 */
  155. }
  156. }
  157. /**
  158. * @brief TIM3 Initialization Function
  159. * @param None
  160. * @retval None
  161. */
  162. static void MX_TIM3_Init(void)
  163. {
  164. /* USER CODE BEGIN TIM3_Init 0 */
  165. HAL_TIM_Base_MspInit(&htim3);
  166. /* USER CODE END TIM3_Init 0 */
  167. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  168. TIM_MasterConfigTypeDef sMasterConfig = {0};
  169. TIM_OC_InitTypeDef sConfigOC = {0};
  170. /* USER CODE BEGIN TIM3_Init 1 */
  171. /* USER CODE END TIM3_Init 1 */
  172. htim3.Instance = TIM3;
  173. htim3.Init.Prescaler = 72;
  174. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  175. htim3.Init.Period = 1000;
  176. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  177. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  178. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  179. {
  180. Error_Handler();
  181. }
  182. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  183. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  184. {
  185. Error_Handler();
  186. }
  187. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  188. {
  189. Error_Handler();
  190. }
  191. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  192. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  193. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  194. {
  195. Error_Handler();
  196. }
  197. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  198. sConfigOC.Pulse = 100;
  199. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  200. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  201. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  202. {
  203. Error_Handler();
  204. }
  205. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  206. {
  207. Error_Handler();
  208. }
  209. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  210. {
  211. Error_Handler();
  212. }
  213. /* USER CODE BEGIN TIM3_Init 2 */
  214. /* USER CODE END TIM3_Init 2 */
  215. HAL_TIM_MspPostInit(&htim3);
  216. }
  217. /**
  218. * @brief TIM8 Initialization Function
  219. * @param None
  220. * @retval None
  221. */
  222. static void MX_TIM8_Init(void)
  223. {
  224. /* USER CODE BEGIN TIM8_Init 0 */
  225. /* USER CODE END TIM8_Init 0 */
  226. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  227. TIM_MasterConfigTypeDef sMasterConfig = {0};
  228. TIM_OC_InitTypeDef sConfigOC = {0};
  229. TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
  230. /* USER CODE BEGIN TIM8_Init 1 */
  231. /* USER CODE END TIM8_Init 1 */
  232. htim8.Instance = TIM8;
  233. htim8.Init.Prescaler = 72;
  234. htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
  235. htim8.Init.Period = 1000;
  236. htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  237. htim8.Init.RepetitionCounter = 0;
  238. htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  239. if (HAL_TIM_Base_Init(&htim8) != HAL_OK)
  240. {
  241. Error_Handler();
  242. }
  243. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  244. if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)
  245. {
  246. Error_Handler();
  247. }
  248. if (HAL_TIM_PWM_Init(&htim8) != HAL_OK)
  249. {
  250. Error_Handler();
  251. }
  252. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  253. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  254. if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK)
  255. {
  256. Error_Handler();
  257. }
  258. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  259. sConfigOC.Pulse = 100;
  260. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  261. sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  262. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  263. sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  264. sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  265. if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  266. {
  267. Error_Handler();
  268. }
  269. if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  270. {
  271. Error_Handler();
  272. }
  273. sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  274. sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  275. sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  276. sBreakDeadTimeConfig.DeadTime = 0;
  277. sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  278. sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  279. sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  280. if (HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfig) != HAL_OK)
  281. {
  282. Error_Handler();
  283. }
  284. /* USER CODE BEGIN TIM8_Init 2 */
  285. /* USER CODE END TIM8_Init 2 */
  286. HAL_TIM_MspPostInit(&htim8);
  287. }
  288. /**
  289. * @brief ADC MSP Initialization
  290. * This function configures the hardware resources used in this example
  291. * @param hadc: ADC handle pointer
  292. * @retval None
  293. */
  294. void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  295. {
  296. GPIO_InitTypeDef GPIO_InitStruct = {0};
  297. if(hadc->Instance==ADC1)
  298. {
  299. /* USER CODE BEGIN ADC1_MspInit 0 */
  300. /* USER CODE END ADC1_MspInit 0 */
  301. /* Peripheral clock enable */
  302. __HAL_RCC_ADC1_CLK_ENABLE();
  303. __HAL_RCC_GPIOC_CLK_ENABLE();
  304. __HAL_RCC_GPIOA_CLK_ENABLE();
  305. /**ADC1 GPIO Configuration
  306. PC0 ------> ADC1_IN10
  307. PA0-WKUP ------> ADC1_IN0
  308. PA1 ------> ADC1_IN1
  309. */
  310. GPIO_InitStruct.Pin = GPIO_PIN_0;
  311. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  312. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  313. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  314. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  315. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  316. /* USER CODE BEGIN ADC1_MspInit 1 */
  317. /* USER CODE END ADC1_MspInit 1 */
  318. }
  319. }
  320. /**
  321. * @brief SPI MSP Initialization
  322. * This function configures the hardware resources used in this example
  323. * @param hspi: SPI handle pointer
  324. * @retval None
  325. */
  326. void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
  327. {
  328. GPIO_InitTypeDef GPIO_InitStruct = {0};
  329. if(hspi->Instance==SPI1)
  330. {
  331. /* USER CODE BEGIN SPI1_MspInit 0 */
  332. /* USER CODE END SPI1_MspInit 0 */
  333. /* Peripheral clock enable */
  334. __HAL_RCC_SPI1_CLK_ENABLE();
  335. __HAL_RCC_GPIOA_CLK_ENABLE();
  336. /**SPI1 GPIO Configuration
  337. PA5 ------> SPI1_SCK
  338. PA6 ------> SPI1_MISO
  339. PA7 ------> SPI1_MOSI
  340. */
  341. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
  342. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  343. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  344. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  345. GPIO_InitStruct.Pin = GPIO_PIN_6;
  346. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  347. GPIO_InitStruct.Pull = GPIO_NOPULL;
  348. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  349. /* USER CODE BEGIN SPI1_MspInit 1 */
  350. /* USER CODE END SPI1_MspInit 1 */
  351. }
  352. }
  353. /*禁用JTAG*/
  354. static int REMAP_SWJ_NOJTAG(void)
  355. {
  356. __HAL_RCC_AFIO_CLK_ENABLE();
  357. /**NOJTAG: JTAG-DP Disabled and SW-DP Enabled
  358. */
  359. __HAL_AFIO_REMAP_SWJ_NOJTAG();
  360. return RT_EOK;
  361. }
  362. /*初始化ADC*/
  363. static int ADC1_INIT(void)
  364. {
  365. HAL_ADC_MspInit(&hadc1);
  366. return RT_EOK;
  367. }
  368. /*初始化SPI*/
  369. static int SPI1_INIT(void)
  370. {
  371. HAL_SPI_MspInit(&hspi1);
  372. return RT_EOK;
  373. }
  374. /*初始化定时器*/
  375. static int PWM3_INIT(void)
  376. {
  377. MX_TIM3_Init();
  378. MX_TIM8_Init();
  379. return RT_EOK;
  380. }
  381. /**
  382. * Function ota_app_vtor_reconfig
  383. * Description Set Vector Table base location to the start addr of app(RT_APP_PART_ADDR).
  384. */
  385. static int ota_app_vtor_reconfig(void)
  386. {
  387. #define NVIC_VTOR_MASK 0x3FFFFF80
  388. /* Set the Vector Table base location by user application firmware definition */
  389. SCB->VTOR = RT_APP_PART_ADDR & NVIC_VTOR_MASK;
  390. return 0;
  391. }
  392. INIT_BOARD_EXPORT(ota_app_vtor_reconfig);
  393. INIT_BOARD_EXPORT(REMAP_SWJ_NOJTAG);
  394. INIT_BOARD_EXPORT(ADC1_INIT);
  395. INIT_BOARD_EXPORT(SPI1_INIT);
  396. INIT_BOARD_EXPORT(PWM3_INIT);