From 4b41a5eeab450b575e448a0fb1685333053d4423 Mon Sep 17 00:00:00 2001 From: Michael Moon Date: Thu, 21 Oct 2010 11:05:08 +1100 Subject: [PATCH] use avr-libc atomic stuff instead of manual SREG manipulation --- analog.c | 17 +++++++---------- dda_queue.c | 23 ++++++++++------------- gcode_process.c | 1 + simulation.h | 3 +++ 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/analog.c b/analog.c index 9ae7b59..3f0e4d1 100644 --- a/analog.c +++ b/analog.c @@ -1,6 +1,7 @@ #include "analog.h" #include +#include #ifndef ANALOG_MASK #warning define ANALOG_MASK as a bitmask of all the analog channels you wish to use @@ -54,19 +55,15 @@ ISR(ADC_vect) { } uint16_t analog_read(uint8_t channel) { - uint8_t sreg; uint16_t r; - // save interrupt flag - sreg = SREG; - // disable interrupts - cli(); - // atomic 16-bit copy - r = adc_result[channel]; - // restore interrupt flag - SREG = sreg; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + // atomic 16-bit copy + r = adc_result[channel]; + } // re-enable analog read loop so we can get new values ADCSRA |= MASK(ADIE); - + return r; } diff --git a/dda_queue.c b/dda_queue.c index 5c1916d..92fc35f 100644 --- a/dda_queue.c +++ b/dda_queue.c @@ -1,8 +1,12 @@ #include "dda_queue.h" #include -#ifndef SIMULATION + +#ifdef SIMULATION + #include "simulation.h" +#else #include + #include #endif #include "config.h" @@ -110,16 +114,9 @@ void print_queue() { } void queue_flush() { - // save interrupt flag - uint8_t sreg = SREG; - - // disable interrupts - cli(); - - // flush queue - mb_tail = mb_head; - movebuffer[mb_head].live = 0; - - // restore interrupt flag - SREG = sreg; + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + // flush queue + mb_tail = mb_head; + movebuffer[mb_head].live = 0; + } } diff --git a/gcode_process.c b/gcode_process.c index 789fd12..fe550ba 100644 --- a/gcode_process.c +++ b/gcode_process.c @@ -13,6 +13,7 @@ #include "heater.h" #include "timer.h" #include "sersendf.h" +#include "debug.h" /**************************************************************************** * * diff --git a/simulation.h b/simulation.h index e514284..0d1ba57 100644 --- a/simulation.h +++ b/simulation.h @@ -77,5 +77,8 @@ void sim_info(const char fmt[], ...); void sim_error(const char msg[]); void sim_assert(bool cond, const char msg[]); +#define ATOMIC_BLOCK(n) if (n) +#define ATOMIC_RESTORESTATE 1 + #endif /* _SIMULATION_H */