board.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-09-22 Bernard add board.h to this bsp
  13. * 2017-12-29 ZYH Correctly generate the 48M clock
  14. */
  15. #include <rtthread.h>
  16. #include "board.h"
  17. /**
  18. * @addtogroup STM32
  19. */
  20. /*@{*/
  21. #ifdef RT_USING_HSI
  22. #error Can not using HSI on this bsp
  23. #endif
  24. #if defined(RCC_PERIPHCLK_SDIO) || defined(RCC_PERIPHCLK_CEC) || defined(RCC_PERIPHCLK_LTDC)\
  25. || defined(RCC_PERIPHCLK_SPDIFRX) || defined(RCC_PERIPHCLK_FMPI2C1) || defined(RCC_PERIPHCLK_LPTIM1)
  26. #warning Please give priority to the correctness of the clock tree when the peripherals are abnormal
  27. #endif
  28. static void SystemClock_Config(void)
  29. {
  30. rt_uint32_t hse_clk, sys_clk;
  31. #if (RT_HSE_VALVE % 1000000 != 0)
  32. #error HSE must be integer of MHz
  33. #endif
  34. hse_clk = HSE_VALUE / 1000000UL;
  35. sys_clk = HCLK_VALUE / 1000000UL;
  36. RCC_OscInitTypeDef RCC_OscInitStruct;
  37. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  38. #if defined(RT_USING_RTC) || defined(RCC_PERIPHCLK_CLK48)
  39. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  40. #endif
  41. /**Configure the main internal regulator output voltage
  42. */
  43. __HAL_RCC_PWR_CLK_ENABLE();
  44. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  45. /**Initializes the CPU, AHB and APB busses clocks
  46. */
  47. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  48. #ifdef RT_USING_RTC
  49. RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_LSI;
  50. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  51. #endif
  52. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  53. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  54. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  55. if (hse_clk % 2 == 0)
  56. {
  57. RCC_OscInitStruct.PLL.PLLM = hse_clk / 2; //Get 2M clock
  58. if ((sys_clk * 2) % 48 == 0)
  59. {
  60. RCC_OscInitStruct.PLL.PLLN = sys_clk;//Get 2*HCLK_VALUE
  61. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;//Get HCLK_VALUE
  62. }
  63. else if ((sys_clk * 4) % 48 == 0)
  64. {
  65. RCC_OscInitStruct.PLL.PLLN = sys_clk * 2;//Get 4*HCLK_VALUE
  66. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;//Get HCLK_VALUE
  67. }
  68. else if ((sys_clk * 6) % 48 == 0)
  69. {
  70. RCC_OscInitStruct.PLL.PLLN = sys_clk * 3;//Get 6*HCLK_VALUE
  71. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV6;//Get HCLK_VALUE
  72. }
  73. else if ((sys_clk * 8) % 48 == 0)
  74. {
  75. RCC_OscInitStruct.PLL.PLLN = sys_clk * 4;//Get 8*HCLK_VALUE
  76. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV8;//Get HCLK_VALUE
  77. }
  78. }
  79. else
  80. {
  81. RCC_OscInitStruct.PLL.PLLM = hse_clk;//Get 1M clock
  82. if ((sys_clk * 2) % 48 == 0)
  83. {
  84. RCC_OscInitStruct.PLL.PLLN = sys_clk * 2;//Get 2*HCLK_VALUE
  85. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;//Get HCLK_VALUE
  86. }
  87. else if ((sys_clk * 4) % 48 == 0)
  88. {
  89. RCC_OscInitStruct.PLL.PLLN = sys_clk * 4;//Get 4*HCLK_VALUE
  90. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;//Get HCLK_VALUE
  91. }
  92. else if ((sys_clk * 6) % 48 == 0)
  93. {
  94. RCC_OscInitStruct.PLL.PLLN = sys_clk * 6;//Get 6*HCLK_VALUE
  95. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV6;//Get HCLK_VALUE
  96. }
  97. else if ((sys_clk * 8) % 48 == 0)
  98. {
  99. RCC_OscInitStruct.PLL.PLLN = sys_clk * 8;//Get 8*HCLK_VALUE
  100. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV8;//Get HCLK_VALUE
  101. }
  102. }
  103. RCC_OscInitStruct.PLL.PLLQ = hse_clk / RCC_OscInitStruct.PLL.PLLM * RCC_OscInitStruct.PLL.PLLN / 48; //Get 48M Clock
  104. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  105. {
  106. while (1)
  107. {}
  108. }
  109. /**Initializes the CPU, AHB and APB busses clocks
  110. */
  111. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  112. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  113. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  114. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  115. #if (RT_HSE_HCLK <= 42000000UL)
  116. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  117. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  118. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  119. {
  120. while (1)
  121. {}
  122. }
  123. #elif (RT_HSE_HCLK <= 84000000UL)
  124. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  125. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  126. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  127. {
  128. while (1)
  129. {}
  130. }
  131. #elif (RT_HSE_HCLK <= 168000000UL)
  132. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  133. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  134. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  135. {
  136. while (1)
  137. {}
  138. }
  139. #else
  140. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8;
  141. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
  142. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  143. {
  144. while (1)
  145. {}
  146. }
  147. #endif
  148. #if defined(RT_USING_RTC) || defined(RCC_PERIPHCLK_CLK48)
  149. PeriphClkInitStruct.PeriphClockSelection = 0;
  150. #ifdef RT_USING_RTC
  151. PeriphClkInitStruct.PeriphClockSelection |= RCC_PERIPHCLK_RTC;
  152. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  153. #endif
  154. #ifdef RCC_PERIPHCLK_CLK48
  155. PeriphClkInitStruct.PeriphClockSelection |= RCC_PERIPHCLK_CLK48;
  156. PeriphClkInitStruct.Clk48ClockSelection = RCC_CLK48CLKSOURCE_PLLQ;
  157. #endif
  158. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  159. {
  160. while (1)
  161. {}
  162. }
  163. #endif
  164. }
  165. /**
  166. * This is the timer interrupt service routine.
  167. *
  168. */
  169. void SysTick_Handler(void)
  170. {
  171. /* enter interrupt */
  172. rt_interrupt_enter();
  173. /* tick for HAL Library */
  174. HAL_IncTick();
  175. rt_tick_increase();
  176. /* leave interrupt */
  177. rt_interrupt_leave();
  178. }
  179. /* re-implementat tick interface for STM32 HAL */
  180. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  181. {
  182. /*Configure the SysTick to have interrupt in 1ms time basis*/
  183. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  184. /*Configure the SysTick IRQ priority */
  185. HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0);
  186. /* Return function status */
  187. return HAL_OK;
  188. }
  189. void HAL_Delay(__IO uint32_t Delay)
  190. {
  191. rt_thread_delay(Delay);
  192. }
  193. void HAL_SuspendTick(void)
  194. {
  195. /* we should not suspend tick */
  196. }
  197. void HAL_ResumeTick(void)
  198. {
  199. /* we should not resume tick */
  200. }
  201. void HAL_MspInit(void)
  202. {
  203. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  204. /* System interrupt init*/
  205. /* MemoryManagement_IRQn interrupt configuration */
  206. HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
  207. /* BusFault_IRQn interrupt configuration */
  208. HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
  209. /* UsageFault_IRQn interrupt configuration */
  210. HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
  211. /* SVCall_IRQn interrupt configuration */
  212. HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
  213. /* DebugMonitor_IRQn interrupt configuration */
  214. HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
  215. /* PendSV_IRQn interrupt configuration */
  216. HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
  217. /* SysTick_IRQn interrupt configuration */
  218. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  219. }
  220. /**
  221. * This function will initial STM32 board.
  222. */
  223. void rt_hw_board_init()
  224. {
  225. /* Configure the system clock @ 84 Mhz */
  226. SystemClock_Config();
  227. HAL_Init();
  228. #ifdef RT_USING_HEAP
  229. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  230. #endif
  231. #ifdef RT_USING_COMPONENTS_INIT
  232. rt_components_board_init();
  233. #endif
  234. #ifdef RT_USING_CONSOLE
  235. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  236. #endif
  237. }