Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ATOMIC_BLOCK instead of cli() and sei() #40

Closed
wants to merge 1 commit into from

Conversation

GrantM11235
Copy link

Use macros from util/atomic.h to prevent blocks of code from being
interrupted by interrupts.

ATOMIC_BLOCK(ATOMIC_FORCEON) { /*...*/ } is used in place of
cli(); /*...*/ sei(); when global interrupts are already on, or when
global interrupts must be enabled afterwards anyway.

ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { /*...*/ } is used to save the
state of SREG (including the Global Interrupt Status flag) and restore
it afterwards.

Use macros from util/atomic.h to prevent blocks of code from being
interrupted by interrupts.

`ATOMIC_BLOCK(ATOMIC_FORCEON) { /*...*/ }` is used in place of
`cli(); /*...*/ sei();` when global interrupts are already on, or when
global interrupts must be enabled afterwards anyway.

`ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { /*...*/ }` is used to save the
state of SREG (including the Global Interrupt Status flag) and restore
it afterwards.
@MCUdude
Copy link
Owner

MCUdude commented Dec 12, 2017

Thanks! I'll have to admit; I've never actually used atomic.h in any of my projects yet. I've done a little research, and as far as I've understood you use ATOMIC instead of cli/sei/SREG when dealing with variables that's shared between the main code and the ISR.

So, I understand that this PR makes the code cleaner, but is it strictly necessary when allready using cli/sei/SREG? Seems like the official Arduino repo also saves SREG rather than using ATOMIC.

I'm interested to know more about this atomic blocks and why a "dedicated library" is necessary to use 🙂

@MCUdude MCUdude closed this May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants