avr-libc-dev
[Top][All Lists]
Advanced

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

Re: [avr-libc-dev] pgm_read_byte_far & Interrupts


From: David Brown
Subject: Re: [avr-libc-dev] pgm_read_byte_far & Interrupts
Date: Thu, 03 Apr 2008 11:20:06 +0200
User-agent: Thunderbird 2.0.0.12 (Windows/20080213)

Joerg Wunsch wrote:
As Moritz Struebe wrote:
<snip>
- To make those functions reentrant they would have to loop the read
functions until RAMZ_before = RAMPZ_after and the write functions
would have to loop until until the corresponding bits get
cleared. Right?

I don't quite follow you on that.  Why looping?  Just wrapping the
access into an ATOMIC_BLOCK (<util/atomic.h>) should IMHO suffice.


I think he is thinking of non-blocking access to avoid disabling interrupts. It goes like this:

RAMPZ = rz;
do {
        // Read flash using RAMPZ
while (RAMPZ != rz);


Then if something else interrupts this sequence and changes RAMPZ, the loop will be re-run. I expect you already know this, but for the benefit of other readers - sequences like this are sometimes convenient when you want atomic access (especially read access) to some data, but want to avoid disabling interrupts or using other locking mechanisms. They are well suited for when there is little chance of the data being changed in the middle of the function, but when the function would otherwise take a long time (thus you don't want to disable interrupts during it) or when other locking mechanisms (RTOS semaphores, for example) would be too costly. Another common example is:

extern volatile uint16_t ticker;
uint16_t readTicker(void) {
        uint16_t tickerA, tickerB;
        tickerA = ticker;
        while (1) {
                tickerB = ticker;
                if (tickerB == tickerA) return tickerA;
                tickerA = tickerB;
        }
}

Of course, the loop must not have side effects!




reply via email to

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