From d8b32d7f63c8453135249734f8b542856947e83a Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Thu, 12 Oct 2017 12:25:48 +0000 Subject: Rework of virtual timers in RT5, preparation. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/rt5_dev_point1@10813 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/STM32/RT-STM32F746G-DISCOVERY/Makefile | 2 +- ...F746G-DISCOVERY (OpenOCD, Flash and Run).launch | 2 +- ...STM32F746G-DISCOVERY (OpenOCD, Just Run).launch | 2 +- os/hal/osal/rt/osal.h | 2 +- os/rt/include/chschd.h | 4 +- os/rt/include/chtime.h | 3 +- os/rt/include/chvt.h | 62 ++++++++-------------- os/rt/src/chthreads.c | 4 +- os/rt/src/chvt.c | 22 ++++---- 9 files changed, 42 insertions(+), 61 deletions(-) diff --git a/demos/STM32/RT-STM32F746G-DISCOVERY/Makefile b/demos/STM32/RT-STM32F746G-DISCOVERY/Makefile index 40b040625..94853d1b3 100644 --- a/demos/STM32/RT-STM32F746G-DISCOVERY/Makefile +++ b/demos/STM32/RT-STM32F746G-DISCOVERY/Makefile @@ -5,7 +5,7 @@ # Compiler options here. ifeq ($(USE_OPT),) - USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16 + USE_OPT = -O0 -ggdb -fomit-frame-pointer -falign-functions=16 endif # C specific options here (added to USE_OPT). diff --git a/demos/STM32/RT-STM32F746G-DISCOVERY/debug/RT-STM32F746G-DISCOVERY (OpenOCD, Flash and Run).launch b/demos/STM32/RT-STM32F746G-DISCOVERY/debug/RT-STM32F746G-DISCOVERY (OpenOCD, Flash and Run).launch index 05650c142..92f436493 100644 --- a/demos/STM32/RT-STM32F746G-DISCOVERY/debug/RT-STM32F746G-DISCOVERY (OpenOCD, Flash and Run).launch +++ b/demos/STM32/RT-STM32F746G-DISCOVERY/debug/RT-STM32F746G-DISCOVERY (OpenOCD, Flash and Run).launch @@ -33,7 +33,7 @@ - + diff --git a/demos/STM32/RT-STM32F746G-DISCOVERY/debug/RT-STM32F746G-DISCOVERY (OpenOCD, Just Run).launch b/demos/STM32/RT-STM32F746G-DISCOVERY/debug/RT-STM32F746G-DISCOVERY (OpenOCD, Just Run).launch index 3e12199dc..c64a7eb53 100644 --- a/demos/STM32/RT-STM32F746G-DISCOVERY/debug/RT-STM32F746G-DISCOVERY (OpenOCD, Just Run).launch +++ b/demos/STM32/RT-STM32F746G-DISCOVERY/debug/RT-STM32F746G-DISCOVERY (OpenOCD, Just Run).launch @@ -33,7 +33,7 @@ - + diff --git a/os/hal/osal/rt/osal.h b/os/hal/osal/rt/osal.h index 727cea623..07072e88f 100644 --- a/os/hal/osal/rt/osal.h +++ b/os/hal/osal/rt/osal.h @@ -636,7 +636,7 @@ static inline bool osalOsIsTimeWithinX(systime_t time, systime_t start, systime_t end) { - return chVTIsTimeWithinX(time, start, end); + return chTimeIsInRangeX(time, start, end); } /** diff --git a/os/rt/include/chschd.h b/os/rt/include/chschd.h index 7f25279a9..9d3b5dd4e 100644 --- a/os/rt/include/chschd.h +++ b/os/rt/include/chschd.h @@ -325,7 +325,7 @@ struct ch_thread { struct ch_virtual_timer { virtual_timer_t *next; /**< @brief Next timer in the list. */ virtual_timer_t *prev; /**< @brief Previous timer in the list. */ - systime_t delta; /**< @brief Time delta before timeout. */ + sysinterval_t delta; /**< @brief Time delta before timeout. */ vtfunc_t func; /**< @brief Timer callback function pointer. */ void *par; /**< @brief Timer callback function @@ -343,7 +343,7 @@ struct ch_virtual_timers_list { list. */ virtual_timer_t *prev; /**< @brief Last timer in the delta list. */ - systime_t delta; /**< @brief Must be initialized to -1. */ + sysinterval_t delta; /**< @brief Must be initialized to -1. */ #if (CH_CFG_ST_TIMEDELTA == 0) || defined(__DOXYGEN__) volatile systime_t systime; /**< @brief System Time counter. */ #endif diff --git a/os/rt/include/chtime.h b/os/rt/include/chtime.h index 5a09735eb..046cde224 100644 --- a/os/rt/include/chtime.h +++ b/os/rt/include/chtime.h @@ -481,8 +481,7 @@ static inline systime_t chTimeAddX(systime_t systime, * * @xclass */ -static inline sysinterval_t chTimeSubtractX(systime_t start, - systime_t end) { +static inline sysinterval_t chTimeDiffX(systime_t start, systime_t end) { return (sysinterval_t)(end - start); } diff --git a/os/rt/include/chvt.h b/os/rt/include/chvt.h index 92adaaf7b..70a70465e 100644 --- a/os/rt/include/chvt.h +++ b/os/rt/include/chvt.h @@ -149,30 +149,9 @@ static inline systime_t chVTGetSystemTime(void) { * * @xclass */ -static inline systime_t chVTTimeElapsedSinceX(systime_t start) { +static inline sysinterval_t chVTTimeElapsedSinceX(systime_t start) { - return chVTGetSystemTimeX() - start; -} - -/** - * @brief Checks if the specified time is within the specified time window. - * @note When start==end then the function returns always true because the - * whole time range is specified. - * @note This function can be called from any context. - * - * @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 chVTIsTimeWithinX(systime_t time, - systime_t start, - systime_t end) { - - return (bool)((systime_t)(time - start) < (systime_t)(end - start)); + return chTimeDiffX(chVTGetSystemTimeX(), start); } /** @@ -190,7 +169,7 @@ static inline bool chVTIsTimeWithinX(systime_t time, */ static inline bool chVTIsSystemTimeWithinX(systime_t start, systime_t end) { - return chVTIsTimeWithinX(chVTGetSystemTimeX(), start, end); + return chTimeIsInRangeX(chVTGetSystemTimeX(), start, end); } /** @@ -208,7 +187,7 @@ static inline bool chVTIsSystemTimeWithinX(systime_t start, systime_t end) { */ static inline bool chVTIsSystemTimeWithin(systime_t start, systime_t end) { - return chVTIsTimeWithinX(chVTGetSystemTime(), start, end); + return chTimeIsInRangeX(chVTGetSystemTime(), start, end); } /** @@ -227,7 +206,7 @@ static inline bool chVTIsSystemTimeWithin(systime_t start, systime_t end) { * * @iclass */ -static inline bool chVTGetTimersStateI(systime_t *timep) { +static inline bool chVTGetTimersStateI(sysinterval_t *timep) { chDbgCheckClassI(); @@ -239,8 +218,10 @@ static inline bool chVTGetTimersStateI(systime_t *timep) { #if CH_CFG_ST_TIMEDELTA == 0 *timep = ch.vtlist.next->delta; #else - *timep = ch.vtlist.lasttime + ch.vtlist.next->delta + - CH_CFG_ST_TIMEDELTA - chVTGetSystemTimeX(); + *timep = chTimeDiffX(chVTGetSystemTimeX(), + chTimeAddX(ch.vtlist.lasttime, + ch.vtlist.next->delta + + (sysinterval_t)CH_CFG_ST_TIMEDELTA)); #endif } @@ -340,7 +321,7 @@ static inline void chVTReset(virtual_timer_t *vtp) { * * @iclass */ -static inline void chVTSetI(virtual_timer_t *vtp, systime_t delay, +static inline void chVTSetI(virtual_timer_t *vtp, sysinterval_t delay, vtfunc_t vtfunc, void *par) { chVTResetI(vtp); @@ -369,7 +350,7 @@ static inline void chVTSetI(virtual_timer_t *vtp, systime_t delay, * * @api */ -static inline void chVTSet(virtual_timer_t *vtp, systime_t delay, +static inline void chVTSet(virtual_timer_t *vtp, sysinterval_t delay, vtfunc_t vtfunc, void *par) { chSysLock(); @@ -395,7 +376,7 @@ static inline void chVTDoTickI(void) { if (&ch.vtlist != (virtual_timers_list_t *)ch.vtlist.next) { /* The list is not empty, processing elements on top.*/ --ch.vtlist.next->delta; - while (ch.vtlist.next->delta == (systime_t)0) { + while (ch.vtlist.next->delta == (sysinterval_t)0) { virtual_timer_t *vtp; vtfunc_t fn; @@ -411,7 +392,8 @@ static inline void chVTDoTickI(void) { } #else /* CH_CFG_ST_TIMEDELTA > 0 */ virtual_timer_t *vtp; - systime_t now, delta; + systime_t now; + sysinterval_t delta; /* First timer to be processed.*/ vtp = ch.vtlist.next; @@ -421,11 +403,11 @@ static inline void chVTDoTickI(void) { note that the loop is stopped by the timers header having "ch.vtlist.vt_delta == (systime_t)-1" which is greater than all deltas.*/ - while (vtp->delta <= (systime_t)(now - ch.vtlist.lasttime)) { + while (vtp->delta <= chTimeDiffX(ch.vtlist.lasttime, now)) { vtfunc_t fn; /* The "last time" becomes this timer's expiration time.*/ - ch.vtlist.lasttime += vtp->delta; + ch.vtlist.lasttime = chTimeAddX(ch.vtlist.lasttime, vtp->delta); vtp->next->prev = (virtual_timer_t *)&ch.vtlist; ch.vtlist.next = vtp->next; @@ -461,14 +443,14 @@ static inline void chVTDoTickI(void) { } /* Recalculating the next alarm time.*/ - delta = ch.vtlist.lasttime + vtp->delta - now; - if (delta < (systime_t)CH_CFG_ST_TIMEDELTA) { - delta = (systime_t)CH_CFG_ST_TIMEDELTA; + delta = chTimeDiffX(now, chTimeAddX(ch.vtlist.lasttime, vtp->delta)); + if (delta < (sysinterval_t)CH_CFG_ST_TIMEDELTA) { + delta = (sysinterval_t)CH_CFG_ST_TIMEDELTA; } - port_timer_set_alarm(now + delta); + port_timer_set_alarm(chTimeAddX(now, delta)); - chDbgAssert((chVTGetSystemTimeX() - ch.vtlist.lasttime) <= - (now + delta - ch.vtlist.lasttime), + chDbgAssert(chTimeDiffX(ch.vtlist.lasttime, chVTGetSystemTimeX()) <= + chTimeDiffX(ch.vtlist.lasttime, chTimeAddX(now, delta)), "exceeding delta"); #endif /* CH_CFG_ST_TIMEDELTA > 0 */ } diff --git a/os/rt/src/chthreads.c b/os/rt/src/chthreads.c index b462df09f..49abadb2d 100644 --- a/os/rt/src/chthreads.c +++ b/os/rt/src/chthreads.c @@ -670,7 +670,7 @@ void chThdSleepUntil(systime_t time) { sysinterval_t interval; chSysLock(); - interval = chTimeSubtractX(time, chVTGetSystemTimeX()); + interval = chTimeDiffX(time, chVTGetSystemTimeX()); if (interval > (sysinterval_t)0) { chThdSleepS(interval); } @@ -697,7 +697,7 @@ systime_t chThdSleepUntilWindowed(systime_t prev, systime_t next) { chSysLock(); time = chVTGetSystemTimeX(); if (chTimeIsInRangeX(time, prev, next)) { - chThdSleepS(chTimeSubtractX(next, time)); + chThdSleepS(chTimeDiffX(next, time)); } chSysUnlock(); diff --git a/os/rt/src/chvt.c b/os/rt/src/chvt.c index 51db54cc3..4d887b8c4 100644 --- a/os/rt/src/chvt.c +++ b/os/rt/src/chvt.c @@ -62,7 +62,7 @@ void _vt_init(void) { ch.vtlist.next = (virtual_timer_t *)&ch.vtlist; ch.vtlist.prev = (virtual_timer_t *)&ch.vtlist; - ch.vtlist.delta = (systime_t)-1; + ch.vtlist.delta = (sysinterval_t)-1; #if CH_CFG_ST_TIMEDELTA == 0 ch.vtlist.systime = (systime_t)0; #else /* CH_CFG_ST_TIMEDELTA > 0 */ @@ -92,7 +92,7 @@ void _vt_init(void) { * * @iclass */ -void chVTDoSetI(virtual_timer_t *vtp, systime_t delay, +void chVTDoSetI(virtual_timer_t *vtp, sysinterval_t delay, vtfunc_t vtfunc, void *par) { virtual_timer_t *p; systime_t delta; @@ -126,7 +126,7 @@ void chVTDoSetI(virtual_timer_t *vtp, systime_t delay, vtp->delta = delay; /* Being the first element in the list the alarm timer is started.*/ - port_timer_start_alarm(ch.vtlist.lasttime + delay); + port_timer_start_alarm(chTimeAddX(ch.vtlist.lasttime, delay)); return; } @@ -136,9 +136,9 @@ void chVTDoSetI(virtual_timer_t *vtp, systime_t delay, /* Delay as delta from 'lasttime'. Note, it can overflow and the value becomes lower than 'now'.*/ - delta = now - ch.vtlist.lasttime + delay; + delta = chTimeDiffX(ch.vtlist.lasttime, now) + delay; - if (delta < now - ch.vtlist.lasttime) { + if (delta < chTimeDiffX(ch.vtlist.lasttime, now)) { /* Scenario where a very large delay excedeed the numeric range, it requires a special handling. We need to skip the first element and adjust the delta to wrap back in the previous numeric range.*/ @@ -148,7 +148,7 @@ void chVTDoSetI(virtual_timer_t *vtp, systime_t delay, else if (delta < p->delta) { /* A small delay that will become the first element in the delta list and next deadline.*/ - port_timer_set_alarm(ch.vtlist.lasttime + delta); + port_timer_set_alarm(chTimeAddX(ch.vtlist.lasttime, delta)); } } #else /* CH_CFG_ST_TIMEDELTA == 0 */ @@ -176,7 +176,7 @@ void chVTDoSetI(virtual_timer_t *vtp, systime_t delay, /* Special case when the timer is in last position in the list, the value in the header must be restored.*/; p->delta -= delta; - ch.vtlist.delta = (systime_t)-1; + ch.vtlist.delta = (sysinterval_t)-1; } /** @@ -205,9 +205,9 @@ void chVTDoResetI(virtual_timer_t *vtp) { /* The above code changes the value in the header when the removed element is the last of the list, restoring it.*/ - ch.vtlist.delta = (systime_t)-1; + ch.vtlist.delta = (sysinterval_t)-1; #else /* CH_CFG_ST_TIMEDELTA > 0 */ - systime_t nowdelta, delta; + sysinterval_t nowdelta, delta; /* If the timer is not the first of the list then it is simply unlinked else the operation is more complex.*/ @@ -246,7 +246,7 @@ void chVTDoResetI(virtual_timer_t *vtp) { }*/ /* Distance in ticks between the last alarm event and current time.*/ - nowdelta = chVTGetSystemTimeX() - ch.vtlist.lasttime; + nowdelta = chTimeDiffX(ch.vtlist.lasttime, chVTGetSystemTimeX()); /* If the current time surpassed the time of the next element in list then the event interrupt is already pending, just return.*/ @@ -263,7 +263,7 @@ void chVTDoResetI(virtual_timer_t *vtp) { delta = (systime_t)CH_CFG_ST_TIMEDELTA; } - port_timer_set_alarm(ch.vtlist.lasttime + nowdelta + delta); + port_timer_set_alarm(chTimeAddX(ch.vtlist.lasttime, nowdelta + delta)); #endif /* CH_CFG_ST_TIMEDELTA > 0 */ } -- cgit v1.2.3