Adaptive time synchronization for TSCH
This commit is contained in:
parent
f9f91363d2
commit
d79ce957a1
|
@ -98,8 +98,8 @@
|
||||||
/* Is the "hash" function collision-free? (e.g. it maps to unique node-ids) */
|
/* Is the "hash" function collision-free? (e.g. it maps to unique node-ids) */
|
||||||
#ifdef ORCHESTRA_CONF_COLLISION_FREE_HASH
|
#ifdef ORCHESTRA_CONF_COLLISION_FREE_HASH
|
||||||
#define ORCHESTRA_COLLISION_FREE_HASH ORCHESTRA_CONF_COLLISION_FREE_HASH
|
#define ORCHESTRA_COLLISION_FREE_HASH ORCHESTRA_CONF_COLLISION_FREE_HASH
|
||||||
#else /* ORCHESTRA_CONF_MAX_HASH */
|
#else /* ORCHESTRA_CONF_COLLISION_FREE_HASH */
|
||||||
#define ORCHESTRA_CONF_COLLISION_FREE_HASH 0 /* Set to 1 if ORCHESTRA_LINKADDR_HASH returns unique hashes */
|
#define ORCHESTRA_COLLISION_FREE_HASH 0 /* Set to 1 if ORCHESTRA_LINKADDR_HASH returns unique hashes */
|
||||||
#endif /* ORCHESTRA_CONF_MAX_HASH */
|
#endif /* ORCHESTRA_CONF_COLLISION_FREE_HASH */
|
||||||
|
|
||||||
#endif /* __ORCHESTRA_CONF_H__ */
|
#endif /* __ORCHESTRA_CONF_H__ */
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
CONTIKI_SOURCEFILES += tsch.c tsch-slot-operation.c tsch-queue.c tsch-packet.c tsch-schedule.c tsch-log.c tsch-rpl.c
|
CONTIKI_SOURCEFILES += tsch.c tsch-slot-operation.c tsch-queue.c tsch-packet.c tsch-schedule.c tsch-log.c tsch-rpl.c tsch-adaptive-timesync.c
|
||||||
|
|
186
core/net/mac/tsch/tsch-adaptive-timesync.c
Normal file
186
core/net/mac/tsch/tsch-adaptive-timesync.c
Normal file
|
@ -0,0 +1,186 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015, SICS Swedish ICT.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of the Institute nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This file is part of the Contiki operating system.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* TSCH adaptive time synchronization
|
||||||
|
* \author
|
||||||
|
* Atis Elsts <atis.elsts@sics.se>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "tsch-adaptive-timesync.h"
|
||||||
|
#include "tsch-log.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#if TSCH_ADAPTIVE_TIMESYNC
|
||||||
|
|
||||||
|
/* Estimated drift of the time-source neighbor. Can be negative.
|
||||||
|
* Units used: ppm multiplied by 256. */
|
||||||
|
static int32_t drift_ppm;
|
||||||
|
/* Ticks compensated locally since the last timesync time */
|
||||||
|
static int32_t compensated_ticks;
|
||||||
|
/* Number of already recorded timesync history entries */
|
||||||
|
static uint8_t timesync_entry_count;
|
||||||
|
/* Since last learning of the drift; may be more than time since last timesync */
|
||||||
|
static uint32_t asn_since_last_learning;
|
||||||
|
|
||||||
|
/* Units in which drift is stored: ppm * 256 */
|
||||||
|
#define TSCH_DRIFT_UNIT (1000L * 1000 * 256)
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
/* Add a value to a moving average estimator */
|
||||||
|
static int32_t
|
||||||
|
timesync_entry_add(int32_t val, uint32_t time_delta)
|
||||||
|
{
|
||||||
|
#define NUM_TIMESYNC_ENTRIES 8
|
||||||
|
static int32_t buffer[NUM_TIMESYNC_ENTRIES];
|
||||||
|
static uint8_t pos;
|
||||||
|
int i;
|
||||||
|
if(timesync_entry_count == 0) {
|
||||||
|
pos = 0;
|
||||||
|
}
|
||||||
|
buffer[pos] = val;
|
||||||
|
if(timesync_entry_count < NUM_TIMESYNC_ENTRIES) {
|
||||||
|
timesync_entry_count++;
|
||||||
|
}
|
||||||
|
pos = (pos + 1) % NUM_TIMESYNC_ENTRIES;
|
||||||
|
|
||||||
|
val = 0;
|
||||||
|
for(i = 0; i < timesync_entry_count; ++i) {
|
||||||
|
val += buffer[i];
|
||||||
|
}
|
||||||
|
return val / timesync_entry_count;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
/* Learn the neighbor drift rate at ppm */
|
||||||
|
static void
|
||||||
|
timesync_learn_drift_ticks(uint32_t time_delta_asn, int32_t drift_ticks)
|
||||||
|
{
|
||||||
|
/* should fit in 32-bit unsigned integer */
|
||||||
|
uint32_t time_delta_ticks = time_delta_asn * tsch_timing[tsch_ts_timeslot_length];
|
||||||
|
int32_t real_drift_ticks = drift_ticks + compensated_ticks;
|
||||||
|
int32_t last_drift_ppm = (int32_t)((int64_t)real_drift_ticks * TSCH_DRIFT_UNIT / time_delta_ticks);
|
||||||
|
|
||||||
|
drift_ppm = timesync_entry_add(last_drift_ppm, time_delta_ticks);
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
/* Either reset or update the neighbor's drift */
|
||||||
|
void
|
||||||
|
tsch_timesync_update(struct tsch_neighbor *n, uint16_t time_delta_asn, int32_t drift_correction)
|
||||||
|
{
|
||||||
|
/* Account the drift if either this is a new timesource,
|
||||||
|
* or the timedelta is not too small, as smaller timedelta
|
||||||
|
* means proportionally larger measurement error. */
|
||||||
|
if(last_timesource_neighbor != n) {
|
||||||
|
last_timesource_neighbor = n;
|
||||||
|
drift_ppm = 0;
|
||||||
|
timesync_entry_count = 0;
|
||||||
|
compensated_ticks = 0;
|
||||||
|
asn_since_last_learning = 0;
|
||||||
|
} else {
|
||||||
|
asn_since_last_learning += time_delta_asn;
|
||||||
|
if(asn_since_last_learning >= 4 * TSCH_SLOTS_PER_SECOND) {
|
||||||
|
timesync_learn_drift_ticks(asn_since_last_learning, drift_correction);
|
||||||
|
compensated_ticks = 0;
|
||||||
|
asn_since_last_learning = 0;
|
||||||
|
} else {
|
||||||
|
/* Too small timedelta, do not recalculate the drift to avoid introducing error. instead account for the corrected ticks */
|
||||||
|
compensated_ticks += drift_correction;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
/* Error-accumulation free compensation algorithm */
|
||||||
|
static int32_t
|
||||||
|
compensate_internal(uint32_t time_delta_usec, int32_t drift_ppm, int32_t *remainder, int16_t *tick_conversion_error)
|
||||||
|
{
|
||||||
|
int64_t d = (int64_t)time_delta_usec * drift_ppm + *remainder;
|
||||||
|
int32_t amount = d / TSCH_DRIFT_UNIT;
|
||||||
|
int32_t amount_ticks;
|
||||||
|
|
||||||
|
*remainder = (int32_t)(d - amount * TSCH_DRIFT_UNIT);
|
||||||
|
|
||||||
|
amount += *tick_conversion_error;
|
||||||
|
amount_ticks = US_TO_RTIMERTICKS(amount);
|
||||||
|
*tick_conversion_error = amount - RTIMERTICKS_TO_US(amount_ticks);
|
||||||
|
|
||||||
|
if(ABS(amount_ticks) > RTIMER_ARCH_SECOND / 128) {
|
||||||
|
TSCH_LOG_ADD(tsch_log_message,
|
||||||
|
snprintf(log->message, sizeof(log->message),
|
||||||
|
"!too big compensation %ld delta %ld", amount_ticks, time_delta_usec));
|
||||||
|
amount_ticks = (amount_ticks > 0 ? RTIMER_ARCH_SECOND : -RTIMER_ARCH_SECOND) / 128;
|
||||||
|
}
|
||||||
|
|
||||||
|
return amount_ticks;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
/* Do the compensation step before scheduling a new timeslot */
|
||||||
|
int32_t
|
||||||
|
tsch_timesync_adaptive_compensate(rtimer_clock_t time_delta_ticks)
|
||||||
|
{
|
||||||
|
int32_t result = 0;
|
||||||
|
uint32_t time_delta_usec = RTIMERTICKS_TO_US_64(time_delta_ticks);
|
||||||
|
|
||||||
|
/* compensate, but not if the neighbor is not known */
|
||||||
|
if(drift_ppm && last_timesource_neighbor != NULL) {
|
||||||
|
static int32_t remainder;
|
||||||
|
static int16_t tick_conversion_error;
|
||||||
|
result = compensate_internal(time_delta_usec, drift_ppm,
|
||||||
|
&remainder, &tick_conversion_error);
|
||||||
|
compensated_ticks += result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(TSCH_BASE_DRIFT_PPM) {
|
||||||
|
static int32_t base_drift_remainder;
|
||||||
|
static int16_t base_drift_tick_conversion_error;
|
||||||
|
result += compensate_internal(time_delta_usec, 256L * TSCH_BASE_DRIFT_PPM,
|
||||||
|
&base_drift_remainder, &base_drift_tick_conversion_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
#else /* TSCH_ADAPTIVE_TIMESYNC */
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
tsch_timesync_update(struct tsch_neighbor *n, uint16_t time_delta_asn, int32_t drift_correction)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
int32_t
|
||||||
|
tsch_timesync_adaptive_compensate(rtimer_clock_t delta_ticks)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
#endif /* TSCH_ADAPTIVE_TIMESYNC */
|
89
core/net/mac/tsch/tsch-adaptive-timesync.h
Normal file
89
core/net/mac/tsch/tsch-adaptive-timesync.h
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015, SICS Swedish ICT.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of the Institute nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This file is part of the Contiki operating system.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __TSCH_ADAPTIVE_TIMESYNC_H__
|
||||||
|
#define __TSCH_ADAPTIVE_TIMESYNC_H__
|
||||||
|
|
||||||
|
/********** Includes **********/
|
||||||
|
|
||||||
|
#include "contiki.h"
|
||||||
|
#include "net/mac/tsch/tsch-private.h"
|
||||||
|
|
||||||
|
/******** Configuration *******/
|
||||||
|
|
||||||
|
/* Use SFD timestamp for synchronization? By default we merely rely on rtimer and busy wait
|
||||||
|
* until SFD is high, which we found to provide greater accuracy on JN516x and CC2420.
|
||||||
|
* Note: for association, however, we always use SFD timestamp to know the time of arrival
|
||||||
|
* of the EB (because we do not busy-wait for the whole scanning process)
|
||||||
|
* */
|
||||||
|
#ifdef TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS
|
||||||
|
#define TSCH_RESYNC_WITH_SFD_TIMESTAMPS TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS
|
||||||
|
#else
|
||||||
|
#define TSCH_RESYNC_WITH_SFD_TIMESTAMPS 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* If enabled, remove jitter due to measurement errors */
|
||||||
|
#ifdef TSCH_CONF_TIMESYNC_REMOVE_JITTER
|
||||||
|
#define TSCH_TIMESYNC_REMOVE_JITTER TSCH_CONF_TIMESYNC_REMOVE_JITTER
|
||||||
|
#else
|
||||||
|
#define TSCH_TIMESYNC_REMOVE_JITTER TSCH_RESYNC_WITH_SFD_TIMESTAMPS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* The jitter to remove in ticks.
|
||||||
|
* This should be the sum of measurement errors on Tx and Rx nodes.
|
||||||
|
* */
|
||||||
|
#define TSCH_TIMESYNC_MEASUREMENT_ERROR US_TO_RTIMERTICKS(32)
|
||||||
|
|
||||||
|
/* Base drift value.
|
||||||
|
* Used to compensate locally know inaccuracies, such as
|
||||||
|
* the effect of having a binary 32.768 kHz timer as the TSCH time base. */
|
||||||
|
#ifdef TSCH_CONF_BASE_DRIFT_PPM
|
||||||
|
#define TSCH_BASE_DRIFT_PPM TSCH_CONF_BASE_DRIFT_PPM
|
||||||
|
#else
|
||||||
|
#define TSCH_BASE_DRIFT_PPM 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* The approximate number of slots per second */
|
||||||
|
#define TSCH_SLOTS_PER_SECOND (1000000 / TSCH_DEFAULT_TS_TIMESLOT_LENGTH)
|
||||||
|
|
||||||
|
/***** External Variables *****/
|
||||||
|
|
||||||
|
/* The neighbor last used as our time source */
|
||||||
|
extern struct tsch_neighbor *last_timesource_neighbor;
|
||||||
|
|
||||||
|
/********** Functions *********/
|
||||||
|
|
||||||
|
void tsch_timesync_update(struct tsch_neighbor *n, uint16_t time_delta_asn, int32_t drift_correction);
|
||||||
|
|
||||||
|
int32_t tsch_timesync_adaptive_compensate(rtimer_clock_t delta_ticks);
|
||||||
|
|
||||||
|
#endif /* __TSCH_ADAPTIVE_TIMESYNC_H__ */
|
|
@ -168,4 +168,11 @@
|
||||||
#define TSCH_WITH_LINK_SELECTOR 0
|
#define TSCH_WITH_LINK_SELECTOR 0
|
||||||
#endif /* TSCH_CONF_WITH_LINK_SELECTOR */
|
#endif /* TSCH_CONF_WITH_LINK_SELECTOR */
|
||||||
|
|
||||||
|
/* Estimate the drift of the time-source neighbor and compensate for it? */
|
||||||
|
#ifdef TSCH_CONF_ADAPTIVE_TIMESYNC
|
||||||
|
#define TSCH_ADAPTIVE_TIMESYNC TSCH_CONF_ADAPTIVE_TIMESYNC
|
||||||
|
#else
|
||||||
|
#define TSCH_ADAPTIVE_TIMESYNC 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* __TSCH_CONF_H__ */
|
#endif /* __TSCH_CONF_H__ */
|
||||||
|
|
|
@ -52,6 +52,7 @@
|
||||||
#include "net/mac/tsch/tsch-log.h"
|
#include "net/mac/tsch/tsch-log.h"
|
||||||
#include "net/mac/tsch/tsch-packet.h"
|
#include "net/mac/tsch/tsch-packet.h"
|
||||||
#include "net/mac/tsch/tsch-security.h"
|
#include "net/mac/tsch/tsch-security.h"
|
||||||
|
#include "net/mac/tsch/tsch-adaptive-timesync.h"
|
||||||
|
|
||||||
#if TSCH_LOG_LEVEL >= 1
|
#if TSCH_LOG_LEVEL >= 1
|
||||||
#define DEBUG DEBUG_PRINT
|
#define DEBUG DEBUG_PRINT
|
||||||
|
@ -126,9 +127,13 @@ static volatile int tsch_locked = 0;
|
||||||
static volatile int tsch_lock_requested = 0;
|
static volatile int tsch_lock_requested = 0;
|
||||||
|
|
||||||
/* Last estimated drift in RTIMER ticks
|
/* Last estimated drift in RTIMER ticks
|
||||||
* (Sky: 1 tick ~= 30.52 uSec) */
|
* (Sky: 1 tick = 30.517578125 usec exactly) */
|
||||||
static int32_t drift_correction = 0;
|
static int32_t drift_correction = 0;
|
||||||
static struct tsch_neighbor *drift_neighbor = NULL;
|
/* Is drift correction used? (Can be true even if drift_correction == 0) */
|
||||||
|
static uint8_t is_drift_correction_used;
|
||||||
|
|
||||||
|
/* The neighbor last used as our time source */
|
||||||
|
struct tsch_neighbor *last_timesource_neighbor = NULL;
|
||||||
|
|
||||||
/* Used from tsch_slot_operation and sub-protothreads */
|
/* Used from tsch_slot_operation and sub-protothreads */
|
||||||
static rtimer_clock_t volatile current_slot_start;
|
static rtimer_clock_t volatile current_slot_start;
|
||||||
|
@ -545,6 +550,7 @@ PT_THREAD(tsch_tx_slot(struct pt *pt, struct rtimer *t))
|
||||||
if(ack_len != 0) {
|
if(ack_len != 0) {
|
||||||
if(is_time_source) {
|
if(is_time_source) {
|
||||||
int32_t eack_time_correction = US_TO_RTIMERTICKS(ack_ies.ie_time_correction);
|
int32_t eack_time_correction = US_TO_RTIMERTICKS(ack_ies.ie_time_correction);
|
||||||
|
int32_t since_last_timesync = ASN_DIFF(current_asn, last_sync_asn);
|
||||||
if(eack_time_correction > SYNC_IE_BOUND) {
|
if(eack_time_correction > SYNC_IE_BOUND) {
|
||||||
drift_correction = SYNC_IE_BOUND;
|
drift_correction = SYNC_IE_BOUND;
|
||||||
} else if(eack_time_correction < -SYNC_IE_BOUND) {
|
} else if(eack_time_correction < -SYNC_IE_BOUND) {
|
||||||
|
@ -558,7 +564,8 @@ PT_THREAD(tsch_tx_slot(struct pt *pt, struct rtimer *t))
|
||||||
"!truncated dr %d %d", (int)eack_time_correction, (int)drift_correction);
|
"!truncated dr %d %d", (int)eack_time_correction, (int)drift_correction);
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
drift_neighbor = current_neighbor;
|
is_drift_correction_used = 1;
|
||||||
|
tsch_timesync_update(current_neighbor, since_last_timesync, drift_correction);
|
||||||
/* Keep track of sync time */
|
/* Keep track of sync time */
|
||||||
last_sync_asn = current_asn;
|
last_sync_asn = current_asn;
|
||||||
tsch_schedule_keepalive();
|
tsch_schedule_keepalive();
|
||||||
|
@ -595,7 +602,7 @@ PT_THREAD(tsch_tx_slot(struct pt *pt, struct rtimer *t))
|
||||||
log->tx.num_tx = current_packet->transmissions;
|
log->tx.num_tx = current_packet->transmissions;
|
||||||
log->tx.datalen = queuebuf_datalen(current_packet->qb);
|
log->tx.datalen = queuebuf_datalen(current_packet->qb);
|
||||||
log->tx.drift = drift_correction;
|
log->tx.drift = drift_correction;
|
||||||
log->tx.drift_used = drift_neighbor != NULL;
|
log->tx.drift_used = is_drift_correction_used;
|
||||||
log->tx.is_data = ((((uint8_t *)(queuebuf_dataptr(current_packet->qb)))[0]) & 7) == FRAME802154_DATAFRAME;
|
log->tx.is_data = ((((uint8_t *)(queuebuf_dataptr(current_packet->qb)))[0]) & 7) == FRAME802154_DATAFRAME;
|
||||||
log->tx.sec_level = queuebuf_attr(current_packet->qb, PACKETBUF_ATTR_SECURITY_LEVEL);
|
log->tx.sec_level = queuebuf_attr(current_packet->qb, PACKETBUF_ATTR_SECURITY_LEVEL);
|
||||||
log->tx.dest = TSCH_LOG_ID_FROM_LINKADDR(queuebuf_addr(current_packet->qb, PACKETBUF_ADDR_RECEIVER));
|
log->tx.dest = TSCH_LOG_ID_FROM_LINKADDR(queuebuf_addr(current_packet->qb, PACKETBUF_ADDR_RECEIVER));
|
||||||
|
@ -643,6 +650,7 @@ PT_THREAD(tsch_rx_slot(struct pt *pt, struct rtimer *t))
|
||||||
static rtimer_clock_t rx_start_time;
|
static rtimer_clock_t rx_start_time;
|
||||||
static rtimer_clock_t expected_rx_time;
|
static rtimer_clock_t expected_rx_time;
|
||||||
static rtimer_clock_t packet_duration;
|
static rtimer_clock_t packet_duration;
|
||||||
|
uint8_t packet_seen;
|
||||||
|
|
||||||
expected_rx_time = current_slot_start + tsch_timing[tsch_ts_tx_offset];
|
expected_rx_time = current_slot_start + tsch_timing[tsch_ts_tx_offset];
|
||||||
/* Default start time: expected Rx time */
|
/* Default start time: expected Rx time */
|
||||||
|
@ -656,13 +664,16 @@ PT_THREAD(tsch_rx_slot(struct pt *pt, struct rtimer *t))
|
||||||
|
|
||||||
/* Start radio for at least guard time */
|
/* Start radio for at least guard time */
|
||||||
NETSTACK_RADIO.on();
|
NETSTACK_RADIO.on();
|
||||||
if(!NETSTACK_RADIO.receiving_packet()) {
|
packet_seen = NETSTACK_RADIO.receiving_packet();
|
||||||
|
if(!packet_seen) {
|
||||||
/* Check if receiving within guard time */
|
/* Check if receiving within guard time */
|
||||||
BUSYWAIT_UNTIL_ABS(NETSTACK_RADIO.receiving_packet(),
|
BUSYWAIT_UNTIL_ABS((packet_seen = NETSTACK_RADIO.receiving_packet()),
|
||||||
current_slot_start, tsch_timing[tsch_ts_rx_offset] + tsch_timing[tsch_ts_rx_wait]);
|
current_slot_start, tsch_timing[tsch_ts_rx_offset] + tsch_timing[tsch_ts_rx_wait]);
|
||||||
|
}
|
||||||
|
if(packet_seen) {
|
||||||
TSCH_DEBUG_RX_EVENT();
|
TSCH_DEBUG_RX_EVENT();
|
||||||
/* Save packet timestamp */
|
/* Save packet timestamp */
|
||||||
rx_start_time = RTIMER_NOW();
|
rx_start_time = RTIMER_NOW() - RADIO_DELAY_BEFORE_DETECT;
|
||||||
}
|
}
|
||||||
if(!NETSTACK_RADIO.receiving_packet() && !NETSTACK_RADIO.pending_packet()) {
|
if(!NETSTACK_RADIO.receiving_packet() && !NETSTACK_RADIO.pending_packet()) {
|
||||||
NETSTACK_RADIO.off();
|
NETSTACK_RADIO.off();
|
||||||
|
@ -724,6 +735,17 @@ PT_THREAD(tsch_rx_slot(struct pt *pt, struct rtimer *t))
|
||||||
int do_nack = 0;
|
int do_nack = 0;
|
||||||
estimated_drift = ((int32_t)expected_rx_time - (int32_t)rx_start_time);
|
estimated_drift = ((int32_t)expected_rx_time - (int32_t)rx_start_time);
|
||||||
|
|
||||||
|
#if TSCH_TIMESYNC_REMOVE_JITTER
|
||||||
|
/* remove jitter due to measurement errors */
|
||||||
|
if(abs(estimated_drift) <= TSCH_TIMESYNC_MEASUREMENT_ERROR) {
|
||||||
|
estimated_drift = 0;
|
||||||
|
} else if(estimated_drift > 0) {
|
||||||
|
estimated_drift -= TSCH_TIMESYNC_MEASUREMENT_ERROR;
|
||||||
|
} else {
|
||||||
|
estimated_drift += TSCH_TIMESYNC_MEASUREMENT_ERROR;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef TSCH_CALLBACK_DO_NACK
|
#ifdef TSCH_CALLBACK_DO_NACK
|
||||||
if(frame.fcf.ack_required) {
|
if(frame.fcf.ack_required) {
|
||||||
do_nack = TSCH_CALLBACK_DO_NACK(current_link,
|
do_nack = TSCH_CALLBACK_DO_NACK(current_link,
|
||||||
|
@ -759,11 +781,13 @@ PT_THREAD(tsch_rx_slot(struct pt *pt, struct rtimer *t))
|
||||||
/* If the sender is a time source, proceed to clock drift compensation */
|
/* If the sender is a time source, proceed to clock drift compensation */
|
||||||
n = tsch_queue_get_nbr(&source_address);
|
n = tsch_queue_get_nbr(&source_address);
|
||||||
if(n != NULL && n->is_time_source) {
|
if(n != NULL && n->is_time_source) {
|
||||||
|
int32_t since_last_timesync = ASN_DIFF(current_asn, last_sync_asn);
|
||||||
/* Keep track of last sync time */
|
/* Keep track of last sync time */
|
||||||
last_sync_asn = current_asn;
|
last_sync_asn = current_asn;
|
||||||
/* Save estimated drift */
|
/* Save estimated drift */
|
||||||
drift_correction = -estimated_drift;
|
drift_correction = -estimated_drift;
|
||||||
drift_neighbor = n;
|
is_drift_correction_used = 1;
|
||||||
|
tsch_timesync_update(n, since_last_timesync, -estimated_drift);
|
||||||
tsch_schedule_keepalive();
|
tsch_schedule_keepalive();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -776,7 +800,7 @@ PT_THREAD(tsch_rx_slot(struct pt *pt, struct rtimer *t))
|
||||||
log->rx.is_unicast = frame.fcf.ack_required;
|
log->rx.is_unicast = frame.fcf.ack_required;
|
||||||
log->rx.datalen = current_input->len;
|
log->rx.datalen = current_input->len;
|
||||||
log->rx.drift = drift_correction;
|
log->rx.drift = drift_correction;
|
||||||
log->rx.drift_used = drift_neighbor != NULL;
|
log->rx.drift_used = is_drift_correction_used;
|
||||||
log->rx.is_data = frame.fcf.frame_type == FRAME802154_DATAFRAME;
|
log->rx.is_data = frame.fcf.frame_type == FRAME802154_DATAFRAME;
|
||||||
log->rx.sec_level = frame.aux_hdr.security_control.security_level;
|
log->rx.sec_level = frame.aux_hdr.security_control.security_level;
|
||||||
log->rx.estimated_drift = estimated_drift;
|
log->rx.estimated_drift = estimated_drift;
|
||||||
|
@ -849,7 +873,7 @@ PT_THREAD(tsch_slot_operation(struct rtimer *t, void *ptr))
|
||||||
NETSTACK_RADIO.set_value(RADIO_PARAM_CHANNEL, current_channel);
|
NETSTACK_RADIO.set_value(RADIO_PARAM_CHANNEL, current_channel);
|
||||||
/* Reset drift correction */
|
/* Reset drift correction */
|
||||||
drift_correction = 0;
|
drift_correction = 0;
|
||||||
drift_neighbor = NULL;
|
is_drift_correction_used = 0;
|
||||||
/* Decide whether it is a TX/RX/IDLE or OFF slot */
|
/* Decide whether it is a TX/RX/IDLE or OFF slot */
|
||||||
/* Actual slot operation */
|
/* Actual slot operation */
|
||||||
if(current_packet != NULL) {
|
if(current_packet != NULL) {
|
||||||
|
@ -878,6 +902,7 @@ PT_THREAD(tsch_slot_operation(struct rtimer *t, void *ptr))
|
||||||
"! leaving the network, last sync %u",
|
"! leaving the network, last sync %u",
|
||||||
(unsigned)ASN_DIFF(current_asn, last_sync_asn));
|
(unsigned)ASN_DIFF(current_asn, last_sync_asn));
|
||||||
);
|
);
|
||||||
|
last_timesource_neighbor = NULL;
|
||||||
tsch_disassociate();
|
tsch_disassociate();
|
||||||
} else {
|
} else {
|
||||||
/* backup of drift correction for printing debug messages */
|
/* backup of drift correction for printing debug messages */
|
||||||
|
@ -908,10 +933,11 @@ PT_THREAD(tsch_slot_operation(struct rtimer *t, void *ptr))
|
||||||
/* Time to next wake up */
|
/* Time to next wake up */
|
||||||
time_to_next_active_slot = timeslot_diff * tsch_timing[tsch_ts_timeslot_length] + drift_correction;
|
time_to_next_active_slot = timeslot_diff * tsch_timing[tsch_ts_timeslot_length] + drift_correction;
|
||||||
drift_correction = 0;
|
drift_correction = 0;
|
||||||
drift_neighbor = NULL;
|
is_drift_correction_used = 0;
|
||||||
/* Update current slot start */
|
/* Update current slot start */
|
||||||
prev_slot_start = current_slot_start;
|
prev_slot_start = current_slot_start;
|
||||||
current_slot_start += time_to_next_active_slot;
|
current_slot_start += time_to_next_active_slot;
|
||||||
|
current_slot_start += tsch_timesync_adaptive_compensate(time_to_next_active_slot);
|
||||||
} while(!tsch_schedule_slot_operation(t, prev_slot_start, time_to_next_active_slot, "main"));
|
} while(!tsch_schedule_slot_operation(t, prev_slot_start, time_to_next_active_slot, "main"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,17 +74,6 @@
|
||||||
#define TSCH_MAX_INCOMING_PACKETS 4
|
#define TSCH_MAX_INCOMING_PACKETS 4
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Use SFD timestamp for synchronization? By default we merely rely on rtimer and busy wait
|
|
||||||
* until SFD is high, which we found to provide greater accuracy on JN516x and CC2420.
|
|
||||||
* Note: for association, however, we always use SFD timestamp to know the time of arrival
|
|
||||||
* of the EB (because we do not busy-wait for the whole scanning process)
|
|
||||||
* */
|
|
||||||
#ifdef TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS
|
|
||||||
#define TSCH_RESYNC_WITH_SFD_TIMESTAMPS TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS
|
|
||||||
#else
|
|
||||||
#define TSCH_RESYNC_WITH_SFD_TIMESTAMPS 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*********** Callbacks *********/
|
/*********** Callbacks *********/
|
||||||
|
|
||||||
/* Called by TSCH form interrupt after receiving a frame, enabled upper-layer to decide
|
/* Called by TSCH form interrupt after receiving a frame, enabled upper-layer to decide
|
||||||
|
|
|
@ -52,6 +52,8 @@
|
||||||
/* Delay between GO signal and start listening
|
/* Delay between GO signal and start listening
|
||||||
* ~50us delay + 129preample + ?? = 183 us */
|
* ~50us delay + 129preample + ?? = 183 us */
|
||||||
#define RADIO_DELAY_BEFORE_RX ((unsigned)US_TO_RTIMERTICKS(183))
|
#define RADIO_DELAY_BEFORE_RX ((unsigned)US_TO_RTIMERTICKS(183))
|
||||||
|
/* Delay between the SFD finishes arriving and it is detected in software */
|
||||||
|
#define RADIO_DELAY_BEFORE_DETECT 0
|
||||||
|
|
||||||
#define PLATFORM_HAS_LEDS 1
|
#define PLATFORM_HAS_LEDS 1
|
||||||
#define PLATFORM_HAS_BUTTON 1
|
#define PLATFORM_HAS_BUTTON 1
|
||||||
|
|
|
@ -49,6 +49,8 @@
|
||||||
/* Delay between GO signal and start listening
|
/* Delay between GO signal and start listening
|
||||||
* ~50us delay + 129preample + ?? = 183 us */
|
* ~50us delay + 129preample + ?? = 183 us */
|
||||||
#define RADIO_DELAY_BEFORE_RX ((unsigned)US_TO_RTIMERTICKS(183))
|
#define RADIO_DELAY_BEFORE_RX ((unsigned)US_TO_RTIMERTICKS(183))
|
||||||
|
/* Delay between the SFD finishes arriving and it is detected in software */
|
||||||
|
#define RADIO_DELAY_BEFORE_DETECT 0
|
||||||
|
|
||||||
#define PLATFORM_HAS_LEDS 1
|
#define PLATFORM_HAS_LEDS 1
|
||||||
#define PLATFORM_HAS_BUTTON 1
|
#define PLATFORM_HAS_BUTTON 1
|
||||||
|
|
Loading…
Reference in a new issue