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  * 2021-05-07     Meco Man     first Version
9  */
10 
11 #include <sys/types.h>
12 #include <rtthread.h>
13 #include <rthw.h>
14 
15 /**
16  * @brief    Delays the execution of the current thread for the specified number of milliseconds.
17  *
18  * @param    msecs The number of milliseconds to sleep.
19  */
msleep(unsigned int msecs)20 void msleep(unsigned int msecs)
21 {
22     rt_thread_mdelay(msecs);
23 }
24 RTM_EXPORT(msleep);
25 
26 /**
27  * @brief    Delays the execution of the current thread for the specified number of seconds.
28  *
29  * @param    seconds The number of seconds to sleep.
30  */
ssleep(unsigned int seconds)31 void ssleep(unsigned int seconds)
32 {
33     msleep(seconds * 1000);
34 }
35 RTM_EXPORT(ssleep);
36 
37 /**
38  * @brief    Delays the execution of the current thread for the specified number of milliseconds.
39  *
40  * @param    msecs The number of milliseconds to delay.
41  */
mdelay(unsigned long msecs)42 void mdelay(unsigned long msecs)
43 {
44     rt_hw_us_delay(msecs * 1000);
45 }
46 RTM_EXPORT(mdelay);
47 
48 /**
49  * @brief    Delays the execution of the current thread for the specified number of microseconds.
50  *
51  * @param    usecs The number of microseconds to delay.
52  */
udelay(unsigned long usecs)53 void udelay(unsigned long usecs)
54 {
55     rt_hw_us_delay(usecs);
56 }
57 RTM_EXPORT(udelay);
58 
59 /**
60  * @brief    Delays the execution of the current thread for approximately one microsecond.
61  *
62  * @param    nsecs This parameter is ignored.
63  */
ndelay(unsigned long nsecs)64 void ndelay(unsigned long nsecs)
65 {
66     rt_hw_us_delay(1);
67 }
68 RTM_EXPORT(ndelay);
69 
70 /**
71  * @brief    Delays the execution of the current thread for the specified number of seconds.
72  *
73  * @param    seconds The number of seconds to sleep.
74  *
75  * @return   Returns 0 on success.
76  */
sleep(unsigned int seconds)77 unsigned int sleep(unsigned int seconds)
78 {
79     if (rt_thread_self() != RT_NULL)
80     {
81         ssleep(seconds);
82     }
83     else /* scheduler has not run yet */
84     {
85         while(seconds > 0)
86         {
87             udelay(1000000u);
88             seconds --;
89         }
90     }
91 
92     return 0;
93 }
94 RTM_EXPORT(sleep);
95 
96 /**
97  * @brief    Delays the execution of the current thread for the specified number of microseconds.
98  *
99  * @param    usec The number of microseconds to sleep.
100  *
101  * @return   Returns 0 on success.
102  */
usleep(useconds_t usec)103 int usleep(useconds_t usec)
104 {
105     if (rt_thread_self() != RT_NULL)
106     {
107         msleep(usec / 1000u);
108         udelay(usec % 1000u);
109     }
110     else  /* scheduler has not run yet */
111     {
112         udelay(usec);
113     }
114 
115     return 0;
116 }
117 RTM_EXPORT(usleep);
118