Contiki 3.x
tsch-adaptive-timesync.h
1 /*
2  * Copyright (c) 2015, SICS Swedish ICT.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 #ifndef __TSCH_ADAPTIVE_TIMESYNC_H__
34 #define __TSCH_ADAPTIVE_TIMESYNC_H__
35 
36 /********** Includes **********/
37 
38 #include "contiki.h"
40 
41 /******** Configuration *******/
42 
43 /* Use SFD timestamp for synchronization? By default we merely rely on rtimer and busy wait
44  * until SFD is high, which we found to provide greater accuracy on JN516x and CC2420.
45  * Note: for association, however, we always use SFD timestamp to know the time of arrival
46  * of the EB (because we do not busy-wait for the whole scanning process)
47  * */
48 #ifdef TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS
49 #define TSCH_RESYNC_WITH_SFD_TIMESTAMPS TSCH_CONF_RESYNC_WITH_SFD_TIMESTAMPS
50 #else
51 #define TSCH_RESYNC_WITH_SFD_TIMESTAMPS 0
52 #endif
53 
54 /* If enabled, remove jitter due to measurement errors */
55 #ifdef TSCH_CONF_TIMESYNC_REMOVE_JITTER
56 #define TSCH_TIMESYNC_REMOVE_JITTER TSCH_CONF_TIMESYNC_REMOVE_JITTER
57 #else
58 #define TSCH_TIMESYNC_REMOVE_JITTER TSCH_RESYNC_WITH_SFD_TIMESTAMPS
59 #endif
60 
61 /* The jitter to remove in ticks.
62  * This should be the sum of measurement errors on Tx and Rx nodes.
63  * */
64 #define TSCH_TIMESYNC_MEASUREMENT_ERROR US_TO_RTIMERTICKS(32)
65 
66 /* Base drift value.
67  * Used to compensate locally know inaccuracies, such as
68  * the effect of having a binary 32.768 kHz timer as the TSCH time base. */
69 #ifdef TSCH_CONF_BASE_DRIFT_PPM
70 #define TSCH_BASE_DRIFT_PPM TSCH_CONF_BASE_DRIFT_PPM
71 #else
72 #define TSCH_BASE_DRIFT_PPM 0
73 #endif
74 
75 /* The approximate number of slots per second */
76 #define TSCH_SLOTS_PER_SECOND (1000000 / TSCH_DEFAULT_TS_TIMESLOT_LENGTH)
77 
78 /***** External Variables *****/
79 
80 /* The neighbor last used as our time source */
81 extern struct tsch_neighbor *last_timesource_neighbor;
82 
83 /********** Functions *********/
84 
85 void tsch_timesync_update(struct tsch_neighbor *n, uint16_t time_delta_asn, int32_t drift_correction);
86 
87 int32_t tsch_timesync_adaptive_compensate(rtimer_clock_t delta_ticks);
88 
89 #endif /* __TSCH_ADAPTIVE_TIMESYNC_H__ */
Private TSCH definitions (meant for use by TSCH implementation files only) ...