Skip to content

Commit 643c892

Browse files
author
Steven Cartmell
committed
Remove counter from nordic critical HAL implementation
hal_critical_section_enter() is safe to call multiple times, however hal_critical_section_exit() is only called on the last exit from a critical section. This will cause a mismatch in the counter and the interrupt state will never be restored if the critical section is nested. Change this to a bool so that the interrupt save state is tracked for hal_in_critical_section() to work correctly.
1 parent 061795c commit 643c892

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

targets/TARGET_NORDIC/TARGET_NRF5/nordic_critical.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "app_util_platform.h"
2020

2121
#if defined(SOFTDEVICE_PRESENT)
22-
static volatile uint32_t nordic_cr_nested = 0;
22+
static volatile bool state_saved = false;
2323

2424
static void nordic_nvic_critical_region_enter(void);
2525
static void nordic_nvic_critical_region_exit(void);
@@ -56,7 +56,7 @@ void hal_critical_section_exit()
5656

5757
bool hal_in_critical_section(void)
5858
{
59-
return (nordic_cr_nested != 0);
59+
return (state_saved != 0);
6060
}
6161

6262

@@ -70,7 +70,7 @@ static inline void nordic_nvic_critical_region_enter(void)
7070
{
7171
int was_masked = __sd_nvic_irq_disable();
7272

73-
if (nordic_cr_nested == 0) {
73+
if (state_saved == false) {
7474
nrf_nvic_state.__irq_masks[0] = ( NVIC->ICER[0] & __NRF_NVIC_APP_IRQS_0 );
7575
NVIC->ICER[0] = __NRF_NVIC_APP_IRQS_0;
7676
#ifdef NRF52
@@ -79,7 +79,7 @@ static inline void nordic_nvic_critical_region_enter(void)
7979
#endif
8080
}
8181

82-
nordic_cr_nested++;
82+
state_saved = true;
8383

8484
if (!was_masked) {
8585
__sd_nvic_irq_enable();
@@ -93,17 +93,15 @@ static inline void nordic_nvic_critical_region_enter(void)
9393
*/
9494
static inline void nordic_nvic_critical_region_exit(void)
9595
{
96-
nordic_cr_nested--;
96+
state_saved = false;
9797

98-
if (nordic_cr_nested == 0) {
99-
int was_masked = __sd_nvic_irq_disable();
100-
NVIC->ISER[0] = nrf_nvic_state.__irq_masks[0];
98+
int was_masked = __sd_nvic_irq_disable();
99+
NVIC->ISER[0] = nrf_nvic_state.__irq_masks[0];
101100
#ifdef NRF52
102-
NVIC->ISER[1] = nrf_nvic_state.__irq_masks[1];
101+
NVIC->ISER[1] = nrf_nvic_state.__irq_masks[1];
103102
#endif
104-
if (!was_masked) {
105-
__sd_nvic_irq_enable();
106-
}
103+
if (!was_masked) {
104+
__sd_nvic_irq_enable();
107105
}
108106
}
109107
#endif

0 commit comments

Comments
 (0)