drv_common.c 3.1 KB

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