use avr-libc atomic stuff instead of manual SREG manipulation

This commit is contained in:
Michael Moon 2010-10-21 11:05:08 +11:00
parent 3e0b40a2cf
commit 4b41a5eeab
4 changed files with 21 additions and 23 deletions

View File

@ -1,6 +1,7 @@
#include "analog.h" #include "analog.h"
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <util/atomic.h>
#ifndef ANALOG_MASK #ifndef ANALOG_MASK
#warning define ANALOG_MASK as a bitmask of all the analog channels you wish to use #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) { uint16_t analog_read(uint8_t channel) {
uint8_t sreg;
uint16_t r; uint16_t r;
// save interrupt flag
sreg = SREG; ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// disable interrupts // atomic 16-bit copy
cli(); r = adc_result[channel];
// atomic 16-bit copy }
r = adc_result[channel];
// restore interrupt flag
SREG = sreg;
// re-enable analog read loop so we can get new values // re-enable analog read loop so we can get new values
ADCSRA |= MASK(ADIE); ADCSRA |= MASK(ADIE);
return r; return r;
} }

View File

@ -1,8 +1,12 @@
#include "dda_queue.h" #include "dda_queue.h"
#include <string.h> #include <string.h>
#ifndef SIMULATION
#ifdef SIMULATION
#include "simulation.h"
#else
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <util/atomic.h>
#endif #endif
#include "config.h" #include "config.h"
@ -110,16 +114,9 @@ void print_queue() {
} }
void queue_flush() { void queue_flush() {
// save interrupt flag ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
uint8_t sreg = SREG; // flush queue
mb_tail = mb_head;
// disable interrupts movebuffer[mb_head].live = 0;
cli(); }
// flush queue
mb_tail = mb_head;
movebuffer[mb_head].live = 0;
// restore interrupt flag
SREG = sreg;
} }

View File

@ -13,6 +13,7 @@
#include "heater.h" #include "heater.h"
#include "timer.h" #include "timer.h"
#include "sersendf.h" #include "sersendf.h"
#include "debug.h"
/**************************************************************************** /****************************************************************************
* * * *

View File

@ -77,5 +77,8 @@ void sim_info(const char fmt[], ...);
void sim_error(const char msg[]); void sim_error(const char msg[]);
void sim_assert(bool cond, const char msg[]); void sim_assert(bool cond, const char msg[]);
#define ATOMIC_BLOCK(n) if (n)
#define ATOMIC_RESTORESTATE 1
#endif /* _SIMULATION_H */ #endif /* _SIMULATION_H */