From b710f08b3a8e22137de453184c1d61774e39dd0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tero=20J=C3=A4=C3=A4sk=C3=B6?= Date: Thu, 12 Apr 2018 12:21:55 +0300 Subject: [PATCH 1/2] nanostack-hal: timer: conditionalize the use of high pri event thread nanostack-hal.critical-section-usable-from-interrupt -tunable was previously added to optionally make critical section code interrupt safe. The IRQ safe critical section is a prequisite for interrupt safe timer callbacks. The same flag can be used to enable calling of the timer callbacks directly from the timer interrupt context, without bouncing them via event thread. This removes the code and RAM consumed by EventQueue and the thread serving the high priority events. If the system does not have any dependencies on mbed_shared_queues, by setting this flag the static RAM usage is now further reduced by ~1600 bytes and code size by 4KB. Note: the default behavior is not changed, one needs to override the "nanostack-hal.critical-section-usable-from-interrupt" to have "true". --- .../arm_hal_timer.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp b/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp index 5b0f832552..d74526ed1e 100644 --- a/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp +++ b/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp @@ -12,15 +12,26 @@ static Timer timer; static Timeout timeout; + +// If critical sections are implemented using mutexes, timers must be called in thread context, and +// we use the high-priority event queue for this. +// If critical sections disable interrupts, we can call timers directly from interrupt. Avoiding the +// event queue can save ~1600B of RAM if the rest of the system is not using the event queue either. +// Caveats of this tunable are listed on arm_hal_interrupt.c. +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT static EventQueue *equeue; +#endif + static uint32_t due; static void (*arm_hal_callback)(void); // Called once at boot void platform_timer_enable(void) { +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT equeue = mbed_highprio_event_queue(); MBED_ASSERT(equeue != NULL); +#endif } // Actually cancels a timer, not the opposite of enable @@ -38,7 +49,14 @@ void platform_timer_set_cb(void (*new_fp)(void)) static void timer_callback(void) { due = 0; + +#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT + // Callback is interrupt safe so it can be called directly without + // bouncing via event queue thread. + arm_hal_callback(); +#else equeue->call(arm_hal_callback); +#endif } // This is called from inside platform_enter_critical - IRQs can't happen From a23b7cffea81e8fbbeac0003ffd1545819106e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tero=20J=C3=A4=C3=A4sk=C3=B6?= Date: Thu, 12 Apr 2018 15:12:34 +0300 Subject: [PATCH 2/2] nanostack-hal: eventloop: silence compiler warning for unused variable Compiler reminded that a variable declaration was left behind when the code using it was put behind #ifdef. Add the missing #ifdef. Warning being fixed: ---8<---8<---- [Warning] ns_event_loop.c@44,0: #177-D: variable "event_thread_id" was declared but never referenced --- .../nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c b/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c index 71af7c870c..45508ecf69 100644 --- a/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c +++ b/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c @@ -41,7 +41,10 @@ static const osThreadAttr_t event_thread_attr = { }; #endif +#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION static osThreadId_t event_thread_id; +#endif + static mbed_rtos_storage_mutex_t event_mutex; static const osMutexAttr_t event_mutex_attr = { .name = "nanostack_event_mutex",