Skip to content

Commit 04de6e2

Browse files
author
Arto Kinnunen
authored
Merge pull request ARMmbed#2638 from PelionIoT/mbed_os_fix_ufsi_calculation
(via Mbed OS) Correct ufsi timing calculation
2 parents 560619d + 2012347 commit 04de6e2

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

source/Service_Libs/fhss/fhss_ws.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

test/nanostack/unittest/service_libs/fhss_ws/test_fhss_ws.c

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,13 +648,36 @@ bool test_fhss_ws_synch_state_set_callback()
648648
return true;
649649
}
650650

651+
static void test_reset_synch_info(uint8_t *synch_info)
652+
{
653+
memset(synch_info, 0, 100);
654+
synch_info[0] = 0x05;
655+
synch_info[1] = 0x15;
656+
synch_info[2] = 0x02;
657+
synch_info[3] = 0x00;
658+
synch_info[4] = 0x00;
659+
synch_info[5] = 0x00;
660+
synch_info[6] = 0x00;
661+
synch_info[7] = 0x05;
662+
synch_info[8] = 0x15;
663+
synch_info[9] = 0x01;
664+
synch_info[10] = 0x04;
665+
synch_info[11] = 0x00;
666+
synch_info[12] = 0x00;
667+
synch_info[13] = 0x00;
668+
synch_info[14] = 0x00;
669+
synch_info[15] = 0x3f;
670+
}
671+
651672
bool test_fhss_ws_write_synch_info_callback()
652673
{
653674
fhss_api_t *api = test_generate_fhss_api();
654675
fhss_common_stub.fhss_struct.ws->fhss_configuration.ws_uc_channel_function = WS_TR51CF;
655676
fhss_common_stub.fhss_struct.rx_channel = 0;
656677
fhss_common_stub.fhss_struct.ws->unicast_timer_running = true;
657-
uint8_t synch_info[100] = {0x05, 0x15, 0x02, 0x00, 0x00, 0x00, 0x00, 0x05, 0x15, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x3f};
678+
uint8_t synch_info[100];
679+
test_reset_synch_info(synch_info);
680+
658681
// Test when FHSS struct not found
659682
disable_fhss_struct();
660683
if (fhss_common_stub.fhss_struct.fhss_api->write_synch_info(api, synch_info, sizeof(synch_info), DEFAULT_FRAME_TYPE, DEFAULT_TX_TIME) != -1) {
@@ -671,6 +694,7 @@ bool test_fhss_ws_write_synch_info_callback()
671694
}
672695
fhss_platform_stub.remaining_slots_value = 100000;
673696
fhss_callbacks_stub.uint32_value = 1000000;
697+
fhss_common_stub.fhss_struct.ws->next_uc_timeout = fhss_callbacks_stub.uint32_value + fhss_platform_stub.remaining_slots_value;
674698
/* Test the UFSI field in synch info
675699
* slot: | 0(200ms) | 1(200ms) | 2(200ms) | 3(200ms) |
676700
* | ufsi(597ms) -> 1001599 |
@@ -680,8 +704,10 @@ bool test_fhss_ws_write_synch_info_callback()
680704
|| (test_ufsi(&synch_info[11], 1001599) != true)) {
681705
return false;
682706
}
707+
test_reset_synch_info(synch_info);
683708
// Test when timestamp overflows
684709
fhss_callbacks_stub.uint32_value = 4294960000;
710+
fhss_common_stub.fhss_struct.ws->next_uc_timeout = fhss_callbacks_stub.uint32_value + fhss_platform_stub.remaining_slots_value;
685711
/* Test the UFSI field in synch info
686712
* slot: | 0(200ms) | 1(200ms) | 2(200ms) | 3(200ms) |
687713
* | ufsi(597ms) -> 1001599 |
@@ -691,7 +717,33 @@ bool test_fhss_ws_write_synch_info_callback()
691717
|| (test_ufsi(&synch_info[11], 1001599) != true)) {
692718
return false;
693719
}
694-
720+
test_reset_synch_info(synch_info);
721+
// Test when TX time has passed
722+
fhss_callbacks_stub.uint32_value = 1000000;
723+
fhss_common_stub.fhss_struct.ws->next_uc_timeout = fhss_callbacks_stub.uint32_value + fhss_platform_stub.remaining_slots_value;
724+
/* Test the UFSI field in synch info
725+
* slot: | 0(200ms) | 1(200ms) | 2(200ms) | 3(200ms) |
726+
* | ufsi(500ms) -> 838860
727+
* timestamps: TX at 995000us|written at 1000000us|
728+
*/
729+
if ((fhss_common_stub.fhss_struct.fhss_api->write_synch_info(api, synch_info, sizeof(synch_info), DEFAULT_FRAME_TYPE, fhss_callbacks_stub.uint32_value - 5000) != 0)
730+
|| (test_ufsi(&synch_info[11], 838860) != true)) {
731+
return false;
732+
}
733+
test_reset_synch_info(synch_info);
734+
// Test when interrupt is delayed
735+
fhss_platform_stub.remaining_slots_value = 0;
736+
fhss_callbacks_stub.uint32_value = 1000000;
737+
fhss_common_stub.fhss_struct.ws->next_uc_timeout = fhss_callbacks_stub.uint32_value - 5000;
738+
/* Test the UFSI field in synch info
739+
* slot: | 0(200ms) | 1(200ms) | 2(200ms) | 3(200ms) |
740+
* | ufsi(597ms) -> 1001599
741+
* timestamps: written at 1000000us|TX at 1005000us|
742+
*/
743+
if ((fhss_common_stub.fhss_struct.fhss_api->write_synch_info(api, synch_info, sizeof(synch_info), DEFAULT_FRAME_TYPE, fhss_callbacks_stub.uint32_value + 7000) != 0)
744+
|| (test_ufsi(&synch_info[11], 1026765) != true)) {
745+
return false;
746+
}
695747
// Test when IE element not found
696748
memset(synch_info, 0, sizeof(synch_info));
697749
if (fhss_common_stub.fhss_struct.fhss_api->write_synch_info(api, synch_info, sizeof(synch_info), DEFAULT_FRAME_TYPE, DEFAULT_TX_TIME) != 0) {

0 commit comments

Comments
 (0)