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"
3737namespace {
3838using namespace mbed ;
3939using namespace events ;
40+ using namespace std ::chrono;
41+ using std::micro;
4042
41- static SingletonPtr<Timer> timer;
4243static bool timer_initialized = false ;
4344static 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.
5455struct 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
6261fhss_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-
6963static 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
7973static 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
8983static 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+ sleep_manager_lock_deep_sleep ();
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
169164static 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!" );
176172fhss_timer_t fhss_functions = {
177173 .fhss_timer_start = platform_fhss_timer_start,
178174 .fhss_timer_stop = platform_fhss_timer_stop,
0 commit comments