Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
sys_time.c File Reference

Architecture independent timing functions. More...

#include "mcu_periph/sys_time.h"
#include "mcu.h"
#include "pprzlink/dl_protocol.h"
#include "limits.h"
Include dependency graph for sys_time.c:

Go to the source code of this file.

Macros

#define SYS_TIME_GPS_SYNC_TIMEOUT   10000
 Time without GPS sync in ms before the ground time is used.
#define SYS_TIME_RTC_SYNC_PERIOD   5000
 Period of the RTC updates in ms.
#define SYS_TIME_GROUND_SYNC_THRESHOLD   200
 Minimum difference in ms before the ground time is applied again, avoids steps (also backwards) from the round trip jitter of the link.
#define SYS_TIME_GPS_LEAP_MS   ((uint64_t)SYS_TIME_GPS_LEAP_SECONDS * 1000)

Functions

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 of wraps, get_sys_time_msec() the exact time.
uint32_t get_sys_time_tow (void)
 Get the GPS time of week in milliseconds.
uint64_t get_sys_time_unix_msec (void)
 Get the UTC unix time in milliseconds.
uint32_t get_sys_time_unix (void)
 Get the UTC unix time in seconds.
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, so only use its age with respect to now
static void set_tow (uint32_t tow, uint64_t msec)
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).
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.
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.
void sys_time_cancel_timer (tid_t id)
 Cancel a system timer by id.
void sys_time_update_timer (tid_t id, float duration)
 Update the duration until a timer elapses.
void sys_time_parse_TIME_SYNC (uint8_t *buf)
 Parse the TIME_SYNC datalink message (ground time).
void sys_time_init (void)

Variables

struct sys_time sys_time

Detailed Description

Architecture independent timing functions.

Definition in file sys_time.c.

Macro Definition Documentation

◆ SYS_TIME_GPS_LEAP_MS

#define SYS_TIME_GPS_LEAP_MS   ((uint64_t)SYS_TIME_GPS_LEAP_SECONDS * 1000)

Definition at line 60 of file sys_time.c.

Referenced by sys_time_sync_gps(), and sys_time_sync_unix().

◆ SYS_TIME_GPS_SYNC_TIMEOUT

#define SYS_TIME_GPS_SYNC_TIMEOUT   10000

Time without GPS sync in ms before the ground time is used.

Definition at line 46 of file sys_time.c.

Referenced by sys_time_sync_unix().

◆ SYS_TIME_GROUND_SYNC_THRESHOLD

#define SYS_TIME_GROUND_SYNC_THRESHOLD   200

Minimum difference in ms before the ground time is applied again, avoids steps (also backwards) from the round trip jitter of the link.

Definition at line 57 of file sys_time.c.

Referenced by sys_time_sync_unix().

◆ SYS_TIME_RTC_SYNC_PERIOD

#define SYS_TIME_RTC_SYNC_PERIOD   5000

Period of the RTC updates in ms.

Definition at line 51 of file sys_time.c.

Referenced by set_unix().

Function Documentation

◆ get_sys_time_tow()

uint32_t get_sys_time_tow ( void )

Get the GPS time of week in milliseconds.

Synced from the GPS, or from the ground (TIME_SYNC) without GPS. Until synced this is the time since startup.

Returns
GPS time of week in milliseconds

Definition at line 72 of file sys_time.c.

References SYS_TIME_MSEC_PER_WEEK, sys_time::tow_offset, and uptime_msec().

Referenced by gps_tow_from_sys_ticks().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_sys_time_unix()

uint32_t get_sys_time_unix ( void )

Get the UTC unix time in seconds.

Synced from the GPS, or from the ground (TIME_SYNC) without GPS.

Returns
unix time in seconds, 0 until synced

Definition at line 87 of file sys_time.c.

References get_sys_time_unix_msec().

Here is the call graph for this function:

◆ get_sys_time_unix_msec()

uint64_t get_sys_time_unix_msec ( void )

Get the UTC unix time in milliseconds.

Returns
unix time in milliseconds, 0 until synced

Definition at line 77 of file sys_time.c.

References sys_time::unix_offset, sys_time::unix_synced, and uptime_msec().

Referenced by get_sys_time_unix().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ msec_of_stamp()

uint64_t msec_of_stamp ( uint32_t stamp,
uint64_t * now )
static

uptime_msec() at the moment of the timestamp in usec the usec stamp wraps after ~71 min, so only use its age with respect to now

Definition at line 94 of file sys_time.c.

References get_sys_time_usec(), and uptime_msec().

Referenced by sys_time_sync_gps(), and sys_time_sync_unix().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_tow()

void set_tow ( uint32_t tow,
uint64_t msec )
static

Definition at line 101 of file sys_time.c.

References SYS_TIME_MSEC_PER_WEEK, and sys_time::tow_offset.

Referenced by sys_time_sync_gps(), and sys_time_sync_unix().

Here is the caller graph for this function:

◆ set_unix()

void set_unix ( uint64_t unix_ms,
uint64_t msec,
uint64_t now )
static

