Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sys_time_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 AggieAir, A Remote Sensing Unmanned Aerial System for Scientific Applications
3  * Utah State University, http://aggieair.usu.edu/
4  *
5  * Michal Podhradsky (michal.podhradsky@aggiemail.usu.edu)
6  * Calvin Coopmans (c.r.coopmans@ieee.org)
7  *
8  * Copyright (C) 2015 Gautier Hattenberger <gautier.hattenberger@enac.fr>
9  *
10  * This file is part of paparazzi.
11  *
12  * paparazzi is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * paparazzi is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with paparazzi; see the file COPYING. If not, write to
24  * the Free Software Foundation, 59 Temple Place - Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */
27 
35 #include "mcu_periph/sys_time_arch.h"
36 #include BOARD_CONFIG
37 #include <ch.h>
38 #include "led.h"
39 
40 static MUTEX_DECL(sys_time_mtx);
41 
42 /*
43  * Sys_tick handler thread
44  */
45 static void thd_sys_tick(void *arg);
46 static THD_WORKING_AREA(wa_thd_sys_tick, 1024);
47 static void sys_tick_handler(void);
48 
50 {
51 
53 
54  /* cpu ticks per desired sys_time timer step */
56 
57  // Create thread (PRIO should be higher than AP threads
58  chThdCreateStatic(wa_thd_sys_tick, sizeof(wa_thd_sys_tick),
59  NORMALPRIO+2, thd_sys_tick, NULL);
60 
61 }
62 
69 {
70  chMtxLock(&sys_time_mtx);
71  uint32_t t = sys_time.nb_sec * 1000000 +
73  usec_of_sys_time_ticks(chVTGetSystemTime() - sys_time.nb_tick);
74  chMtxUnlock(&sys_time_mtx);
75  return t;
76 }
77 
79 {
80  chMtxLock(&sys_time_mtx);
81  uint32_t t = sys_time.nb_sec * 1000 +
83  msec_of_sys_time_ticks(chVTGetSystemTime() - sys_time.nb_tick);
84  chMtxUnlock(&sys_time_mtx);
85  return t;
86 }
87 
96 {
97  uint64_t wait_st = ((uint64_t)us * CH_CFG_ST_FREQUENCY) / 1000000UL;
98  chThdSleep((systime_t)wait_st);
99 }
100 
102 {
103  uint64_t wait_st = ((uint64_t)ms * CH_CFG_ST_FREQUENCY) / 1000UL;
104  chThdSleep((systime_t)wait_st);
105 }
106 
108 {
109  chThdSleep(S2ST(s));
110 }
111 
112 /*
113  * Sys_tick thread
114  */
115 static __attribute__((noreturn)) void thd_sys_tick(void *arg)
116 {
117  (void) arg;
118  chRegSetThreadName("sys_tick_handler");
119 
120  while (TRUE) {
122  chThdSleepMilliseconds(1);
123  }
124 }
125 
126 static void sys_tick_handler(void)
127 {
128  chMtxLock(&sys_time_mtx);
129  /* current time in sys_ticks */
130  sys_time.nb_tick = chVTGetSystemTime();
131  /* max time is 2^32 / CH_CFG_ST_FREQUENCY, i.e. around 10 days at 10kHz */
133 #ifdef SYS_TIME_LED
134  if (sec > sys_time.nb_sec) {
135  LED_TOGGLE(SYS_TIME_LED);
136  }
137 #endif
138  sys_time.nb_sec = sec;
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  chMtxUnlock(&sys_time_mtx);
154 }
155 
unsigned short uint16_t
Definition: types.h:16
float resolution
sys_time_timer resolution in seconds
Definition: sys_time.h:77
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:78
uint32_t cpu_ticks_per_sec
cpu ticks per second
Definition: sys_time.h:80
void sys_time_ssleep(uint8_t s)
static uint32_t msec_of_sys_time_ticks(uint32_t ticks)
Definition: sys_time.h:158
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition: sys_time.h:74
static void thd_sys_tick(void *arg)
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
Definition: sys_time_arch.c:95
#define TRUE
Definition: std.h:4
unsigned long long uint64_t
Definition: types.h:20
static THD_WORKING_AREA(wa_thd_sys_tick, 1024)
static void sys_tick_handler(void)
volatile bool elapsed
Definition: sys_time.h:66
unsigned long uint32_t
Definition: types.h:18
uint32_t end_time
in SYS_TIME_TICKS
Definition: sys_time.h:67
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition: sys_time.h:73
#define LED_TOGGLE(i)
Definition: led_hw.h:52
static uint32_t usec_of_sys_time_ticks(uint32_t ticks)
Definition: sys_time.h:163
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition: sys_time.h:75
void sys_time_msleep(uint16_t ms)
static uint32_t sys_time_ticks_of_sec(float seconds)
Definition: sys_time.h:138
unsigned char uint8_t
Definition: types.h:14
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:72
static MUTEX_DECL(sys_time_mtx)
sys_time_cb cb
Definition: sys_time.h:65
arch independent LED (Light Emitting Diodes) API
#define SYS_TIME_NB_TIMER
Definition: sys_time.h:40
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chconf.h:51
uint32_t duration
in SYS_TIME_TICKS
Definition: sys_time.h:68
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
void sys_time_arch_init(void)
Definition: sys_time_arch.c:49
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition: sys_time.h:79