Skip to content

Commit 9f052bc

Browse files
committed
Update function get_interrupts_disabled() to return a bool value rather
than an integer. This removes the need for all users to mask the returned value with 0x1 to determine interrupt status. Expose this function externally to allow other users to check interrupt status in a manner which will work for both cortex-A and cortex-M . Usage: bool disabled = get_interrupts_disabled();
1 parent 9197f94 commit 9f052bc

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

hal/api/critical.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
extern "C" {
2525
#endif
2626

27+
28+
/** Determine the current interrupts disabled state
29+
*
30+
* This function can be called to determine whether or not interrupts are currently disabled.
31+
* \note
32+
* NOTE:
33+
* This function works for both cortex-A and cortex-M, although the underlyng implementation
34+
* differs.
35+
* @return true if interrupts are disabled, false otherwise
36+
*/
37+
bool get_interrupts_disabled(void);
38+
2739
/** Mark the start of a critical section
2840
*
2941
* This function should be called to mark the start of a critical section of code.

hal/common/critical.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@
2727
#define EXCLUSIVE_ACCESS (!defined (__CORTEX_M0) && !defined (__CORTEX_M0PLUS))
2828

2929
static volatile uint32_t interrupt_enable_counter = 0;
30-
static volatile uint32_t critical_interrupts_disabled = 0;
30+
static volatile bool critical_interrupts_disabled = false;
3131

32-
static inline uint32_t get_interrupts_disabled(void)
32+
bool get_interrupts_disabled(void)
3333
{
3434
#if defined(__CORTEX_A9)
35-
uint32_t interrupts_disabled = (__get_CPSR() & 0x80) >> 7;
35+
bool interrupts_disabled = (bool)(((__get_CPSR() & 0x80) >> 7) & 0x1);
3636
#else
37-
uint32_t interrupts_disabled = __get_PRIMASK();
37+
bool interrupts_disabled = (bool)(__get_PRIMASK() & 0x1);
3838
#endif
3939
return interrupts_disabled;
4040
}
4141

4242
void core_util_critical_section_enter()
4343
{
44-
uint32_t interrupts_disabled = get_interrupts_disabled();
44+
bool interrupts_disabled = get_interrupts_disabled();
4545
__disable_irq();
4646

4747
/* Save the interrupt disabled state as it was prior to any nested critical section lock use */
4848
if (!interrupt_enable_counter) {
49-
critical_interrupts_disabled = interrupts_disabled & 0x1;
49+
critical_interrupts_disabled = interrupts_disabled;
5050
}
5151

5252
/* If the interrupt_enable_counter overflows or we are in a nested critical section and interrupts
@@ -56,7 +56,7 @@ void core_util_critical_section_enter()
5656
// FIXME
5757
#ifndef FEATURE_UVISOR
5858
if (interrupt_enable_counter > 0) {
59-
MBED_ASSERT(interrupts_disabled & 0x1);
59+
MBED_ASSERT(interrupts_disabled);
6060
}
6161
#else
6262
#warning "core_util_critical_section_enter needs fixing to work from unprivileged code"
@@ -71,9 +71,9 @@ void core_util_critical_section_exit()
7171

7272
// FIXME
7373
#ifndef FEATURE_UVISOR
74-
uint32_t interrupts_disabled = get_interrupts_disabled(); /* get the current interrupt disabled state */
74+
bool interrupts_disabled = get_interrupts_disabled(); /* get the current interrupt disabled state */
7575

76-
MBED_ASSERT(interrupts_disabled & 0x1); /* Interrupts must be disabled on invoking an exit from a critical section */
76+
MBED_ASSERT(interrupts_disabled); /* Interrupts must be disabled on invoking an exit from a critical section */
7777
#else
7878
#warning "core_util_critical_section_exit needs fixing to work from unprivileged code"
7979
#endif /* FEATURE_UVISOR */

0 commit comments

Comments
 (0)