Skip to content

Commit 1bdc4e6

Browse files
author
Jarkko Paso
authored
Merge pull request ARMmbed#1800 from ARMmbed/IOTTHD-2504
Iotthd 2504
2 parents f27a29c + 6ed53e6 commit 1bdc4e6

File tree

8 files changed

+61
-13
lines changed

8 files changed

+61
-13
lines changed

nanostack/fhss_api.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ typedef int mac_read_mac_address(const fhss_api_t *fhss_api, uint8_t *mac_addres
215215
*/
216216
typedef uint32_t mac_read_datarate(const fhss_api_t *fhss_api);
217217

218+
/**
219+
* @brief Read 32-bit timestamp.
220+
* @param fhss_api FHSS instance.
221+
* @return Timestamp.
222+
*/
223+
typedef uint32_t mac_read_timestamp(const fhss_api_t *fhss_api);
224+
218225
/**
219226
* @brief Change channel.
220227
* @param fhss_api FHSS instance.
@@ -287,6 +294,7 @@ struct fhss_callback {
287294
mac_read_tx_queue_size *read_tx_queue_size; /**< Read MAC TX queue size. */
288295
mac_read_mac_address *read_mac_address; /**< Read MAC address. */
289296
mac_read_datarate *read_datarate; /**< Read PHY datarate. */
297+
mac_read_timestamp *read_timestamp; /**< Read timestamp. */
290298
mac_change_channel *change_channel; /**< Change channel. */
291299
mac_send_fhss_frame *send_fhss_frame; /**< Send FHSS frame. */
292300
mac_synch_lost_notification *synch_lost_notification; /**< Send notification when FHSS synchronization is lost. */

source/MAC/IEEE802_15_4/mac_fhss_callbacks.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ uint32_t mac_read_phy_datarate(const fhss_api_t *fhss_api)
6565
return datarate;
6666
}
6767

68+
uint32_t mac_read_phy_timestamp(const fhss_api_t *fhss_api)
69+
{
70+
protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
71+
if (!mac_setup) {
72+
return 0;
73+
}
74+
uint32_t timestamp;
75+
mac_setup->dev_driver->phy_driver->extension(PHY_EXTENSION_GET_TIMESTAMP, (uint8_t *)&timestamp);
76+
return timestamp;
77+
}
78+
6879
int mac_set_channel(const fhss_api_t *fhss_api, uint8_t channel_number)
6980
{
7081
protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);

source/MAC/IEEE802_15_4/mac_fhss_callbacks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
uint16_t mac_read_tx_queue_sizes(const fhss_api_t *fhss_api, bool broadcast_queue);
2222
int mac_read_64bit_mac_address(const fhss_api_t *fhss_api, uint8_t *mac_address);
2323
uint32_t mac_read_phy_datarate(const fhss_api_t *fhss_api);
24+
uint32_t mac_read_phy_timestamp(const fhss_api_t *fhss_api);
2425
int mac_set_channel(const fhss_api_t *fhss_api, uint8_t channel_number);
2526
int mac_fhss_frame_tx(const fhss_api_t *fhss_api, int frame_type);
2627
int mac_synch_lost(const fhss_api_t *fhss_api);

source/MAC/IEEE802_15_4/sw_mac.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ int ns_sw_mac_fhss_register(mac_api_t *mac_api, fhss_api_t *fhss_api)
176176
fhss_callback_t callbacks;
177177
callbacks.read_tx_queue_size = &mac_read_tx_queue_sizes;
178178
callbacks.read_datarate = &mac_read_phy_datarate;
179+
callbacks.read_timestamp = &mac_read_phy_timestamp;
179180
callbacks.read_mac_address = &mac_read_64bit_mac_address;
180181
callbacks.change_channel = &mac_set_channel;
181182
callbacks.send_fhss_frame = &mac_fhss_frame_tx;

source/Service_Libs/fhss/fhss_ws.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434

