board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. }
  115. void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
  116. {
  117. GPIO_InitTypeDef GPIO_InitStruct = {0};
  118. if(htim->Instance==TIM3)
  119. {
  120. /* USER CODE BEGIN TIM3_MspPostInit 0 */
  121. /* USER CODE END TIM3_MspPostInit 0 */
  122. __HAL_RCC_GPIOB_CLK_ENABLE();
  123. __HAL_RCC_GPIOC_CLK_ENABLE();
  124. /**TIM3 GPIO Configuration
  125. PC6 ------> TIM3_CH1
  126. PC7 ------> TIM3_CH2
  127. PC8 ------> TIM3_CH3
  128. */
  129. /**TIM3 GPIO Configuration
  130. PB0 ------> TIM3_CH3
  131. PB1 ------> TIM3_CH4
  132. PB4/PC6 ------> TIM3_CH1
  133. */
  134. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  135. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  136. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  137. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  138. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8;
  139. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  140. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  141. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  142. //吸虫塔屏蔽下面这行,孢子仪打开
  143. __HAL_AFIO_REMAP_TIM3_ENABLE();
  144. /* USER CODE BEGIN TIM3_MspPostInit 1 */
  145. /* USER CODE END TIM3_MspPostInit 1 */
  146. }
  147. }
  148. /**
  149. * @brief TIM3 Initialization Function
  150. * @param None
  151. * @retval None
  152. */
  153. static void MX_TIM3_Init(void)
  154. {
  155. /* USER CODE BEGIN TIM3_Init 0 */
  156. HAL_TIM_Base_MspInit(&htim3);
  157. /* USER CODE END TIM3_Init 0 */
  158. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  159. TIM_MasterConfigTypeDef sMasterConfig = {0};
  160. TIM_OC_InitTypeDef sConfigOC = {0};
  161. /* USER CODE BEGIN TIM3_Init 1 */
  162. /* USER CODE END TIM3_Init 1 */
  163. htim3.Instance = TIM3;
  164. htim3.Init.Prescaler = 72;
  165. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  166. htim3.Init.Period = 1000;
  167. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  168. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  169. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  170. {
  171. Error_Handler();
  172. }
  173. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  174. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  175. {
  176. Error_Handler();
  177. }
  178. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  179. {
  180. Error_Handler();
  181. }
  182. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  183. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  184. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  185. {
  186. Error_Handler();
  187. }
  188. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  189. sConfigOC.Pulse = 100;
  190. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  191. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  192. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  193. {
  194. Error_Handler();
  195. }
  196. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  197. {
  198. Error_Handler();
  199. }
  200. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  201. {
  202. Error_Handler();
  203. }
  204. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  205. {
  206. Error_Handler();
  207. }
  208. /* USER CODE BEGIN TIM3_Init 2 */
  209. /* USER CODE END TIM3_Init 2 */
  210. HAL_TIM_MspPostInit(&htim3);
  211. }
  212. /**
  213. * @brief TIM8 Initialization Function
  214. * @param None
  215. * @retval None
  216. */
  217. static void MX_TIM8_Init(void)
  218. {
  219. }
  220. /**
  221. * @brief ADC MSP Initialization
  222. * This function configures the hardware resources used in this example
  223. * @param hadc: ADC handle pointer
  224. * @retval None
  225. */
  226. void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  227. {
  228. GPIO_InitTypeDef GPIO_InitStruct = {0};
  229. if(hadc->Instance==ADC1)
  230. {
  231. /* USER CODE BEGIN ADC1_MspInit 0 */
  232. /* USER CODE END ADC1_MspInit 0 */
  233. /* Peripheral clock enable */
  234. __HAL_RCC_ADC1_CLK_ENABLE();
  235. __HAL_RCC_GPIOC_CLK_ENABLE();
  236. __HAL_RCC_GPIOA_CLK_ENABLE();
  237. /**ADC1 GPIO Configuration
  238. PC0 ------> ADC1_IN10
  239. PA0-WKUP ------> ADC1_IN0
  240. PA1 ------> ADC1_IN1
  241. */
  242. GPIO_InitStruct.Pin = GPIO_PIN_0;
  243. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  244. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  245. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  246. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  247. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  248. /* USER CODE BEGIN ADC1_MspInit 1 */
  249. /* USER CODE END ADC1_MspInit 1 */
  250. }
  251. }
  252. /**
  253. * @brief SPI MSP Initialization
  254. * This function configures the hardware resources used in this example
  255. * @param hspi: SPI handle pointer
  256. * @retval None
  257. */
  258. void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
  259. {
  260. GPIO_InitTypeDef GPIO_InitStruct = {0};
  261. if(hspi->Instance==SPI1)
  262. {
  263. /* USER CODE BEGIN SPI1_MspInit 0 */
  264. /* USER CODE END SPI1_MspInit 0 */
  265. /* Peripheral clock enable */
  266. __HAL_RCC_SPI1_CLK_ENABLE();
  267. __HAL_RCC_GPIOA_CLK_ENABLE();
  268. /**SPI1 GPIO Configuration
  269. PA5 ------> SPI1_SCK
  270. PA6 ------> SPI1_MISO
  271. PA7 ------> SPI1_MOSI
  272. */
  273. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
  274. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  275. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  276. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  277. GPIO_InitStruct.Pin = GPIO_PIN_6;
  278. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  279. GPIO_InitStruct.Pull = GPIO_NOPULL;
  280. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  281. /* USER CODE BEGIN SPI1_MspInit 1 */
  282. /* USER CODE END SPI1_MspInit 1 */
  283. }
  284. }
  285. /*禁用JTAG*/
  286. static int REMAP_SWJ_NOJTAG(void)
  287. {
  288. __HAL_RCC_AFIO_CLK_ENABLE();
  289. /**NOJTAG: JTAG-DP Disabled and SW-DP Enabled
  290. */
  291. __HAL_AFIO_REMAP_SWJ_NOJTAG();
  292. return RT_EOK;
  293. }
  294. /*初始化ADC*/
  295. static int ADC1_INIT(void)
  296. {
  297. HAL_ADC_MspInit(&hadc1);
  298. return RT_EOK;
  299. }
  300. /*初始化SPI*/
  301. static int SPI1_INIT(void)
  302. {
  303. HAL_SPI_MspInit(&hspi1);
  304. return RT_EOK;
  305. }
  306. /*初始化定时器*/
  307. static int PWM3_INIT(void)
  308. {
  309. MX_TIM3_Init();
  310. MX_TIM8_Init();
  311. return RT_EOK;
  312. }
  313. /**
  314. * Function ota_app_vtor_reconfig
  315. * Description Set Vector Table base location to the start addr of app(RT_APP_PART_ADDR).
  316. */
  317. static int ota_app_vtor_reconfig(void)
  318. {
  319. #define NVIC_VTOR_MASK 0x3FFFFF80
  320. /* Set the Vector Table base location by user application firmware definition */
  321. SCB->VTOR = RT_APP_PART_ADDR & NVIC_VTOR_MASK;
  322. return 0;
  323. }
  324. INIT_BOARD_EXPORT(ota_app_vtor_reconfig);
  325. INIT_BOARD_EXPORT(REMAP_SWJ_NOJTAG);
  326. INIT_BOARD_EXPORT(ADC1_INIT);
  327. INIT_BOARD_EXPORT(SPI1_INIT);
  328. INIT_BOARD_EXPORT(PWM3_INIT);