Skip to content

Commit 296e455

Browse files
author
Jarkko Paso
committed
FHSS: Time unit defines added
1 parent 724696c commit 296e455

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

source/Service_Libs/fhss/fhss_ws.c

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@
4242
void (*fhss_uc_switch)(void) = NULL;
4343
void (*fhss_bc_switch)(void) = NULL;
4444
#endif /*FHSS_CHANNEL_DEBUG_CBS*/
45-
45+
// Seconds to milliseconds
46+
#define S_TO_MS(x) (((uint32_t)x)*1000)
47+
// Milliseconds to seconds
48+
#define MS_TO_S(x) divide_integer(x, 1000)
49+
// Seconds to microseconds
50+
#define S_TO_US(x) (((uint32_t)x)*1000000)
51+
// Microseconds to seconds
52+
#define US_TO_S(x) divide_integer(x, 1000000)
53+
// Milliseconds to microseconds
54+
#define MS_TO_US(x) (((uint32_t)x)*1000)
55+
// Microseconds to milliseconds
56+
#define US_TO_MS(x) divide_integer(x, 1000)
4657
#define DEF_2E24 0x1000000
4758
#define IE_HEADER_LENGTH_MASK 0x007f
4859
#define IE_HEADER_ID_MASK 0x7f80
@@ -185,7 +196,7 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
185196
return;
186197
}
187198
if (fhss_structure->ws->is_on_bc_channel == false) {
188-
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);
199+
fhss_start_timer(fhss_structure, MS_TO_US(fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval) - (delay * fhss_structure->platform_functions.fhss_resolution_divider), fhss_broadcast_handler);
189200
fhss_structure->ws->is_on_bc_channel = true;
190201
next_channel = fhss_structure->ws->bc_channel = fhss_ws_calc_bc_channel(fhss_structure);
191202

