Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
sys_time_arch.c
Go to the documentation of this file.
1/*
2 *
3 * Copyright (C) 2009-2013 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
27#include "mcu_periph/sys_time.h"
28#include <stdio.h>
29#include <pthread.h>
30#include <sys/timerfd.h>
31#include <time.h>
32#include "rt_priority.h"
33
34#include "std.h"
35#include <unistd.h>
36
37#ifdef SYS_TIME_LED
38#include "led.h"
39#endif
40
41#ifndef SYS_TIME_THREAD_PRIO
42#define SYS_TIME_THREAD_PRIO 29
43#endif
44
45static struct timespec startup_time;
46
47void sys_tick_handler(void);
48
49void *sys_time_thread_main(void *data);
50
51#define NSEC_OF_SEC(sec) ((sec) * 1e9)
52
53void *sys_time_thread_main(void *data)
54{
55 int fd;
56
57 /* Create the timer */
59 if (fd == -1) {
60 perror("Could not set up timer.");
61 return NULL;
62 }
63
65
66 /* Make the timer periodic */
67 struct itimerspec timer;
68 /* timer expires after sys_time.resolution sec */
69 timer.it_value.tv_sec = 0;
70 timer.it_value.tv_nsec = NSEC_OF_SEC(sys_time.resolution);
71 /* and every SYS_TIME_RESOLUTION sec after that */
72 timer.it_interval.tv_sec = 0;
73 timer.it_interval.tv_nsec = NSEC_OF_SEC(sys_time.resolution);
74
75 if (timerfd_settime(fd, 0, &timer, NULL) == -1) {
76 perror("Could not set up timer.");
77 return NULL;
78 }
79
80 while (1) {
81 unsigned long long missed;
82 /* Wait for the next timer event. If we have missed any the
83 number is written to "missed" */
84 int r = read(fd, &missed, sizeof(missed));
85 if (r == -1) {
86 perror("Couldn't read timer!");
87 }
88 if (missed > 1) {
89 fprintf(stderr, "Missed %lld timer events!\n", missed);
90 }
91 /* set current sys_time */
93 }
94 return NULL;
95}
96
98{
101
103
106 if (ret) {
107 perror("Could not setup sys_time_thread");
108 return;
109 }
110#ifndef __APPLE__
111 pthread_setname_np(tid, "sys_time");
112#endif
113}
114
116{
117 /* get current time */
118 struct timespec now;
120
121 /* time difference to startup */
122 time_t d_sec = now.tv_sec - startup_time.tv_sec;
123 long d_nsec = now.tv_nsec - startup_time.tv_nsec;
124
125 /* wrap if negative nanoseconds */
126 if (d_nsec < 0) {
127 d_sec -= 1;
128 d_nsec += 1000000000L;
129 }
130
131#ifdef SYS_TIME_LED
132 if (d_sec > sys_time.nb_sec) {
134 }
135#endif
136
140
141 /* advance virtual timers */
142 for (unsigned int i = 0; i < SYS_TIME_NB_TIMER; i++) {
143 if (sys_time.timer[i].in_use &&
146 sys_time.timer[i].elapsed = true;
147 /* call registered callbacks, WARNING: they will be executed in the sys_time thread! */
148 if (sys_time.timer[i].cb) {
149 sys_time.timer[i].cb(i);
150 }
151 }
152 }
153}
154
161{
162 /* get current time */
163 struct timespec now;
165
166 /* time difference to startup */
167 time_t d_sec = now.tv_sec - startup_time.tv_sec;
168 long d_nsec = now.tv_nsec - startup_time.tv_nsec;
169
170 /* wrap if negative nanoseconds */
171 if (d_nsec < 0) {
172 d_sec -= 1;
173 d_nsec += 1000000000L;
174 }
175 return d_sec * 1000000 + d_nsec / 1000;
176}
177
184{
185 /* get current time */
186 struct timespec now;
188
189 /* time difference to startup */
190 time_t d_sec = now.tv_sec - startup_time.tv_sec;
191 long d_nsec = now.tv_nsec - startup_time.tv_nsec;
192
193 /* wrap if negative nanoseconds */
194 if (d_nsec < 0) {
195 d_sec -= 1;
196 d_nsec += 1000000000L;
197 }
198 return d_sec * 10000 + d_nsec / 100000;
199}
200
206{
207 /* get current time */
208 struct timespec now;
210
211 /* time difference to startup */
212 time_t d_sec = now.tv_sec - startup_time.tv_sec;
213 long d_nsec = now.tv_nsec - startup_time.tv_nsec;
214
215 /* wrap if negative nanoseconds */
216 if (d_nsec < 0) {
217 d_sec -= 1;
218 d_nsec += 1000000000L;
219 }
220 return d_sec * 1000 + d_nsec / 1000000;
221}
222
224{
225 usleep(us);
226}
227
#define LED_TOGGLE(i)
Definition led_hw.h:53
void sys_time_msleep(uint32_t ms)
Sleep.
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
uint32_t get_sys_time_usec100(void)
Get the time in microseconds divided by 100 since startup.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
void sys_time_arch_init(void)
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
void sys_tick_handler(void)
arch independent LED (Light Emitting Diodes) API
#define SYS_TIME_THREAD_PRIO
void * sys_time_thread_main(void *data)
static struct timespec startup_time
#define NSEC_OF_SEC(sec)
uint16_t foo
Definition main_demo5.c:58
Functions to obtain rt priority or set the nice level.
static int get_rt_prio(int prio)
Definition rt_priority.h:32
int fd
Definition serial.c:26
Architecture independent timing functions.
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition sys_time.h:74
static uint32_t sys_time_ticks_of_sec(float seconds)
Definition sys_time.h:177
volatile uint32_t nb_sec
full seconds since startup
Definition sys_time.h:72
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
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition sys_time.h:79
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
sys_time_cb cb
Definition sys_time.h:65
#define SYS_TIME_NB_TIMER
Definition sys_time.h:44
static uint32_t cpu_ticks_of_nsec(uint32_t nsec)
Definition sys_time.h:227
uint32_t end_time
in SYS_TIME_TICKS
Definition sys_time.h:67
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition sys_time.h:75
volatile bool elapsed
Definition sys_time.h:66
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.