delay.c 2.4 KB

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