Skip to content

Commit 7349bc8

Browse files
author
Kimmo Vaisanen
committed
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.
1 parent 46603f8 commit 7349bc8

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "platform/mbed_assert.h"
2525
#include "Timeout.h"
2626
#include "Timer.h"
27+
#include "Ticker.h"
2728
#include "events/Event.h"
2829
#include "events/mbed_shared_queues.h"
2930

@@ -45,6 +46,61 @@ static EventQueue *equeue;
4546
static uint32_t due;
4647
static void (*arm_hal_callback)(void);
4748

49+
#if defined(NS_EVENTLOOP_USE_TICK_TIMER)
50+
51+
#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
52+
static SingletonPtr<Ticker> tick_ticker;
53+
#endif
54+
55+
static int tick_timer_id;
56+
static void (*tick_timer_cb)(void);
57+
58+
int8_t platform_tick_timer_register(void (*tick_timer_cb_handler)(void))
59+
{
60+
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
61+
equeue = mbed_highprio_event_queue();
62+
MBED_ASSERT(equeue != NULL);
63+
#endif
64+
65+
tick_timer_cb = tick_timer_cb_handler;
66+
return 0;
67+
}
68+
69+
int8_t platform_tick_timer_start(uint32_t period_ms)
70+
{
71+
int8_t retval = -1;
72+
if (tick_timer_cb && tick_timer_id == 0) {
73+
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
74+
tick_timer_id = equeue->call_every(period_ms, tick_timer_cb);
75+
if (tick_timer_id != 0) {
76+
retval = 0;
77+
}
78+
#else
79+
tick_ticker->attach_us(tick_timer_cb, period_ms * 1000);
80+
tick_timer_id = 1;
81+
retval = 0;
82+
#endif
83+
}
84+
return retval;
85+
}
86+
87+
int8_t platform_tick_timer_stop(void)
88+
{
89+
int8_t retval = -1;
90+
if (tick_timer_id != 0) {
91+
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
92+
equeue->cancel(tick_timer_id);
93+
#else
94+
tick_ticker->detach();
95+
#endif
96+
tick_timer_id = 0;
97+
retval = 0;
98+
}
99+
return retval;
100+
}
101+
#endif // NS_EVENTLOOP_USE_TICK_TIMER
102+
103+
48104
// Called once at boot
49105
void platform_timer_enable(void)
50106
{

features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ void ns_hal_init(void *heap, size_t h_size, void (*passed_fptr)(heap_fail_t), me
4343
}
4444
platform_critical_init();
4545
ns_dyn_mem_init(heap, h_size, passed_fptr, info_ptr);
46+
#ifndef NS_EXCLUDE_HIGHRES_TIMER
4647
platform_timer_enable();
48+
#endif
4749
eventOS_scheduler_init();
4850

4951
// We do not initialise randlib, as it should be done after

0 commit comments

Comments
 (0)