drv_common.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-20 Abbcc first version
  9. * 2022-03-04 stevetong459 FINSH_FUNCTION_EXPORT_ALIAS change to MSH_CMD_EXPORT for reboot function.
  10. */
  11. #include "drv_common.h"
  12. #include "board.h"
  13. #ifdef RT_USING_SERIAL
  14. #ifdef RT_USING_SERIAL_V2
  15. #include "drv_usart_v2.h"
  16. #else
  17. #include "drv_usart.h"
  18. #endif
  19. #endif
  20. #ifdef RT_USING_FINSH
  21. #include <finsh.h>
  22. static void reboot(uint8_t argc, char **argv)
  23. {
  24. rt_hw_cpu_reset();
  25. }
  26. MSH_CMD_EXPORT(reboot, Reboot System);
  27. #endif /* RT_USING_FINSH */
  28. /* SysTick configuration */
  29. void rt_hw_systick_init(void)
  30. {
  31. SysTick_Config(RCM_ReadHCLKFreq() / RT_TICK_PER_SECOND);
  32. /* AHB clock selected as SysTick clock source. */
  33. SysTick->CTRL |= 0x00000004U;
  34. NVIC_SetPriority(SysTick_IRQn, 0xFF);
  35. }
  36. /**
  37. * This is the timer interrupt service routine.
  38. *
  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. * This function will delay for some us.
  50. *
  51. * @param us the delay time of us
  52. */
  53. void rt_hw_us_delay(rt_uint32_t us)
  54. {
  55. rt_uint32_t ticks;
  56. rt_uint32_t told, tnow, tcnt = 0;
  57. rt_uint32_t reload = SysTick->LOAD;
  58. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  59. told = SysTick->VAL;
  60. while (1)
  61. {
  62. tnow = SysTick->VAL;
  63. if (tnow != told)
  64. {
  65. if (tnow < told)
  66. {
  67. tcnt += told - tnow;
  68. }
  69. else
  70. {
  71. tcnt += reload - tnow + told;
  72. }
  73. told = tnow;
  74. if (tcnt >= ticks)
  75. {
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. /**
  82. * This function will config the board for initialization.
  83. */
  84. rt_weak void rt_hw_board_init(void)
  85. {
  86. /* Systick initialization */
  87. rt_hw_systick_init();
  88. /* Heap initialization */
  89. #if defined(RT_USING_HEAP)
  90. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  91. #endif
  92. /* Pin driver initialization is open by default */
  93. #ifdef RT_USING_PIN
  94. rt_hw_pin_init();
  95. #endif
  96. /* USART driver initialization is open by default */
  97. #ifdef RT_USING_SERIAL
  98. rt_hw_usart_init();
  99. #endif
  100. /* Set the shell console output device */
  101. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  102. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  103. #endif
  104. /* Board underlying hardware initialization */
  105. #ifdef RT_USING_COMPONENTS_INIT
  106. rt_components_board_init();
  107. #endif
  108. }