1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2023-06-14     muaxiaohei   first version
9  */
10 
11 #include <rtthread.h>
12 #include "drv_common.h"
13 #include <board.h> /* for rt_hw_us_delay */
14 
15 #define DBG_TAG    "drv.common"
16 #define DBG_LVL    DBG_INFO
17 #include <rtdbg.h>
18 
rt_hw_us_delay(rt_uint32_t us)19 void rt_hw_us_delay(rt_uint32_t us)
20 {
21     rt_uint64_t total_delay_ticks, us_ticks, start, now, delta, reload;
22 
23     start = SysTick->CNT;
24     reload = SysTick->CMP;
25     us_ticks = SystemCoreClock / 8000000UL;
26     total_delay_ticks = us * us_ticks;
27     if (total_delay_ticks >= reload)
28     {
29         LOG_E("rt_hw_us_delay: the us parameter exceeds the maximum limit!");
30     }
31 
32     do {
33         now = SysTick->CNT;
34         delta = start > now ? start - now : reload + start - now;
35     } while(delta < total_delay_ticks);
36 }
37