From 1e198e16eae1e02e68d5cddbe8e8ffaa8699e476 Mon Sep 17 00:00:00 2001 From: Jim McGee Date: Sun, 8 May 2011 22:03:14 -0700 Subject: [PATCH] Fix handling of the steptimeout variable. Steptimeout is used both inside and outside of interrupts, and as such it needs special attention. Specifically, the increment outside of the interrupt context needs to occur when interrupts are disabled, or a clear of the variable can be missed. The variable was also made volatile. This is not strictly necessary given the current code, but it is the more conservative approach to dealing with the variable (and costs nothing in the current code). --- clock.c | 9 ++++++++- dda.c | 2 +- dda.h | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/clock.c b/clock.c index 53adf65..d0868c2 100644 --- a/clock.c +++ b/clock.c @@ -16,6 +16,7 @@ #ifdef TEMP_INTERCOM #include "intercom.h" #endif +#include "memory_barrier.h" /*! do stuff every 1/4 second @@ -25,8 +26,14 @@ void clock_250ms() { if (steptimeout > (30 * 4)) { power_off(); } - else if (heaters_all_off()) + else if (heaters_all_off()) { + uint8_t save_reg = SREG; + cli(); + CLI_SEI_BUG_MEMORY_BARRIER(); steptimeout++; + MEMORY_BARRIER(); + SREG = save_reg; + } ifclock(CLOCK_FLAG_1S) { if (debug_flags & DEBUG_POSITION) { diff --git a/dda.c b/dda.c index 846af3a..ac62a54 100644 --- a/dda.c +++ b/dda.c @@ -23,7 +23,7 @@ #endif /// step timeout -uint8_t steptimeout = 0; +volatile uint8_t steptimeout = 0; /* position tracking diff --git a/dda.h b/dda.h index 4d05685..6699d27 100644 --- a/dda.h +++ b/dda.h @@ -122,7 +122,8 @@ typedef struct { */ /// steptimeout is set to zero when we step, and increases over time so we can turn the motors off when they've been idle for a while -extern uint8_t steptimeout; +/// It is also used inside and outside of interrupts, which is why it has been made volatile +extern volatile uint8_t steptimeout; /// startpoint holds the endpoint of the most recently created DDA, so we know where the next one created starts. could also be called last_endpoint extern TARGET startpoint;