Skip to content

Commit 6e103c3

Browse files
authored
Merge pull request #1883 from adbridge/master
Update function get_interrupts_disabled() to return a bool value rather
2 parents 9197f94 + 329f8a1 commit 6e103c3

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
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 enabled state
29+
*
30+
* This function can be called to determine whether or not interrupts are currently enabled.
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 enabled, false otherwise
36+
*/
37+
bool core_util_are_interrupts_enabled(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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,25 @@
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 core_util_are_interrupts_enabled(void)
3333
{
3434
#if defined(__CORTEX_A9)
35-
uint32_t interrupts_disabled = (__get_CPSR() & 0x80) >> 7;
35+
return ((__get_CPSR() & 0x80) == 0);
3636
#else
37-
uint32_t interrupts_disabled = __get_PRIMASK();
37+
return ((__get_PRIMASK() & 0x1) == 0);
3838
#endif
39-
return interrupts_disabled;
4039
}
4140

4241
void core_util_critical_section_enter()
4342
{
44-
uint32_t interrupts_disabled = get_interrupts_disabled();
43+
bool interrupts_disabled = !core_util_are_interrupts_enabled();
4544
__disable_irq();
4645

4746
/* Save the interrupt disabled state as it was prior to any nested critical section lock use */
4847
if (!interrupt_enable_counter) {
49-
critical_interrupts_disabled = interrupts_disabled & 0x1;
48+
critical_interrupts_disabled = interrupts_disabled;
5049
}
5150

5251
/* If the interrupt_enable_counter overflows or we are in a nested critical section and interrupts
@@ -56,7 +55,7 @@ void core_util_critical_section_enter()
5655
// FIXME
5756
#ifndef FEATURE_UVISOR
5857
if (interrupt_enable_counter > 0) {
59-
MBED_ASSERT(interrupts_disabled & 0x1);
58+
MBED_ASSERT(interrupts_disabled);
6059
}
6160
#else
6261
#warning "core_util_critical_section_enter needs fixing to work from unprivileged code"
@@ -71,9 +70,9 @@ void core_util_critical_section_exit()
7170

7271
// FIXME
7372
#ifndef FEATURE_UVISOR
74-
uint32_t interrupts_disabled = get_interrupts_disabled(); /* get the current interrupt disabled state */
73+
bool interrupts_disabled = !core_util_are_interrupts_enabled(); /* get the current interrupt disabled state */
7574

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

0 commit comments

Comments
 (0)