Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
sys_time.h
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#ifndef SYS_TIME_H
31#define SYS_TIME_H
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#include <inttypes.h>
38#include <stdlib.h>
39#include "std.h"
40
41#include BOARD_CONFIG
42
43#ifndef SYS_TIME_NB_TIMER
44#define SYS_TIME_NB_TIMER 16
45#endif
46
47
52#ifndef SYS_TIME_FREQUENCY
53#if defined PERIODIC_FREQUENCY
54#define SYS_TIME_FREQUENCY (2 * PERIODIC_FREQUENCY)
55#else /* !defined PERIODIC_FREQUENCY */
56#define SYS_TIME_FREQUENCY 1000
57#endif
58#endif
59
60#define SYS_TIME_MSEC_PER_WEEK (1000UL * 60 * 60 * 24 * 7)
61#define SYS_TIME_UNIX_GPS_EPOCH_MS 315964800000ULL
62
64#ifndef SYS_TIME_GPS_LEAP_SECONDS
65#define SYS_TIME_GPS_LEAP_SECONDS 18
66#endif
67
68typedef int8_t tid_t;
69typedef void (*sys_time_cb)(uint8_t id);
70
78
100
101extern struct sys_time sys_time;
102
103extern void sys_time_init(void);
104
109
114
115
122
129
135
141
149
156
162
169void sys_time_sync_gps(uint16_t week, uint32_t tow, uint32_t stamp);
170
177void sys_time_sync_unix(uint32_t unix_time, uint16_t unix_ms, uint32_t stamp);
178
183
189void sys_time_rtc_set(uint32_t unix_time, uint16_t unix_ms);
190
197extern tid_t sys_time_register_timer(float duration, sys_time_cb cb);
198
207
212extern void sys_time_cancel_timer(tid_t id);
213
219extern void sys_time_update_timer(tid_t id, float duration);
220
227{
228 if ((id < SYS_TIME_NB_TIMER) && (id >= 0)) {
229 if (sys_time.timer[id].elapsed) {
230 sys_time.timer[id].elapsed = false;
231 return true;
232 }
233 }
234 return false;
235}
236
241static inline float get_sys_time_float(void)
242{
243 return (float)(sys_time.nb_sec + (float)(sys_time.nb_sec_rem) / sys_time.cpu_ticks_per_sec);
244}
245
246
247/*
248 * Convenience functions to convert between seconds and sys_time ticks.
249 */
250static inline uint32_t sys_time_ticks_of_sec(float seconds)
251{
252 return (uint32_t)(seconds * sys_time.ticks_per_sec + 0.5);
253}
254
256{
257 return msec * sys_time.ticks_per_sec / 1000;
258}
259
261{
262 return usec * sys_time.ticks_per_sec / 1000000;
263}
264
266{
267 return (float)ticks * sys_time.resolution;
268}
269
271{
272 return ticks * 1000 / sys_time.ticks_per_sec;
273}
274
276{
277 return ticks * 1000 / sys_time.ticks_per_sec * 1000;
278}
279
280
281
282/*
283 * Convenience functions to convert between seconds and CPU ticks.
284 */
285static inline uint32_t cpu_ticks_of_sec(float seconds)
286{
287 return (uint32_t)(seconds * sys_time.cpu_ticks_per_sec + 0.5);
288}
289
291{
292 return usec * (sys_time.cpu_ticks_per_sec / 1000000);
293}
294
296{
297 return usec * ((int32_t)sys_time.cpu_ticks_per_sec / 1000000);
298}
299
301{
302 return nsec * (sys_time.cpu_ticks_per_sec / 1000000) / 1000;
303}
304
306{
307 return cpu_ticks / (sys_time.cpu_ticks_per_sec / 1000);
308}
309
311{
312 return cpu_ticks / (sys_time.cpu_ticks_per_sec / 1000000);
313}
314
316{
317 return cpu_ticks / (sys_time.cpu_ticks_per_sec / 1000000) / 1000;
318}
319
320
321
322#define USEC_OF_SEC(sec) ((sec) * 1e6)
323
324/* architecture specific init implementation */
325void sys_time_arch_init(void);
326
327
328/* Generic timer macros */
329#define SysTimeTimerStart(_t) { _t = get_sys_time_usec(); }
330#define SysTimeTimer(_t) ( get_sys_time_usec() - (_t))
331#define SysTimeTimerStop(_t) { _t = ( get_sys_time_usec() - (_t)); }
332
333#ifdef __cplusplus
334}
335#endif
336
337#endif /* SYS_TIME_H */
static uint32_t cpu_ticks
int ticks
Definition gps_sirf.c:193
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition sys_time.h:82
static uint32_t msec_of_cpu_ticks(uint32_t cpu_ticks)
Definition sys_time.h:305
uint32_t ticks_per_sec
sys_time ticks per second (SYS_TIME_FREQUENCY)
Definition sys_time.h:86
void sys_time_msleep(uint32_t ms)
Sleep.
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
uint64_t ground_offset
last applied ground UTC unix time at startup in ms
Definition sys_time.h:91
static uint32_t nsec_of_cpu_ticks(uint32_t cpu_ticks)
Definition sys_time.h:315
static uint32_t sys_time_ticks_of_sec(float seconds)
Definition sys_time.h:250
void sys_time_update_timer(tid_t id, float duration)
Update the duration until a timer elapses.
Definition sys_time.c:215
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
tid_t sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition sys_time.c:172
void(* sys_time_cb)(uint8_t id)
Definition sys_time.h:69
uint32_t cpu_ticks_per_sec
cpu ticks per second
Definition sys_time.h:88
uint32_t duration
in SYS_TIME_TICKS
Definition sys_time.h:76
static uint32_t sys_time_ticks_of_usec(uint32_t usec)
Definition sys_time.h:260
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
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
static bool sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition sys_time.h:226
uint64_t unix_offset
UTC unix time at startup in ms.
Definition sys_time.h:90
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition sys_time.h:87
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
void sys_time_usleep(uint32_t us)
Sleep.
uint32_t get_sys_time_usec100(void)
Get the time in 100microseconds since startup.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition sys_time.h:81
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).
float resolution
sys_time_timer resolution in seconds
Definition sys_time.h:85
static uint32_t cpu_ticks_of_usec(uint32_t usec)
Definition sys_time.h:290
void sys_time_arch_init(void)
uint64_t get_sys_time_unix_msec(void)
Get the UTC unix time in milliseconds.
Definition sys_time.c:77
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
static uint32_t msec_of_sys_time_ticks(uint32_t ticks)
Definition sys_time.h:270
static uint32_t cpu_ticks_of_nsec(uint32_t nsec)
Definition sys_time.h:300
static uint32_t sys_time_ticks_of_msec(uint32_t msec)
Definition sys_time.h:255
uint32_t end_time
in SYS_TIME_TICKS
Definition sys_time.h:75
void sys_time_cancel_timer(tid_t id)
Cancel a system timer by id.
Definition sys_time.c:205
static uint32_t cpu_ticks_of_sec(float seconds)
Definition sys_time.h:285
uint32_t get_sys_time_tow(void)
Get the GPS time of week in milliseconds.
Definition sys_time.c:72
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition sys_time.h:83
static uint32_t usec_of_cpu_ticks(uint32_t cpu_ticks)
Definition sys_time.h:310
bool gps_tow_synced
time of week synced with a GPS
Definition sys_time.h:95
static float sec_of_sys_time_ticks(uint32_t ticks)
Definition sys_time.h:265
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
static uint32_t usec_of_sys_time_ticks(uint32_t ticks)
Definition sys_time.h:275
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 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
uint32_t get_sys_time_unix(void)
Get the UTC unix time in seconds.
Definition sys_time.c:87
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition sys_time.h:241
bool unix_synced
unix time synced (GPS or ground)
Definition sys_time.h:97
uint32_t sys_time_rtc_millisecond(void)
Get the RTC time in milliseconds since midnight.
int8_t tid_t
sys_time timer id type
Definition sys_time.h:68
static int32_t signed_cpu_ticks_of_usec(int32_t usec)
Definition sys_time.h:295
static const float offset[]
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
int int32_t
Typedef defining 32 bit int 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.
signed char int8_t
Typedef defining 8 bit char type.