Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros 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 8
41 #endif
42 
43 typedef uint8_t tid_t;
44 typedef void (*sys_time_cb) (uint8_t id);
45 
47  bool_t in_use;
49  volatile bool_t elapsed;
52 };
53 
54 struct sys_time {
55  volatile uint32_t nb_sec;
56  volatile uint32_t nb_sec_rem;
57  volatile uint32_t nb_tick;
59 };
60 
61 extern struct sys_time sys_time;
62 
63 //FIXME temporary hack
64 #define cpu_time_sec sys_time.nb_sec
65 #define cpu_time_ticks sys_time.nb_sec_rem
66 
67 extern void sys_time_init(void);
68 
75 extern int sys_time_register_timer(float duration, sys_time_cb cb);
76 
81 extern void sys_time_cancel_timer(tid_t id);
82 
88 extern void sys_time_update_timer(tid_t id, float duration);
89 
90 static inline bool_t sys_time_check_and_ack_timer(tid_t id) {
91  if (sys_time.timer[id].elapsed) {
93  return TRUE;
94  }
95  return FALSE;
96 }
97 
98 #define GET_CUR_TIME_FLOAT() ((float)sys_time.nb_sec + SEC_OF_CPU_TICKS((float)sys_time.nb_sec_rem))
99 
100 
101 /* CPU clock */
102 #define CPU_TICKS_OF_USEC(us) CPU_TICKS_OF_SEC((us) * 1e-6)
103 #define CPU_TICKS_OF_NSEC(ns) CPU_TICKS_OF_SEC((ns) * 1e-9)
104 #define SIGNED_CPU_TICKS_OF_USEC(us) SIGNED_CPU_TICKS_OF_SEC((us) * 1e-6)
105 #define SIGNED_CPU_TICKS_OF_NSEC(us) SIGNED_CPU_TICKS_OF_SEC((us) * 1e-9)
106 
107 #define CPU_TICKS_PER_SEC CPU_TICKS_OF_SEC( 1.)
108 
109 
110 /* paparazzi sys_time timers */
111 #ifndef SYS_TIME_RESOLUTION
112 #define SYS_TIME_RESOLUTION ( 1./1024. )
113 #endif
114 #define SYS_TIME_RESOLUTION_CPU_TICKS CPU_TICKS_OF_SEC(SYS_TIME_RESOLUTION)
115 
116 #define SYS_TIME_TICKS_OF_SEC(s) (uint32_t)((s) / SYS_TIME_RESOLUTION + 0.5)
117 #define SYS_TIME_TICKS_OF_USEC(us) SYS_TIME_TICKS_OF_SEC((us) * 1e-6)
118 #define SYS_TIME_TICKS_OF_NSEC(ns) SYS_TIME_TICKS_OF_SEC((ns) * 1e-9)
119 
120 #define SEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION)
121 #define USEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION / 1e-6)
122 #define NSEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION / 1e-9)
123 
124 #define USEC_OF_SEC(sec) ((sec) * 1e6)
125 
126 
127 #include "mcu_periph/sys_time_arch.h"
128 
129 /* architecture specific init implementation */
130 extern void sys_time_arch_init(void);
131 
132 
133 #endif /* SYS_TIME_H */
volatile bool_t elapsed
Definition: sys_time.h:49
sys_time_cb cb
Definition: sys_time.h:48
uint8_t tid_t
sys_time timer id type
Definition: sys_time.h:43
void sys_time_arch_init(void)
Definition: sys_time_arch.c:96
uint32_t end_time
in SYS_TICKS
Definition: sys_time.h:50
#define FALSE
Definition: imu_chimu.h:141
void sys_time_init(void)
Definition: sys_time.c:69
void(* sys_time_cb)(uint8_t id)
Definition: sys_time.h:44
unsigned long uint32_t
Definition: types.h:18
volatile uint32_t nb_sec_rem
remainder of second in CPU_TICKS
Definition: sys_time.h:56
static bool_t sys_time_check_and_ack_timer(tid_t id)
Definition: sys_time.h:90
volatile uint32_t nb_tick
in SYS_TICKS with SYS_TIME_RESOLUTION
Definition: sys_time.h:57
#define TRUE
Definition: imu_chimu.h:144
unsigned char uint8_t
Definition: types.h:14
uint32_t duration
in SYS_TICKS
Definition: sys_time.h:51
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:55
#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:35
bool_t in_use
Definition: sys_time.h:47
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition: sys_time.h:58
void sys_time_update_timer(tid_t id, float duration)
Update the duration until a timer elapses.
Definition: sys_time.c:62
void sys_time_cancel_timer(tid_t id)
Cancel a system timer by id.
Definition: sys_time.c:51