Skip to content

Commit f0cc00e

Browse files
TeroJaaskokjbracey
authored andcommitted
nanostack-hal: add alternative critical section implementation
The nanostack hal's critical section uses a mutex for mutual exclusion, which is nice for many use cases. But when one needs to use the critical section from interrupts, the RTX will have a assertion failure and panic. Add a configurable for mbed_lib, which can be used to enable a alternative version of critical section, which uses the underlying OS primitives, which disables the interrupts. Note: the default behavior is not changed, one needs to override the "nanostack-hal.critical-section-usable-from-interrupt" to have "true". Reason for this change is that there is a need for sending events using nanostack event queue from interrupt context, eg. from a socket callback.
1 parent d24c7c4 commit f0cc00e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/arm_hal_interrupt.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,25 @@
44

55
#include "arm_hal_interrupt.h"
66
#include "arm_hal_interrupt_private.h"
7+
8+
#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
9+
#include "platform/mbed_critical.h"
10+
#else
711
#include "cmsis_os2.h"
812
#include "mbed_rtos_storage.h"
13+
#endif
14+
915
#include <mbed_assert.h>
1016

17+
18+
// The critical section has two alternative implementations, the default which uses mutex
19+
// as locking primitive and the optional, which will bluntly disable interrupts
20+
// for the critical section. The mutex version will not cause issues by delaying
21+
// interrupts, but it has a down side that it can not be used from the interrupt
22+
// service routines. The IRQ-safe version will do the mutual exclusion by temporarily
23+
// disabling the interrupts, so it will have effect on interrupt latency.
24+
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
25+
1126
static uint8_t sys_irq_disable_counter;
1227

1328
static mbed_rtos_storage_mutex_t critical_mutex;
@@ -19,20 +34,34 @@ static const osMutexAttr_t critical_mutex_attr = {
1934
};
2035
static osMutexId_t critical_mutex_id;
2136

37+
#endif
38+
2239
void platform_critical_init(void)
2340
{
41+
#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
42+
// nothing to do here
43+
#else
2444
critical_mutex_id = osMutexNew(&critical_mutex_attr);
2545
MBED_ASSERT(critical_mutex_id);
46+
#endif
2647
}
2748

2849
void platform_enter_critical(void)
2950
{
51+
#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
52+
core_util_critical_section_enter();
53+
#else
3054
osMutexAcquire(critical_mutex_id, osWaitForever);
3155
sys_irq_disable_counter++;
56+
#endif
3257
}
3358

3459
void platform_exit_critical(void)
3560
{
61+
#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
62+
core_util_critical_section_exit();
63+
#else
3664
--sys_irq_disable_counter;
3765
osMutexRelease(critical_mutex_id);
66+
#endif
3867
}

features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/mbed_lib.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"event_loop_thread_stack_size": {
99
"help": "Define event-loop thread stack size.",
1010
"value": 6144
11+
},
12+
"critical-section-usable-from-interrupt": {
13+
"help": "Make critical section API usable from interrupt context. Else a mutex is used as locking primitive. Consult arm_hal_interrupt.c for possible side effects on interrupt latency.",
14+
"value": false
1115
}
1216
,
1317
"event-loop-dispatch-from-application": {

0 commit comments

Comments
 (0)