time.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Amazon FreeRTOS POSIX V1.1.0
  3. * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://aws.amazon.com/freertos
  23. * http://www.FreeRTOS.org
  24. */
  25. /**
  26. * @file time.h
  27. * @brief Time types.
  28. *
  29. * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html
  30. */
  31. #ifndef _FREERTOS_POSIX_TIME_H_
  32. #define _FREERTOS_POSIX_TIME_H_
  33. /* FreeRTOS+POSIX includes. */
  34. #include "FreeRTOS_POSIX/sys/types.h"
  35. #include "FreeRTOS_POSIX/signal.h"
  36. /**
  37. * @name Unit conversion constants.
  38. */
  39. /**@{ */
  40. #define MICROSECONDS_PER_SECOND ( 1000000LL ) /**< Microseconds per second. */
  41. #define NANOSECONDS_PER_SECOND ( 1000000000LL ) /**< Nanoseconds per second. */
  42. #define NANOSECONDS_PER_TICK ( NANOSECONDS_PER_SECOND / configTICK_RATE_HZ ) /**< Nanoseconds per FreeRTOS tick. */
  43. /**@} */
  44. /**
  45. * @name Clock identifiers.
  46. */
  47. /**@{ */
  48. #define CLOCK_REALTIME 0 /**< The identifier of the system-wide clock measuring real time. */
  49. #define CLOCK_MONOTONIC 1 /**< The identifier for the system-wide monotonic clock.*/
  50. /**@} */
  51. /**
  52. * @name A number used to convert the value returned by the clock() function into seconds.
  53. */
  54. /**@{ */
  55. #define CLOCKS_PER_SEC ( ( clock_t ) configTICK_RATE_HZ )
  56. /**@} */
  57. /**
  58. * @name Flag indicating time is absolute.
  59. *
  60. * For functions taking timer objects, this refers to the clock associated with the timer.
  61. */
  62. /**@{ */
  63. #define TIMER_ABSTIME 0x01
  64. /**@} */
  65. #if !defined( posixconfigENABLE_TIMESPEC ) || ( posixconfigENABLE_TIMESPEC == 1 )
  66. /**
  67. * @brief represents an elapsed time
  68. */
  69. struct timespec
  70. {
  71. time_t tv_sec; /**< Seconds. */
  72. long tv_nsec; /**< Nanoseconds. */
  73. };
  74. #endif
  75. #if !defined( posixconfigENABLE_ITIMERSPEC ) || ( posixconfigENABLE_ITIMERSPEC == 1 )
  76. /**
  77. * @brief timer
  78. */
  79. struct itimerspec
  80. {
  81. struct timespec it_interval; /**< Timer period. */
  82. struct timespec it_value; /**< Timer expiration. */
  83. };
  84. #endif
  85. /**
  86. * @brief Report CPU time used.
  87. *
  88. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock.html
  89. *
  90. * @return The number of FreeRTOS ticks since the scheduler
  91. * was started minus the ticks spent in the idle task.
  92. *
  93. * @note This function does NOT report the number of ticks spent by the calling thread.
  94. */
  95. clock_t clock( void );
  96. /**
  97. * @brief Access a process CPU-time clock.
  98. *
  99. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getcpuclockid.html
  100. *
  101. * @retval EPERM
  102. *
  103. * @note This function is currently unsupported.
  104. *
  105. */
  106. int clock_getcpuclockid( pid_t pid,
  107. clockid_t * clock_id );
  108. /**
  109. * @brief Returns the resolution of a clock.
  110. *
  111. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html
  112. *
  113. * @note clock_id is ignored
  114. * @note This function stores the resolution of the FreeRTOS tick count in the object res points to.
  115. *
  116. * @retval 0 - Upon successful execution
  117. */
  118. int clock_getres( clockid_t clock_id,
  119. struct timespec * res );
  120. /**
  121. * @brief Returns the current value for the specified clock, clock_id.
  122. *
  123. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html
  124. *
  125. * @note clock_id is ignored
  126. * @note this function does not check for overflows of time_t.
  127. *
  128. * @retval 0 - Upon successful completion.
  129. */
  130. int clock_gettime( clockid_t clock_id,
  131. struct timespec * tp );
  132. /**
  133. * @brief High resolution sleep with specifiable clock.
  134. *
  135. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_nanosleep.html
  136. *
  137. * @note clock_id is ignored, as this function uses the FreeRTOS tick count as its clock.
  138. * @note flags is ignored, if INCLUDE_vTaskDelayUntil is 0. i.e. the FreeRTOS function vTaskDelayUntil isn't available.
  139. * @note rmtp is also ignored, as signals are not implemented.
  140. *
  141. * @retval 0 - Upon successful completion.
  142. * @retval EINVAL - The rqtp argument specified a nanosecond value less than zero or greater than or equal to 1000 million.
  143. */
  144. int clock_nanosleep( clockid_t clock_id,
  145. int flags,
  146. const struct timespec * rqtp,
  147. struct timespec * rmtp );
  148. /**
  149. * @brief Sets the time for the specified clock.
  150. *
  151. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_settime.html
  152. *
  153. * @retval -1 with errno set to EPERM.
  154. *
  155. * @note This function is currently unsupported, as FreeRTOS does not provide a function to modify the tick count.
  156. */
  157. int clock_settime( clockid_t clock_id,
  158. const struct timespec * tp );
  159. /**
  160. * @brief High resolution sleep.
  161. *
  162. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html
  163. *
  164. * @note rmtp is ignored, as signals are not implemented.
  165. *
  166. * @retval 0 - Upon successful completion.
  167. * @retval -1 - The rqtp argument is invalid OR the rqtp argument specified a nanosecond value less than zero or greater than or equal to 1000 million.
  168. *
  169. */
  170. int nanosleep( const struct timespec * rqtp,
  171. struct timespec * rmtp );
  172. /**
  173. * @brief Create a per-process timer.
  174. *
  175. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html
  176. *
  177. * @note clock_id is ignored, as this function used the FreeRTOS tick count as its clock.
  178. * @note evp.sigev_notify must be set to SIGEV_THREAD, since signals are currently not supported.
  179. *
  180. * @retval 0 - Upon successful completion, with location referenced by timerid updated.
  181. * @retval -1 - If an error occurs. errno is also set.
  182. *
  183. * @sideeffect Possible errno values
  184. * <br>
  185. * ENOTSUP - If evp is NULL OR evp->sigen_notify == SIGEV_SIGNAL.
  186. * <br>
  187. * EAGAIN - The system lacks sufficient signal queuing resources to honor the request.
  188. */
  189. int timer_create( clockid_t clockid,
  190. struct sigevent * evp,
  191. timer_t * timerid );
  192. /**
  193. * @brief Delete a per-process timer.
  194. *
  195. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html
  196. *
  197. * @retval 0 - Upon successful completion.
  198. */
  199. int timer_delete( timer_t timerid );
  200. /**
  201. * @brief Get the timer overrun count.
  202. *
  203. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html
  204. *
  205. * @retval 0 - Always return 0, since signals are not supported.
  206. */
  207. int timer_getoverrun( timer_t timerid );
  208. /**
  209. * @brief Get the amount of time until the timer expires.
  210. *
  211. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_gettime.html
  212. *
  213. * @retval 0 - Upon successful completion.
  214. */
  215. int timer_gettime( timer_t timerid,
  216. struct itimerspec * value );
  217. /**
  218. * @brief Set the time until the next expiration of the timer.
  219. *
  220. * http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_settime.html
  221. *
  222. * @retval 0 - Upon successful completion.
  223. * @retval -1 - An error occurred, errno is also set.
  224. *
  225. * @sideeffect Possible errno values
  226. * <br>
  227. * EINVAL - A value structure specified a nanosecond value less than zero or greater than or equal to 1000 million,
  228. * AND the it_value member of that structure did not specify zero seconds and nanoseconds.
  229. */
  230. int timer_settime( timer_t timerid,
  231. int flags,
  232. const struct itimerspec * value,
  233. struct itimerspec * ovalue );
  234. /*------------------------ IAR System time.h ----------------------------------*/
  235. #if _DLIB_SUPPORT_FOR_AEABI
  236. #if _DLIB_TIME_USES_LONG
  237. typedef signed long __time32_t;
  238. typedef signed long clock_t;
  239. #else
  240. typedef unsigned int __time32_t;
  241. typedef unsigned int clock_t;
  242. #endif
  243. #else
  244. typedef signed long __time32_t;
  245. typedef signed long clock_t;
  246. #endif
  247. struct tm
  248. { /* date and time components */
  249. int tm_sec;
  250. int tm_min;
  251. int tm_hour;
  252. int tm_mday;
  253. int tm_mon;
  254. int tm_year;
  255. int tm_wday;
  256. int tm_yday;
  257. int tm_isdst;
  258. #if _DLIB_SUPPORT_FOR_AEABI
  259. int __BSD_bug_filler1;
  260. int __BSD_bug_filler2;
  261. #endif
  262. };
  263. struct __timespec32
  264. {
  265. __time32_t tv_sec;
  266. long tv_nsec;
  267. };
  268. _EXTERN_C
  269. /* low-level functions */
  270. __ATTRIBUTES time_t time(time_t *);
  271. /* declarations */
  272. __DEPREC_ATTRS char * asctime(const struct tm *);
  273. //__ATTRIBUTES clock_t clock(void);
  274. __DEPREC_ATTRS char * ctime(const time_t *);
  275. __EFF_NE __ATTRIBUTES double difftime(time_t, time_t);
  276. __DEPREC_ATTRS struct tm * gmtime(const time_t *);
  277. __DEPREC_ATTRS struct tm * localtime(const time_t *);
  278. __ATTRIBUTES time_t mktime(struct tm *);
  279. __ATTRIBUTES int timespec_get(struct timespec *, int);
  280. #if !__AEABI_PORTABILITY_INTERNAL_LEVEL
  281. __ATTRIBUTES __time32_t __time32(__time32_t *);
  282. __ATTRIBUTES char * __ctime32(const __time32_t *);
  283. __EFF_NE __ATTRIBUTES double __difftime32(__time32_t, __time32_t);
  284. __ATTRIBUTES struct tm * __gmtime32(const __time32_t *);
  285. __ATTRIBUTES struct tm * __localtime32(const __time32_t *);
  286. __ATTRIBUTES __time32_t __mktime32(struct tm *);
  287. __ATTRIBUTES int __timespec_get32(struct __timespec32 *, int);
  288. #endif
  289. __ATTRIBUTES size_t strftime(char *_Restrict, size_t,
  290. const char *_Restrict,
  291. const struct tm *_Restrict);
  292. _EXTERN_C_END
  293. #endif /* ifndef _FREERTOS_POSIX_TIME_H_ */