@@ -194,12 +205,12 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
194205
* Max random is 1/10 of the channel dwell interval.
195206
* Event timer resolution is 50us.
196207
*/
197-
uint32_t bc_dwell_us = fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval*1000;
208+
uint32_t bc_dwell_us = MS_TO_US(fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval);
198209
uint16_t bc_min_random = (bc_dwell_us / 50) / 50;
199210
uint16_t bc_max_random = (bc_dwell_us / 10) / 50;
200211
eventOS_callback_timer_start(fhss_structure->fhss_event_timer, randLIB_get_random_in_range(bc_min_random, bc_max_random));
201212
} else {
202-
uint32_t timeout = (fhss_structure->ws->fhss_configuration.fhss_broadcast_interval - fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval) * 1000;
213+
uint32_t timeout = MS_TO_US(fhss_structure->ws->fhss_configuration.fhss_broadcast_interval - fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval);
203214
fhss_start_timer(fhss_structure, timeout - (delay * fhss_structure->platform_functions.fhss_resolution_divider), fhss_broadcast_handler);
204215
fhss_structure->ws->is_on_bc_channel = false;
205216
// Should return to own (unicast) listening channel after broadcast channel
@@ -209,7 +220,7 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
209220
* Max random is 1/10 of the TX slot length.
210221
* Event timer resolution is 50us.
211222
*/
212-
uint32_t txrx_slot_length_us = fhss_structure->ws->txrx_slot_length_ms * 1000;
223+
uint32_t txrx_slot_length_us = MS_TO_US(fhss_structure->ws->txrx_slot_length_ms);
213224
uint16_t uc_min_random = (txrx_slot_length_us / 30) / 50;
214225
uint16_t uc_max_random = (txrx_slot_length_us / 10) / 50;
215226
bool tx_allowed = fhss_ws_check_tx_allowed(fhss_structure);
@@ -256,8 +267,8 @@ static void fhss_event_timer_cb(int8_t timer_id, uint16_t slots)
256267
queue_size = fhss_structure->callbacks.read_tx_queue_size(fhss_structure->fhss_api, true);
257268
} else {
258269
// On unicast, start timer to trigger polling event on next TX slot
259-
uint32_t delay_between_tx_slots_us = fhss_structure->ws->txrx_slot_length_ms*1000*2;
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)) {
270+
uint32_t delay_between_tx_slots_us = MS_TO_US(fhss_structure->ws->txrx_slot_length_ms)*2;
271+
if (delay_between_tx_slots_us < get_remaining_slots_us(fhss_structure, fhss_broadcast_handler, MS_TO_US(fhss_structure->ws->fhss_configuration.fhss_broadcast_interval))) {
261272
eventOS_callback_timer_start(fhss_structure->fhss_event_timer, delay_between_tx_slots_us/50);
262273
}
263274
queue_size = fhss_structure->callbacks.read_tx_queue_size(fhss_structure->fhss_api, false);
@@ -275,11 +286,11 @@ static uint32_t fhss_ws_calculate_ufsi(fhss_structure_t *fhss_structure, uint32_
275286
cur_slot = fhss_structure->number_of_channels;
276287
}
277288
cur_slot--;
278-
uint32_t remaining_time_ms = divide_integer(get_remaining_slots_us(fhss_structure, fhss_unicast_handler, fhss_structure->ws->fhss_configuration.fhss_uc_dwell_interval*1000), 1000);
289+
uint32_t remaining_time_ms = US_TO_MS(get_remaining_slots_us(fhss_structure, fhss_unicast_handler, MS_TO_US(fhss_structure->ws->fhss_configuration.fhss_uc_dwell_interval)));
279290
uint32_t time_to_tx = 0;
280291
uint32_t cur_time = fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api);
281292
if (cur_time < tx_time) {
282-
time_to_tx = divide_integer((tx_time - cur_time), 1000);
293+
time_to_tx = US_TO_MS(tx_time - cur_time);
283294
}
284295
uint64_t ms_since_seq_start = (cur_slot * dwell_time) + (dwell_time-remaining_time_ms) + time_to_tx;
285296
uint32_t seq_length = 0x10000;
@@ -294,14 +305,14 @@ static uint32_t fhss_ws_calculate_broadcast_interval_offset(fhss_structure_t *fh
294305
{
295306
uint8_t dwell_time = fhss_structure->ws->fhss_configuration.fhss_bc_dwell_interval;
296307
uint32_t broadcast_interval = fhss_structure->ws->fhss_configuration.fhss_broadcast_interval;
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);
308+
uint32_t remaining_time_ms = US_TO_MS(get_remaining_slots_us(fhss_structure, fhss_broadcast_handler, MS_TO_US(fhss_structure->ws->fhss_configuration.fhss_broadcast_interval)));
298309
if (fhss_structure->ws->is_on_bc_channel == true) {
299310
remaining_time_ms += (broadcast_interval - dwell_time);
300311
}
301312
uint32_t time_to_tx = 0;
302313
uint32_t cur_time = fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api);
303314
if (cur_time < tx_time) {
304-
time_to_tx = divide_integer(tx_time - cur_time, 1000);
315+
time_to_tx = US_TO_MS(tx_time - cur_time);
305316
}
306317
return (broadcast_interval-remaining_time_ms) + time_to_tx;
307318
}
@@ -316,12 +327,12 @@ static uint16_t fhss_ws_calculate_destination_slot(fhss_ws_neighbor_timing_info_
316327
seq_length = neighbor_timing_info->uc_timing_info.unicast_number_of_channels;
317328
}
318329
uint32_t dest_ms_since_seq_start = own_ceil((float)((uint64_t)ufsi*seq_length*dwell_time) / DEF_2E24);
319-
return (own_floor(((float)(divide_integer((tx_time - ufsi_timestamp),1000) + dest_ms_since_seq_start) / dwell_time)) % seq_length);
330+
return (own_floor(((float)(US_TO_MS(tx_time - ufsi_timestamp) + dest_ms_since_seq_start) / dwell_time)) % seq_length);
320331
}
321332

322333
static uint32_t fhss_ws_get_sf_timeout_callback(fhss_structure_t *fhss_structure)
323334
{
324-
return fhss_structure->ws->fhss_configuration.fhss_uc_dwell_interval * 1000;
335+
return MS_TO_US(fhss_structure->ws->fhss_configuration.fhss_uc_dwell_interval);
325336
}
326337

