drv_common.c 3.1 KB

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