avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] volatile...


From: Paulo Marques
Subject: Re: [avr-gcc-list] volatile...
Date: Fri, 15 Dec 2006 12:56:52 +0000
User-agent: Thunderbird 1.5.0.7 (X11/20060909)

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."




reply via email to

[Prev in Thread] Current Thread [Next in Thread]