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-02-22     airm2m       first version
9  */
10 
11 #include "drv_common.h"
12 #include "board.h"
13 
14 #ifdef RT_USING_SERIAL
15 #ifdef RT_USING_SERIAL_V2
16 #include "drv_usart_v2.h"
17 #else
18 #include "drv_usart.h"
19 #endif /* RT_USING_SERIAL */
20 #endif /* RT_USING_SERIAL_V2 */
21 
22 #define DBG_TAG    "drv_common"
23 #define DBG_LVL    DBG_INFO
24 #include <rtdbg.h>
25 
26 #ifdef RT_USING_FINSH
27 #include <finsh.h>
reboot(uint8_t argc,char ** argv)28 static void reboot(uint8_t argc, char **argv)
29 {
30     rt_hw_cpu_reset();
31 }
32 MSH_CMD_EXPORT(reboot, Reboot System);
33 #endif /* RT_USING_FINSH */
34 
35 
36 
37 /* SysTick configuration */
rt_hw_systick_init(void)38 void rt_hw_systick_init(void)
39 {
40     SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
41 
42     NVIC_SetPriority(SysTick_IRQn, 0xFF);
43 }
44 
45 /**
46  * This is the timer interrupt service routine.
47  *
48  */
SysTick_Handler(void)49 void SysTick_Handler(void)
50 {
51     /* enter interrupt */
52     rt_interrupt_enter();
53     rt_tick_increase();
54     /* leave interrupt */
55     rt_interrupt_leave();
56 }
57 
58 /**
59   * @brief  This function is executed in case of error occurrence.
60   * @param  None
61   * @retval None
62   */
_Error_Handler(char * s,int num)63 void _Error_Handler(char *s, int num)
64 {
65     /* USER CODE BEGIN Error_Handler */
66     /* User can add his own implementation to report the HAL error return state */
67     LOG_E("Error_Handler at file:%s num:%d", s, num);
68     while (1)
69     {
70     }
71     /* USER CODE END Error_Handler */
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