drv_common.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2006-2018, 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. #ifdef RT_USING_SERIAL
  12. #include "drv_usart.h"
  13. #endif
  14. #ifdef RT_USING_FINSH
  15. #include <finsh.h>
  16. static void reboot(uint8_t argc, char **argv)
  17. {
  18. rt_hw_cpu_reset();
  19. }
  20. FINSH_FUNCTION_EXPORT_ALIAS(reboot, __cmd_reboot, Reboot System);
  21. #endif /* RT_USING_FINSH */
  22. /* SysTick configuration */
  23. void rt_hw_systick_init(void)
  24. {
  25. #if defined (SOC_SERIES_STM32H7)
  26. HAL_SYSTICK_Config((HAL_RCCEx_GetD1SysClockFreq()) / RT_TICK_PER_SECOND);
  27. #else
  28. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);
  29. #endif
  30. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  31. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  32. }
  33. /**
  34. * This is the timer interrupt service routine.
  35. *
  36. */
  37. void SysTick_Handler(void)
  38. {
  39. /* enter interrupt */
  40. rt_interrupt_enter();
  41. HAL_IncTick();
  42. rt_tick_increase();
  43. /* leave interrupt */
  44. rt_interrupt_leave();
  45. }
  46. uint32_t HAL_GetTick(void)
  47. {
  48. return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
  49. }
  50. void HAL_SuspendTick(void)
  51. {
  52. }
  53. void HAL_ResumeTick(void)
  54. {
  55. }
  56. void HAL_Delay(__IO uint32_t Delay)
  57. {
  58. }
  59. /* re-implement tick interface for STM32 HAL */
  60. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  61. {
  62. /* Return function status */
  63. return HAL_OK;
  64. }
  65. /**
  66. * @brief This function is executed in case of error occurrence.
  67. * @param None
  68. * @retval None
  69. */
  70. void _Error_Handler(char *s, int num)
  71. {
  72. /* USER CODE BEGIN Error_Handler */
  73. /* User can add his own implementation to report the HAL error return state */
  74. while(1)
  75. {
  76. }
  77. /* USER CODE END Error_Handler */
  78. }
  79. /**
  80. * This function will delay for some us.
  81. *
  82. * @param us the delay time of us
  83. */
  84. void rt_hw_us_delay(rt_uint32_t us)
  85. {
  86. rt_uint32_t start, now, delta, reload, us_tick;
  87. start = SysTick->VAL;
  88. reload = SysTick->LOAD;
  89. us_tick = SystemCoreClock / 1000000UL;
  90. do {
  91. now = SysTick->VAL;
  92. delta = start > now ? start - now : reload + start - now;
  93. } while(delta < us_tick * us);
  94. }
  95. /**
  96. * This function will initial STM32 board.
  97. */
  98. RT_WEAK void rt_hw_board_init()
  99. {
  100. #ifdef SCB_EnableICache
  101. /* Enable I-Cache---------------------------------------------------------*/
  102. SCB_EnableICache();
  103. #endif
  104. #ifdef SCB_EnableDCache
  105. /* Enable D-Cache---------------------------------------------------------*/
  106. SCB_EnableDCache();
  107. #endif
  108. /* HAL_Init() function is called at the beginning of the program */
  109. HAL_Init();
  110. /* enable interrupt */
  111. __set_PRIMASK(0);
  112. /* System clock initialization */
  113. SystemClock_Config();
  114. /* disbale interrupt */
  115. __set_PRIMASK(1);
  116. rt_hw_systick_init();
  117. /* Heap initialization */
  118. #if defined(RT_USING_HEAP)
  119. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  120. #endif
  121. /* Pin driver initialization is open by default */
  122. #ifdef RT_USING_PIN
  123. rt_hw_pin_init();
  124. #endif
  125. /* USART driver initialization is open by default */
  126. #ifdef RT_USING_SERIAL
  127. rt_hw_usart_init();
  128. #endif
  129. /* Set the shell console output device */
  130. #ifdef RT_USING_CONSOLE
  131. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  132. #endif
  133. /* Board underlying hardware initialization */
  134. #ifdef RT_USING_COMPONENTS_INIT
  135. rt_components_board_init();
  136. #endif
  137. }