42
42
void (* fhss_uc_switch )(void ) = NULL ;
43
43
void (* fhss_bc_switch )(void ) = NULL ;
44
44
#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)
46
57
#define DEF_2E24 0x1000000
47
58
#define IE_HEADER_LENGTH_MASK 0x007f
48
59
#define IE_HEADER_ID_MASK 0x7f80
@@ -185,7 +196,7 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
185
196
return ;
186
197
}
187
198
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 );
189
200
fhss_structure -> ws -> is_on_bc_channel = true;
190
201
next_channel = fhss_structure -> ws -> bc_channel = fhss_ws_calc_bc_channel (fhss_structure );
191
202
@@ -194,12 +205,12 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
194
205
* Max random is 1/10 of the channel dwell interval.
195
206
* Event timer resolution is 50us.
196
207
*/
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 ) ;
198
209
uint16_t bc_min_random = (bc_dwell_us / 50 ) / 50 ;
199
210
uint16_t bc_max_random = (bc_dwell_us / 10 ) / 50 ;
200
211
eventOS_callback_timer_start (fhss_structure -> fhss_event_timer , randLIB_get_random_in_range (bc_min_random , bc_max_random ));
201
212
} 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 );
203
214
fhss_start_timer (fhss_structure , timeout - (delay * fhss_structure -> platform_functions .fhss_resolution_divider ), fhss_broadcast_handler );
204
215
fhss_structure -> ws -> is_on_bc_channel = false;
205
216
// 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)
209
220
* Max random is 1/10 of the TX slot length.
210
221
* Event timer resolution is 50us.
211
222
*/
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 ) ;
213
224
uint16_t uc_min_random = (txrx_slot_length_us / 30 ) / 50 ;
214
225
uint16_t uc_max_random = (txrx_slot_length_us / 10 ) / 50 ;
215
226
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)
256
267
queue_size = fhss_structure -> callbacks .read_tx_queue_size (fhss_structure -> fhss_api , true);
257
268
} else {
258
269
// 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 ) )) {
261
272
eventOS_callback_timer_start (fhss_structure -> fhss_event_timer , delay_between_tx_slots_us /50 );
262
273
}
263
274
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_
275
286
cur_slot = fhss_structure -> number_of_channels ;
276
287
}
277
288
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 )) );
279
290
uint32_t time_to_tx = 0 ;
280
291
uint32_t cur_time = fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api );
281
292
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 );
283
294
}
284
295
uint64_t ms_since_seq_start = (cur_slot * dwell_time ) + (dwell_time - remaining_time_ms ) + time_to_tx ;
285
296
uint32_t seq_length = 0x10000 ;
@@ -294,14 +305,14 @@ static uint32_t fhss_ws_calculate_broadcast_interval_offset(fhss_structure_t *fh
294
305
{
295
306
uint8_t dwell_time = fhss_structure -> ws -> fhss_configuration .fhss_bc_dwell_interval ;
296
307
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 )) );
298
309
if (fhss_structure -> ws -> is_on_bc_channel == true) {
299
310
remaining_time_ms += (broadcast_interval - dwell_time );
300
311
}
301
312
uint32_t time_to_tx = 0 ;
302
313
uint32_t cur_time = fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api );
303
314
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 );
305
316
}
306
317
return (broadcast_interval - remaining_time_ms ) + time_to_tx ;
307
318
}
@@ -316,12 +327,12 @@ static uint16_t fhss_ws_calculate_destination_slot(fhss_ws_neighbor_timing_info_
316
327
seq_length = neighbor_timing_info -> uc_timing_info .unicast_number_of_channels ;
317
328
}
318
329
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 );
320
331
}
321
332
322
333
static uint32_t fhss_ws_get_sf_timeout_callback (fhss_structure_t * fhss_structure )
323
334
{
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 ) ;
325
336
}
326
337
327
338
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
342
353
// Start unicast schedule
343
354
if ((fhss_structure -> ws -> fhss_configuration .ws_uc_channel_function != WS_FIXED_CHANNEL )) {
344
355
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 );
346
357
fhss_structure -> ws -> unicast_timer_running = true;
347
358
}
348
359
}
@@ -473,7 +484,7 @@ static bool fhss_ws_check_tx_allowed(fhss_structure_t *fhss_structure)
473
484
return true;
474
485
}
475
486
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 ;
477
488
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 ));
478
489
uint32_t rx_slot_begin = tx_slot_begin - fhss_structure -> ws -> txrx_slot_length_ms ;
479
490
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],
716
727
if (!bc_timing_info -> broadcast_interval || !bc_timing_info -> broadcast_dwell_interval ) {
717
728
return -1 ;
718
729
}
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 ) {
720
731
return 0 ;
721
732
}
722
733
fhss_structure -> ws -> synchronization_time = fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api );
723
734
platform_enter_critical ();
724
735
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 );
726
737
uint32_t true_bc_interval_offset = (bc_timing_info -> broadcast_interval_offset + time_from_reception_ms ) % bc_timing_info -> broadcast_interval ;
727
738
if (true_bc_interval_offset >= bc_timing_info -> broadcast_dwell_interval ) {
728
739
fhss_structure -> ws -> is_on_bc_channel = false;
729
740
}
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 );
731
742
732
743
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 );
734
745
}
735
746
fhss_start_timer (fhss_structure , timeout , fhss_broadcast_handler );
736
747
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
770
781
fhss_structure -> ws -> unicast_timer_running = false;
771
782
}
772
783
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 );
774
785
fhss_structure -> ws -> unicast_timer_running = true;
775
786
}
776
787
fhss_structure -> ws -> fhss_configuration = * fhss_configuration ;
0 commit comments