Graham Davies wrote:
"David Brown" wrote:
You are missing a number of points ...
Well, I think we're getting close to complete coverage now!
Well, since we are going for complete coverage, I'll add my 2 cents, then.
Sometimes I don't use volatile at all on the variables, and just use
special functions to access the data.
Example (not compiled, might have syntax errors):
uint16_t counter;
SIGNAL(TMR0_OVERFLOW)
{
counter++
if (counter == 100)
counter = 0;
}
static uint16_t atomic_read_16(uint16_t *ptr)
{
uint16_t ret;
cli();
ret = (volatile)(*ptr);
sei();
return ret;
}
void main(void)
{
while (1) {
if (atomic_read_16(&counter) > 50)
PORTB = 0x10;
else
PORTB = 0x00;
}
}
The advantage of this method is that, inside the interrupt routine the
variable can be optimized as a regular variable (caching in registers,
etc.).
It also guarantees that we don't forget the cli/sei (or store flags /
cli / restore flags) sequence to access a data value that uses more than
one byte.
We could do an "atomic.h" include file with all the read / write, 16 /
32 bits variants optimized in assembly, with the same
restore-flags-one-instruction-earlier trick that we do for the stack
pointer in function prologues to decrease interrupt latency.
As I said, just my 2 cents,
--
Paulo Marques - www.grupopie.com
"The face of a child can say it all, especially the
mouth part of the face."
_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list