drv_common.c 4.2 KB

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