3535
// Enable this flag to use channel traces
3636
// #define FHSS_CHANNEL_DEBUG
37+
// Enable this flag to use debug callbacks
38+
// #define FHSS_CHANNEL_DEBUG_CBS
39+
40+
#ifdef FHSS_CHANNEL_DEBUG_CBS
41+
void (*fhss_uc_switch)(void) = NULL;
42+
void (*fhss_bc_switch)(void) = NULL;
43+
#endif /*FHSS_CHANNEL_DEBUG_CBS*/
3744

3845
#define DEF_2E24 0x1000000
3946
#define IE_HEADER_LENGTH_MASK 0x007f
@@ -108,7 +115,6 @@ static int32_t fhss_ws_calc_bc_channel(fhss_structure_t *fhss_structure)
108115

109116
static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
110117
{
111-
(void) delay;
112118
int32_t next_channel;
113119
fhss_structure_t *fhss_structure = fhss_get_object_with_api(fhss_api);
114120
if (!fhss_structure) {
@@ -121,12 +127,12 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
121127
return;
122128
}
123129
if (fhss_structure->ws->is_on_bc_channel == false) {
124-
fhss_start_timer(fhss_structure, fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval*1000, fhss_broadcast_handler);
130+
fhss_start_timer(fhss_structure, fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval*1000 - (delay * fhss_structure->platform_functions.fhss_resolution_divider), fhss_broadcast_handler);
125131
fhss_structure->ws->is_on_bc_channel = true;
126132
next_channel = fhss_structure->ws->bc_channel = fhss_ws_calc_bc_channel(fhss_structure);
127133
} else {
128134
uint32_t timeout = (fhss_structure->ws->fhss_configuration.fhss_broadcast_interval - fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval) * 1000;
129-
fhss_start_timer(fhss_structure, timeout, fhss_broadcast_handler);
135+
fhss_start_timer(fhss_structure, timeout - (delay * fhss_structure->platform_functions.fhss_resolution_divider), fhss_broadcast_handler);
130136
fhss_structure->ws->is_on_bc_channel = false;
131137
// Should return to own (unicast) listening channel after broadcast channel
132138
next_channel = fhss_structure->rx_channel;
@@ -135,6 +141,11 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
135141
#endif /*FHSS_CHANNEL_DEBUG*/
136142
}
137143
fhss_structure->callbacks.change_channel(fhss_structure->fhss_api, next_channel);
144+
#ifdef FHSS_CHANNEL_DEBUG_CBS
145+
if (fhss_bc_switch) {
146+
fhss_bc_switch();
147+
}
148+
#endif /*FHSS_CHANNEL_DEBUG_CBS*/
138149
fhss_ws_start_tx_poll_timer(fhss_structure, fhss_structure->callbacks.read_tx_queue_size(fhss_structure->fhss_api, true),
139150
fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval);
140151
}
@@ -162,7 +173,11 @@ static uint32_t fhss_ws_calculate_ufsi(fhss_structure_t *fhss_structure, uint32_
162173
}
163174
cur_slot--;
164175
uint32_t remaining_time = (fhss_structure->platform_functions.fhss_get_remaining_slots(fhss_unicast_handler, fhss_structure->fhss_api) / 1000);
165-
uint32_t time_to_tx = (tx_time - fhss_structure->fhss_api->read_timestamp(fhss_structure->fhss_api)) / 1000;
176+
uint32_t time_to_tx = 0;
177+
uint32_t cur_time = fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api);
178+
if (cur_time < tx_time) {
179+
time_to_tx = (tx_time - cur_time) / 1000;
180+
}
166181
uint64_t ms_since_seq_start = (cur_slot * dwell_time) + (dwell_time-remaining_time) + time_to_tx;
167182
uint32_t seq_length = 0x10000;
168183
if (fhss_structure->ws->fhss_configuration.ws_channel_function == WS_TR51CF) {
@@ -181,7 +196,7 @@ static uint32_t fhss_ws_calculate_broadcast_interval_offset(fhss_structure_t *fh
181196
if (fhss_structure->ws->is_on_bc_channel == true) {
182197
remaining_time += (broadcast_interval - dwell_time);
183198
}
184-
uint32_t time_to_tx = (tx_time - fhss_structure->fhss_api->read_timestamp(fhss_structure->fhss_api)) / 1000;
199+
uint32_t time_to_tx = (tx_time - fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api)) / 1000;
185200
return (broadcast_interval-remaining_time) + time_to_tx;
186201
}
187202

