drv_common.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2006-2023, 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. #ifdef RT_USING_PIN
  13. #include <drv_gpio.h>
  14. #endif
  15. #ifdef RT_USING_SERIAL
  16. #ifdef RT_USING_SERIAL_V2
  17. #include <drv_usart_v2.h>
  18. #else
  19. #include <drv_usart.h>
  20. #endif /* RT_USING_SERIAL */
  21. #endif /* RT_USING_SERIAL_V2 */
  22. #define DBG_TAG "drv_common"
  23. #define DBG_LVL DBG_INFO
  24. #include <rtdbg.h>
  25. #ifdef RT_USING_FINSH
  26. #include <finsh.h>
  27. static void reboot(uint8_t argc, char **argv)
  28. {
  29. rt_hw_cpu_reset();
  30. }
  31. MSH_CMD_EXPORT(reboot, Reboot System);
  32. #endif /* RT_USING_FINSH */
  33. extern __IO uint32_t uwTick;
  34. static uint32_t _systick_ms = 1;
  35. /* SysTick configuration */
  36. void rt_hw_systick_init(void)
  37. {
  38. // Updates the variable SystemCoreClock
  39. SystemCoreClockUpdate();
  40. HAL_SYSTICK_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  41. NVIC_SetPriority(SysTick_IRQn, 0xFF);
  42. _systick_ms = 1000u / RT_TICK_PER_SECOND;
  43. if (_systick_ms == 0)
  44. _systick_ms = 1;
  45. }
  46. /**
  47. * This is the timer interrupt service routine.
  48. *
  49. */
  50. void SysTick_Handler(void)
  51. {
  52. /* enter interrupt */
  53. rt_interrupt_enter();
  54. if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)
  55. HAL_IncTick();
  56. rt_tick_increase();
  57. /* leave interrupt */
  58. rt_interrupt_leave();
  59. }
  60. uint32_t HAL_GetTick(void)
  61. {
  62. if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)
  63. HAL_IncTick();
  64. return uwTick;
  65. }
  66. void HAL_IncTick(void)
  67. {
  68. uwTick += _systick_ms;
  69. }
  70. void HAL_SuspendTick(void)
  71. {
  72. }
  73. void HAL_ResumeTick(void)
  74. {
  75. }
  76. void HAL_Delay(__IO uint32_t Delay)
  77. {
  78. if (rt_thread_self())
  79. {
  80. rt_thread_mdelay(Delay);
  81. }
  82. else
  83. {
  84. for (rt_uint32_t count = 0; count < Delay; count++)
  85. {
  86. rt_hw_us_delay(1000);
  87. }
  88. }
  89. }
  90. /* re-implement tick interface for STM32 HAL */
  91. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  92. {
  93. rt_hw_systick_init();
  94. /* Return function status */
  95. return HAL_OK;
  96. }
  97. /**
  98. * @brief This function is executed in case of error occurrence.
  99. * @param None
  100. * @retval None
  101. */
  102. void _Error_Handler(char *s, int num)
  103. {
  104. /* USER CODE BEGIN Error_Handler */
  105. /* User can add his own implementation to report the HAL error return state */
  106. LOG_E("Error_Handler at file:%s num:%d", s, num);
  107. while (1)
  108. {
  109. }
  110. /* USER CODE END Error_Handler */
  111. }
  112. /**
  113. * This function will delay for some us.
  114. *
  115. * @param us the delay time of us
  116. */
  117. void rt_hw_us_delay(rt_uint32_t us)
  118. {
  119. rt_uint64_t ticks;
  120. rt_uint32_t told, tnow, tcnt = 0;
  121. rt_uint32_t reload = SysTick->LOAD;
  122. ticks = us * (reload / (1000000 / RT_TICK_PER_SECOND));
  123. told = SysTick->VAL;
  124. while (1)
  125. {
  126. tnow = SysTick->VAL;
  127. if (tnow != told)
  128. {
  129. if (tnow < told)
  130. {
  131. tcnt += told - tnow;
  132. }
  133. else
  134. {
  135. tcnt += reload - tnow + told;
  136. }
  137. told = tnow;
  138. if (tcnt >= ticks)
  139. {
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * This function will initial STM32 board.
  147. */
  148. rt_weak void rt_hw_board_init(void)
  149. {
  150. #ifdef BSP_SCB_ENABLE_I_CACHE
  151. /* Enable I-Cache---------------------------------------------------------*/
  152. SCB_EnableICache();
  153. #endif
  154. #ifdef BSP_SCB_ENABLE_D_CACHE
  155. /* Enable D-Cache---------------------------------------------------------*/
  156. SCB_EnableDCache();
  157. #endif
  158. /* HAL_Init() function is called at the beginning of the program */
  159. HAL_Init();
  160. /* System clock initialization */
  161. SystemClock_Config();
  162. #if defined(RT_USING_HEAP)
  163. /* Heap initialization */
  164. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  165. #endif
  166. #ifdef RT_USING_PIN
  167. rt_hw_pin_init();
  168. #endif
  169. #ifdef RT_USING_SERIAL
  170. rt_hw_usart_init();
  171. #endif
  172. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  173. /* Set the shell console output device */
  174. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  175. #endif
  176. #if defined(RT_USING_CONSOLE) && defined(RT_USING_NANO)
  177. extern void rt_hw_console_init(void);
  178. rt_hw_console_init();
  179. #endif
  180. #ifdef RT_USING_COMPONENTS_INIT
  181. /* Board underlying hardware initialization */
  182. rt_components_board_init();
  183. #endif
  184. }