23
23
#include " mbed_trace.h"
24
24
#include " platform/SingletonPtr.h"
25
25
#include " platform/arm_hal_interrupt.h"
26
- #include < Timer.h >
26
+ #include " platform/mbed_power_mgmt.h "
27
27
#include " equeue.h"
28
28
#include " events/EventQueue.h"
29
29
#include " mbed_shared_queues.h"
37
37
namespace {
38
38
using namespace mbed ;
39
39
using namespace events ;
40
+ using namespace std ::chrono;
41
+ using std::micro;
40
42
41
- static SingletonPtr<Timer> timer;
42
43
static bool timer_initialized = false ;
43
44
static const fhss_api_t *fhss_active_handle = NULL ;
44
45
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
@@ -53,45 +54,41 @@ static EventQueue *equeue;
53
54
// an initialized-data cost.
54
55
struct fhss_timeout_s {
55
56
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 ;
58
57
bool active = false ;
59
58
SingletonPtr<Timeout> timeout;
60
59
};
61
60
62
61
fhss_timeout_s fhss_timeout[NUMBER_OF_SIMULTANEOUS_TIMEOUTS];
63
62
64
- static uint32_t read_current_time (void )
65
- {
66
- return timer->read_us ();
67
- }
68
-
69
63
static fhss_timeout_s *find_timeout (void (*callback)(const fhss_api_t *api, uint16_t ))
70
64
{
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 ;
74
68
}
75
69
}
76
- return NULL ;
70
+ return nullptr ;
77
71
}
78
72
79
73
static fhss_timeout_s *allocate_timeout (void )
80
74
{
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 ;
84
78
}
85
79
}
86
- return NULL ;
80
+ return nullptr ;
87
81
}
88
82
89
83
static void fhss_timeout_handler (void )
90
84
{
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
+ }
95
92
}
96
93
}
97
94
}
@@ -114,7 +111,7 @@ static int platform_fhss_timer_start(uint32_t slots, void (*callback)(const fhss
114
111
equeue = mbed_highprio_event_queue ();
115
112
MBED_ASSERT (equeue != NULL );
116
113
#endif
117
- timer-> start ();
114
+ sleep_manager_lock_deep_sleep ();
118
115
timer_initialized = true ;
119
116
}
120
117
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
127
124
return ret_val;
128
125
}
129
126
fhss_tim->fhss_timer_callback = callback;
130
- fhss_tim->start_time = read_current_time ();
131
- fhss_tim->stop_time = fhss_tim->start_time + slots;
132
127
fhss_tim->active = true ;
133
- fhss_tim->timeout ->attach_us (timer_callback, slots);
128
+ fhss_tim->timeout ->attach (timer_callback, microseconds{ slots} );
134
129
fhss_active_handle = callback_param;
135
130
ret_val = 0 ;
136
131
platform_exit_critical ();
@@ -161,18 +156,19 @@ static uint32_t platform_fhss_get_remaining_slots(void (*callback)(const fhss_ap
161
156
platform_exit_critical ();
162
157
return 0 ;
163
158
}
164
- uint32_t remaining_slots = fhss_tim->stop_time - read_current_time ();
159
+ microseconds remaining_slots = fhss_tim->timeout -> remaining_time ();
165
160
platform_exit_critical ();
166
- return remaining_slots;
161
+ return remaining_slots. count () ;
167
162
}
168
163
169
164
static uint32_t platform_fhss_timestamp_read (const fhss_api_t *api)
170
165
{
171
166
(void )api;
172
- return read_current_time ();
167
+ return HighResClock::now (). time_since_epoch (). count ();
173
168
}
174
169
} // anonymous namespace
175
170
171
+ static_assert (std::ratio_equal<HighResClock::period, micro>::value, " HighResClock not microseconds!" );
176
172
fhss_timer_t fhss_functions = {
177
173
.fhss_timer_start = platform_fhss_timer_start,
178
174
.fhss_timer_stop = platform_fhss_timer_stop,
0 commit comments