1 /*
2 * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2023-12-16 CDT first version
9 */
10
11 #include "board.h"
12
13 #define DBG_TAG "drv_common"
14 #define DBG_LVL DBG_INFO
15 #include <rtdbg.h>
16
17 #ifdef RT_USING_PIN
18 #include <drv_gpio.h>
19 #endif
20
21 #ifdef RT_USING_SERIAL
22 #ifdef RT_USING_SERIAL_V2
23 #include <drv_usart_v2.h>
24 #else
25 #include <drv_usart.h>
26 #endif /* RT_USING_SERIAL */
27 #endif /* RT_USING_SERIAL_V2 */
28
29 #ifdef RT_USING_FINSH
30 #include <finsh.h>
reboot(uint8_t argc,char ** argv)31 static void reboot(uint8_t argc, char **argv)
32 {
33 rt_hw_cpu_reset();
34 }
35 MSH_CMD_EXPORT(reboot, Reboot System);
36 #endif /* RT_USING_FINSH */
37
38 /**
39 * This function is executed in case of error occurrence.
40 */
_Error_Handler(char * s,int num)41 void _Error_Handler(char *s, int num)
42 {
43 LOG_E("Error_Handler at file:%s num:%d", s, num);
44 while (1)
45 {
46 }
47 }
48
49 /**
50 * This is the timer interrupt service routine.
51 */
SysTick_Handler(void)52 void SysTick_Handler(void)
53 {
54 /* enter interrupt */
55 rt_interrupt_enter();
56 rt_tick_increase();
57 /* leave interrupt */
58 rt_interrupt_leave();
59 }
60
61 /**
62 * Configures the SysTick for OS tick.
63 */
SysTick_Configuration(void)64 void SysTick_Configuration(void)
65 {
66 rt_uint32_t cnts;
67 stc_clock_freq_t stcClkFreq;
68
69 CLK_GetClockFreq(&stcClkFreq);
70 cnts = (rt_uint32_t)stcClkFreq.u32HclkFreq / RT_TICK_PER_SECOND;
71 SysTick_Config(cnts);
72 }
73
74 /**
75 * This function will initial HC32 board.
76 */
rt_hw_board_init()77 void rt_hw_board_init()
78 {
79 PeripheralRegister_Unlock();
80
81 SystemBase_Config();
82 SystemClock_Config();
83 PeripheralClock_Config();
84 /* Configure the SysTick */
85 SysTick_Configuration();
86
87 /* Heap initialization */
88 #if defined(RT_USING_HEAP)
89 rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
90 #endif
91
92 #ifdef RT_USING_PIN
93 rt_hw_pin_init();
94 #endif
95
96 #ifdef RT_USING_SERIAL
97 rt_hw_usart_init();
98 #endif
99
100 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
101 rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
102 #endif
103
104 /* Board underlying hardware initialization */
105 #ifdef RT_USING_COMPONENTS_INIT
106 rt_components_board_init();
107 #endif
108 }
109
rt_hw_us_delay(rt_uint32_t us)110 void rt_hw_us_delay(rt_uint32_t us)
111 {
112 uint32_t start, now, delta, reload, us_tick;
113 start = SysTick->VAL;
114 reload = SysTick->LOAD;
115 us_tick = SystemCoreClock / 1000000UL;
116
117 do
118 {
119 now = SysTick->VAL;
120 delta = start > now ? start - now : reload + start - now;
121 }
122 while (delta < us_tick * us);
123 }
124