32#include "pprzlink/dl_protocol.h"
36#if (SYS_TIME_NB_TIMER >= SCHAR_MAX)
37 WARNING(
"Too many sys timers (SYS_TIME_NB_TIMER >= SCHAR_MAX). Consider increasing the size of tid_d type (currently int8_t")
45#ifndef SYS_TIME_GPS_SYNC_TIMEOUT
46#define SYS_TIME_GPS_SYNC_TIMEOUT 10000
50#ifndef SYS_TIME_RTC_SYNC_PERIOD
51#define SYS_TIME_RTC_SYNC_PERIOD 5000
56#ifndef SYS_TIME_GROUND_SYNC_THRESHOLD
57#define SYS_TIME_GROUND_SYNC_THRESHOLD 200
60#define SYS_TIME_GPS_LEAP_MS ((uint64_t)SYS_TIME_GPS_LEAP_SECONDS * 1000)
69 return (wraps << 32) + ms;
150 if (!use_unix && !use_tow) {
223 sys_time_sync_unix(pprzlink_get_DL_TIME_SYNC_unix_time(buf), pprzlink_get_DL_TIME_SYNC_unix_ms(buf),
232static void cmd_sys_time(
shell_stream_t *sh,
int argc,
const char *
const argv[])
236 chprintf(sh,
"Usage: sys_time\r\n");
240 chprintf(sh,
"Current sys_time\r\n");
249 uint32_t rtc_m = (rtc_ms % 3600000) / 60000;
250 float rtc_s = (rtc_ms % 60000) / 1000.0;
251 chprintf(sh,
" rtc_time: %02u:%02u:%02.03f\r\n", rtc_h, rtc_m, rtc_s);
253 chprintf(sh,
"Registered timers\r\n");
256 chprintf(sh,
"id: %d, duration: %lu, end_time: %lu, cb: 0x%lx\r\n", i,
uint32_t sys_time_rtc_millisecond()
Get the RTC time in milliseconds since midnight.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
void sys_time_rtc_set(uint32_t unix_time, uint16_t unix_ms)
Set the RTC to the UTC unix time (architecture specific, may do nothing).
void sys_time_arch_init(void)
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
void chprintf(BaseSequentialStream *lchp, const char *fmt,...)
Arch independent mcu ( Micro Controller Unit ) utilities.
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
Mini printf-like functionality.
void shell_add_entry(char *cmd_name, shell_cmd_t *cmd)
Add dynamic entry.
BaseSequentialStream shell_stream_t
#define SYS_TIME_GPS_SYNC_TIMEOUT
Time without GPS sync in ms before the ground time is used.
void sys_time_parse_TIME_SYNC(uint8_t *buf)
Parse the TIME_SYNC datalink message (ground time).
void sys_time_update_timer(tid_t id, float duration)
Update the duration until a timer elapses.
tid_t sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
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.
static uint64_t msec_of_stamp(uint32_t stamp, uint64_t *now)
uptime_msec() at the moment of the timestamp in usec the usec stamp wraps after ~71 min,...
static void set_unix(uint64_t unix_ms, uint64_t msec, uint64_t now)
void sys_time_sync_gps(uint16_t week, uint32_t tow, uint32_t stamp)
Synchronize the time with a GPS measurement (has priority over the ground).
uint64_t get_sys_time_unix_msec(void)
Get the UTC unix time in milliseconds.
void sys_time_cancel_timer(tid_t id)
Cancel a system timer by id.
uint32_t get_sys_time_tow(void)
Get the GPS time of week in milliseconds.
#define SYS_TIME_GPS_LEAP_MS
#define SYS_TIME_RTC_SYNC_PERIOD
Period of the RTC updates in ms.
static void set_tow(uint32_t tow, uint64_t msec)
void sys_time_sync_unix(uint32_t unix_time, uint16_t unix_ms, uint32_t stamp)
Synchronize the time with the ground, only used without recent GPS sync.
uint32_t get_sys_time_unix(void)
Get the UTC unix time in seconds.
static uint64_t uptime_msec(void)
Milliseconds since startup without the ~49.7 days wrap of get_sys_time_msec() nb_sec gives the number...
#define SYS_TIME_GROUND_SYNC_THRESHOLD
Minimum difference in ms before the ground time is applied again, avoids steps (also backwards) from ...
Architecture independent timing functions.
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
uint32_t ticks_per_sec
sys_time ticks per second (SYS_TIME_FREQUENCY)
#define SYS_TIME_FREQUENCY
(Default) sys_time timer frequency in Hz.
uint64_t ground_offset
last applied ground UTC unix time at startup in ms
static uint32_t sys_time_ticks_of_sec(float seconds)
volatile uint32_t nb_sec
full seconds since startup
bool ground_synced
a ground time was applied
void(* sys_time_cb)(uint8_t id)
uint32_t duration
in SYS_TIME_TICKS
uint32_t tow_offset
GPS time of week at startup in ms (0 until synced).
uint32_t gps_tow_sync
uptime in ms of the last GPS time of week sync
uint64_t unix_offset
UTC unix time at startup in ms.
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
float resolution
sys_time_timer resolution in seconds
#define SYS_TIME_MSEC_PER_WEEK
bool gps_unix_synced
unix time synced with a GPS
#define SYS_TIME_NB_TIMER
uint32_t rtc_sync
uptime in ms of the last RTC update
uint32_t end_time
in SYS_TIME_TICKS
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
bool gps_tow_synced
time of week synced with a GPS
#define SYS_TIME_UNIX_GPS_EPOCH_MS
unix time of the GPS epoch (1980-01-06) in ms
uint32_t gps_unix_sync
uptime in ms of the last GPS unix time sync
bool unix_synced
unix time synced (GPS or ground)
int8_t tid_t
sys_time timer id type
static const float offset[]
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned long long uint64_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.