From: Joseph Coffland Date: Sat, 18 Jun 2016 23:26:13 +0000 (-0700) Subject: Handle RTC wrap around X-Git-Url: https://git.buildbotics.com/?a=commitdiff_plain;h=af2e24c59a672329f54ec2f1754b1b05b7f93219;p=bbctrl-firmware Handle RTC wrap around --- diff --git a/src/huanyang.c b/src/huanyang.c index 50ef3fa..8ee5d4e 100644 --- a/src/huanyang.c +++ b/src/huanyang.c @@ -500,7 +500,7 @@ void huanyang_reset() { void huanyang_rtc_callback() { - if (ha.last && HUANYANG_TIMEOUT < rtc_get_time() - ha.last) { + if (ha.last && rtc_expired(ha.last + HUANYANG_TIMEOUT)) { if (ha.retry < HUANYANG_RETRIES) _retry_command(); else { if (ha.debug) printf_P(PSTR("huanyang: timedout\n")); diff --git a/src/motor.c b/src/motor.c index 86b2683..965fcf5 100644 --- a/src/motor.c +++ b/src/motor.c @@ -285,7 +285,7 @@ stat_t motor_power_callback() { // called by controller for (int motor = 0; motor < MOTORS; motor++) // Deenergize motor if disabled, in error or after timeout when not holding if (motors[motor].power_mode == MOTOR_DISABLED || motor_error(motor) || - motors[motor].timeout < rtc_get_time()) + rtc_expired(motors[motor].timeout)) _deenergize(motor); return STAT_OK; diff --git a/src/plan/calibrate.c b/src/plan/calibrate.c index e128839..ae6909f 100644 --- a/src/plan/calibrate.c +++ b/src/plan/calibrate.c @@ -82,7 +82,7 @@ static stat_t _exec_calibrate(mpBuf_t *bf) { const float time = MIN_SEGMENT_TIME; // In minutes const float maxDeltaV = JOG_ACCELERATION * time; - if (cal.wait <= rtc_get_time()) + if (rtc_expired(cal.wait)) switch (cal.state) { case CAL_START: { cal.axis = motor_get_axis(cal.motor); diff --git a/src/rtc.c b/src/rtc.c index d856e52..4f7f016 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -67,3 +67,8 @@ void rtc_init() { uint32_t rtc_get_time() {return ticks;} + + +bool rtc_expired(uint32_t t) { + return 0 <= (int32_t)(ticks - t); +} diff --git a/src/rtc.h b/src/rtc.h index e47a6c6..d5f422d 100644 --- a/src/rtc.h +++ b/src/rtc.h @@ -30,8 +30,11 @@ #include +#include #define RTC_MILLISECONDS 10 // interrupt on every 10 RTC ticks (~10 ms) void rtc_init(); // initialize and start general timer uint32_t rtc_get_time(); +int32_t rtc_diff(uint32_t t); +bool rtc_expired(uint32_t t);