board.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2009-01-05 Bernard first implementation
  10. * 2017-10-20 ZYH emmm...setup for HAL Libraries
  11. * 2017-11-15 ZYH update to 3.0.0
  12. */
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #include "board.h"
  16. /**
  17. * @addtogroup STM32
  18. */
  19. /*@{*/
  20. void HAL_MspInit(void)
  21. {
  22. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
  23. /* System interrupt init*/
  24. __HAL_RCC_AFIO_CLK_ENABLE();
  25. /* MemoryManagement_IRQn interrupt configuration */
  26. HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0);
  27. /* BusFault_IRQn interrupt configuration */
  28. HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0);
  29. /* UsageFault_IRQn interrupt configuration */
  30. HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0);
  31. /* SVCall_IRQn interrupt configuration */
  32. HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0);
  33. /* DebugMonitor_IRQn interrupt configuration */
  34. HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0);
  35. /* PendSV_IRQn interrupt configuration */
  36. HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
  37. /* SysTick_IRQn interrupt configuration */
  38. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  39. /**DISABLE: JTAG-DP Disabled and SW-DP Disabled**/
  40. __HAL_AFIO_REMAP_SWJ_NOJTAG();
  41. }
  42. void SystemClock_Config(void)
  43. {
  44. rt_err_t ret = RT_EOK;
  45. RCC_OscInitTypeDef RCC_OscInitStruct;
  46. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  47. #if !defined(RT_USING_HSI)
  48. /* Initializes the CPU, AHB and APB busses clocks */
  49. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  50. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  51. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  52. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  53. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  54. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  55. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  56. ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  57. if(ret == HAL_TIMEOUT)
  58. {
  59. /* HSE timeout switch to HSI */
  60. rt_memset(&RCC_OscInitStruct, 0, sizeof(RCC_OscInitStruct));
  61. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  62. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  63. RCC_OscInitStruct.HSICalibrationValue = 16;
  64. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  65. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  66. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  67. ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  68. RT_ASSERT(ret == HAL_OK);
  69. }
  70. /* Initializes the CPU, AHB and APB busses clocks */
  71. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
  72. RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  73. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  74. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  75. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  76. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  77. ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  78. RT_ASSERT(ret == HAL_OK);
  79. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  80. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  81. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  82. #else
  83. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  84. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  85. RCC_OscInitStruct.HSICalibrationValue = 16;
  86. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  87. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  88. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  89. ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  90. RT_ASSERT(ret == HAL_OK);
  91. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
  92. RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  93. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  94. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  95. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  96. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  97. ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
  98. RT_ASSERT(ret == HAL_OK);
  99. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/RT_TICK_PER_SECOND);
  100. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  101. HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
  102. #endif
  103. }
  104. static void m3_m4_delay_us(rt_uint32_t us)
  105. {
  106. __IO uint32_t Delay = us * (SystemCoreClock / 8U / 1000000U);
  107. do
  108. {
  109. __NOP();
  110. }
  111. while (Delay --);
  112. }
  113. void HAL_Delay(__IO uint32_t Delay)
  114. {
  115. m3_m4_delay_us(Delay * 10);
  116. }
  117. extern __IO uint32_t uwTick;
  118. uint32_t HAL_GetTick(void)
  119. {
  120. HAL_Delay(1);
  121. uwTick ++;
  122. return uwTick;
  123. }
  124. void HAL_SuspendTick(void)
  125. {
  126. /* we should not suspend tick */
  127. }
  128. void HAL_ResumeTick(void)
  129. {
  130. /* we should not resume tick */
  131. }
  132. /**
  133. * This is the timer interrupt service routine.
  134. *
  135. */
  136. void SysTick_Handler(void)
  137. {
  138. /* enter interrupt */
  139. rt_interrupt_enter();
  140. HAL_IncTick();
  141. rt_tick_increase();
  142. /* leave interrupt */
  143. rt_interrupt_leave();
  144. }
  145. /**
  146. * This function will initial STM32 board.
  147. */
  148. void rt_hw_board_init(void)
  149. {
  150. HAL_Init();
  151. SystemClock_Config();
  152. #ifdef RT_USING_HEAP
  153. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  154. #endif
  155. #ifdef RT_USING_COMPONENTS_INIT
  156. rt_components_board_init();
  157. #endif
  158. #ifdef RT_USING_CONSOLE
  159. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  160. #endif
  161. }
  162. /**
  163. * This function will delay for some us.
  164. *
  165. * @param us the delay time of us
  166. */
  167. void rt_hw_us_delay(rt_uint32_t us)
  168. {
  169. rt_uint32_t delta;
  170. us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND));
  171. delta = SysTick->VAL;
  172. if (delta < us)
  173. {
  174. /* wait current OSTick left time gone */
  175. while (SysTick->VAL < us);
  176. us -= delta;
  177. delta = SysTick->LOAD;
  178. }
  179. while (delta - SysTick->VAL < us);
  180. }