drv_common.c 3.3 KB

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