1
0

drv_common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * 2020-08-20 Abbcc 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
  18. #endif
  19. #ifdef RT_USING_FINSH
  20. #include <finsh.h>
  21. static void reboot(uint8_t argc, char **argv)
  22. {
  23. rt_hw_cpu_reset();
  24. }
  25. FINSH_FUNCTION_EXPORT_ALIAS(reboot, __cmd_reboot, Reboot System);
  26. #endif /* RT_USING_FINSH */
  27. /* SysTick configuration */
  28. void rt_hw_systick_init(void)
  29. {
  30. SysTick_Config(RCM_ReadHCLKFreq()/RT_TICK_PER_SECOND);
  31. /* AHB clock selected as SysTick clock source. */
  32. SysTick->CTRL |= 0x00000004U;
  33. NVIC_SetPriority(SysTick_IRQn, 0xFF);
  34. }
  35. /**
  36. * This is the timer interrupt service routine.
  37. *
  38. */
  39. void SysTick_Handler(void)
  40. {
  41. /* enter interrupt */
  42. rt_interrupt_enter();
  43. rt_tick_increase();
  44. /* leave interrupt */
  45. rt_interrupt_leave();
  46. }
  47. /**
  48. * This function will delay for some us.
  49. *
  50. * @param us the delay time of us
  51. */
  52. void rt_hw_us_delay(rt_uint32_t us)
  53. {
  54. rt_uint32_t ticks;
  55. rt_uint32_t told, tnow, tcnt = 0;
  56. rt_uint32_t reload = SysTick->LOAD;
  57. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  58. told = SysTick->VAL;
  59. while (1)
  60. {
  61. tnow = SysTick->VAL;
  62. if (tnow != told)
  63. {
  64. if (tnow < told)
  65. {
  66. tcnt += told - tnow;
  67. }
  68. else
  69. {
  70. tcnt += reload - tnow + told;
  71. }
  72. told = tnow;
  73. if (tcnt >= ticks)
  74. {
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * This function will initial STM32 board.
  82. */
  83. RT_WEAK void rt_hw_board_init()
  84. {
  85. /* Systick initialization */
  86. rt_hw_systick_init();
  87. /* Heap initialization */
  88. #if defined(RT_USING_HEAP)
  89. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  90. #endif
  91. /* Pin driver initialization is open by default */
  92. #ifdef RT_USING_PIN
  93. rt_hw_pin_init();
  94. #endif
  95. /* USART driver initialization is open by default */
  96. #ifdef RT_USING_SERIAL
  97. rt_hw_usart_init();
  98. #endif
  99. /* Set the shell console output device */
  100. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  101. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  102. #endif
  103. /* Board underlying hardware initialization */
  104. #ifdef RT_USING_COMPONENTS_INIT
  105. rt_components_board_init();
  106. #endif
  107. }