Paparazzi UAS v7.0_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
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
60typedef int8_t tid_t;
61typedef void (*sys_time_cb)(uint8_t id);
62
70
82
83extern struct sys_time sys_time;
84
85extern void sys_time_init(void);
86
91
96
97
104
111
117
124extern tid_t sys_time_register_timer(float duration, sys_time_cb cb);
125
134
139extern void sys_time_cancel_timer(tid_t id);
140
146extern void sys_time_update_timer(tid_t id, float duration);
147
154{
155 if ((id < SYS_TIME_NB_TIMER) && (id >= 0)) {
156 if (sys_time.timer[id].elapsed) {
157 sys_time.timer[id].elapsed = false;
158 return true;
159 }
160 }
161 return false;
162}
163
168static inline float get_sys_time_float(void)
169{
171}
172
173
174/*
175 * Convenience functions to convert between seconds and sys_time ticks.
176 */
178{
179 return (uint32_t)(seconds * sys_time.ticks_per_sec + 0.5);
180}
181
183{
184 return msec * sys_time.ticks_per_sec / 1000;
185}
186
188{
189 return usec * sys_time.ticks_per_sec / 1000000;
190}
191
193{
194 return (float)ticks * sys_time.resolution;
195}
196
198{
199 return ticks * 1000 / sys_time.ticks_per_sec;
200}
201
203{
204 return ticks * 1000 / sys_time.ticks_per_sec * 1000;
205}
206
207
208
209/*
210 * Convenience functions to convert between seconds and CPU ticks.
211 */
213{
214 return (uint32_t)(seconds * sys_time.cpu_ticks_per_sec + 0.5);
215}
216
218{
219 return usec * (sys_time.cpu_ticks_per_sec / 1000000);
220}
221
223{
224 return usec * ((int32_t)sys_time.cpu_ticks_per_sec / 1000000);
225}
226
228{
229 return nsec * (sys_time.cpu_ticks_per_sec / 1000000) / 1000;
230}
231
233{
234 return cpu_ticks / (sys_time.cpu_ticks_per_sec / 1000);
235}
236
238{
239 return cpu_ticks / (sys_time.cpu_ticks_per_sec / 1000000);
240}
241
243{
244 return cpu_ticks / (sys_time.cpu_ticks_per_sec / 1000000) / 1000;
245}
246
247
248
249#define USEC_OF_SEC(sec) ((sec) * 1e6)
250
251/* architecture specific init implementation */
252void sys_time_arch_init(void);
253
254
255/* Generic timer macros */
256#define SysTimeTimerStart(_t) { _t = get_sys_time_usec(); }
257#define SysTimeTimer(_t) ( get_sys_time_usec() - (_t))
258#define SysTimeTimerStop(_t) { _t = ( get_sys_time_usec() - (_t)); }
259
260#ifdef __cplusplus
261}
262#endif
263
264#endif /* SYS_TIME_H */
static uint32_t cpu_ticks
static const float offset[]
int ticks
Definition gps_sirf.c:193
uint16_t foo
Definition main_demo5.c:58
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition sys_time.h:74
static uint32_t msec_of_cpu_ticks(uint32_t cpu_ticks)
Definition sys_time.h:232
uint32_t ticks_per_sec
sys_time ticks per second (SYS_TIME_FREQUENCY)
Definition sys_time.h:78
void sys_time_msleep(uint32_t ms)
Sleep.
void sys_time_init(void)
Definition sys_time.c:119
static uint32_t nsec_of_cpu_ticks(uint32_t cpu_ticks)
Definition sys_time.h:242
static uint32_t sys_time_ticks_of_sec(float seconds)
Definition sys_time.h:177
void sys_time_update_timer(tid_t id, float duration)
Update the duration until a timer elapses.
Definition sys_time.c:86
volatile uint32_t nb_sec
full seconds since startup
Definition sys_time.h:72
tid_t sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition sys_time.c:43
void(* sys_time_cb)(uint8_t id)
Definition sys_time.h:61
uint32_t cpu_ticks_per_sec
cpu ticks per second
Definition sys_time.h:80
uint32_t duration
in SYS_TIME_TICKS
Definition sys_time.h:68
static uint32_t sys_time_ticks_of_usec(uint32_t usec)
Definition sys_time.h:187
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:59
static bool sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition sys_time.h:153
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition sys_time.h:79
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:73
float resolution
sys_time_timer resolution in seconds
Definition sys_time.h:77
static uint32_t cpu_ticks_of_usec(uint32_t usec)
Definition sys_time.h:217
void sys_time_arch_init(void)
sys_time_cb cb
Definition sys_time.h:65
#define SYS_TIME_NB_TIMER
Definition sys_time.h:44
static uint32_t msec_of_sys_time_ticks(uint32_t ticks)
Definition sys_time.h:197
static uint32_t cpu_ticks_of_nsec(uint32_t nsec)
Definition sys_time.h:227
static uint32_t sys_time_ticks_of_msec(uint32_t msec)
Definition sys_time.h:182
uint32_t end_time
in SYS_TIME_TICKS
Definition sys_time.h:67
void sys_time_cancel_timer(tid_t id)
Cancel a system timer by id.
Definition sys_time.c:76
static uint32_t cpu_ticks_of_sec(float seconds)
Definition sys_time.h:212
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition sys_time.h:75
static uint32_t usec_of_cpu_ticks(uint32_t cpu_ticks)
Definition sys_time.h:237
static float sec_of_sys_time_ticks(uint32_t ticks)
Definition sys_time.h:192
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:202
volatile bool elapsed
Definition sys_time.h:66
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition sys_time.h:168
int8_t tid_t
sys_time timer id type
Definition sys_time.h:60
static int32_t signed_cpu_ticks_of_usec(int32_t usec)
Definition sys_time.h:222
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
signed char int8_t
Typedef defining 8 bit char type.