fix the represntation of at_the_end_of_time to be 63 one bits rather than 32

This commit is contained in:
graham sanderson 2021-02-17 14:15:00 -06:00
parent debef7471e
commit 684986aae6
5 changed files with 27 additions and 5 deletions

View File

@ -37,7 +37,8 @@ static inline uint64_t to_us_since_boot(absolute_time_t t) {
/*! fn update_us_since_boot /*! fn update_us_since_boot
* \brief update an absolute_time_t value to represent a given number of microseconds since boot * \brief update an absolute_time_t value to represent a given number of microseconds since boot
* \param t the absolute time value to update * \param t the absolute time value to update
* \param us_since_boot the number of microseconds since boot to represent * \param us_since_boot the number of microseconds since boot to represent. Note this should be representable
* as a signed 64 bit integer
*/ */
static inline void update_us_since_boot(absolute_time_t *t, uint64_t us_since_boot) { static inline void update_us_since_boot(absolute_time_t *t, uint64_t us_since_boot) {
*t = us_since_boot; *t = us_since_boot;
@ -49,11 +50,23 @@ typedef struct {
uint64_t _private_us_since_boot; uint64_t _private_us_since_boot;
} absolute_time_t; } absolute_time_t;
/*! fn to_us_since_boot
* \brief convert an absolute_time_t into a number of microseconds since boot.
* \param t the number of microseconds since boot
* \return an absolute_time_t value equivalent to t
*/
static inline uint64_t to_us_since_boot(absolute_time_t t) { static inline uint64_t to_us_since_boot(absolute_time_t t) {
return t._private_us_since_boot; return t._private_us_since_boot;
} }
/*! fn update_us_since_boot
* \brief update an absolute_time_t value to represent a given number of microseconds since boot
* \param t the absolute time value to update
* \param us_since_boot the number of microseconds since boot to represent. Note this should be representable
* as a signed 64 bit integer
*/
static inline void update_us_since_boot(absolute_time_t *t, uint64_t us_since_boot) { static inline void update_us_since_boot(absolute_time_t *t, uint64_t us_since_boot) {
assert(us_since_boot <= INT64_MAX);
t->_private_us_since_boot = us_since_boot; t->_private_us_since_boot = us_since_boot;
} }
#define ABSOLUTE_TIME_INITIALIZED_VAR(name, value) name = {value} #define ABSOLUTE_TIME_INITIALIZED_VAR(name, value) name = {value}

View File

@ -155,7 +155,9 @@ static inline int64_t absolute_time_diff_us(absolute_time_t from, absolute_time_
return (int64_t)(to_us_since_boot(to) - to_us_since_boot(from)); return (int64_t)(to_us_since_boot(to) - to_us_since_boot(from));
} }
/*! \brief The timestamp representing the end of time; no timestamp is after this /*! \brief The timestamp representing the end of time; this is actually not the maximum possible
* timestamp, but is set to 0x7fffffff_ffffffff microseconds to avoid sign overflows with time
* arithmetic. This is still over 7 million years, so should be sufficient.
* \ingroup timestamp * \ingroup timestamp
*/ */
extern const absolute_time_t at_the_end_of_time; extern const absolute_time_t at_the_end_of_time;

View File

@ -11,10 +11,9 @@
#include "pico/time.h" #include "pico/time.h"
#include "pico/util/pheap.h" #include "pico/util/pheap.h"
#include "hardware/sync.h" #include "hardware/sync.h"
#include "hardware/gpio.h"
const absolute_time_t ABSOLUTE_TIME_INITIALIZED_VAR(nil_time, 0); const absolute_time_t ABSOLUTE_TIME_INITIALIZED_VAR(nil_time, 0);
const absolute_time_t ABSOLUTE_TIME_INITIALIZED_VAR(at_the_end_of_time, ULONG_MAX); const absolute_time_t ABSOLUTE_TIME_INITIALIZED_VAR(at_the_end_of_time, INT64_MAX);
typedef struct alarm_pool_entry { typedef struct alarm_pool_entry {
absolute_time_t target; absolute_time_t target;

View File

@ -51,7 +51,6 @@ uint32_t PICO_WEAK_FUNCTION_IMPL_NAME(timer_us_32)() {
PICO_WEAK_FUNCTION_DEF(time_reached) PICO_WEAK_FUNCTION_DEF(time_reached)
bool PICO_WEAK_FUNCTION_IMPL_NAME(time_reached)(absolute_time_t t) { bool PICO_WEAK_FUNCTION_IMPL_NAME(time_reached)(absolute_time_t t) {
uint64_t target = to_us_since_boot(t); uint64_t target = to_us_since_boot(t);
if (target > 0xffffffffu) return false;
return time_us_64() >= target; return time_us_64() >= target;
} }

View File

@ -206,6 +206,15 @@ int main() {
PICOTEST_END_SECTION(); PICOTEST_END_SECTION();
PICOTEST_START_SECTION("end of time");
PICOTEST_CHECK(absolute_time_diff_us(at_the_end_of_time, get_absolute_time()) < 0, "now should be before the end of time")
PICOTEST_CHECK(absolute_time_diff_us(get_absolute_time(), at_the_end_of_time) > 0, "the end of time should be after now")
PICOTEST_CHECK(absolute_time_diff_us(at_the_end_of_time, at_the_end_of_time) == 0, "the end of time should equal itself")
absolute_time_t near_the_end_of_time;
update_us_since_boot(&near_the_end_of_time, 0x7ffffeffffffffff);
PICOTEST_CHECK(absolute_time_diff_us(near_the_end_of_time, at_the_end_of_time) > 0, "near the end of time should be before the end of time")
PICOTEST_END_SECTION();
PICOTEST_END_TEST(); PICOTEST_END_TEST();
} }