drv_common.c 4.0 KB

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