Definition at line 107 of file sys_time.c.

References sys_time::rtc_sync, sys_time_rtc_set(), SYS_TIME_RTC_SYNC_PERIOD, sys_time::unix_offset, and sys_time::unix_synced.

Referenced by sys_time_sync_gps(), and sys_time_sync_unix().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sys_time_cancel_timer()

void sys_time_cancel_timer ( tid_t id)

Cancel a system timer by id.

Parameters
idTimer id.

Definition at line 205 of file sys_time.c.

References sys_time_timer::cb, sys_time_timer::duration, sys_time_timer::elapsed, sys_time_timer::end_time, sys_time_timer::in_use, and sys_time::timer.

Referenced by mavlink_mission_cancel_timer(), and px4flash_event().

Here is the caller graph for this function:

◆ sys_time_init()

◆ sys_time_parse_TIME_SYNC()

void sys_time_parse_TIME_SYNC ( uint8_t * buf)

Parse the TIME_SYNC datalink message (ground time).

Definition at line 221 of file sys_time.c.

References get_sys_time_usec(), and sys_time_sync_unix().

Here is the call graph for this function:

◆ sys_time_register_timer()

tid_t sys_time_register_timer ( float duration,
sys_time_cb cb )

Register a new system timer.

Parameters
durationDuration in seconds until the timer elapses.
cbCallback function that is called from the ISR when timer elapses, or NULL
Returns
-1 if it failed, the timer id otherwise

Definition at line 172 of file sys_time.c.

References sys_time_timer::cb, sys_time_timer::duration, sys_time_timer::elapsed, sys_time_timer::end_time, sys_time_timer::in_use, sys_time::nb_tick, start_time, SYS_TIME_NB_TIMER, sys_time_ticks_of_sec(), and sys_time::timer.

Referenced by main_ap_init(), main_fbw_init(), main_init(), main_init(), main_init(), main_init(), main_init(), main_recovery_init(), mavlink_mission_set_timer(), and px4flash_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sys_time_register_timer_offset()

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.

Parameters
timertimer providing start time and duration
offsetoffset in seconds beetween the timers (will overlap if longer than duration)
cbCallback function that is called from the ISR when timer elapses, or NULL
Returns
-1 if it failed, the timer id otherwise

Definition at line 188 of file sys_time.c.

References sys_time_timer::cb, sys_time_timer::duration, sys_time_timer::elapsed, sys_time_timer::end_time, sys_time_timer::in_use, offset, SYS_TIME_NB_TIMER, sys_time_ticks_of_sec(), and sys_time::timer.

Referenced by main_ap_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sys_time_sync_gps()

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).

Parameters
weekGPS week, 0 if unknown (only syncs the time of week)
towGPS time of week in milliseconds
stampget_sys_time_usec() timestamp at which tow was valid

Definition at line 120 of file sys_time.c.

References sys_time::gps_tow_sync, sys_time::gps_tow_synced, sys_time::gps_unix_sync, sys_time::gps_unix_synced, msec_of_stamp(), set_tow(), set_unix(), SYS_TIME_GPS_LEAP_MS, SYS_TIME_MSEC_PER_WEEK, and SYS_TIME_UNIX_GPS_EPOCH_MS.

Referenced by gps_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sys_time_sync_unix()

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.

Parameters
unix_timeUTC unix time in seconds
unix_msmilliseconds part of the unix time
stampget_sys_time_usec() timestamp at which the time was valid

Definition at line 137 of file sys_time.c.

References sys_time::gps_tow_sync, sys_time::gps_tow_synced, sys_time::gps_unix_sync, sys_time::gps_unix_synced, sys_time::ground_offset, sys_time::ground_synced, msec_of_stamp(), offset, set_tow(), set_unix(), SYS_TIME_GPS_LEAP_MS, SYS_TIME_GPS_SYNC_TIMEOUT, SYS_TIME_GROUND_SYNC_THRESHOLD, SYS_TIME_MSEC_PER_WEEK, and SYS_TIME_UNIX_GPS_EPOCH_MS.

Referenced by sys_time_parse_TIME_SYNC().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sys_time_update_timer()

void sys_time_update_timer ( tid_t id,
float duration )

Update the duration until a timer elapses.

Parameters
idTimer id
durationDuration in seconds until the timer elapses.

Definition at line 215 of file sys_time.c.

References sys_time_timer::duration, sys_time_timer::end_time, sys_time_ticks_of_sec(), and sys_time::timer.

Referenced by mavlink_mission_set_timer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uptime_msec()

uint64_t uptime_msec ( void )
static

Milliseconds since startup without the ~49.7 days wrap of get_sys_time_msec() nb_sec gives the number of wraps, get_sys_time_msec() the exact time.

Definition at line 64 of file sys_time.c.

References get_sys_time_msec(), and sys_time::nb_sec.

Referenced by get_sys_time_tow(), get_sys_time_unix_msec(), and msec_of_stamp().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ sys_time

struct sys_time sys_time

Definition at line 42 of file sys_time.c.