Skip to content

Commit bc2fb64

Browse files
author
Jarkko Paso
committed
FHSS WS: time convert to support negative values
1 parent 7dce509 commit bc2fb64

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

source/Service_Libs/fhss/fhss_ws.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ void (*fhss_uc_switch)(void) = NULL;
4343
void (*fhss_bc_switch)(void) = NULL;
4444
#endif /*FHSS_CHANNEL_DEBUG_CBS*/
4545
// Seconds to milliseconds
46-
#define S_TO_MS(x) (((uint32_t)x)*1000)
46+
#define S_TO_MS(x) (((int32_t)x)*1000)
4747
// Milliseconds to seconds
4848
#define MS_TO_S(x) divide_integer(x, 1000)
4949
// Seconds to microseconds
50-
#define S_TO_US(x) (((uint32_t)x)*1000000)
50+
#define S_TO_US(x) (((int32_t)x)*1000000)
5151
// Microseconds to seconds
5252
#define US_TO_S(x) divide_integer(x, 1000000)
5353
// Milliseconds to microseconds
54-
#define MS_TO_US(x) (((uint32_t)x)*1000)
54+
#define MS_TO_US(x) (((int32_t)x)*1000)
5555
// Microseconds to milliseconds
5656
#define US_TO_MS(x) divide_integer(x, 1000)
5757
// Milliseconds to nanoseconds
58-
#define MS_TO_NS(x) (((uint32_t)x)*1000000)
58+
#define MS_TO_NS(x) (((int32_t)x)*1000000)
5959
// Nanoseconds to milliseconds
6060
#define NS_TO_MS(x) divide_integer(x, 1000000)
6161
// Microseconds to nanoseconds
62-
#define US_TO_NS(x) (((uint32_t)x)*1000)
62+
#define US_TO_NS(x) (((int32_t)x)*1000)
6363
// Nanoseconds to microseconds
6464
#define NS_TO_US(x) divide_integer(x, 1000)
6565
#define DEF_2E24 0x1000000
@@ -85,8 +85,11 @@ static bool fhss_ws_check_tx_allowed(fhss_structure_t *fhss_structure);
8585
static uint8_t fhss_set_txrx_slot_length(fhss_structure_t *fhss_structure);
8686

8787
// This function supports rounding up
88-
static uint32_t divide_integer(uint32_t dividend, uint32_t divisor)
88+
static int32_t divide_integer(int32_t dividend, int32_t divisor)
8989
{
90+
if (dividend < 0) {
91+
return (dividend - divisor / 2) / divisor;
92+
}
9093
return (dividend + divisor / 2) / divisor;
9194
}
9295

@@ -778,14 +781,8 @@ int fhss_ws_set_parent(fhss_structure_t *fhss_structure, const uint8_t eui64[8],
778781
fhss_structure->ws->parent_bc_info = bc_timing_info;
779782
if (prev_synchronization_time) {
780783
if (SYNCH_COMPENSATION_MIN_INTERVAL <= US_TO_S(time_since_last_synch_us)) {
781-
// Compensate clock drift
782-
if (true_bc_interval_offset > own_bc_interval_offset) {
783-
// Our clock runs too slow compared to parent, mark as positive drift.
784-
fhss_structure->ws->drift_per_millisecond_ns += divide_integer(MS_TO_NS(true_bc_interval_offset - own_bc_interval_offset), US_TO_MS(time_since_last_synch_us));
785-
} else {
786-
// Our clock runs too fast compared to parent, mark as negative drift.
787-
fhss_structure->ws->drift_per_millisecond_ns -= divide_integer(MS_TO_NS(own_bc_interval_offset - true_bc_interval_offset), US_TO_MS(time_since_last_synch_us));
788-
}
784+
// Update clock drift
785+
fhss_structure->ws->drift_per_millisecond_ns += divide_integer(MS_TO_NS(true_bc_interval_offset - own_bc_interval_offset), US_TO_MS(time_since_last_synch_us));
789786
}
790787
tr_debug("synch to parent: %s, drift: %ims in %u seconds, compensation: %ins per ms", trace_array(eui64, 8), true_bc_interval_offset - own_bc_interval_offset, US_TO_S(time_since_last_synch_us), fhss_structure->ws->drift_per_millisecond_ns);
791788
}

0 commit comments

Comments
 (0)