when is loop_until_bit_is_set/clear required after setting a bit?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

when is loop_until_bit_is_set/clear required after setting a bit?

Britton Kerin
I remember reading somewhere in avr libc docs to do e.g. this:

    PCIFR = _BV (some_bit)
    loop_until_bit_is_clear (PCIFR, some_bit);

This may be a bad example becuase of the odd write-one-to-clear
semantics.  That isn't the point here, I don't remember which
registers this advice applied to.  And I can't find it now.


The only thing I could find in the datasheet that related to this was
in section 13.2.4 of ATMega328P datasheet, where it says that you
have to wait a cycle after assigning an output pin value before
reading it back in.

Are there other cases where loop_until_* or a nop is required?

Thanks,
Britton

_______________________________________________
AVR-chat mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-chat
Reply | Threaded
Open this post in threaded view
|

Re: when is loop_until_bit_is_set/clear required after setting a bit?

Ormund Williams
On Wed, 2016-09-07 at 14:19 -0800, Britton Kerin wrote:

> I remember reading somewhere in avr libc docs to do e.g. this:
>
>     PCIFR = _BV (some_bit)
>     loop_until_bit_is_clear (PCIFR, some_bit);
>
> This may be a bad example becuase of the odd write-one-to-clear
> semantics.  That isn't the point here, I don't remember which
> registers this advice applied to.  And I can't find it now.
>
>
> The only thing I could find in the datasheet that related to this was
> in section 13.2.4 of ATMega328P datasheet, where it says that you
> have to wait a cycle after assigning an output pin value before
> reading it back in.
>
> Are there other cases where loop_until_* or a nop is required?
The only thing I can think of is writing to the EEPROM.  You can also
use that function to block waiting for a flag to be set by a timer,
interrupt or other peripheral.

__
Ormund


_______________________________________________
AVR-chat mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-chat
Reply | Threaded
Open this post in threaded view
|

Re: when is loop_until_bit_is_set/clear required after setting a bit?

Erik Christiansen-2
In reply to this post by Britton Kerin
On 07.09.16 14:19, Britton Kerin wrote:
> I remember reading somewhere in avr libc docs to do e.g. this:
>
>     PCIFR = _BV (some_bit)
>     loop_until_bit_is_clear (PCIFR, some_bit);
>
> This may be a bad example becuase of the odd write-one-to-clear
> semantics.  That isn't the point here, I don't remember which
> registers this advice applied to.  And I can't find it now.

The most common time that makes sense is when you need the result of a
hardware operation whose availability is flagged by the bit in question,
e.g. the result of an ADC measurement, or EEPROM write. It is of course
more efficient to take advantage of interrupts, where available, to
signal operation completion, as the cpu can do other stuff in the
interim.

Alternatively, if the bit were one of the general purpose flag bits,
being used as an interprocess semaphore, then you'd suspend the waiting
process, not loop.

> The only thing I could find in the datasheet that related to this was
> in section 13.2.4 of ATMega328P datasheet, where it says that you
> have to wait a cycle after assigning an output pin value before
> reading it back in.
>
> Are there other cases where loop_until_* or a nop is required?

Neither a loop nor the nop is required if some other unrelated
instruction is moved in between, to take its place, but that does assume
we're writing the routine in assembler, and I hear that there are few of
us remaining who often do.

Erik

_______________________________________________
AVR-chat mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/avr-chat