Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 #include <inttypes.h>
34 #include <stdlib.h>
35 #include "std.h"
36 
37 #include BOARD_CONFIG
38 
39 #ifndef SYS_TIME_NB_TIMER
40 #define SYS_TIME_NB_TIMER 16
41 #endif
42 
43 
48 #ifndef SYS_TIME_FREQUENCY
49 #if defined PERIODIC_FREQUENCY
50 #define SYS_TIME_FREQUENCY (2 * PERIODIC_FREQUENCY)
51 #else
52 #define SYS_TIME_FREQUENCY 1000
53 #endif
54 #endif
55 
56 
57 typedef uint8_t tid_t;
58 typedef void (*sys_time_cb)(uint8_t id);
59 
61  bool_t in_use;
63  volatile bool_t elapsed;
66 };
67 
68 struct sys_time {
69  volatile uint32_t nb_sec;
70  volatile uint32_t nb_sec_rem;
71  volatile uint32_t nb_tick;
73 
74  float resolution;
78 };
79 
80 extern struct sys_time sys_time;
81 
82 
83 extern void sys_time_init(void);
84 
91 extern int sys_time_register_timer(float duration, sys_time_cb cb);
92 
97 extern void sys_time_cancel_timer(tid_t id);
98 
104 extern void sys_time_update_timer(tid_t id, float duration);
105 
111 static inline bool_t sys_time_check_and_ack_timer(tid_t id)
112 {
113  if (sys_time.timer[id].elapsed) {
114  sys_time.timer[id].elapsed = FALSE;
115  return TRUE;
116  }
117  return FALSE;
118 }
119 
124 static inline float get_sys_time_float(void)
125 {
126  return (float)(sys_time.nb_sec + (float)(sys_time.nb_sec_rem) / sys_time.cpu_ticks_per_sec);
127 }
128 
129 
130 /*
131  * Convenience functions to convert between seconds and sys_time ticks.
132  */
133 static inline uint32_t sys_time_ticks_of_sec(float seconds)
134 {
135  return (uint32_t)(seconds * sys_time.ticks_per_sec + 0.5);
136 }
137 
139 {
140  return msec * sys_time.ticks_per_sec / 1000;
141 }
142 
144 {
145  return usec * sys_time.ticks_per_sec / 1000000;
146 }
147 
149 {
150  return (float)ticks * sys_time.resolution;
151 }
152 
154 {
155  return ticks * 1000 / sys_time.ticks_per_sec;
156 }
157 
159 {
160  return ticks * 1000 / sys_time.ticks_per_sec * 1000;
161 }
162 
163 
164 
165 /*
166  * Convenience functions to convert between seconds and CPU ticks.
167  */
168 static inline uint32_t cpu_ticks_of_sec(float seconds)
169 {
170  return (uint32_t)(seconds * sys_time.cpu_ticks_per_sec + 0.5);
171 }
172 
174 {
175  return usec * (sys_time.cpu_ticks_per_sec / 1000000);
176 }
177 
179 {
180  return usec * ((int32_t)sys_time.cpu_ticks_per_sec / 1000000);
181 }
182 
184 {
185  return nsec * (sys_time.cpu_ticks_per_sec / 1000000) / 1000;
186 }
187 
188 static inline uint32_t msec_of_cpu_ticks(uint32_t cpu_ticks)
189 {
190  return cpu_ticks / (sys_time.cpu_ticks_per_sec / 1000);
191 }
192 
193 static inline uint32_t usec_of_cpu_ticks(uint32_t cpu_ticks)
194 {
195  return cpu_ticks / (sys_time.cpu_ticks_per_sec / 1000000);
196 }
197 
198 static inline uint32_t nsec_of_cpu_ticks(uint32_t cpu_ticks)
199 {
200  return cpu_ticks / (sys_time.cpu_ticks_per_sec / 1000000) / 1000;
201 }
202 
203 
204 
205 #define USEC_OF_SEC(sec) ((sec) * 1e6)
206 
207 #include "mcu_periph/sys_time_arch.h"
208 
209 /* architecture specific init implementation */
210 extern void sys_time_arch_init(void);
211 
212 /* Generic timer macros */
213 #define SysTimeTimerStart(_t) { _t = get_sys_time_usec(); }
214 #define SysTimeTimer(_t) ( get_sys_time_usec() - (_t))
215 #define SysTimeTimerStop(_t) { _t = ( get_sys_time_usec() - (_t)); }
216 
217 #endif /* SYS_TIME_H */
static uint32_t msec_of_cpu_ticks(uint32_t cpu_ticks)
Definition: sys_time.h:188
static uint32_t cpu_ticks_of_nsec(uint32_t nsec)
Definition: sys_time.h:183
float resolution
sys_time_timer resolution in seconds
Definition: sys_time.h:74
uint32_t cpu_ticks_per_sec
cpu ticks per second
Definition: sys_time.h:77
static float sec_of_sys_time_ticks(uint32_t ticks)
Definition: sys_time.h:148
static uint32_t msec_of_sys_time_ticks(uint32_t ticks)
Definition: sys_time.h:153
uint8_t tid_t
sys_time timer id type
Definition: sys_time.h:57
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition: sys_time.h:71
void sys_time_arch_init(void)
Initialize SysTick.
Definition: sys_time_arch.c:94
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:124
#define FALSE
Definition: std.h:5
void sys_time_init(void)
Definition: sys_time.c:73
uint32_t ticks_per_sec
sys_time ticks per second (SYS_TIME_FREQUENCY)
Definition: sys_time.h:75
static uint32_t usec_of_cpu_ticks(uint32_t cpu_ticks)
Definition: sys_time.h:193
#define TRUE
Definition: std.h:4
void(* sys_time_cb)(uint8_t id)
Definition: sys_time.h:58
static uint32_t sys_time_ticks_of_msec(uint32_t msec)
Definition: sys_time.h:138
unsigned long uint32_t
Definition: types.h:18
uint32_t end_time
in SYS_TIME_TICKS
Definition: sys_time.h:64
static int32_t signed_cpu_ticks_of_usec(int32_t usec)
Definition: sys_time.h:178
int ticks
Definition: gps_sirf.c:114
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition: sys_time.h:70
static bool_t sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition: sys_time.h:111
signed long int32_t
Definition: types.h:19
bool_t in_use
Definition: sys_time.h:61
volatile bool_t elapsed
Definition: sys_time.h:63
static uint32_t usec_of_sys_time_ticks(uint32_t ticks)
Definition: sys_time.h:158
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition: sys_time.h:72
static uint32_t sys_time_ticks_of_sec(float seconds)
Definition: sys_time.h:133
unsigned char uint8_t
Definition: types.h:14
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:69
static uint32_t nsec_of_cpu_ticks(uint32_t cpu_ticks)
Definition: sys_time.h:198
sys_time_cb cb
Definition: sys_time.h:62
#define SYS_TIME_NB_TIMER
Definition: sys_time.h:40
int sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:37
uint32_t duration
in SYS_TIME_TICKS
Definition: sys_time.h:65
static uint32_t sys_time_ticks_of_usec(uint32_t usec)
Definition: sys_time.h:143
static uint32_t cpu_ticks_of_sec(float seconds)
Definition: sys_time.h:168
void sys_time_update_timer(tid_t id, float duration)
Update the duration until a timer elapses.
Definition: sys_time.c:65
static uint32_t cpu_ticks_of_usec(uint32_t usec)
Definition: sys_time.h:173
void sys_time_cancel_timer(tid_t id)
Cancel a system timer by id.
Definition: sys_time.c:55
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition: sys_time.h:76