drv_common.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-7 SummerGift first version
  9. */
  10. #include "drv_common.h"
  11. #include "board.h"
  12. /* HAL_Delay 的"tick 不动"兜底阈值: 连续这么多次循环 tick 都没变化,
  13. 就认为系统 tick 还没启动, 直接返回而不是死等。 */
  14. #define HAL_DELAY_NO_TICK_GUARD 1000000u
  15. #ifdef RT_USING_FINSH
  16. #include <finsh.h>
  17. static void reboot(uint8_t argc, char **argv)
  18. {
  19. rt_hw_cpu_reset();
  20. }
  21. FINSH_FUNCTION_EXPORT_ALIAS(reboot, __cmd_reboot, Reboot System);
  22. MSH_CMD_EXPORT_ALIAS(reboot, reboot, Reboot System);
  23. #endif /* RT_USING_FINSH */
  24. /* SysTick configuration */
  25. void rt_hw_systick_init(void)
  26. {
  27. #if defined (SOC_SERIES_STM32H7)
  28. HAL_SYSTICK_Config((HAL_RCCEx_GetD1SysClockFreq()) / RT_TICK_PER_SECOND);
  29. #else
  30. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  31. #endif
  32. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  33. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  34. }
  35. /**
  36. * This is the timer interrupt service routine.
  37. *
  38. */
  39. void SysTick_Handler(void)
  40. {
  41. /* enter interrupt */
  42. rt_interrupt_enter();
  43. HAL_IncTick();
  44. rt_tick_increase();
  45. /* leave interrupt */
  46. rt_interrupt_leave();
  47. }
  48. uint32_t HAL_GetTick(void)
  49. {
  50. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  51. }
  52. void HAL_SuspendTick(void)
  53. {
  54. }
  55. void HAL_ResumeTick(void)
  56. {
  57. }
  58. /* P3-6: 原来这里是空实现, HAL 内部所有依赖 HAL_Delay 的上电/复位时序被静默跳过
  59. (例如 HAL_RCC_OscConfig 的 LSE 起振等待、FLASH 编程等待等)。
  60. ⚠️ 不能简单写成 rt_thread_mdelay():
  61. ① 本文件开头的 hw_board_init() 在**调度器启动之前**就调用了 HAL_Init(),
  62. 此时 rt_thread_mdelay 会永久挂起 -> 整机起不来;
  63. ② 某些 HAL 内部会在中断上下文调用它。
  64. HAL_GetTick() 本文件已重定义为 rt_tick_get()*1000/RT_TICK_PER_SECOND, 用 tick
  65. 差值忙等对两种上下文都安全。
  66. ⚠️ 但 rt_hw_systick_init() 之前 rt_tick 根本不涨(恒为 0), 此时忙等会死循环;
  67. 所以额外做一个"tick 不动就放弃"的兜底 —— 宁可延时不精确, 也不能卡死在启动阶段。 */
  68. void HAL_Delay(__IO uint32_t Delay)
  69. {
  70. uint32_t start = HAL_GetTick();
  71. uint32_t last = start;
  72. uint32_t guard = 0;
  73. while ((HAL_GetTick() - start) < Delay)
  74. {
  75. uint32_t now = HAL_GetTick();
  76. if (now != last)
  77. {
  78. last = now;
  79. guard = 0; /* tick 在走, 正常计时 */
  80. }
  81. else if (++guard > HAL_DELAY_NO_TICK_GUARD)
  82. {
  83. break; /* tick 根本没在跑(启动早期), 不再死等 */
  84. }
  85. }
  86. }
  87. /* re-implement tick interface for STM32 HAL */
  88. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  89. {
  90. /* Return function status */
  91. return HAL_OK;
  92. }
  93. /**
  94. * @brief This function is executed in case of error occurrence.
  95. * @param None
  96. * @retval None
  97. */
  98. void _Error_Handler(char *s, int num)
  99. {
  100. /* USER CODE BEGIN Error_Handler */
  101. /* User can add his own implementation to report the HAL error return state */
  102. while(1)
  103. {
  104. }
  105. /* USER CODE END Error_Handler */
  106. }
  107. /**
  108. * This function will delay for some us.
  109. *
  110. * @param us the delay time of us
  111. */
  112. void rt_hw_us_delay(rt_uint32_t us)
  113. {
  114. rt_uint32_t start, now, delta, reload, us_tick;
  115. reload = SysTick->LOAD;
  116. us_tick = SystemCoreClock / 1000000UL;
  117. /* ★ 逐拍累计(2026-10-15 同步 master 修复):
  118. 原实现 `while(delta < us_tick * us)` 的目标值在 us >= reload/us_tick
  119. (本机约 1000us) 时超过 SysTick 装载值 —— delta 最大值就是 reload,
  120. 永远达不到目标 -> 死循环。改为"每拍最多等 reload/us_tick 微秒、
  121. 凑满再取新基准", 任意 us 都能正确返回。 */
  122. while (us > 0)
  123. {
  124. rt_uint32_t step = reload / us_tick; /* 一个 SysTick 周期覆盖的最大微秒数 */
  125. if (step == 0) step = 1;
  126. if (us < step) step = us;
  127. start = SysTick->VAL;
  128. do {
  129. now = SysTick->VAL;
  130. delta = start > now ? start - now : reload + start - now;
  131. } while(delta < us_tick * step);
  132. us -= step;
  133. }
  134. }
  135. /**
  136. * This function will initial STM32 board.
  137. */
  138. void hw_board_init(char *clock_src, int32_t clock_src_freq, int32_t clock_target_freq)
  139. {
  140. extern void rt_hw_systick_init(void);
  141. extern void clk_init(char *clk_source, int source_freq, int target_freq);
  142. #ifdef SCB_EnableICache
  143. /* Enable I-Cache---------------------------------------------------------*/
  144. SCB_EnableICache();
  145. #endif
  146. #ifdef SCB_EnableDCache
  147. /* Enable D-Cache---------------------------------------------------------*/
  148. SCB_EnableDCache();
  149. #endif
  150. /* HAL_Init() function is called at the beginning of the program */
  151. HAL_Init();
  152. /* enable interrupt */
  153. __set_PRIMASK(0);
  154. /* System clock initialization */
  155. clk_init(clock_src, clock_src_freq, clock_target_freq);
  156. /* disbale interrupt */
  157. __set_PRIMASK(1);
  158. rt_hw_systick_init();
  159. /* Pin driver initialization is open by default */
  160. #ifdef RT_USING_PIN
  161. extern int rt_hw_pin_init(void);
  162. rt_hw_pin_init();
  163. #endif
  164. /* USART driver initialization is open by default */
  165. #ifdef RT_USING_SERIAL
  166. extern int rt_hw_usart_init(void);
  167. rt_hw_usart_init();
  168. #endif
  169. }