• R/O
  • SSH
  • HTTPS

chibios: Commit


Commit MetaInfo

Revision14016 (tree)
Time2021-01-11 21:14:32
Authorcinsights

Log Message

Add time conversion macros and functions for monotonic time stamps

Change Summary

Incremental Difference

--- trunk/os/rt/include/chtime.h (revision 14015)
+++ trunk/os/rt/include/chtime.h (revision 14016)
@@ -274,6 +274,64 @@
274274 (time_msecs_t)((((time_conv_t)(interval) * (time_conv_t)1000000) + \
275275 (time_conv_t)CH_CFG_ST_FREQUENCY - (time_conv_t)1) / \
276276 (time_conv_t)CH_CFG_ST_FREQUENCY)
277+
278+/**
279+ * @brief Time stamp interval to seconds.
280+ * @details Converts from time stamp ticks number to seconds.
281+ * @note The result is rounded down to the second boundary.
282+ * @note Use of this macro for large values is not secure because of
283+ * integer overflows. Make sure your value can be correctly
284+ * converted.
285+ *
286+ * @param[in] interval time stamp in ticks
287+ *
288+ * @return The number of seconds.
289+ *
290+ * @api
291+ */
292+#define TIME_TS2S(timestamp) \
293+ (time_secs_t)(((systimestamp_t)(timestamp) / \
294+ (systimestamp_t)CH_CFG_ST_FREQUENCY))
295+
296+/**
297+ * @brief Time stamp interval to milliseconds.
298+ * @details Converts from time stamp ticks number to milliseconds.
299+ * @note The result is rounded down to the millisecond boundary.
300+ * @note Use of this macro for large values is not secure because of
301+ * integer overflows. Make sure your value can be correctly
302+ * converted.
303+ *
304+ * @param[in] interval time stamp in ticks
305+ *
306+ * @return The number of milliseconds.
307+ *
308+ * @api
309+ */
310+#define TIME_TS2MS(interval) \
311+ (time_msecs_t)((((systimestamp_t)(interval) % \
312+ (systimestamp_t)(CH_CFG_ST_FREQUENCY)) / \
313+ (systimestamp_t)((systimestamp_t)CH_CFG_ST_FREQUENCY / \
314+ (systimestamp_t)1000)) % 1000)
315+
316+/**
317+ * @brief Time stamp interval to microseconds.
318+ * @details Converts from time stamp ticks number to microseconds.
319+ * @note The result is rounded down to the microsecond boundary.
320+ * @note Use of this macro for large values is not secure because of
321+ * integer overflows. Make sure your value can be correctly
322+ * converted.
323+ *
324+ * @param[in] interval time stamp in ticks
325+ *
326+ * @return The number of microseconds.
327+ *
328+ * @api
329+ */
330+#define TIME_TS2US(interval) \
331+ (time_usecs_t)((((systimestamp_t)(interval) % \
332+ (systimestamp_t)(CH_CFG_ST_FREQUENCY)) / \
333+ (systimestamp_t)((systimestamp_t)CH_CFG_ST_FREQUENCY / \
334+ (systimestamp_t)1000000)) % 1000000)
277335 /** @} */
278336
279337 /*===========================================================================*/
@@ -434,6 +492,75 @@
434492 }
435493
436494 /**
495+ * @brief Time stamp interval to seconds.
496+ * @details Converts from time stamp interval to seconds.
497+ * @note The result is rounded up to the next second boundary.
498+ *
499+ * @param[in] interval interval in ticks
500+ * @return The number of seconds.
501+ *
502+ * @special
503+ */
504+static inline time_secs_t chTimeTS2S(systimestamp_t interval) {
505+ systimestamp_t secs;
506+
507+ secs = ((interval +
508+ (systimestamp_t)CH_CFG_ST_FREQUENCY - (systimestamp_t)1) /
509+ (systimestamp_t)CH_CFG_ST_FREQUENCY);
510+
511+ chDbgAssert(secs < (systimestamp_t)((time_secs_t)-1),
512+ "conversion overflow");
513+
514+ return (time_secs_t)secs;
515+}
516+
517+/**
518+ * @brief Time stamp interval to milliseconds.
519+ * @details Converts from time stamp interval to milliseconds.
520+ * @note The result is rounded up to the next millisecond boundary.
521+ *
522+ * @param[in] interval interval in ticks
523+ * @return The number of milliseconds.
524+ *
525+ * @special
526+ */
527+static inline time_msecs_t chTimeTS2MS(systimestamp_t interval) {
528+ systimestamp_t msecs;
529+
530+ msecs = ((interval * (systimestamp_t)1000) +
531+ (systimestamp_t)CH_CFG_ST_FREQUENCY - (systimestamp_t)1) /
532+ (systimestamp_t)CH_CFG_ST_FREQUENCY;
533+
534+ chDbgAssert(msecs < (systimestamp_t)((time_msecs_t)-1),
535+ "conversion overflow");
536+
537+ return (time_msecs_t)msecs;
538+}
539+
540+/**
541+ * @brief Time stamp interval to microseconds.
542+ * @details Converts from time stamp interval to microseconds.
543+ * @note The result is rounded up to the next microsecond boundary.
544+ *
545+ * @param[in] interval interval in ticks
546+ * @return The number of microseconds.
547+ *
548+ * @special
549+ */
550+static inline time_usecs_t chTimeTS2US(systimestamp_t interval) {
551+ systimestamp_t usecs;
552+
553+ usecs = ((interval * (systimestamp_t)1000000) +
554+ (systimestamp_t)CH_CFG_ST_FREQUENCY - (systimestamp_t)1) /
555+ (systimestamp_t)CH_CFG_ST_FREQUENCY;
556+
557+ chDbgAssert(usecs < (systimestamp_t)((time_usecs_t)-1),
558+ "conversion overflow");
559+
560+ return (time_usecs_t)usecs;
561+}
562+
563+/**
437564 * @brief Adds an interval to a system time returning a system time.
438565 *
439566 * @param[in] systime base system time
@@ -490,6 +617,23 @@
490617 (systime_t)((systime_t)end - (systime_t)start));
491618 }
492619
620+/**
621+ * @brief Time stamp millisecond count in current second.
622+ * @details Gets millisecond count of current second.
623+ * @note The result is rounded up to the next millisecond boundary.
624+ *
625+ * @param[in] interval interval in time stamp ticks
626+ * @return The count of milliseconds.
627+ *
628+ * @special
629+ */
630+static inline time_msecs_t chTSSecMS(systimestamp_t interval) {
631+
632+ return (time_msecs_t)(interval /
633+ ((systimestamp_t)CH_CFG_ST_FREQUENCY / (systimestamp_t)1000)
634+ % (time_msecs_t)1000);
635+}
636+
493637 /** @} */
494638
495639 #endif /* CHTIME_H */
--- trunk/readme.txt (revision 14015)
+++ trunk/readme.txt (revision 14016)
@@ -74,6 +74,7 @@
7474 *****************************************************************************
7575
7676 *** Next ***
77+- NEW: Added time conversion macros and functions for monotonic time stamps
7778 - NEW: Added support for STM32WB55.
7879 - NEW: Added chscanf() and buffered streams, contributed by Alex Lewontin.
7980 - NEW: Added option to LWIP bindings to use memory pools instead of heap
Show on old repository browser