drv_common.c 4.1 KB

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