Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
sys_time.c
Go to the documentation of this file.
1/*
2 *
3 * Copyright (C) 2009-2011 The Paparazzi Team
4 *
5 * This file is part of paparazzi.
6 *
7 * paparazzi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * paparazzi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with paparazzi; see the file COPYING. If not, write to
19 * the Free Software Foundation, 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 */
23
29
30#include "mcu_periph/sys_time.h"
31#include "mcu.h"
32#include "pprzlink/dl_protocol.h"
33
34// check the number of timers against max tid_t value
35#include "limits.h"
36#if (SYS_TIME_NB_TIMER >= SCHAR_MAX)
37 WARNING("Too many sys timers (SYS_TIME_NB_TIMER >= SCHAR_MAX). Consider increasing the size of tid_d type (currently int8_t")
38#endif
39
41
43
45#ifndef SYS_TIME_GPS_SYNC_TIMEOUT
46#define SYS_TIME_GPS_SYNC_TIMEOUT 10000
47#endif
48
50#ifndef SYS_TIME_RTC_SYNC_PERIOD
51#define SYS_TIME_RTC_SYNC_PERIOD 5000
52#endif
53
56#ifndef SYS_TIME_GROUND_SYNC_THRESHOLD
57#define SYS_TIME_GROUND_SYNC_THRESHOLD 200
58#endif
59
60#define SYS_TIME_GPS_LEAP_MS ((uint64_t)SYS_TIME_GPS_LEAP_SECONDS * 1000)
61
65{
67 int64_t diff = (int64_t)sys_time.nb_sec * 1000 - ms;
68 uint64_t wraps = (uint64_t)((diff + (1LL << 31)) >> 32);
69 return (wraps << 32) + ms;
70}
71
76
78{
79 // 64-bit is not read/written atomically on 32-bit MCUs,
80 // fine as long as the syncs and the users run in the same thread
81 if (!sys_time.unix_synced) {
82 return 0;
83 }
85}
86
88{
89 return (uint32_t)(get_sys_time_unix_msec() / 1000);
90}
91
95{
96 *now = uptime_msec();
97 uint32_t age_ms = (get_sys_time_usec() - stamp) / 1000;
98 return *now - age_ms;
99}
100
106
107static void set_unix(uint64_t unix_ms, uint64_t msec, uint64_t now)
108{
109 sys_time.unix_offset = unix_ms - msec;
110 sys_time.unix_synced = true;
111
112 // Keep the RTC in sync
115 uint64_t unix_now = sys_time.unix_offset + now;
116 sys_time_rtc_set((uint32_t)(unix_now / 1000), (uint16_t)(unix_now % 1000));
117 }
118}
119
121{
122 uint64_t now;
123 uint64_t msec = msec_of_stamp(stamp, &now);
124
125 set_tow(tow, msec);
128
129 if (week != 0) {
130 uint64_t gps_ms = (uint64_t)week * SYS_TIME_MSEC_PER_WEEK + tow;
134 }
135}
136
137void sys_time_sync_unix(uint32_t unix_time, uint16_t unix_ms, uint32_t stamp)
138{
139 uint64_t unix_msec = (uint64_t)unix_time * 1000 + unix_ms;
140 if (unix_msec < SYS_TIME_UNIX_GPS_EPOCH_MS) {
141 return; // invalid time
142 }
143
144 uint64_t now;
145 uint64_t msec = msec_of_stamp(stamp, &now);
146
147 // GPS has priority
150 if (!use_unix && !use_tow) {
151 return;
152 }
153
154 // Only step when the difference with the last applied ground time is significant
155 uint64_t offset = unix_msec - msec;
156 int64_t diff = (int64_t)(offset - sys_time.ground_offset);
157 if (sys_time.ground_synced && diff < SYS_TIME_GROUND_SYNC_THRESHOLD && diff > -SYS_TIME_GROUND_SYNC_THRESHOLD) {
158 return;
159 }
161 sys_time.ground_synced = true;
162
163 if (use_unix) {
164 set_unix(unix_msec, msec, now);
165 }
166 if (use_tow) {
168 set_tow((uint32_t)(gps_ms % SYS_TIME_MSEC_PER_WEEK), msec);
169 }
170}
171
173{
175 for (tid_t i = 0; i < SYS_TIME_NB_TIMER; i++) {
176 if (!sys_time.timer[i].in_use) {
177 sys_time.timer[i].cb = cb;
178 sys_time.timer[i].elapsed = false;
181 sys_time.timer[i].in_use = true;
182 return i;
183 }
184 }
185 return -1;
186}
187
189{
190 if (timer >=0 && timer < SYS_TIME_NB_TIMER) {
191 for (tid_t i = 0; i < SYS_TIME_NB_TIMER; i++) {
192 if (!sys_time.timer[i].in_use && (timer < i)) { // timer should be already registered
193 sys_time.timer[i].cb = cb;
194 sys_time.timer[i].elapsed = false;
195 sys_time.timer[i].end_time = sys_time.timer[timer].end_time + sys_time_ticks_of_sec(offset); // add offset to end time
196 sys_time.timer[i].duration = sys_time.timer[timer].duration; // copy duration
197 sys_time.timer[i].in_use = true;
198 return i;
199 }
200 }
201 }
202 return -1;
203}
204
206{
207 sys_time.timer[id].in_use = false;
208 sys_time.timer[id].cb = NULL;
209 sys_time.timer[id].elapsed = false;
210 sys_time.timer[id].end_time = 0;
211 sys_time.timer[id].duration = 0;
212}
213
214// FIXME: race condition ??
215void sys_time_update_timer(tid_t id, float duration)
216{
219}
220
222{
223 sys_time_sync_unix(pprzlink_get_DL_TIME_SYNC_unix_time(buf), pprzlink_get_DL_TIME_SYNC_unix_ms(buf),
225}
226
227#if USE_SHELL
228#include "modules/core/shell.h"
229#include "printf.h"
230#include "string.h"
231
232static void cmd_sys_time(shell_stream_t *sh, int argc, const char * const argv[])
233{
234 (void) argv;
235 if (argc > 0) {
236 chprintf(sh, "Usage: sys_time\r\n");
237 return;
238 }
239
240 chprintf(sh, "Current sys_time\r\n");
241 chprintf(sh, " nb_sec: %lu\r\n", sys_time.nb_sec);
242 chprintf(sh, " nb_sec_rem: %lu\r\n", sys_time.nb_sec_rem);
243 chprintf(sh, " nb_tick: %lu\r\n", sys_time.nb_tick);
244 chprintf(sh, " tow: %lu\r\n", get_sys_time_tow());
245 chprintf(sh, " unix: %lu\r\n", get_sys_time_unix());
246
248 uint32_t rtc_h = rtc_ms / 3600000;
249 uint32_t rtc_m = (rtc_ms % 3600000) / 60000;
250 float rtc_s = (rtc_ms % 60000) / 1000.0;
251 chprintf(sh, " rtc_time: %02u:%02u:%02.03f\r\n", rtc_h, rtc_m, rtc_s);
252
253 chprintf(sh, "Registered timers\r\n");
254 for (tid_t i = 0; i < SYS_TIME_NB_TIMER; i++) {
255 if (sys_time.timer[i].in_use) {
256 chprintf(sh, "id: %d, duration: %lu, end_time: %lu, cb: 0x%lx\r\n", i,
258 }
259 }
260}
261#endif
262
264{
265 sys_time.nb_sec = 0;
267 sys_time.nb_tick = 0;
273 sys_time.rtc_sync = 0;
274 sys_time.gps_tow_synced = false;
276 sys_time.unix_synced = false;
277 sys_time.ground_synced = false;
278
281
282 for (unsigned int i = 0; i < SYS_TIME_NB_TIMER; i++) {
283 sys_time.timer[i].in_use = false;
284 sys_time.timer[i].cb = NULL;
285 sys_time.timer[i].elapsed = false;
286 sys_time.timer[i].end_time = 0;
287 sys_time.timer[i].duration = 0;
288 }
289
291
292#if USE_SHELL
293 shell_add_entry("sys_time", cmd_sys_time);
294#endif
295}
uint32_t sys_time_rtc_millisecond()
Get the RTC time in milliseconds since midnight.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
void sys_time_rtc_set(uint32_t unix_time, uint16_t unix_ms)
Set the RTC to the UTC unix time (architecture specific, may do nothing).
void sys_time_arch_init(void)
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
int start_time
Definition gps_sirf.c:192
void chprintf(BaseSequentialStream *lchp, const char *fmt,...)
Definition printf.c:395
Arch independent mcu ( Micro Controller Unit ) utilities.
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
Mini printf-like functionality.
void shell_add_entry(char *cmd_name, shell_cmd_t *cmd)
Add dynamic entry.
Definition shell_arch.c:91
BaseSequentialStream shell_stream_t
Definition shell_arch.h:31
#define SYS_TIME_GPS_SYNC_TIMEOUT
Time without GPS sync in ms before the ground time is used.
Definition sys_time.c:46
void sys_time_init(void)
Definition sys_time.c:263
void sys_time_parse_TIME_SYNC(uint8_t *buf)
Parse the TIME_SYNC datalink message (ground time).
Definition sys_time.c:221
void sys_time_update_timer(tid_t id, float duration)
Update the duration until a timer elapses.
Definition sys_time.c:215
tid_t sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition sys_time.c:172
tid_t sys_time_register_timer_offset(tid_t timer, float offset, sys_time_cb cb)
Register a new system timer with an fixed offset from another one.
Definition sys_time.c:188
static uint64_t msec_of_stamp(uint32_t stamp, uint64_t *now)
uptime_msec() at the moment of the timestamp in usec the usec stamp wraps after ~71 min,...
Definition sys_time.c:94
static void set_unix(uint64_t unix_ms, uint64_t msec, uint64_t now)
Definition sys_time.c:107
void sys_time_sync_gps(uint16_t week, uint32_t tow, uint32_t stamp)
Synchronize the time with a GPS measurement (has priority over the ground).
Definition sys_time.c:120
uint64_t get_sys_time_unix_msec(void)
Get the UTC unix time in milliseconds.
Definition sys_time.c:77
void sys_time_cancel_timer(tid_t id)
Cancel a system timer by id.
Definition sys_time.c:205
uint32_t get_sys_time_tow(void)
Get the GPS time of week in milliseconds.
Definition sys_time.c:72
#define SYS_TIME_GPS_LEAP_MS
Definition sys_time.c:60
#define SYS_TIME_RTC_SYNC_PERIOD
Period of the RTC updates in ms.
Definition sys_time.c:51
static void set_tow(uint32_t tow, uint64_t msec)
Definition sys_time.c:101
void sys_time_sync_unix(uint32_t unix_time, uint16_t unix_ms, uint32_t stamp)
Synchronize the time with the ground, only used without recent GPS sync.
Definition sys_time.c:137
uint32_t get_sys_time_unix(void)
Get the UTC unix time in seconds.
Definition sys_time.c:87
static uint64_t uptime_msec(void)
Milliseconds since startup without the ~49.7 days wrap of get_sys_time_msec() nb_sec gives the number...
Definition sys_time.c:64
#define SYS_TIME_GROUND_SYNC_THRESHOLD
Minimum difference in ms before the ground time is applied again, avoids steps (also backwards) from ...
Definition sys_time.c:57
Architecture independent timing functions.
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition sys_time.h:82
uint32_t ticks_per_sec
sys_time ticks per second (SYS_TIME_FREQUENCY)
Definition sys_time.h:86
#define SYS_TIME_FREQUENCY
(Default) sys_time timer frequency in Hz.
Definition sys_time.h:56
uint64_t ground_offset
last applied ground UTC unix time at startup in ms
Definition sys_time.h:91
static uint32_t sys_time_ticks_of_sec(float seconds)
Definition sys_time.h:250
volatile uint32_t nb_sec
full seconds since startup
Definition sys_time.h:80
bool ground_synced
a ground time was applied
Definition sys_time.h:98
void(* sys_time_cb)(uint8_t id)
Definition sys_time.h:69
uint32_t duration
in SYS_TIME_TICKS
Definition sys_time.h:76
uint32_t tow_offset
GPS time of week at startup in ms (0 until synced).
Definition sys_time.h:89
uint32_t gps_tow_sync
uptime in ms of the last GPS time of week sync
Definition sys_time.h:92
uint64_t unix_offset
UTC unix time at startup in ms.
Definition sys_time.h:90
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition sys_time.h:81
float resolution
sys_time_timer resolution in seconds
Definition sys_time.h:85
#define SYS_TIME_MSEC_PER_WEEK
Definition sys_time.h:60
sys_time_cb cb
Definition sys_time.h:73
bool gps_unix_synced
unix time synced with a GPS
Definition sys_time.h:96
#define SYS_TIME_NB_TIMER
Definition sys_time.h:44
uint32_t rtc_sync
uptime in ms of the last RTC update
Definition sys_time.h:94
uint32_t end_time
in SYS_TIME_TICKS
Definition sys_time.h:75
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition sys_time.h:83
bool gps_tow_synced
time of week synced with a GPS
Definition sys_time.h:95
#define SYS_TIME_UNIX_GPS_EPOCH_MS
unix time of the GPS epoch (1980-01-06) in ms
Definition sys_time.h:61
uint32_t gps_unix_sync
uptime in ms of the last GPS unix time sync
Definition sys_time.h:93
volatile bool elapsed
Definition sys_time.h:74
bool unix_synced
unix time synced (GPS or ground)
Definition sys_time.h:97
int8_t tid_t
sys_time timer id type
Definition sys_time.h:68
static const float offset[]
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned long long uint64_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.