drv_common.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-05-16 shelton first version
  9. */
  10. #include "drv_common.h"
  11. #ifdef RT_USING_SERIAL
  12. #include "drv_usart.h"
  13. #endif
  14. #define DBG_TAG "drv_common"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #ifdef RT_USING_FINSH
  18. #include <finsh.h>
  19. static void reboot(uint8_t argc, char **argv)
  20. {
  21. rt_hw_cpu_reset();
  22. }
  23. MSH_CMD_EXPORT(reboot, Reboot System);
  24. #endif /* RT_USING_FINSH */
  25. extern __IO uint32_t uwTick;
  26. static uint32_t _systick_ms = 1;
  27. /* systick configuration */
  28. void rt_hw_systick_init(void)
  29. {
  30. systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
  31. SysTick_Config(system_core_clock / RT_TICK_PER_SECOND);
  32. nvic_irq_enable(SysTick_IRQn, 0, 0);
  33. _systick_ms = 1000u / RT_TICK_PER_SECOND;
  34. if(_systick_ms == 0)
  35. _systick_ms = 1;
  36. }
  37. /**
  38. * this is the timer interrupt service routine.
  39. */
  40. void SysTick_Handler(void)
  41. {
  42. /* enter interrupt */
  43. rt_interrupt_enter();
  44. rt_tick_increase();
  45. /* leave interrupt */
  46. rt_interrupt_leave();
  47. }
  48. /**
  49. * @brief this function is executed in case of error occurrence.
  50. * @param none
  51. * @retval none
  52. */
  53. void _Error_Handler(char *s, int num)
  54. {
  55. /* User can add his own implementation to report the HAL error return state */
  56. LOG_E("Error_Handler at file:%s num:%d", s, num);
  57. while (1)
  58. {
  59. }
  60. }
  61. /**
  62. * this function will delay for some us.
  63. *
  64. * @param us the delay time of us
  65. */
  66. void rt_hw_us_delay(rt_uint32_t us)
  67. {
  68. rt_uint32_t ticks;
  69. rt_uint32_t told, tnow, tcnt = 0;
  70. rt_uint32_t reload = SysTick->LOAD;
  71. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  72. told = SysTick->VAL;
  73. while (1)
  74. {
  75. tnow = SysTick->VAL;
  76. if (tnow != told)
  77. {
  78. if (tnow < told)
  79. {
  80. tcnt += told - tnow;
  81. }
  82. else
  83. {
  84. tcnt += reload - tnow + told;
  85. }
  86. told = tnow;
  87. if (tcnt >= ticks)
  88. {
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * this function will initial at32 board.
  96. */
  97. RT_WEAK void rt_hw_board_init()
  98. {
  99. /* system clock initialization */
  100. system_clock_config();
  101. /* configure nvic priority group */
  102. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  103. /* systick init */
  104. rt_hw_systick_init();
  105. /* heap initialization */
  106. #if defined(RT_USING_HEAP)
  107. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  108. #endif
  109. /* pin driver initialization is open by default */
  110. #ifdef RT_USING_PIN
  111. rt_hw_pin_init();
  112. #endif
  113. /* usart driver initialization is open by default */
  114. #ifdef RT_USING_SERIAL
  115. rt_hw_usart_init();
  116. #endif
  117. /* set the shell console output device */
  118. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  119. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  120. #endif
  121. /* board underlying hardware initialization */
  122. #ifdef RT_USING_COMPONENTS_INIT
  123. rt_components_board_init();
  124. #endif
  125. }