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  * 2022-05-16     shelton      first version
9  */
10 
11 #include "drv_common.h"
12 
13 #ifdef RT_USING_SERIAL
14 #include "drv_usart.h"
15 #endif
16 
17 #define DBG_TAG                         "drv_common"
18 #define DBG_LVL                         DBG_INFO
19 #include <rtdbg.h>
20 
21 #ifdef RT_USING_FINSH
22 #include <finsh.h>
reboot(uint8_t argc,char ** argv)23 static void reboot(uint8_t argc, char **argv)
24 {
25     rt_hw_cpu_reset();
26 }
27 MSH_CMD_EXPORT(reboot, Reboot System);
28 #endif /* RT_USING_FINSH */
29 
30 extern __IO uint32_t uwTick;
31 static uint32_t _systick_ms = 1;
32 
33 /* systick configuration */
rt_hw_systick_init(void)34 void rt_hw_systick_init(void)
35 {
36     systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
37     SysTick_Config(system_core_clock / RT_TICK_PER_SECOND);
38 
39     nvic_irq_enable(SysTick_IRQn, 0, 0);
40 
41     _systick_ms = 1000u / RT_TICK_PER_SECOND;
42     if(_systick_ms == 0)
43         _systick_ms = 1;
44 }
45 
46 /**
47  * this is the timer interrupt service routine.
48  */
SysTick_Handler(void)49 void SysTick_Handler(void)
50 {
51     /* enter interrupt */
52     rt_interrupt_enter();
53 
54     rt_tick_increase();
55 
56     /* leave interrupt */
57     rt_interrupt_leave();
58 }
59 
60 /**
61   * @brief  this function is executed in case of error occurrence.
62   * @param  none
63   * @retval none
64   */
_Error_Handler(char * s,int num)65 void _Error_Handler(char *s, int num)
66 {
67     /* User can add his own implementation to report the HAL error return state */
68     LOG_E("Error_Handler at file:%s num:%d", s, num);
69     while (1)
70     {
71     }
72 }
73 
74 /**
75  * this function will delay for some us.
76  *
77  * @param us the delay time of us
78  */
rt_hw_us_delay(rt_uint32_t us)79 void rt_hw_us_delay(rt_uint32_t us)
80 {
81     rt_uint32_t ticks;
82     rt_uint32_t told, tnow, tcnt = 0;
83     rt_uint32_t reload = SysTick->LOAD;
84 
85     ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
86     told = SysTick->VAL;
87     while (1)
88     {
89         tnow = SysTick->VAL;
90         if (tnow != told)
91         {
92             if (tnow < told)
93             {
94                 tcnt += told - tnow;
95             }
96             else
97             {
98                 tcnt += reload - tnow + told;
99             }
100             told = tnow;
101             if (tcnt >= ticks)
102             {
103                 break;
104             }
105         }
106     }
107 }
108 
109 /**
110  * this function will initial at32 board.
111  */
rt_hw_board_init()112 rt_weak void rt_hw_board_init()
113 {
114     /* system clock initialization */
115     system_clock_config();
116 
117     /* configure nvic priority group */
118     nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
119 
120     /* systick init */
121     rt_hw_systick_init();
122 
123     /* heap initialization */
124 #if defined(RT_USING_HEAP)
125     rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
126 #endif
127 
128     /* pin driver initialization is open by default */
129 #ifdef RT_USING_PIN
130     rt_hw_pin_init();
131 #endif
132 
133     /* usart driver initialization is open by default */
134 #ifdef RT_USING_SERIAL
135     rt_hw_usart_init();
136 #endif
137 
138     /* set the shell console output device */
139 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
140     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
141 #endif
142 
143     /* board underlying hardware initialization */
144 #ifdef RT_USING_COMPONENTS_INIT
145     rt_components_board_init();
146 #endif
147 }
148 
149