board.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2022-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-01-22 chasel first version
  9. */
  10. #include <board.h>
  11. extern uint32_t SystemCoreClock;
  12. extern void SystemInit(void);
  13. /**
  14. * this function will delay for some us.
  15. *
  16. * @param us the delay time of us
  17. */
  18. void rt_hw_us_delay(rt_uint32_t us)
  19. {
  20. rt_uint32_t ticks;
  21. rt_uint32_t told, tnow, tcnt = 0;
  22. rt_uint32_t reload = SysTick->LOAD;
  23. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  24. told = SysTick->VAL;
  25. while (1) {
  26. tnow = SysTick->VAL;
  27. if (tnow != told) {
  28. if (tnow < told) {
  29. tcnt += told - tnow;
  30. } else {
  31. tcnt += reload - tnow + told;
  32. }
  33. told = tnow;
  34. if (tcnt >= ticks) {
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. static void bsp_clock_config(void)
  41. {
  42. SystemInit();
  43. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  44. SysTick->CTRL |= 0x00000004UL;
  45. }
  46. void SysTick_Handler(void)
  47. {
  48. /* enter interrupt */
  49. rt_interrupt_enter();
  50. rt_tick_increase();
  51. /* leave interrupt */
  52. rt_interrupt_leave();
  53. }
  54. void rt_hw_board_init()
  55. {
  56. bsp_clock_config();
  57. #if defined(RT_USING_HEAP)
  58. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  59. #endif
  60. #ifdef RT_USING_PIN
  61. extern int rt_hw_pin_init(void);
  62. rt_hw_pin_init();
  63. #endif
  64. #ifdef RT_USING_SERIAL
  65. extern int rt_hw_uart_init(void);
  66. rt_hw_uart_init();
  67. #endif
  68. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  69. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  70. #endif
  71. #ifdef RT_USING_COMPONENTS_INIT
  72. rt_components_board_init();
  73. #endif
  74. }