327338
static int16_t fhss_ws_synch_state_set_callback(const fhss_api_t *api, fhss_states fhss_state, uint16_t pan_id)
@@ -342,7 +353,7 @@ static int16_t fhss_ws_synch_state_set_callback(const fhss_api_t *api, fhss_stat
342353
// Start unicast schedule
343354
if ((fhss_structure->ws->fhss_configuration.ws_uc_channel_function != WS_FIXED_CHANNEL)) {
344355
fhss_ws_update_uc_channel_callback(fhss_structure);
345-
fhss_start_timer(fhss_structure, fhss_structure->ws->fhss_configuration.fhss_uc_dwell_interval*1000, fhss_unicast_handler);
356+
fhss_start_timer(fhss_structure, MS_TO_US(fhss_structure->ws->fhss_configuration.fhss_uc_dwell_interval), fhss_unicast_handler);
346357
fhss_structure->ws->unicast_timer_running = true;
347358
}
348359
}
@@ -473,7 +484,7 @@ static bool fhss_ws_check_tx_allowed(fhss_structure_t *fhss_structure)
473484
return true;
474485
}
475486

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;
487+
uint32_t remaining_time_ms = get_remaining_slots_us(fhss_structure, fhss_broadcast_handler, MS_TO_US(fhss_structure->ws->fhss_configuration.fhss_broadcast_interval)) / 1000;
477488
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));
478489
uint32_t rx_slot_begin = tx_slot_begin - fhss_structure->ws->txrx_slot_length_ms;
479490
uint8_t n_o_tx_slots = number_of_tx_slots;
@@ -716,21 +727,21 @@ int fhss_ws_set_parent(fhss_structure_t *fhss_structure, const uint8_t eui64[8],
716727
if (!bc_timing_info->broadcast_interval || !bc_timing_info->broadcast_dwell_interval) {
717728
return -1;
718729
}
719-
if ((((uint32_t)fhss_structure->ws->min_synch_interval*1000000) > (fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api) - fhss_structure->ws->synchronization_time)) && !force_synch) {
730+
if ((S_TO_US(fhss_structure->ws->min_synch_interval) > (fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api) - fhss_structure->ws->synchronization_time)) && !force_synch) {
720731
return 0;
721732
}
722733
fhss_structure->ws->synchronization_time = fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api);
723734
platform_enter_critical();
724735
fhss_stop_timer(fhss_structure, fhss_broadcast_handler);
725-
uint32_t time_from_reception_ms = divide_integer(fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api) - bc_timing_info->bt_rx_timestamp, 1000);
736+
uint32_t time_from_reception_ms = US_TO_MS(fhss_structure->callbacks.read_timestamp(fhss_structure->fhss_api) - bc_timing_info->bt_rx_timestamp);
726737
uint32_t true_bc_interval_offset = (bc_timing_info->broadcast_interval_offset + time_from_reception_ms) % bc_timing_info->broadcast_interval;
727738
if (true_bc_interval_offset >= bc_timing_info->broadcast_dwell_interval) {
728739
fhss_structure->ws->is_on_bc_channel = false;
729740
}
730-
uint32_t timeout = ((bc_timing_info->broadcast_interval-true_bc_interval_offset)*1000);
741+
uint32_t timeout = MS_TO_US(bc_timing_info->broadcast_interval-true_bc_interval_offset);
731742

732743
if (fhss_structure->ws->is_on_bc_channel) {
733-
timeout -= ((bc_timing_info->broadcast_interval - bc_timing_info->broadcast_dwell_interval) *1000);
744+
timeout -= MS_TO_US(bc_timing_info->broadcast_interval-bc_timing_info->broadcast_dwell_interval);
734745
}
735746
fhss_start_timer(fhss_structure, timeout, fhss_broadcast_handler);
736747
uint16_t slots_since_reception = (bc_timing_info->broadcast_interval_offset + time_from_reception_ms) / bc_timing_info->broadcast_interval;
@@ -770,7 +781,7 @@ int fhss_ws_configuration_set(fhss_structure_t *fhss_structure, const fhss_ws_co
770781
fhss_structure->ws->unicast_timer_running = false;
771782
}
772783
if ((fhss_structure->ws->unicast_timer_running == false) && (fhss_configuration->ws_uc_channel_function != WS_FIXED_CHANNEL) && fhss_configuration->fhss_uc_dwell_interval) {
773-
fhss_start_timer(fhss_structure, fhss_configuration->fhss_uc_dwell_interval*1000, fhss_unicast_handler);
784+
fhss_start_timer(fhss_structure, MS_TO_US(fhss_configuration->fhss_uc_dwell_interval), fhss_unicast_handler);
774785
fhss_structure->ws->unicast_timer_running = true;
775786
}
776787
fhss_structure->ws->fhss_configuration = *fhss_configuration;

0 commit comments

Comments
 (0)