summaryrefslogtreecommitdiff
path: root/components/libc/posix/delay/delay.c
blob: 76b9e508342bc78d9ae3d70034acc6dfe9e75db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-05-07     Meco Man     first Version
 */

#include <sys/types.h>
#include <rtthread.h>
#include <rthw.h>

/**
 * @brief    Delays the execution of the current thread for the specified number of milliseconds.
 *
 * @param    msecs The number of milliseconds to sleep.
 */
void msleep(unsigned int msecs)
{
    rt_thread_mdelay(msecs);
}
RTM_EXPORT(msleep);

/**
 * @brief    Delays the execution of the current thread for the specified number of seconds.
 *
 * @param    seconds The number of seconds to sleep.
 */
void ssleep(unsigned int seconds)
{
    msleep(seconds * 1000);
}
RTM_EXPORT(ssleep);

/**
 * @brief    Delays the execution of the current thread for the specified number of milliseconds.
 *
 * @param    msecs The number of milliseconds to delay.
 */
void mdelay(unsigned long msecs)
{
    rt_hw_us_delay(msecs * 1000);
}
RTM_EXPORT(mdelay);

/**
 * @brief    Delays the execution of the current thread for the specified number of microseconds.
 *
 * @param    usecs The number of microseconds to delay.
 */
void udelay(unsigned long usecs)
{
    rt_hw_us_delay(usecs);
}
RTM_EXPORT(udelay);

/**
 * @brief    Delays the execution of the current thread for approximately one microsecond.
 *
 * @param    nsecs This parameter is ignored.
 */
void ndelay(unsigned long nsecs)
{
    rt_hw_us_delay(1);
}
RTM_EXPORT(ndelay);

/**
 * @brief    Delays the execution of the current thread for the specified number of seconds.
 *
 * @param    seconds The number of seconds to sleep.
 *
 * @return   Returns 0 on success.
 */
unsigned int sleep(unsigned int seconds)
{
    if (rt_thread_self() != RT_NULL)
    {
        ssleep(seconds);
    }
    else /* scheduler has not run yet */
    {
        while(seconds > 0)
        {
            udelay(1000000u);
            seconds --;
        }
    }

    return 0;
}
RTM_EXPORT(sleep);

/**
 * @brief    Delays the execution of the current thread for the specified number of microseconds.
 *
 * @param    usec The number of microseconds to sleep.
 *
 * @return   Returns 0 on success.
 */
int usleep(useconds_t usec)
{
    if (rt_thread_self() != RT_NULL)
    {
        msleep(usec / 1000u);
        udelay(usec % 1000u);
    }
    else  /* scheduler has not run yet */
    {
        udelay(usec);
    }

    return 0;
}
RTM_EXPORT(usleep);