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

Add __irq_enabled() function #633

Open
mbenkmann opened this issue Jan 14, 2022 · 4 comments
Open

Add __irq_enabled() function #633

mbenkmann opened this issue Jan 14, 2022 · 4 comments

Comments

@mbenkmann
Copy link

It is better for a function to not simply disable and enable interrupts, so the following pattern is generally better:

int enabled = __irq_enabled();
__disable_irq();
...
if (enabled) __enable_irq();

To be able to use this pattern, a function __irq_enabled() is necessary. The following can be inserted right into the relevant headers (kinetis.h for Teensy 3, imxrt.h for Teensy 4) next to __enable_irq() and disable_irq():

static inline int __irq_enabled() __attribute__((always_inline, unused));
static inline int __irq_enabled()
{
    uint32_t pm;
    __asm__ volatile("MRS %0,PRIMASK" : "=r"(pm)::);
    return (pm & 1) == 0;
};
@FrankBoesing
Copy link
Contributor

Why not return a bool?

@mbenkmann
Copy link
Author

@FrankBoesing
I don't know what C standard is supposed to be supported.

@FrankBoesing
Copy link
Contributor

gnu++14 (C++ 14)

@ssilverman
Copy link
Contributor

ssilverman commented Mar 17, 2023

The "atomic" macros can help here, from <util/atomic.h>. They disable/enable interrupts, optionally with saving state. The syntax looks like:

ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  // Do stuff
}

This form guarantees the interrupts get reenabled appropriately.

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

No branches or pull requests

3 participants