@@ -194,7 +209,7 @@ static uint16_t fhss_ws_calculate_destination_slot(fhss_ws_neighbor_timing_info_
194209
if (neighbor_timing_info->uc_timing_info.unicast_channel_function == WS_TR51CF) {
195210
seq_length = neighbor_timing_info->uc_timing_info.unicast_number_of_channels;
196211
}
197-
uint32_t dest_ms_since_seq_start = own_ceil((float)(ufsi*seq_length*dwell_time) / DEF_2E24);
212+
uint32_t dest_ms_since_seq_start = own_ceil((float)((uint64_t)ufsi*seq_length*dwell_time) / DEF_2E24);
198213
return (own_floor(((float)((tx_time - ufsi_timestamp)/1000 + dest_ms_since_seq_start) / dwell_time)) % seq_length);
199214
}
200215

@@ -230,7 +245,7 @@ static void fhss_ws_synch_state_set_callback(const fhss_api_t *api, fhss_states
230245
static void fhss_ws_update_uc_channel_callback(fhss_structure_t *fhss_structure)
231246
{
232247
uint8_t mac_address[8];
233-
int32_t next_channel;
248+
int32_t next_channel = fhss_structure->rx_channel;
234249
fhss_structure->callbacks.read_mac_address(fhss_structure->fhss_api, mac_address);
235250
if (fhss_structure->ws->fhss_configuration.ws_channel_function == WS_FIXED_CHANNEL) {
236251
return;
@@ -255,6 +270,11 @@ static void fhss_ws_update_uc_channel_callback(fhss_structure_t *fhss_structure)
255270
tr_info("%"PRIu32" UC %u %u", fhss_structure->platform_functions.fhss_get_timestamp(fhss_structure->fhss_api), next_channel, fhss_structure->ws->uc_slot);
256271
#endif /*FHSS_CHANNEL_DEBUG*/
257272
fhss_structure->callbacks.change_channel(fhss_structure->fhss_api, next_channel);
273+
#ifdef FHSS_CHANNEL_DEBUG_CBS
274+
if (fhss_uc_switch) {
275+
fhss_uc_switch();
276+
}
277+
#endif /*FHSS_CHANNEL_DEBUG_CBS*/
258278
}
259279

260280
static int fhss_ws_tx_handle_callback(const fhss_api_t *api, bool is_broadcast_addr, uint8_t *destination_address, int frame_type, uint16_t frame_length, uint8_t phy_header_length, uint8_t phy_tail_length, uint32_t tx_time)
@@ -575,7 +595,7 @@ int fhss_ws_set_callbacks(fhss_structure_t *fhss_structure)
575595
fhss_structure->fhss_api->data_tx_done = &fhss_ws_data_tx_done_callback;
576596
fhss_structure->fhss_api->data_tx_fail = &fhss_ws_data_tx_fail_callback;
577597
fhss_structure->fhss_api->synch_state_set = &fhss_ws_synch_state_set_callback;
578-
fhss_structure->fhss_api->read_timestamp = &fhss_read_timestamp_cb;
598+
fhss_structure->fhss_api->read_timestamp = NULL;
579599
fhss_structure->fhss_api->get_retry_period = NULL;
580600
fhss_structure->fhss_api->write_synch_info = &fhss_ws_write_synch_info_callback;
581601
fhss_structure->fhss_api->init_callbacks = &fhss_init_callbacks_cb;
@@ -595,7 +615,7 @@ int fhss_ws_set_parent(fhss_structure_t *fhss_structure, const uint8_t eui64[8],
595615
return -1;
596616
}
597617

598-
uint32_t time_from_reception_ms = (fhss_structure->fhss_api->read_timestamp(fhss_structure->fhss_api) - bc_timing_info->bt_rx_timestamp)/1000;
618+
uint32_t time_from_reception_ms = (fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api) - bc_timing_info->bt_rx_timestamp)/1000;
599619
uint32_t true_bc_interval_offset = (bc_timing_info->broadcast_interval_offset + time_from_reception_ms) % bc_timing_info->broadcast_interval;
600620
uint32_t timeout = ((bc_timing_info->broadcast_interval-true_bc_interval_offset)*1000);
601621
fhss_start_timer(fhss_structure, timeout, fhss_broadcast_handler);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ static fhss_api_t *test_generate_fhss_api(void)
144144
fhss_common_stub.fhss_struct.callbacks.change_channel = &mac_set_channel;
145145
fhss_common_stub.fhss_struct.callbacks.read_mac_address = &mac_read_64bit_mac_address;
146146
fhss_common_stub.fhss_struct.callbacks.read_tx_queue_size = &mac_read_tx_queue_sizes;
147+
fhss_common_stub.fhss_struct.callbacks.read_timestamp = &mac_read_phy_timestamp;
147148
fhss_common_stub.fhss_struct.callbacks.tx_poll = &mac_poll_tx_queue;
148149
test_set_platform_api(&fhss_common_stub.fhss_struct.platform_functions);
149150
fhss_ws_set_callbacks(&fhss_common_stub.fhss_struct);
@@ -469,24 +470,24 @@ bool test_fhss_ws_write_synch_info_callback()
469470
return false;
470471
}
471472
fhss_platform_stub.remaining_slots_value = 100000;
472-
fhss_common_stub.uint32_value = 1000000;
473+
fhss_callbacks_stub.uint32_value = 1000000;
473474
/* Test the UFSI field in synch info
474475
* slot: | 0(200ms) | 1(200ms) | 2(200ms) | 3(200ms) |
475476
* | ufsi(597ms) -> 1001599 |
476477
* timestamps: written at 1000000us|TX at 1097000us|
477478
*/
478-
if ((fhss_common_stub.fhss_struct.fhss_api->write_synch_info(api, synch_info, sizeof(synch_info), DEFAULT_FRAME_TYPE, fhss_common_stub.uint32_value + 97000) != 0)
479+
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 + 97000) != 0)
479480
|| (test_ufsi(&synch_info[11], 1001599) != true)) {
480481
return false;
481482
}
482483
// Test when timestamp overflows
483-
fhss_common_stub.uint32_value = 4294960000;
484+
fhss_callbacks_stub.uint32_value = 4294960000;
484485
/* Test the UFSI field in synch info
485486
* slot: | 0(200ms) | 1(200ms) | 2(200ms) | 3(200ms) |
486487
* | ufsi(597ms) -> 1001599 |
487488
* timestamps: written at 4294960000us| TX at 89704us|
488489
*/
489-
if ((fhss_common_stub.fhss_struct.fhss_api->write_synch_info(api, synch_info, sizeof(synch_info), DEFAULT_FRAME_TYPE, fhss_common_stub.uint32_value + 97000) != 0)
490+
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 + 97000) != 0)
490491
|| (test_ufsi(&synch_info[11], 1001599) != true)) {
491492
return false;
492493
}

test/nanostack/unittest/stub/fhss_callbacks_stub.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ uint32_t mac_read_phy_datarate(const fhss_api_t *fhss_api)
3535
return 250000;
3636
}
3737

38+
uint32_t mac_read_phy_timestamp(const fhss_api_t *fhss_api)
39+
{
40+
return fhss_callbacks_stub.uint32_value;
41+
}
42+
3843
int mac_set_channel(const fhss_api_t *fhss_api, uint8_t channel_number)
3944
{
4045
fhss_callbacks_stub.uint8_value = channel_number;

test/nanostack/unittest/stub/fhss_callbacks_stub.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern "C" {
2424
typedef struct {
2525
uint8_t uint8_value;
2626
int8_t int8_value;
27+
uint32_t uint32_value;
2728
uint8_t address[8];
2829
} fhss_callbacks_stub_def;
2930

0 commit comments

Comments
 (0)