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  * 2022-09-13     xjy198903    first implementation
9  */
10 
11 #include <rtthread.h>
12 #include "clock_config.h"
13 
rt_hw_us_delay(rt_uint32_t us)14 void rt_hw_us_delay(rt_uint32_t us)
15 {
16     rt_uint32_t ticks;
17     rt_uint32_t told, tnow, tcnt = 0;
18     rt_uint32_t reload = SysTick->LOAD;
19 
20     ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
21     told = SysTick->VAL;
22     while (1)
23     {
24         tnow = SysTick->VAL;
25         if (tnow != told)
26         {
27             if (tnow < told)
28             {
29                 tcnt += told - tnow;
30             }
31             else
32             {
33                 tcnt += reload - tnow + told;
34             }
35             told = tnow;
36             if (tcnt >= ticks)
37             {
38                 break;
39             }
40         }
41     }
42 }
43