@@ -43,23 +43,23 @@ 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)
46
+ #define S_TO_MS (x ) (((int32_t )x)*1000)
47
47
// Milliseconds to seconds
48
48
#define MS_TO_S (x ) divide_integer(x, 1000)
49
49
// Seconds to microseconds
50
- #define S_TO_US (x ) (((uint32_t )x)*1000000)
50
+ #define S_TO_US (x ) (((int32_t )x)*1000000)
51
51
// Microseconds to seconds
52
52
#define US_TO_S (x ) divide_integer(x, 1000000)
53
53
// Milliseconds to microseconds
54
- #define MS_TO_US (x ) (((uint32_t )x)*1000)
54
+ #define MS_TO_US (x ) (((int32_t )x)*1000)
55
55
// Microseconds to milliseconds
56
56
#define US_TO_MS (x ) divide_integer(x, 1000)
57
57
// Milliseconds to nanoseconds
58
- #define MS_TO_NS (x ) (((uint32_t )x)*1000000)
58
+ #define MS_TO_NS (x ) (((int32_t )x)*1000000)
59
59
// Nanoseconds to milliseconds
60
60
#define NS_TO_MS (x ) divide_integer(x, 1000000)
61
61
// Microseconds to nanoseconds
62
- #define US_TO_NS (x ) (((uint32_t )x)*1000)
62
+ #define US_TO_NS (x ) (((int32_t )x)*1000)
63
63
// Nanoseconds to microseconds
64
64
#define NS_TO_US (x ) divide_integer(x, 1000)
65
65
#define DEF_2E24 0x1000000
@@ -85,8 +85,11 @@ static bool fhss_ws_check_tx_allowed(fhss_structure_t *fhss_structure);
85
85
static uint8_t fhss_set_txrx_slot_length (fhss_structure_t * fhss_structure );
86
86
87
87
// 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 )
89
89
{
90
+ if (dividend < 0 ) {
91
+ return (dividend - divisor / 2 ) / divisor ;
92
+ }
90
93
return (dividend + divisor / 2 ) / divisor ;
91
94
}
92
95
@@ -778,14 +781,8 @@ int fhss_ws_set_parent(fhss_structure_t *fhss_structure, const uint8_t eui64[8],
778
781
fhss_structure -> ws -> parent_bc_info = bc_timing_info ;
779
782
if (prev_synchronization_time ) {
780
783
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 ));
789
786
}
790
787
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 );
791
788
}
0 commit comments