1
0

drv_common.c 3.7 KB

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