From 81873255605a9589ca7cb0aa5ac1dbf013d530e1 Mon Sep 17 00:00:00 2001 From: Alban Jeantheau Date: Tue, 8 Jun 2021 15:48:55 +0200 Subject: [PATCH 1/3] Correct ufsi timing calculation When calculating ufsi, the function was relying on the slot processed by the unicast fhss timer callback, which can be delayed. When it happens the slot value is wrong, and the ufsi is incorrect. The ufsi is then used by the peer to determined the reply channel, so the devices are thus unsynchronized until the next uplink packet. --- .../source/Service_Libs/fhss/fhss_ws.c | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c b/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c index 6c4e62e0d99..b5b1b18c509 100644 --- a/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c +++ b/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c @@ -499,16 +499,35 @@ static uint32_t fhss_ws_calculate_ufsi(fhss_structure_t *fhss_structure, uint32_ } } cur_slot--; - uint32_t remaining_time_ms = 0; - if (fhss_structure->ws->unicast_timer_running == true) { - 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)))); - } + uint32_t time_to_tx = 0; uint32_t cur_time = fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api); if (cur_time < tx_time) { time_to_tx = US_TO_MS(tx_time - cur_time); } - uint64_t ms_since_seq_start = (cur_slot * dwell_time) + (dwell_time - remaining_time_ms) + time_to_tx; + uint64_t ms_since_seq_start; + if (fhss_structure->ws->unicast_timer_running == true) { + if (fhss_structure->ws->next_uc_timeout < cur_time) { + // The unicast timer has already expired, so count all previous slots + // plus 1 completed slot + // plus the time from timer expiration to now + // plus the time until Tx + ms_since_seq_start = ((cur_slot + 1) * dwell_time) + US_TO_MS(cur_time - fhss_structure->ws->next_uc_timeout) + time_to_tx; + } else { + // The unicast timer is still running, so count all previous slots + // plus the remaining time in the slot + // plus the time until Tx + uint32_t remaining_time_ms = US_TO_MS(fhss_structure->ws->next_uc_timeout - cur_time); + ms_since_seq_start = (cur_slot * dwell_time) + (dwell_time - remaining_time_ms) + time_to_tx; + } + } else { + // The unicast timer is not running. Act as if the slot has completed. + // count all previous slots + // plus 1 completed slot + // plus the time until Tx + ms_since_seq_start = ( (cur_slot + 1) * dwell_time) + time_to_tx; + } + uint32_t seq_length = 0x10000; if (fhss_structure->ws->fhss_configuration.ws_uc_channel_function == WS_TR51CF) { ms_since_seq_start %= (dwell_time * fhss_structure->number_of_uc_channels); From b4b35c6a88986e0e514dbba7a048d35ee113da57 Mon Sep 17 00:00:00 2001 From: Alban Jeantheau Date: Fri, 11 Jun 2021 10:35:47 +0200 Subject: [PATCH 2/3] coding style --- .../sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c b/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c index b5b1b18c509..88ff16115ee 100644 --- a/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c +++ b/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c @@ -525,7 +525,7 @@ static uint32_t fhss_ws_calculate_ufsi(fhss_structure_t *fhss_structure, uint32_ // count all previous slots // plus 1 completed slot // plus the time until Tx - ms_since_seq_start = ( (cur_slot + 1) * dwell_time) + time_to_tx; + ms_since_seq_start = ((cur_slot + 1) * dwell_time) + time_to_tx; } uint32_t seq_length = 0x10000; From 3001e52216e4050be0e614d963432aa2e71f177d Mon Sep 17 00:00:00 2001 From: AlbanJeantheau-silabs <77835662+AlbanJeantheau-silabs@users.noreply.github.com> Date: Fri, 11 Jun 2021 10:39:16 +0200 Subject: [PATCH 3/3] Handle timer rollover in calculate_ufsi Co-authored-by: Jarkko Paso --- .../sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c b/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c index 88ff16115ee..ed174150a51 100644 --- a/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c +++ b/connectivity/nanostack/sal-stack-nanostack/source/Service_Libs/fhss/fhss_ws.c @@ -502,12 +502,14 @@ static uint32_t fhss_ws_calculate_ufsi(fhss_structure_t *fhss_structure, uint32_ uint32_t time_to_tx = 0; uint32_t cur_time = fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api); - if (cur_time < tx_time) { + // High time to TX value (1000ms) is because actual TX time already passed. + if (US_TO_MS(tx_time - cur_time) < 1000) { time_to_tx = US_TO_MS(tx_time - cur_time); } uint64_t ms_since_seq_start; if (fhss_structure->ws->unicast_timer_running == true) { - if (fhss_structure->ws->next_uc_timeout < cur_time) { + // Allow timer interrupt to delay max 10 seconds, otherwise assume next_uc_timeout overflowed + if ((fhss_structure->ws->next_uc_timeout < cur_time) && ((cur_time - fhss_structure->ws->next_uc_timeout) < 10000000)) { // The unicast timer has already expired, so count all previous slots // plus 1 completed slot // plus the time from timer expiration to now