Skip to content

Commit c037d50

Browse files
committed
Nanostack HAL: Convert to Chrono
1 parent d1ae0d5 commit c037d50

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

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

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "mbed_trace.h"
2424
#include "platform/SingletonPtr.h"
2525
#include "platform/arm_hal_interrupt.h"
26-
#include <Timer.h>
26+
#include "platform/mbed_power_mgmt.h"
2727
#include "equeue.h"
2828
#include "events/EventQueue.h"
2929
#include "mbed_shared_queues.h"
@@ -37,8 +37,9 @@
3737
namespace {
3838
using namespace mbed;
3939
using namespace events;
40+
using namespace std::chrono;
41+
using std::micro;
4042

41-
static SingletonPtr<Timer> timer;
4243
static bool timer_initialized = false;
4344
static const fhss_api_t *fhss_active_handle = NULL;
4445
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
@@ -53,45 +54,41 @@ static EventQueue *equeue;
5354
// an initialized-data cost.
5455
struct fhss_timeout_s {
5556
void (*fhss_timer_callback)(const fhss_api_t *fhss_api, uint16_t) = nullptr;
56-
uint32_t start_time = 0;
57-
uint32_t stop_time = 0;
5857
bool active = false;
5958
SingletonPtr<Timeout> timeout;
6059
};
6160

6261
fhss_timeout_s fhss_timeout[NUMBER_OF_SIMULTANEOUS_TIMEOUTS];
6362

64-
static uint32_t read_current_time(void)
65-
{
66-
return timer->read_us();
67-
}
68-
6963
static fhss_timeout_s *find_timeout(void (*callback)(const fhss_api_t *api, uint16_t))
7064
{
71-
for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) {
72-
if (fhss_timeout[i].fhss_timer_callback == callback) {
73-
return &fhss_timeout[i];
65+
for (fhss_timeout_s &t : fhss_timeout) {
66+
if (t.fhss_timer_callback == callback) {
67+
return &t;
7468
}
7569
}
76-
return NULL;
70+
return nullptr;
7771
}
7872

7973
static fhss_timeout_s *allocate_timeout(void)
8074
{
81-
for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) {
82-
if (fhss_timeout[i].fhss_timer_callback == NULL) {
83-
return &fhss_timeout[i];
75+
for (fhss_timeout_s &t : fhss_timeout) {
76+
if (t.fhss_timer_callback == NULL) {
77+
return &t;
8478
}
8579
}
86-
return NULL;
80+
return nullptr;
8781
}
8882

8983
static void fhss_timeout_handler(void)
9084
{
91-
for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) {
92-
if (fhss_timeout[i].active && ((fhss_timeout[i].stop_time - fhss_timeout[i].start_time) <= (read_current_time() - fhss_timeout[i].start_time))) {
93-
fhss_timeout[i].active = false;
94-
fhss_timeout[i].fhss_timer_callback(fhss_active_handle, read_current_time() - fhss_timeout[i].stop_time);
85+
for (fhss_timeout_s &t : fhss_timeout) {
86+
if (t.active) {
87+
microseconds remaining_time = t.timeout->remaining_time();
88+
if (remaining_time <= 0s) {
89+
t.active = false;
90+
t.fhss_timer_callback(fhss_active_handle, -remaining_time.count());
91+
}
9592
}
9693
}
9794
}
@@ -114,7 +111,7 @@ static int platform_fhss_timer_start(uint32_t slots, void (*callback)(const fhss
114111
equeue = mbed_highprio_event_queue();
115112
MBED_ASSERT(equeue != NULL);
116113
#endif
117-
timer->start();
114+
HighResClock::lock();
118115
timer_initialized = true;
119116
}
120117
fhss_timeout_s *fhss_tim = find_timeout(callback);
@@ -127,10 +124,8 @@ static int platform_fhss_timer_start(uint32_t slots, void (*callback)(const fhss
127124
return ret_val;
128125
}
129126
fhss_tim->fhss_timer_callback = callback;
130-
fhss_tim->start_time = read_current_time();
131-
fhss_tim->stop_time = fhss_tim->start_time + slots;
132127
fhss_tim->active = true;
133-
fhss_tim->timeout->attach_us(timer_callback, slots);
128+
fhss_tim->timeout->attach(timer_callback, microseconds{slots});
134129
fhss_active_handle = callback_param;
135130
ret_val = 0;
136131
platform_exit_critical();
@@ -161,18 +156,19 @@ static uint32_t platform_fhss_get_remaining_slots(void (*callback)(const fhss_ap
161156
platform_exit_critical();
162157
return 0;
163158
}
164-
uint32_t remaining_slots = fhss_tim->stop_time - read_current_time();
159+
microseconds remaining_slots = fhss_tim->timeout->remaining_time();
165160
platform_exit_critical();
166-
return remaining_slots;
161+
return remaining_slots.count();
167162
}
168163

169164
static uint32_t platform_fhss_timestamp_read(const fhss_api_t *api)
170165
{
171166
(void)api;
172-
return read_current_time();
167+
return HighResClock::now().time_since_epoch().count();
173168
}
174169
} // anonymous namespace
175170

171+
static_assert(std::ratio_equal<HighResClock::period, micro>::value, "HighResClock not microseconds!");
176172
fhss_timer_t fhss_functions = {
177173
.fhss_timer_start = platform_fhss_timer_start,
178174
.fhss_timer_stop = platform_fhss_timer_stop,

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
#include "platform/arm_hal_interrupt.h"
2424
#include "platform/mbed_assert.h"
2525
#include "Timeout.h"
26-
#include "Timer.h"
27-
#include "Ticker.h"
2826
#include "events/Event.h"
2927
#include "events/mbed_shared_queues.h"
3028

3129
using namespace mbed;
3230
using namespace events;
31+
using std::ratio;
32+
using std::milli;
33+
using std::micro;
34+
using namespace std::chrono;
3335

34-
static SingletonPtr<Timer> timer;
3536
static SingletonPtr<Timeout> timeout;
3637

3738
// If critical sections are implemented using mutexes, timers must be called in thread context, and
@@ -42,8 +43,11 @@ static SingletonPtr<Timeout> timeout;
4243
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
4344
static EventQueue *equeue;
4445
#endif
45-
46-
static uint32_t due;
46+
namespace {
47+
using slot_period = std::ratio_multiply<ratio<50>, micro>;
48+
using slots_t = duration<uint32_t, slot_period>;
49+
}
50+
static HighResClock::time_point due;
4751
static void (*arm_hal_callback)(void);
4852

4953
#if defined(NS_EVENTLOOP_USE_TICK_TIMER)
@@ -69,14 +73,15 @@ int8_t platform_tick_timer_register(void (*tick_timer_cb_handler)(void))
6973
int8_t platform_tick_timer_start(uint32_t period_ms)
7074
{
7175
int8_t retval = -1;
76+
auto period = duration<uint32_t, milli>(period_ms);
7277
if (tick_timer_cb && tick_timer_id == 0) {
7378
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
74-
tick_timer_id = equeue->call_every(period_ms, tick_timer_cb);
79+
tick_timer_id = equeue->call_every(period, tick_timer_cb);
7580
if (tick_timer_id != 0) {
7681
retval = 0;
7782
}
7883
#else
79-
tick_ticker->attach_us(tick_timer_cb, period_ms * 1000);
84+
tick_ticker->attach(tick_timer_cb, period);
8085
tick_timer_id = 1;
8186
retval = 0;
8287
#endif
@@ -108,7 +113,6 @@ void platform_timer_enable(void)
108113
equeue = mbed_highprio_event_queue();
109114
MBED_ASSERT(equeue != NULL);
110115
#endif
111-
timer->start();
112116
// Prime the SingletonPtr - can't construct from IRQ/critical section
113117
timeout.get();
114118
}
@@ -127,7 +131,7 @@ void platform_timer_set_cb(void (*new_fp)(void))
127131

128132
static void timer_callback(void)
129133
{
130-
due = 0;
134+
due = HighResClock::time_point{};
131135

132136
#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
133137
// Callback is interrupt safe so it can be called directly without
@@ -141,17 +145,17 @@ static void timer_callback(void)
141145
// This is called from inside platform_enter_critical - IRQs can't happen
142146
void platform_timer_start(uint16_t slots)
143147
{
144-
timer->reset();
145-
due = slots * UINT32_C(50);
146-
timeout->attach_us(timer_callback, due);
148+
slots_t rel_time{slots};
149+
due = HighResClock::now() + rel_time;
150+
timeout->attach_absolute(timer_callback, due);
147151
}
148152

149153
// This is called from inside platform_enter_critical - IRQs can't happen
150154
uint16_t platform_timer_get_remaining_slots(void)
151155
{
152-
uint32_t elapsed = timer->read_us();
153-
if (elapsed < due) {
154-
return (uint16_t)((due - elapsed) / 50);
156+
auto now = HighResClock::now();
157+
if (now < due) {
158+
return duration_cast<slots_t>(due - now).count();
155159
} else {
156160
return 0;
157161
}

0 commit comments

Comments
 (0)