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 
11 #include <board.h>
12 extern uint32_t SystemCoreClock;
13 extern void SystemInit(void);
14 
15 /**
16  * this function will delay for some us.
17  *
18  * @param us the delay time of us
19  */
rt_hw_us_delay(rt_uint32_t us)20 void rt_hw_us_delay(rt_uint32_t us)
21 {
22     rt_uint32_t ticks;
23     rt_uint32_t told, tnow, tcnt = 0;
24     rt_uint32_t reload = SysTick->LOAD;
25 
26     ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
27     told = SysTick->VAL;
28     while (1) {
29         tnow = SysTick->VAL;
30         if (tnow != told) {
31             if (tnow < told) {
32                 tcnt += told - tnow;
33             } else {
34                 tcnt += reload - tnow + told;
35             }
36             told = tnow;
37             if (tcnt >= ticks) {
38                 break;
39             }
40         }
41     }
42 }
43 
bsp_clock_config(void)44 static void bsp_clock_config(void)
45 {
46    SystemInit();
47    SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
48    SysTick->CTRL |= 0x00000004UL;
49 }
50 
SysTick_Handler(void)51 void SysTick_Handler(void)
52 {
53     /* enter interrupt */
54     rt_interrupt_enter();
55 
56     rt_tick_increase();
57 
58     /* leave interrupt */
59     rt_interrupt_leave();
60 }
61 
rt_hw_board_init()62 void rt_hw_board_init()
63 {
64     bsp_clock_config();
65 
66 #if defined(RT_USING_HEAP)
67     rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
68 #endif
69 
70 #ifdef RT_USING_PIN
71     extern int rt_hw_pin_init(void);
72     rt_hw_pin_init();
73 #endif
74 
75 #ifdef RT_USING_SERIAL
76     extern int rt_hw_uart_init(void);
77     rt_hw_uart_init();
78 #endif
79 
80 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
81     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
82 #endif
83 
84 #ifdef RT_USING_COMPONENTS_INIT
85     rt_components_board_init();
86 #endif
87 }
88