drv_common.c 3.4 KB

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