aboutsummaryrefslogtreecommitdiffstats
path: root/os/rt/include
diff options
context:
space:
mode:
authorGiovanni Di Sirio <gdisirio@gmail.com>2017-10-12 08:37:47 +0000
committerGiovanni Di Sirio <gdisirio@gmail.com>2017-10-12 08:37:47 +0000
commit286deccd1280b0d66ccc6d9b2617719582607f0d (patch)
tree26c4066c47d36dd4dbeaea4c48c8ac9b3da3a4e7 /os/rt/include
parentb172abc34df730bea1487f9d2fe518a9b3d098b9 (diff)
downloadChibiOS-286deccd1280b0d66ccc6d9b2617719582607f0d.tar.gz
ChibiOS-286deccd1280b0d66ccc6d9b2617719582607f0d.tar.bz2
ChibiOS-286deccd1280b0d66ccc6d9b2617719582607f0d.zip
EXPERIMENTAL: Introduced sysinterval_t in RT, now system time and intervals are different types and could have different sizes.
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/rt5_dev_point1@10812 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'os/rt/include')
-rw-r--r--os/rt/include/ch.h19
-rw-r--r--os/rt/include/chcond.h4
-rw-r--r--os/rt/include/chevents.h6
-rw-r--r--os/rt/include/chschd.h2
-rw-r--r--os/rt/include/chsem.h4
-rw-r--r--os/rt/include/chsystypes.h11
-rw-r--r--os/rt/include/chthreads.h22
-rw-r--r--os/rt/include/chtime.h515
-rw-r--r--os/rt/include/chvt.h277
9 files changed, 547 insertions, 313 deletions
diff --git a/os/rt/include/ch.h b/os/rt/include/ch.h
index e11a049ac..d3bd8217b 100644
--- a/os/rt/include/ch.h
+++ b/os/rt/include/ch.h
@@ -66,7 +66,7 @@
#define CH_KERNEL_PATCH 0
/** @} */
-/* Core headers.*/
+/* Configuration headers and checks.*/
#include "chtypes.h"
#include "chconf.h"
@@ -78,12 +78,23 @@
#error "obsolete or unknown configuration file"
#endif
+/* Early function prototypes required by the following headers.*/
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void chSysHalt(const char *reason);
+#ifdef __cplusplus
+}
+#endif
+
+/* Including everything else.*/
#include "chlicense.h"
#include "chchecks.h"
#include "chsystypes.h"
+#include "chdebug.h"
+#include "chtime.h"
#include "chalign.h"
#include "chcore.h"
-#include "chdebug.h"
#include "chtrace.h"
#include "chtm.h"
#include "chstats.h"
@@ -108,10 +119,6 @@
#include "chfactory.h"
#include "chdynamic.h"
-#if !defined(_CHIBIOS_RT_CONF_)
-#error "missing or wrong configuration file"
-#endif
-
#endif /* CH_H */
/** @} */
diff --git a/os/rt/include/chcond.h b/os/rt/include/chcond.h
index bf1e02cd5..9b753f274 100644
--- a/os/rt/include/chcond.h
+++ b/os/rt/include/chcond.h
@@ -98,8 +98,8 @@ extern "C" {
msg_t chCondWait(condition_variable_t *cp);
msg_t chCondWaitS(condition_variable_t *cp);
#if CH_CFG_USE_CONDVARS_TIMEOUT == TRUE
- msg_t chCondWaitTimeout(condition_variable_t *cp, systime_t time);
- msg_t chCondWaitTimeoutS(condition_variable_t *cp, systime_t time);
+ msg_t chCondWaitTimeout(condition_variable_t *cp, sysinterval_t timeout);
+ msg_t chCondWaitTimeoutS(condition_variable_t *cp, sysinterval_t timeout);
#endif
#ifdef __cplusplus
}
diff --git a/os/rt/include/chevents.h b/os/rt/include/chevents.h
index 8b14ed112..d155e0e6d 100644
--- a/os/rt/include/chevents.h
+++ b/os/rt/include/chevents.h
@@ -141,9 +141,9 @@ extern "C" {
eventmask_t chEvtWaitAll(eventmask_t events);
#endif
#if CH_CFG_USE_EVENTS_TIMEOUT == TRUE
- eventmask_t chEvtWaitOneTimeout(eventmask_t events, systime_t time);
- eventmask_t chEvtWaitAnyTimeout(eventmask_t events, systime_t time);
- eventmask_t chEvtWaitAllTimeout(eventmask_t events, systime_t time);
+ eventmask_t chEvtWaitOneTimeout(eventmask_t events, sysinterval_t timeout);
+ eventmask_t chEvtWaitAnyTimeout(eventmask_t events, sysinterval_t timeout);
+ eventmask_t chEvtWaitAllTimeout(eventmask_t events, sysinterval_t timeout);
#endif
#ifdef __cplusplus
}
diff --git a/os/rt/include/chschd.h b/os/rt/include/chschd.h
index 79ff437a1..7f25279a9 100644
--- a/os/rt/include/chschd.h
+++ b/os/rt/include/chschd.h
@@ -476,7 +476,7 @@ extern "C" {
thread_t *chSchReadyI(thread_t *tp);
thread_t *chSchReadyAheadI(thread_t *tp);
void chSchGoSleepS(tstate_t newstate);
- msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t time);
+ msg_t chSchGoSleepTimeoutS(tstate_t newstate, sysinterval_t timeout);
void chSchWakeupS(thread_t *ntp, msg_t msg);
void chSchRescheduleS(void);
bool chSchIsPreemptionRequired(void);
diff --git a/os/rt/include/chsem.h b/os/rt/include/chsem.h
index ef2848b16..d8958eeeb 100644
--- a/os/rt/include/chsem.h
+++ b/os/rt/include/chsem.h
@@ -93,8 +93,8 @@ extern "C" {
void chSemResetI(semaphore_t *sp, cnt_t n);
msg_t chSemWait(semaphore_t *sp);
msg_t chSemWaitS(semaphore_t *sp);
- msg_t chSemWaitTimeout(semaphore_t *sp, systime_t time);
- msg_t chSemWaitTimeoutS(semaphore_t *sp, systime_t time);
+ msg_t chSemWaitTimeout(semaphore_t *sp, sysinterval_t timeout);
+ msg_t chSemWaitTimeoutS(semaphore_t *sp, sysinterval_t timeout);
void chSemSignal(semaphore_t *sp);
void chSemSignalI(semaphore_t *sp);
void chSemAddCounterI(semaphore_t *sp, cnt_t n);
diff --git a/os/rt/include/chsystypes.h b/os/rt/include/chsystypes.h
index 527ff2e8b..dac7d297b 100644
--- a/os/rt/include/chsystypes.h
+++ b/os/rt/include/chsystypes.h
@@ -45,17 +45,6 @@
/*===========================================================================*/
/**
- * @brief Type of system time.
- */
-#if (CH_CFG_ST_RESOLUTION == 32) || defined(__DOXYGEN__)
-typedef uint32_t systime_t;
-#elif CH_CFG_ST_RESOLUTION == 16
-typedef uint16_t systime_t;
-#else
-#error "invalid CH_CFG_ST_RESOLUTION setting"
-#endif
-
-/**
* @extends threads_queue_t
*
* @brief Type of a thread structure.
diff --git a/os/rt/include/chthreads.h b/os/rt/include/chthreads.h
index 6ac40d3b8..ade7dc30b 100644
--- a/os/rt/include/chthreads.h
+++ b/os/rt/include/chthreads.h
@@ -190,7 +190,7 @@ typedef struct {
*
* @api
*/
-#define chThdSleepSeconds(sec) chThdSleep(S2ST(sec))
+#define chThdSleepSeconds(sec) chThdSleep(TIME_S2I(sec))
/**
* @brief Delays the invoking thread for the specified number of
@@ -206,7 +206,7 @@ typedef struct {
*
* @api
*/
-#define chThdSleepMilliseconds(msec) chThdSleep(MS2ST(msec))
+#define chThdSleepMilliseconds(msec) chThdSleep(TIME_MS2I(msec))
/**
* @brief Delays the invoking thread for the specified number of
@@ -222,7 +222,7 @@ typedef struct {
*
* @api
*/
-#define chThdSleepMicroseconds(usec) chThdSleep(US2ST(usec))
+#define chThdSleepMicroseconds(usec) chThdSleep(TIME_US2I(usec))
/** @} */
/*===========================================================================*/
@@ -255,14 +255,14 @@ extern "C" {
tprio_t chThdSetPriority(tprio_t newprio);
void chThdTerminate(thread_t *tp);
msg_t chThdSuspendS(thread_reference_t *trp);
- msg_t chThdSuspendTimeoutS(thread_reference_t *trp, systime_t timeout);
+ msg_t chThdSuspendTimeoutS(thread_reference_t *trp, sysinterval_t timeout);
void chThdResumeI(thread_reference_t *trp, msg_t msg);
void chThdResumeS(thread_reference_t *trp, msg_t msg);
void chThdResume(thread_reference_t *trp, msg_t msg);
- msg_t chThdEnqueueTimeoutS(threads_queue_t *tqp, systime_t timeout);
+ msg_t chThdEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout);
void chThdDequeueNextI(threads_queue_t *tqp, msg_t msg);
void chThdDequeueAllI(threads_queue_t *tqp, msg_t msg);
- void chThdSleep(systime_t time);
+ void chThdSleep(sysinterval_t time);
void chThdSleepUntil(systime_t time);
systime_t chThdSleepUntilWindowed(systime_t prev, systime_t next);
void chThdYield(void);
@@ -376,9 +376,9 @@ static inline thread_t *chThdStartI(thread_t *tp) {
}
/**
- * @brief Suspends the invoking thread for the specified time.
+ * @brief Suspends the invoking thread for the specified number of ticks.
*
- * @param[in] time the delay in system ticks, the special values are
+ * @param[in] ticks the delay in system ticks, the special values are
* handled as follow:
* - @a TIME_INFINITE the thread enters an infinite sleep
* state.
@@ -387,11 +387,11 @@ static inline thread_t *chThdStartI(thread_t *tp) {
*
* @sclass
*/
-static inline void chThdSleepS(systime_t time) {
+static inline void chThdSleepS(sysinterval_t ticks) {
- chDbgCheck(time != TIME_IMMEDIATE);
+ chDbgCheck(ticks != TIME_IMMEDIATE);
- (void) chSchGoSleepTimeoutS(CH_STATE_SLEEPING, time);
+ (void) chSchGoSleepTimeoutS(CH_STATE_SLEEPING, ticks);
}
/**
diff --git a/os/rt/include/chtime.h b/os/rt/include/chtime.h
new file mode 100644
index 000000000..5a09735eb
--- /dev/null
+++ b/os/rt/include/chtime.h
@@ -0,0 +1,515 @@
+/*
+ ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio.
+
+ This file is part of ChibiOS.
+
+ ChibiOS is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file chtime.h
+ * @brief Time and intervals macros and structures.
+ *
+ * @addtogroup time_intervals
+ * @{
+ */
+
+#ifndef CHTIME_H
+#define CHTIME_H
+
+/*===========================================================================*/
+/* Module constants. */
+/*===========================================================================*/
+
+/**
+ * @name Special time constants
+ * @{
+ */
+/**
+ * @brief Zero interval specification for some functions with a timeout
+ * specification.
+ * @note Not all functions accept @p TIME_IMMEDIATE as timeout parameter,
+ * see the specific function documentation.
+ */
+#define TIME_IMMEDIATE ((sysinterval_t)0)
+
+/**
+ * @brief Infinite interval specification for all functions with a timeout
+ * specification.
+ * @note Not all functions accept @p TIME_INFINITE as timeout parameter,
+ * see the specific function documentation.
+ */
+#define TIME_INFINITE ((sysinterval_t)-1)
+
+/**
+ * @brief Maximum interval constant usable as timeout.
+ */
+#define TIME_MAXIMUM ((sysinterval_t)-2)
+/** @} */
+
+/*===========================================================================*/
+/* Module pre-compile time settings. */
+/*===========================================================================*/
+
+/**
+ * @brief System time counter resolution.
+ * @note Allowed values are 16, 32 or 64 bits.
+ */
+#if !defined(CH_CFG_ST_RESOLUTION) || defined(__DOXYGEN__)
+#define CH_CFG_ST_RESOLUTION 32
+#endif
+
+/**
+ * @brief System tick frequency.
+ * @details Frequency of the system timer that drives the system ticks. This
+ * setting also defines the system tick time unit.
+ */
+#if !defined(CH_CFG_ST_FREQUENCY) || defined(__DOXYGEN__)
+#define CH_CFG_ST_FREQUENCY 1000
+#endif
+
+/**
+ * @brief Time intervals data size.
+ * @note Allowed values are 16, 32 or 64 bits.
+ */
+#if !defined(CH_CFG_INTERVALS_SIZE) || defined(__DOXYGEN__)
+#define CH_CFG_INTERVALS_SIZE 32
+#endif
+
+/**
+ * @brief Time types data size.
+ * @note Allowed values are 16 or 32 bits.
+ */
+#if !defined(CH_CFG_TIME_TYPES_SIZE) || defined(__DOXYGEN__)
+#define CH_CFG_TIME_TYPES_SIZE 32
+#endif
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+#if (CH_CFG_ST_RESOLUTION != 16) && (CH_CFG_ST_RESOLUTION != 32) && \
+ (CH_CFG_ST_RESOLUTION != 64)
+#error "invalid CH_CFG_ST_RESOLUTION specified, must be 16, 32 or 64"
+#endif
+
+#if CH_CFG_ST_FREQUENCY < 10
+#error "invalid CH_CFG_ST_FREQUENCY specified, must be >= 10"
+#endif
+
+#if (CH_CFG_INTERVALS_SIZE != 16) && (CH_CFG_INTERVALS_SIZE != 32) && \
+ (CH_CFG_INTERVALS_SIZE != 64)
+#error "invalid CH_CFG_INTERVALS_SIZE specified, must be 16, 32 or 64"
+#endif
+
+#if (CH_CFG_TIME_TYPES_SIZE != 16) && (CH_CFG_TIME_TYPES_SIZE != 32)
+#error "invalid CH_CFG_TIME_TYPES_SIZE specified, must be 16 or 32"
+#endif
+
+#if CH_CFG_INTERVALS_SIZE < CH_CFG_ST_RESOLUTION
+#error "CH_CFG_INTERVALS_SIZE must be >= CH_CFG_ST_RESOLUTION"
+#endif
+
+/*===========================================================================*/
+/* Module data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief Type of system time.
+ * @note It is selectable in configuration between 16, 32 or 64 bits
+ */
+#if (CH_CFG_ST_RESOLUTION == 64) || defined(__DOXYGEN__)
+typedef uint64_t systime_t;
+#elif CH_CFG_ST_RESOLUTION == 32
+typedef uint32_t systime_t;
+#elif CH_CFG_ST_RESOLUTION == 16
+typedef uint16_t systime_t;
+#endif
+
+/**
+ * @brief Type of time interval.
+ * @note It is selectable in configuration between 16, 32 or 64 bits
+ */
+#if (CH_CFG_ST_RESOLUTION == 64) || defined(__DOXYGEN__)
+typedef uint64_t sysinterval_t;
+#elif CH_CFG_ST_RESOLUTION == 32
+typedef uint32_t sysinterval_t;
+#elif CH_CFG_ST_RESOLUTION == 16
+typedef uint16_t sysinterval_t;
+#endif
+
+#if (CH_CFG_TIME_TYPES_SIZE == 32) || defined(__DOXYGEN__)
+/**
+ * @brief Type of seconds.
+ * @note It is selectable in configuration between 16 or 32 bits
+ */
+typedef uint32_t time_secs_t;
+
+/**
+ * @brief Type of milliseconds.
+ * @note It is selectable in configuration between 16 or 32 bits
+ */
+typedef uint32_t time_msecs_t;
+
+/**
+ * @brief Type of microseconds.
+ * @note It is selectable in configuration between 16 or 32 bits
+ */
+typedef uint32_t time_usecs_t;
+
+/**
+ * @brief Type of time conversion variable.
+ * @note This type must have double width than other time types, it is
+ * only used internally for conversions.
+ */
+typedef uint64_t time_conv_t;
+
+#else
+typedef uint16_t time_secs_t;
+typedef uint16_t time_msecs_t;
+typedef uint16_t time_usecs_t;
+typedef uint32_t time_conv_t;
+#endif
+
+/*===========================================================================*/
+/* Module macros. */
+/*===========================================================================*/
+
+
+/**
+ * @name Fast time conversion utilities
+ * @{
+ */
+/**
+ * @brief Seconds to time interval.
+ * @details Converts from seconds to system ticks number.
+ * @note The result is rounded upward to the next tick boundary.
+ * @note Use of this macro for large values is not secure because
+ * integer overflows, make sure your value can be correctly
+ * converted.
+ *
+ * @param[in] secs number of seconds
+ * @return The number of ticks.
+ *
+ * @api
+ */
+#define TIME_S2I(secs) \
+ ((sysinterval_t)((time_conv_t)(secs) * (time_conv_t)CH_CFG_ST_FREQUENCY))
+
+/**
+ * @brief Milliseconds to time interval.
+ * @details Converts from milliseconds to system ticks number.
+ * @note The result is rounded upward to the next tick boundary.
+ * @note Use of this macro for large values is not secure because
+ * integer overflows, make sure your value can be correctly
+ * converted.
+ *
+ * @param[in] msecs number of milliseconds
+ * @return The number of ticks.
+ *
+ * @api
+ */
+#define TIME_MS2I(msecs) \
+ ((sysinterval_t)((((time_conv_t)(msecs) * \
+ (time_conv_t)CH_CFG_ST_FREQUENCY) + \
+ (time_conv_t)999) / (time_conv_t)1000))
+
+/**
+ * @brief Microseconds to time interval.
+ * @details Converts from microseconds to system ticks number.
+ * @note The result is rounded upward to the next tick boundary.
+ * @note Use of this macro for large values is not secure because
+ * integer overflows, make sure your value can be correctly
+ * converted.
+ *
+ * @param[in] usecs number of microseconds
+ * @return The number of ticks.
+ *
+ * @api
+ */
+#define TIME_US2I(usecs) \
+ ((sysinterval_t)((((time_conv_t)(usecs) * \
+ (time_conv_t)CH_CFG_ST_FREQUENCY) + \
+ (time_conv_t)999999) / (time_conv_t)1000000))
+
+/**
+ * @brief Time interval to seconds.
+ * @details Converts from system ticks number to seconds.
+ * @note The result is rounded up to the next second boundary.
+ * @note Use of this macro for large values is not secure because
+ * integer overflows, make sure your value can be correctly
+ * converted.
+ *
+ * @param[in] interval interval in ticks
+ * @return The number of seconds.
+ *
+ * @api
+ */
+#define TIME_I2S(interval) \
+ (time_secs_t)(((time_conv_t)(interval) + \
+ (time_conv_t)CH_CFG_ST_FREQUENCY - \
+ (time_conv_t)1) / (time_conv_t)CH_CFG_ST_FREQUENCY)
+
+/**
+ * @brief Time interval to milliseconds.
+ * @details Converts from system ticks number to milliseconds.
+ * @note The result is rounded up to the next millisecond boundary.
+ * @note Use of this macro for large values is not secure because
+ * integer overflows, make sure your value can be correctly
+ * converted.
+ *
+ * @param[in] interval interval in ticks
+ * @return The number of milliseconds.
+ *
+ * @api
+ */
+#define TIME_I2MS(interval) \
+ (time_msecs_t)((((time_conv_t)(interval) * (time_conv_t)1000) + \
+ (time_conv_t)CH_CFG_ST_FREQUENCY - (time_conv_t)1) / \
+ (time_conv_t)CH_CFG_ST_FREQUENCY)
+
+/**
+ * @brief Time interval to microseconds.
+ * @details Converts from system ticks number to microseconds.
+ * @note The result is rounded up to the next microsecond boundary.
+ * @note Use of this macro for large values is not secure because
+ * integer overflows, make sure your value can be correctly
+ * converted.
+ *
+ * @param[in] interval interval in ticks
+ * @return The number of microseconds.
+ *
+ * @api
+ */
+#define TIME_I2US(interval) \
+ (time_msecs_t)((((time_conv_t)(interval) * (time_conv_t)1000000) + \
+ (time_conv_t)CH_CFG_ST_FREQUENCY - (time_conv_t)1) / \
+ (time_conv_t)CH_CFG_ST_FREQUENCY)
+/** @} */
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+/*
+ * Virtual Timers APIs.
+ */
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+/*===========================================================================*/
+/* Module inline functions. */
+/*===========================================================================*/
+
+/**
+ * @name Secure time conversion utilities
+ * @{
+ */
+/**
+ * @brief Seconds to time interval.
+ * @details Converts from seconds to system ticks number.
+ * @note The result is rounded upward to the next tick boundary.
+ *
+ * @param[in] sec number of seconds
+ * @return The number of ticks.
+ *
+ * @special
+ */
+static inline sysinterval_t chTimeS2I(time_secs_t secs) {
+ time_conv_t ticks;
+
+ ticks = (time_conv_t)secs * (time_conv_t)CH_CFG_ST_FREQUENCY;
+
+ chDbgAssert(ticks <= (time_conv_t)TIME_MAXIMUM,
+ "conversion overflow");
+
+ return (sysinterval_t)ticks;
+}
+
+/**
+ * @brief Milliseconds to time interval.
+ * @details Converts from milliseconds to system ticks number.
+ * @note The result is rounded upward to the next tick boundary.
+ *
+ * @param[in] msec number of milliseconds
+ * @return The number of ticks.
+ *
+ * @special
+ */
+static inline sysinterval_t chTimeMS2I(time_msecs_t msec) {
+ time_conv_t ticks;
+
+ ticks = (((time_conv_t)msec * (time_conv_t)CH_CFG_ST_FREQUENCY) +
+ (time_conv_t)999) / (time_conv_t)1000;
+
+ chDbgAssert(ticks <= (time_conv_t)TIME_MAXIMUM,
+ "conversion overflow");
+
+ return (sysinterval_t)ticks;
+}
+
+/**
+ * @brief Microseconds to time interval.
+ * @details Converts from microseconds to system ticks number.
+ * @note The result is rounded upward to the next tick boundary.
+ *
+ * @param[in] usec number of microseconds
+ * @return The number of ticks.
+ *
+ * @special
+ */
+static inline sysinterval_t chTimeUS2I(time_usecs_t usec) {
+ time_conv_t ticks;
+
+ ticks = (((time_conv_t)usec * (time_conv_t)CH_CFG_ST_FREQUENCY) +
+ (time_conv_t)999999) / (time_conv_t)1000000;
+
+ chDbgAssert(ticks <= (time_conv_t)TIME_MAXIMUM,
+ "conversion overflow");
+
+ return (sysinterval_t)ticks;
+}
+
+/**
+ * @brief Time interval to seconds.
+ * @details Converts from system interval to seconds.
+ * @note The result is rounded up to the next second boundary.
+ *
+ * @param[in] interval interval in ticks
+ * @return The number of seconds.
+ *
+ * @special
+ */
+static inline time_secs_t chTimeI2S(sysinterval_t interval) {
+ time_conv_t secs;
+
+ secs = ((time_conv_t)interval +
+ (time_conv_t)CH_CFG_ST_FREQUENCY -
+ (time_conv_t)1) / (time_conv_t)CH_CFG_ST_FREQUENCY;
+
+ chDbgAssert(secs < (time_conv_t)((time_secs_t)-1),
+ "conversion overflow");
+
+ return (time_secs_t)secs;
+}
+
+/**
+ * @brief Time interval to milliseconds.
+ * @details Converts from system interval to milliseconds.
+ * @note The result is rounded up to the next millisecond boundary.
+ *
+ * @param[in] interval interval in ticks
+ * @return The number of milliseconds.
+ *
+ * @special
+ */
+static inline time_msecs_t chTimeI2MS(sysinterval_t interval) {
+ time_conv_t msecs;
+
+ msecs = (((time_conv_t)interval * (time_conv_t)1000) +
+ (time_conv_t)CH_CFG_ST_FREQUENCY - (time_conv_t)1) /
+ (time_conv_t)CH_CFG_ST_FREQUENCY;
+
+ chDbgAssert(msecs < (time_conv_t)((time_msecs_t)-1),
+ "conversion overflow");
+
+ return (time_msecs_t)msecs;
+}
+
+/**
+ * @brief Time interval to microseconds.
+ * @details Converts from system interval to microseconds.
+ * @note The result is rounded up to the next microsecond boundary.
+ *
+ * @param[in] interval interval in ticks
+ * @return The number of microseconds.
+ *
+ * @special
+ */
+static inline time_usecs_t chTimeI2US(sysinterval_t interval) {
+ time_conv_t usecs;
+
+ usecs = (((time_conv_t)interval * (time_conv_t)1000000) +
+ (time_conv_t)CH_CFG_ST_FREQUENCY - (time_conv_t)1) /
+ (time_conv_t)CH_CFG_ST_FREQUENCY;
+
+ chDbgAssert(usecs <= (time_conv_t)((time_usecs_t)-1),
+ "conversion overflow");
+
+ return (time_usecs_t)usecs;
+}
+
+/**
+ * @brief Adds an interval to a system time returning a system time.
+ *
+ * @param[in] systime base system time
+ * @param[in] interval interval to be added
+ * @return The new system time.
+ *
+ * @xclass
+ */
+static inline systime_t chTimeAddX(systime_t systime,
+ sysinterval_t interval) {
+
+ chDbgCheck(interval <= (sysinterval_t)((systime_t)-1));
+
+ return systime + (systime_t)interval;
+}
+
+/**
+ * @brief Subtracts two system times returning an interval.
+ *
+ * @param[in] start first system time
+ * @param[in] end second system time
+ * @return The interval representing the time difference.
+ *
+ * @xclass
+ */
+static inline sysinterval_t chTimeSubtractX(systime_t start,
+ systime_t end) {
+
+ return (sysinterval_t)(end - start);
+}
+
+
+/**
+ * @brief Checks if the specified time is within the specified time range.
+ * @note When start==end then the function returns always true because the
+ * whole time range is specified.
+ *
+ * @param[in] time the time to be verified
+ * @param[in] start the start of the time window (inclusive)
+ * @param[in] end the end of the time window (non inclusive)
+ * @retval true current time within the specified time window.
+ * @retval false current time not within the specified time window.
+ *
+ * @xclass
+ */
+static inline bool chTimeIsInRangeX(systime_t time,
+ systime_t start,
+ systime_t end) {
+
+ return (bool)((systime_t)(time - start) < (systime_t)(end - start));
+}
+
+/** @} */
+
+#endif /* CHTIME_H */
+
+/** @} */
diff --git a/os/rt/include/chvt.h b/os/rt/include/chvt.h
index dc08dbe07..92adaaf7b 100644
--- a/os/rt/include/chvt.h
+++ b/os/rt/include/chvt.h
@@ -32,37 +32,6 @@
/* Module constants. */
/*===========================================================================*/
-/**
- * @name Special time constants
- * @{
- */
-/**
- * @brief Zero time specification for some functions with a timeout
- * specification.
- * @note Not all functions accept @p TIME_IMMEDIATE as timeout parameter,
- * see the specific function documentation.
- */
-#define TIME_IMMEDIATE ((systime_t)0)
-
-/**
- * @brief Infinite time specification for all functions with a timeout
- * specification.
- * @note Not all functions accept @p TIME_INFINITE as timeout parameter,
- * see the specific function documentation.
- */
-#define TIME_INFINITE ((systime_t)-1)
-
-/**
- * @brief Maximum time constant.
- */
-#define TIME_MAXIMUM ((systime_t)-2)
-/** @} */
-
-/**
- * @brief Maximum unsigned integer.
- */
-#define __UINT_MAX ((unsigned int)-1)
-
/*===========================================================================*/
/* Module pre-compile time settings. */
/*===========================================================================*/
@@ -71,14 +40,6 @@
/* Derived constants and error checks. */
/*===========================================================================*/
-#if (CH_CFG_ST_RESOLUTION != 16) && (CH_CFG_ST_RESOLUTION != 32)
-#error "invalid CH_CFG_ST_RESOLUTION specified, must be 16 or 32"
-#endif
-
-#if CH_CFG_ST_FREQUENCY <= 0
-#error "invalid CH_CFG_ST_FREQUENCY specified, must be greater than zero"
-#endif
-
#if (CH_CFG_ST_TIMEDELTA < 0) || (CH_CFG_ST_TIMEDELTA == 1)
#error "invalid CH_CFG_ST_TIMEDELTA specified, must " \
"be zero or greater than one"
@@ -100,108 +61,6 @@
/* Module macros. */
/*===========================================================================*/
-/**
- * @name Fast time conversion utilities
- * @{
- */
-/**
- * @brief Seconds to system ticks.
- * @details Converts from seconds to system ticks number.
- * @note The result is rounded upward to the next tick boundary.
- * @note Use of this macro for large values is not secure because
- * integer overflows, make sure your value can be correctly
- * converted.
- *
- * @param[in] sec number of seconds
- * @return The number of ticks.
- *
- * @api
- */
-#define S2ST(sec) \
- ((systime_t)((uint32_t)(sec) * (uint32_t)CH_CFG_ST_FREQUENCY))
-
-/**
- * @brief Milliseconds to system ticks.
- * @details Converts from milliseconds to system ticks number.
- * @note The result is rounded upward to the next tick boundary.
- * @note Use of this macro for large values is not secure because
- * integer overflows, make sure your value can be correctly
- * converted.
- *
- * @param[in] msec number of milliseconds
- * @return The number of ticks.
- *
- * @api
- */
-#define MS2ST(msec) \
- ((systime_t)(((((uint32_t)(msec)) * \
- ((uint32_t)CH_CFG_ST_FREQUENCY)) + 999UL) / 1000UL))
-
-/**
- * @brief Microseconds to system ticks.
- * @details Converts from microseconds to system ticks number.
- * @note The result is rounded upward to the next tick boundary.
- * @note Use of this macro for large values is not secure because
- * integer overflows, make sure your value can be correctly
- * converted.
- *
- * @param[in] usec number of microseconds
- * @return The number of ticks.
- *
- * @api
- */
-#define US2ST(usec) \
- ((systime_t)(((((uint32_t)(usec)) * \
- ((uint32_t)CH_CFG_ST_FREQUENCY)) + 999999UL) / 1000000UL))
-
-/**
- * @brief System ticks to seconds.
- * @details Converts from system ticks number to seconds.
- * @note The result is rounded up to the next second boundary.
- * @note Use of this macro for large values is not secure because
- * integer overflows, make sure your value can be correctly
- * converted.
- *
- * @param[in] n number of system ticks
- * @return The number of seconds.
- *
- * @api
- */
-#define ST2S(n) (((n) + CH_CFG_ST_FREQUENCY - 1UL) / CH_CFG_ST_FREQUENCY)
-
-/**
- * @brief System ticks to milliseconds.
- * @details Converts from system ticks number to milliseconds.
- * @note The result is rounded up to the next millisecond boundary.
- * @note Use of this macro for large values is not secure because
- * integer overflows, make sure your value can be correctly
- * converted.
- *
- * @param[in] n number of system ticks
- * @return The number of milliseconds.
- *
- * @api
- */
-#define ST2MS(n) (((n) * 1000UL + CH_CFG_ST_FREQUENCY - 1UL) / \
- CH_CFG_ST_FREQUENCY)
-
-/**
- * @brief System ticks to microseconds.
- * @details Converts from system ticks number to microseconds.
- * @note The result is rounded up to the next microsecond boundary.
- * @note Use of this macro for large values is not secure because
- * integer overflows, make sure your value can be correctly
- * converted.
- *
- * @param[in] n number of system ticks
- * @return The number of microseconds.
- *
- * @api
- */
-#define ST2US(n) (((n) * 1000000UL + CH_CFG_ST_FREQUENCY - 1UL) / \
- CH_CFG_ST_FREQUENCY)
-/** @} */
-
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
@@ -225,142 +84,6 @@ extern "C" {
/*===========================================================================*/
/**
- * @name Secure time conversion utilities
- * @{
- */
-/**
- * @brief Seconds to system ticks.
- * @details Converts from seconds to system ticks number.
- * @note The result is rounded upward to the next tick boundary.
- * @note This function uses a 64 bits internal representation,
- * use with non-constant parameters can lead to inefficient
- * code because 64 bits arithmetic would be used at runtime.
- *
- * @param[in] sec number of seconds
- * @return The number of ticks.
- *
- * @api
- */
-static inline systime_t LL_S2ST(unsigned int sec) {
- uint64_t ticks = (uint64_t)sec * (uint64_t)CH_CFG_ST_FREQUENCY;
-
- chDbgAssert(ticks <= (uint64_t)TIME_MAXIMUM, "conversion overflow");
-
- return (systime_t)ticks;
-}
-
-/**
- * @brief Milliseconds to system ticks.
- * @details Converts from milliseconds to system ticks number.
- * @note The result is rounded upward to the next tick boundary.
- * @note This function uses a 64 bits internal representation,
- * use with non-constant parameters can lead to inefficient
- * code because 64 bits arithmetic would be used at runtime.
- *
- * @param[in] msec number of milliseconds
- * @return The number of ticks.
- *
- * @api
- */
-static inline systime_t LL_MS2ST(unsigned int msec) {
- uint64_t ticks = (((uint64_t)msec * (uint64_t)CH_CFG_ST_FREQUENCY) + 999ULL)
- / 1000ULL;
-
- chDbgAssert(ticks <= (uint64_t)TIME_MAXIMUM, "conversion overflow");
-
- return (systime_t)ticks;
-}
-
-/**
- * @brief Microseconds to system ticks.
- * @details Converts from microseconds to system ticks number.
- * @note The result is rounded upward to the next tick boundary.
- * @note This function uses a 64 bits internal representation,
- * use with non-constant parameters can lead to inefficient
- * code because 64 bits arithmetic would be used at runtime.
- *
- * @param[in] usec number of microseconds
- * @return The number of ticks.
- *
- * @api
- */
-static inline systime_t LL_US2ST(unsigned int usec) {
- uint64_t ticks = (((uint64_t)usec * (uint64_t)CH_CFG_ST_FREQUENCY) + 999999ULL)
- / 1000000ULL;
-
- chDbgAssert(ticks <= (uint64_t)TIME_MAXIMUM, "conversion overflow");
-
- return (systime_t)ticks;
-}
-
-/**
- * @brief System ticks to seconds.
- * @details Converts from system ticks number to seconds.
- * @note The result is rounded up to the next second boundary.
- * @note This function uses a 64 bits internal representation,
- * use with non-constant parameters can lead to inefficient
- * code because 64 bits arithmetic would be used at runtime.
- *
- * @param[in] n number of system ticks
- * @return The number of seconds.
- *
- * @api
- */
-static inline unsigned int LL_ST2S(systime_t n) {
- uint64_t sec = ((uint64_t)n + (uint64_t)CH_CFG_ST_FREQUENCY - 1ULL)
- / (uint64_t)CH_CFG_ST_FREQUENCY;
-
- chDbgAssert(sec < (uint64_t)__UINT_MAX, "conversion overflow");
-
- return (unsigned int)sec;
-}
-
-/**
- * @brief System ticks to milliseconds.
- * @details Converts from system ticks number to milliseconds.
- * @note The result is rounded up to the next millisecond boundary.
- * @note This function uses a 64 bits internal representation,
- * use with non-constant parameters can lead to inefficient
- * code because 64 bits arithmetic would be used at runtime.
- *
- * @param[in] n number of system ticks
- * @return The number of milliseconds.
- *
- * @api
- */
-static inline unsigned int LL_ST2MS(systime_t n) {
- uint64_t msec = (((uint64_t)n * 1000ULL) + (uint64_t)CH_CFG_ST_FREQUENCY - 1ULL)
- / (uint64_t)CH_CFG_ST_FREQUENCY;
-
- chDbgAssert(msec < (uint64_t)__UINT_MAX, "conversion overflow");
-
- return (unsigned int)msec;
-}
-
-/**
- * @brief System ticks to microseconds.
- * @details Converts from system ticks number to microseconds.
- * @note The result is rounded up to the next microsecond boundary.
- * @note This function uses a 64 bits internal representation,
- * use with non-constant parameters can lead to inefficient
- * code because 64 bits arithmetic would be used at runtime.
- *
- * @param[in] n number of system ticks
- * @return The number of microseconds.
- *
- * @api
- */
-static inline unsigned int LL_ST2US(systime_t n) {
- uint64_t usec = (((uint64_t)n * 1000000ULL) + (uint64_t)CH_CFG_ST_FREQUENCY - 1ULL)
- / (uint64_t)CH_CFG_ST_FREQUENCY;
-
- chDbgAssert(usec < (uint64_t)__UINT_MAX, "conversion overflow");
-
- return (unsigned int)usec;
-}
-/** @} */
-
-/**
* @brief Initializes a @p virtual_timer_t object.
* @note Initializing a timer object is not strictly required because
* the function @p chVTSetI() initializes the object too. This