drv_common.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-22 airm2m first version
  9. */
  10. #include "drv_common.h"
  11. #include "board.h"
  12. #ifdef RT_USING_SERIAL
  13. #ifdef RT_USING_SERIAL_V2
  14. #include "drv_usart_v2.h"
  15. #else
  16. #include "drv_usart.h"
  17. #endif /* RT_USING_SERIAL */
  18. #endif /* RT_USING_SERIAL_V2 */
  19. #define DBG_TAG "drv_common"
  20. #define DBG_LVL DBG_INFO
  21. #include <rtdbg.h>
  22. #ifdef RT_USING_FINSH
  23. #include <finsh.h>
  24. static void reboot(uint8_t argc, char **argv)
  25. {
  26. rt_hw_cpu_reset();
  27. }
  28. MSH_CMD_EXPORT(reboot, Reboot System);
  29. #endif /* RT_USING_FINSH */
  30. /**
  31. * This is the timer interrupt service routine.
  32. *
  33. */
  34. void SysTick_Handler(void)
  35. {
  36. /* enter interrupt */
  37. rt_interrupt_enter();
  38. rt_tick_increase();
  39. /* leave interrupt */
  40. rt_interrupt_leave();
  41. }
  42. /**
  43. * @brief This function is executed in case of error occurrence.
  44. * @param None
  45. * @retval None
  46. */
  47. void _Error_Handler(char *s, int num)
  48. {
  49. /* USER CODE BEGIN Error_Handler */
  50. /* User can add his own implementation to report the HAL error return state */
  51. LOG_E("Error_Handler at file:%s num:%d", s, num);
  52. while (1)
  53. {
  54. }
  55. /* USER CODE END Error_Handler */
  56. }
  57. /**
  58. * This function will delay for some us.
  59. *
  60. * @param us the delay time of us
  61. */
  62. void rt_hw_us_delay(rt_uint32_t us)
  63. {
  64. rt_uint32_t ticks;
  65. rt_uint32_t told, tnow, tcnt = 0;
  66. rt_uint32_t reload = SysTick->LOAD;
  67. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  68. told = SysTick->VAL;
  69. while (1)
  70. {
  71. tnow = SysTick->VAL;
  72. if (tnow != told)
  73. {
  74. if (tnow < told)
  75. {
  76. tcnt += told - tnow;
  77. }
  78. else
  79. {
  80. tcnt += reload - tnow + told;
  81. }
  82. told = tnow;
  83. if (tcnt >= ticks)
  84. {
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * This function will initial STM32 board.
  92. */
  93. rt_weak void rt_hw_board_init(void)
  94. {
  95. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
  96. /* System clock initialization */
  97. SystemClock_Config();
  98. RCC_ClocksTypeDef clocks;
  99. RCC_GetClocksFreq(&clocks); //获取时钟频率
  100. SysTick_Config(clocks.SYSCLK_Frequency/RT_TICK_PER_SECOND);
  101. /* Heap initialization */
  102. #if defined(RT_USING_HEAP)
  103. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  104. #endif
  105. /* Pin driver initialization is open by default */
  106. #ifdef RT_USING_PIN
  107. rt_hw_pin_init();
  108. #endif
  109. /* USART driver initialization is open by default */
  110. #ifdef RT_USING_SERIAL
  111. rt_hw_usart_init();
  112. #endif
  113. /* Set the shell console output device */
  114. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  115. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  116. #endif
  117. /* Board underlying hardware initialization */
  118. #ifdef RT_USING_COMPONENTS_INIT
  119. rt_components_board_init();
  120. #endif
  121. }