50 #if TSCH_ADAPTIVE_TIMESYNC 54 static int32_t drift_ppm;
56 static int32_t compensated_ticks;
58 static uint8_t timesync_entry_count;
60 static uint32_t asn_since_last_learning;
65 #define TSCH_DRIFT_UNIT (1000L * 1000 * 256) 71 return (
long int)drift_ppm / 256;
76 timesync_entry_add(int32_t val)
78 #define NUM_TIMESYNC_ENTRIES 8 79 static int32_t buffer[NUM_TIMESYNC_ENTRIES];
82 if(timesync_entry_count == 0) {
86 if(timesync_entry_count < NUM_TIMESYNC_ENTRIES) {
87 timesync_entry_count++;
93 pos = (pos + 1) % NUM_TIMESYNC_ENTRIES;
96 for(i = 0; i < timesync_entry_count; ++i) {
99 return val / timesync_entry_count;
104 timesync_learn_drift_ticks(uint32_t time_delta_asn, int32_t drift_ticks)
107 int32_t time_delta_ticks = time_delta_asn * tsch_timing[tsch_ts_timeslot_length];
108 int32_t real_drift_ticks = drift_ticks + compensated_ticks;
109 int32_t last_drift_ppm = (int32_t)(((int64_t)real_drift_ticks * TSCH_DRIFT_UNIT) / time_delta_ticks);
111 drift_ppm = timesync_entry_add(last_drift_ppm);
114 snprintf(log->message,
sizeof(log->message),
115 "drift %ld ppm (min/max delta seen: %"PRId32
"/%"PRId32
")",
117 min_drift_seen, max_drift_seen));
127 if(last_timesource_neighbor != n) {
129 last_timesource_neighbor = n;
131 asn_since_last_learning += time_delta_asn;
132 if(asn_since_last_learning >= 4 * TSCH_SLOTS_PER_SECOND) {
133 timesync_learn_drift_ticks(asn_since_last_learning, drift_correction);
134 compensated_ticks = 0;
135 asn_since_last_learning = 0;
138 compensated_ticks += drift_correction;
141 min_drift_seen = MIN(drift_correction, min_drift_seen);
142 max_drift_seen = MAX(drift_correction, max_drift_seen);
147 compensate_internal(uint32_t time_delta_usec, int32_t drift_ppm, int32_t *remainder, int16_t *tick_conversion_error)
149 int64_t d = (int64_t)time_delta_usec * drift_ppm + *remainder;
150 int32_t amount = d / TSCH_DRIFT_UNIT;
151 int32_t amount_ticks;
153 *remainder = (int32_t)(d - amount * TSCH_DRIFT_UNIT);
155 amount += *tick_conversion_error;
156 amount_ticks = US_TO_RTIMERTICKS(amount);
157 *tick_conversion_error = amount - RTIMERTICKS_TO_US(amount_ticks);
159 if(ABS(amount_ticks) > RTIMER_ARCH_SECOND / 128) {
161 snprintf(log->message,
sizeof(log->message),
162 "!too big compensation %ld delta %ld", (
long int)amount_ticks, (
long int)time_delta_usec));
163 amount_ticks = (amount_ticks > 0 ? RTIMER_ARCH_SECOND : -RTIMER_ARCH_SECOND) / 128;
174 uint32_t time_delta_usec = RTIMERTICKS_TO_US_64(time_delta_ticks);
177 if(drift_ppm && last_timesource_neighbor != NULL) {
178 static int32_t remainder;
179 static int16_t tick_conversion_error;
180 result = compensate_internal(time_delta_usec, drift_ppm,
181 &remainder, &tick_conversion_error);
182 compensated_ticks += result;
185 if(TSCH_BASE_DRIFT_PPM) {
186 static int32_t base_drift_remainder;
187 static int16_t base_drift_tick_conversion_error;
188 result += compensate_internal(time_delta_usec, 256L * TSCH_BASE_DRIFT_PPM,
189 &base_drift_remainder, &base_drift_tick_conversion_error);
198 last_timesource_neighbor = NULL;
200 timesync_entry_count = 0;
201 compensated_ticks = 0;
202 asn_since_last_learning = 0;
#define TSCH_LOG_ADD(log_type, init_code)
Use this macro to add a log to the queue (will be printed out later, after leaving interrupt context)...
TSCH neighbor information.
void tsch_set_ka_timeout(uint32_t timeout)
Set the desynchronization timeout after which a node sends a unicasst keep-alive (KA) to its time sou...
void tsch_timesync_update(struct tsch_neighbor *n, uint16_t time_delta_asn, int32_t drift_correction)
Updates timesync information for a given neighbor.
int32_t tsch_timesync_adaptive_compensate(rtimer_clock_t delta_ticks)
Computes time compensation for a given point in the future.
Main API declarations for TSCH.
long int tsch_adaptive_timesync_get_drift_ppm(void)
Gives the estimated clock drift w.r.t.
void tsch_adaptive_timesync_reset(void)
Reset the status of the module.