@@ -499,16 +499,37 @@ static uint32_t fhss_ws_calculate_ufsi(fhss_structure_t *fhss_structure, uint32_
499499 }
500500 }
501501 cur_slot -- ;
502- uint32_t remaining_time_ms = 0 ;
503- if (fhss_structure -> ws -> unicast_timer_running == true) {
504- remaining_time_ms = US_TO_MS (get_remaining_slots_us (fhss_structure , fhss_unicast_handler , MS_TO_US (dwell_time ) - NS_TO_US ((int64_t )(fhss_structure -> ws -> drift_per_millisecond_ns * dwell_time ))));
505- }
502+
506503 uint32_t time_to_tx = 0 ;
507504 uint32_t cur_time = fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api );
508- if (cur_time < tx_time ) {
505+ // High time to TX value (1000ms) is because actual TX time already passed.
506+ if (US_TO_MS (tx_time - cur_time ) < 1000 ) {
509507 time_to_tx = US_TO_MS (tx_time - cur_time );
510508 }
511- uint64_t ms_since_seq_start = (cur_slot * dwell_time ) + (dwell_time - remaining_time_ms ) + time_to_tx ;
509+ uint64_t ms_since_seq_start ;
510+ if (fhss_structure -> ws -> unicast_timer_running == true) {
511+ // Allow timer interrupt to delay max 10 seconds, otherwise assume next_uc_timeout overflowed
512+ if ((fhss_structure -> ws -> next_uc_timeout < cur_time ) && ((cur_time - fhss_structure -> ws -> next_uc_timeout ) < 10000000 )) {
513+ // The unicast timer has already expired, so count all previous slots
514+ // plus 1 completed slot
515+ // plus the time from timer expiration to now
516+ // plus the time until Tx
517+ ms_since_seq_start = ((cur_slot + 1 ) * dwell_time ) + US_TO_MS (cur_time - fhss_structure -> ws -> next_uc_timeout ) + time_to_tx ;
518+ } else {
519+ // The unicast timer is still running, so count all previous slots
520+ // plus the remaining time in the slot
521+ // plus the time until Tx
522+ uint32_t remaining_time_ms = US_TO_MS (fhss_structure -> ws -> next_uc_timeout - cur_time );
523+ ms_since_seq_start = (cur_slot * dwell_time ) + (dwell_time - remaining_time_ms ) + time_to_tx ;
524+ }
525+ } else {
526+ // The unicast timer is not running. Act as if the slot has completed.
527+ // count all previous slots
528+ // plus 1 completed slot
529+ // plus the time until Tx
530+ ms_since_seq_start = ((cur_slot + 1 ) * dwell_time ) + time_to_tx ;
531+ }
532+
512533 uint32_t seq_length = 0x10000 ;
513534 if (fhss_structure -> ws -> fhss_configuration .ws_uc_channel_function == WS_TR51CF ) {
514535 ms_since_seq_start %= (dwell_time * fhss_structure -> number_of_uc_channels );
0 commit comments