board.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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-03-02 FMD-AE first version
  9. */
  10. #include "board.h"
  11. #ifdef RT_USING_SERIAL
  12. #include "drv_usart.h"
  13. #endif /* RT_USING_SERIAL */
  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. __IO uint32_t uwTick;
  26. static uint32_t _systick_ms = 1;
  27. void IncTick(void)
  28. {
  29. uwTick += _systick_ms;
  30. }
  31. /**
  32. * This is the timer interrupt service routine.
  33. *
  34. */
  35. void SysTick_Handler(void)
  36. {
  37. /* enter interrupt */
  38. rt_interrupt_enter();
  39. if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)
  40. IncTick();
  41. rt_tick_increase();
  42. /* leave interrupt */
  43. rt_interrupt_leave();
  44. }
  45. uint32_t GetTick(void)
  46. {
  47. if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)
  48. IncTick();
  49. return uwTick;
  50. }
  51. void SuspendTick(void)
  52. {
  53. }
  54. void ResumeTick(void)
  55. {
  56. }
  57. void Delay(__IO uint32_t Delay)
  58. {
  59. if (rt_thread_self())
  60. {
  61. rt_thread_mdelay(Delay);
  62. }
  63. else
  64. {
  65. for (rt_uint32_t count = 0; count < Delay; count++)
  66. {
  67. rt_hw_us_delay(1000);
  68. }
  69. }
  70. }
  71. /**
  72. * This function will delay for some us.
  73. *
  74. * @param us the delay time of us
  75. */
  76. void rt_hw_us_delay(rt_uint32_t us)
  77. {
  78. rt_uint32_t ticks;
  79. rt_uint32_t told, tnow, tcnt = 0;
  80. rt_uint32_t reload = SysTick->LOAD;
  81. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  82. told = SysTick->VAL;
  83. while (1)
  84. {
  85. tnow = SysTick->VAL;
  86. if (tnow != told)
  87. {
  88. if (tnow < told)
  89. {
  90. tcnt += told - tnow;
  91. }
  92. else
  93. {
  94. tcnt += reload - tnow + told;
  95. }
  96. told = tnow;
  97. if (tcnt >= ticks)
  98. {
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * This function will initial FT32 board.
  106. */
  107. RT_WEAK void rt_hw_board_init()
  108. {
  109. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  110. /* Heap initialization */
  111. #if defined(RT_USING_HEAP)
  112. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  113. #endif
  114. /* Pin driver initialization is open by default */
  115. #ifdef RT_USING_PIN
  116. rt_hw_pin_init();
  117. #endif
  118. /* USART driver initialization is open by default */
  119. #ifdef RT_USING_SERIAL
  120. rt_hw_usart_init();
  121. #endif
  122. /* Set the shell console output device */
  123. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  124. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  125. #endif
  126. /* Board underlying hardware initialization */
  127. #ifdef RT_USING_COMPONENTS_INIT
  128. rt_components_board_init();
  129. #endif
  130. }