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 <avr/interrupt.h>
#include <util/atomic.h>
#ifndef ANALOG_MASK
#warning define ANALOG_MASK as a bitmask of all the analog channels you wish to use
@ -54,16 +55,12 @@ 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);

View File

@ -1,8 +1,12 @@
#include "dda_queue.h"
#include <string.h>
#ifndef SIMULATION
#ifdef SIMULATION
#include "simulation.h"
#else
#include <avr/interrupt.h>
#include <util/atomic.h>
#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;
}
}

View File

@ -13,6 +13,7 @@
#include "heater.h"
#include "timer.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_assert(bool cond, const char msg[]);
#define ATOMIC_BLOCK(n) if (n)
#define ATOMIC_RESTORESTATE 1
#endif /* _SIMULATION_H */