From 7349bc8a47355d9679706a067bc3d9cc8d2fc055 Mon Sep 17 00:00:00 2001 From: Kimmo Vaisanen Date: Wed, 24 Apr 2019 14:37:16 +0300 Subject: [PATCH] Implement nanostack eventloop tick timer Nanostack eventloop tick timer can be used in case high resolution platform timer is not needed. One usecase for that is Pelion Cloud client when using for example cellular connectivity. This enables PDMC application to enter deep sleep state. --- .../arm_hal_timer.cpp | 56 +++++++++++++++++++ .../ns_hal_init.c | 2 + 2 files changed, 58 insertions(+) diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp index c7952d1be9..d9b870c0f8 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp @@ -24,6 +24,7 @@ #include "platform/mbed_assert.h" #include "Timeout.h" #include "Timer.h" +#include "Ticker.h" #include "events/Event.h" #include "events/mbed_shared_queues.h" @@ -45,6 +46,61 @@ static EventQueue *equeue; static uint32_t due; static void (*arm_hal_callback)(void); +#if defined(NS_EVENTLOOP_USE_TICK_TIMER) + +#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT +static SingletonPtr tick_ticker; +#endif + +static int tick_timer_id; +static void (*tick_timer_cb)(void); + +int8_t platform_tick_timer_register(void (*tick_timer_cb_handler)(void)) +{ +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT + equeue = mbed_highprio_event_queue(); + MBED_ASSERT(equeue != NULL); +#endif + + tick_timer_cb = tick_timer_cb_handler; + return 0; +} + +int8_t platform_tick_timer_start(uint32_t period_ms) +{ + int8_t retval = -1; + if (tick_timer_cb && tick_timer_id == 0) { +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT + tick_timer_id = equeue->call_every(period_ms, tick_timer_cb); + if (tick_timer_id != 0) { + retval = 0; + } +#else + tick_ticker->attach_us(tick_timer_cb, period_ms * 1000); + tick_timer_id = 1; + retval = 0; +#endif + } + return retval; +} + +int8_t platform_tick_timer_stop(void) +{ + int8_t retval = -1; + if (tick_timer_id != 0) { +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT + equeue->cancel(tick_timer_id); +#else + tick_ticker->detach(); +#endif + tick_timer_id = 0; + retval = 0; + } + return retval; +} +#endif // NS_EVENTLOOP_USE_TICK_TIMER + + // Called once at boot void platform_timer_enable(void) { diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c index 0a81c9ef17..9b97d67a4b 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c @@ -43,7 +43,9 @@ void ns_hal_init(void *heap, size_t h_size, void (*passed_fptr)(heap_fail_t), me } platform_critical_init(); ns_dyn_mem_init(heap, h_size, passed_fptr, info_ptr); +#ifndef NS_EXCLUDE_HIGHRES_TIMER platform_timer_enable(); +#endif eventOS_scheduler_init(); // We do not initialise randlib, as it should be done after