@@ -71,6 +71,16 @@ static uint32_t divide_integer(uint32_t dividend, uint32_t divisor)
71
71
return (dividend + divisor /2 ) / divisor ;
72
72
}
73
73
74
+ static uint32_t get_remaining_slots_us (fhss_structure_t * fhss_structure , void (* callback )(const fhss_api_t * api , uint16_t ), uint32_t max_timeout_us )
75
+ {
76
+ uint32_t remaining_time_us = fhss_structure -> platform_functions .fhss_get_remaining_slots (callback , fhss_structure -> fhss_api );
77
+ // When remaining time goes negative, use 0.
78
+ if (remaining_time_us > max_timeout_us ) {
79
+ remaining_time_us = 0 ;
80
+ }
81
+ return remaining_time_us ;
82
+ }
83
+
74
84
fhss_structure_t * fhss_ws_enable (fhss_api_t * fhss_api , const fhss_ws_configuration_t * fhss_configuration , const fhss_timer_t * fhss_timer )
75
85
{
76
86
if (!fhss_api || !fhss_configuration || !fhss_timer ) {
@@ -247,7 +257,7 @@ static void fhss_event_timer_cb(int8_t timer_id, uint16_t slots)
247
257
} else {
248
258
// On unicast, start timer to trigger polling event on next TX slot
249
259
uint32_t delay_between_tx_slots_us = fhss_structure -> ws -> txrx_slot_length_ms * 1000 * 2 ;
250
- if (delay_between_tx_slots_us < fhss_structure -> platform_functions . fhss_get_remaining_slots ( fhss_broadcast_handler , fhss_structure -> fhss_api )) {
260
+ if (delay_between_tx_slots_us < get_remaining_slots_us ( fhss_structure , fhss_broadcast_handler , fhss_structure -> ws -> fhss_configuration . fhss_broadcast_interval * 1000 )) {
251
261
eventOS_callback_timer_start (fhss_structure -> fhss_event_timer , delay_between_tx_slots_us /50 );
252
262
}
253
263
queue_size = fhss_structure -> callbacks .read_tx_queue_size (fhss_structure -> fhss_api , false);
@@ -265,16 +275,13 @@ static uint32_t fhss_ws_calculate_ufsi(fhss_structure_t *fhss_structure, uint32_
265
275
cur_slot = fhss_structure -> number_of_channels ;
266
276
}
267
277
cur_slot -- ;
268
- uint32_t remaining_time = (fhss_structure -> platform_functions .fhss_get_remaining_slots (fhss_unicast_handler , fhss_structure -> fhss_api ) / 1000 );
269
- if (remaining_time > dwell_time ) {
270
- remaining_time = 0 ;
271
- }
278
+ uint32_t remaining_time_ms = get_remaining_slots_us (fhss_structure , fhss_unicast_handler , fhss_structure -> ws -> fhss_configuration .fhss_uc_dwell_interval * 1000 ) / 1000 ;
272
279
uint32_t time_to_tx = 0 ;
273
280
uint32_t cur_time = fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api );
274
281
if (cur_time < tx_time ) {
275
282
time_to_tx = (tx_time - cur_time ) / 1000 ;
276
283
}
277
- uint64_t ms_since_seq_start = (cur_slot * dwell_time ) + (dwell_time - remaining_time ) + time_to_tx ;
284
+ uint64_t ms_since_seq_start = (cur_slot * dwell_time ) + (dwell_time - remaining_time_ms ) + time_to_tx ;
278
285
uint32_t seq_length = 0x10000 ;
279
286
if (fhss_structure -> ws -> fhss_configuration .ws_uc_channel_function == WS_TR51CF ) {
280
287
ms_since_seq_start %= (dwell_time * fhss_structure -> number_of_channels );
@@ -287,17 +294,16 @@ static uint32_t fhss_ws_calculate_broadcast_interval_offset(fhss_structure_t *fh
287
294
{
288
295
uint8_t dwell_time = fhss_structure -> ws -> fhss_configuration .fhss_bc_dwell_interval ;
289
296
uint32_t broadcast_interval = fhss_structure -> ws -> fhss_configuration .fhss_broadcast_interval ;
290
-
291
- uint32_t remaining_time = divide_integer (fhss_structure -> platform_functions .fhss_get_remaining_slots (fhss_broadcast_handler , fhss_structure -> fhss_api ), 1000 );
297
+ uint32_t remaining_time_ms = divide_integer (get_remaining_slots_us (fhss_structure , fhss_broadcast_handler , fhss_structure -> ws -> fhss_configuration .fhss_broadcast_interval * 1000 ), 1000 );
292
298
if (fhss_structure -> ws -> is_on_bc_channel == true) {
293
- remaining_time += (broadcast_interval - dwell_time );
299
+ remaining_time_ms += (broadcast_interval - dwell_time );
294
300
}
295
301
uint32_t time_to_tx = 0 ;
296
302
uint32_t cur_time = fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api );
297
303
if (cur_time < tx_time ) {
298
304
time_to_tx = divide_integer (tx_time - cur_time , 1000 );
299
305
}
300
- return (broadcast_interval - remaining_time ) + time_to_tx ;
306
+ return (broadcast_interval - remaining_time_ms ) + time_to_tx ;
301
307
}
302
308
303
309
static uint16_t fhss_ws_calculate_destination_slot (fhss_ws_neighbor_timing_info_t * neighbor_timing_info , uint32_t tx_time )
@@ -466,7 +472,8 @@ static bool fhss_ws_check_tx_allowed(fhss_structure_t *fhss_structure)
466
472
if (!number_of_tx_slots ) {
467
473
return true;
468
474
}
469
- uint32_t remaining_time_ms = fhss_structure -> platform_functions .fhss_get_remaining_slots (fhss_broadcast_handler , fhss_structure -> fhss_api ) / 1000 ;
475
+
476
+ uint32_t remaining_time_ms = get_remaining_slots_us (fhss_structure , fhss_broadcast_handler , fhss_structure -> ws -> fhss_configuration .fhss_broadcast_interval * 1000 ) / 1000 ;
470
477
uint32_t tx_slot_begin = (fhss_structure -> ws -> fhss_configuration .fhss_broadcast_interval - fhss_structure -> ws -> fhss_configuration .fhss_bc_dwell_interval ) - (fhss_structure -> ws -> txrx_slot_length_ms * (fhss_structure -> own_hop & 1 ));
471
478
uint32_t rx_slot_begin = tx_slot_begin - fhss_structure -> ws -> txrx_slot_length_ms ;
472
479
uint8_t n_o_tx_slots = number_of_tx_slots ;
@@ -488,8 +495,8 @@ static bool fhss_ws_check_tx_time(fhss_structure_t *fhss_structure, uint16_t tx_
488
495
return true;
489
496
}
490
497
uint32_t tx_time = fhss_get_tx_time (fhss_structure , tx_length , phy_header_length , phy_tail_length );
491
- uint32_t time_to_bc_channel = fhss_structure -> platform_functions . fhss_get_remaining_slots ( fhss_broadcast_handler , fhss_structure -> fhss_api );
492
- if (tx_time > time_to_bc_channel ) {
498
+ uint32_t time_to_bc_channel_us = get_remaining_slots_us ( fhss_structure , fhss_broadcast_handler , fhss_structure -> ws -> fhss_configuration . fhss_broadcast_interval );
499
+ if (tx_time > time_to_bc_channel_us ) {
493
500
return false;
494
501
}
495
502
return true;
0 commit comments