Handle RTC wrap around
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Sat, 18 Jun 2016 23:26:13 +0000 (16:26 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Sat, 18 Jun 2016 23:26:13 +0000 (16:26 -0700)
src/huanyang.c
src/motor.c
src/plan/calibrate.c
src/rtc.c
src/rtc.h

index 50ef3fa709e1b70949c0ae14f693ebb5246ea4bf..8ee5d4eb336b0fb70d0508395c81e1f4c3ade67d 100644 (file)
@@ -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"));
index 86b2683bf8b903db0767af3ef5adc3c4aca3c15b..965fcf56a8a07f27e096307102b0b7df50b825c9 100644 (file)
@@ -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;
index e1288398a2b04c4ce43b05df1791a8d8d675e638..ae6909fe030315b50832f4916bbc70dd0d98837e 100644 (file)
@@ -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);
index d856e5216cc01c508fd18ede45fb843bcc0275d3..4f7f016d79eae851c8a0ce5f47590d17fba9fbba 100644 (file)
--- 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);
+}
index e47a6c68ed544c3220ba464a93b7a153fc4a7c1b..d5f422d7ac57709eebf47adb33c3d3ce5422775f 100644 (file)
--- a/src/rtc.h
+++ b/src/rtc.h
 
 
 #include <stdint.h>
+#include <stdbool.h>
 
 #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);