Skip to content

Commit

Permalink
fix log time overflow (#3056)
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoudSmeur committed Sep 7, 2023
1 parent 63fd015 commit 13f2d21
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 2 deletions.
16 changes: 16 additions & 0 deletions sw/airborne/arch/chibios/mcu_periph/sys_time_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ uint32_t get_sys_time_usec(void)
return t;
}

/**
* Get the time in microseconds divided by 100 since startup.
* WARNING: overflows after 7000min!
* @return 100microseconds since startup as uint32_t
*/
uint32_t get_sys_time_usec100(void)
{
chMtxLock(&sys_time_mtx);
uint32_t current = chSysGetRealtimeCounterX();
uint32_t t = sys_time.nb_sec * 10000 +
TIME_I2US(sys_time.nb_sec_rem)/100 +
RTC2US(STM32_SYSCLK, current - cpu_counter)/100;
chMtxUnlock(&sys_time_mtx);
return t;
}

uint32_t get_sys_time_msec(void)
{
chMtxLock(&sys_time_mtx);
Expand Down
1 change: 1 addition & 0 deletions sw/airborne/arch/chibios/mcu_periph/sys_time_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#endif

extern uint32_t get_sys_time_usec(void);
extern uint32_t get_sys_time_usec100(void);
extern uint32_t get_sys_time_msec(void);
extern void sys_time_usleep(uint32_t us);
extern void sys_time_msleep(uint16_t ms);
Expand Down
23 changes: 23 additions & 0 deletions sw/airborne/arch/linux/mcu_periph/sys_time_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,29 @@ uint32_t get_sys_time_usec(void)
return d_sec * 1000000 + d_nsec / 1000;
}

/**
* Get the time in 100microseconds since startup.
* WARNING: overflows after 7000 minutes!
* @return current system time as uint32_t
*/
uint32_t get_sys_time_usec100(void)
{
/* get current time */
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);

/* time difference to startup */
time_t d_sec = now.tv_sec - startup_time.tv_sec;
long d_nsec = now.tv_nsec - startup_time.tv_nsec;

/* wrap if negative nanoseconds */
if (d_nsec < 0) {
d_sec -= 1;
d_nsec += 1000000000L;
}
return d_sec * 10000 + d_nsec / 100000;
}

/**
* Get the time in milliseconds since startup.
* @return milliseconds since startup as uint32_t
Expand Down
7 changes: 7 additions & 0 deletions sw/airborne/arch/linux/mcu_periph/sys_time_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
*/
extern uint32_t get_sys_time_usec(void);

/**
* Get the time in microseconds since startup.
* WARNING: overflows after 7000 minutes!
* @return current system time as uint32_t
*/
extern uint32_t get_sys_time_usec100(void);

/**
* Get the time in milliseconds since startup.
* @return milliseconds since startup as uint32_t
Expand Down
10 changes: 10 additions & 0 deletions sw/airborne/arch/sim/mcu_periph/sys_time_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ static inline uint32_t get_sys_time_usec(void)
usec_of_cpu_ticks(sys_time.nb_sec_rem);
}

/**
* Get the time in 100microseconds since startup.
* @return 100microseconds since startup as uint32_t
*/
static inline uint32_t get_sys_time_usec100(void)
{
return sys_time.nb_sec * 10000 +
usec_of_cpu_ticks(sys_time.nb_sec_rem)/100;
}

/**
* Get the time in milliseconds since startup.
* @return milliseconds since startup as uint32_t
Expand Down
12 changes: 12 additions & 0 deletions sw/airborne/arch/stm32/mcu_periph/sys_time_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ static inline uint32_t get_sys_time_usec(void)
usec_of_cpu_ticks(systick_get_reload() - systick_get_value());
}

/**
* Get the time in 100microseconds since startup.
* WARNING: overflows after 7000min!
* @return 100microseconds since startup as uint32_t
*/
static inline uint32_t get_sys_time_usec100(void)
{
return sys_time.nb_sec * 10000 +
usec_of_cpu_ticks(sys_time.nb_sec_rem)/100 +
usec_of_cpu_ticks(systick_get_reload() - systick_get_value())/100;
}

/**
* Get the time in milliseconds since startup.
* @return milliseconds since startup as uint32_t
Expand Down
2 changes: 1 addition & 1 deletion sw/airborne/modules/loggers/pprzlog_tp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct pprzlog_transport pprzlog_tp;

void pprzlog_tp_init(void)
{
pprzlog_transport_init(&pprzlog_tp, get_sys_time_usec);
pprzlog_transport_init(&pprzlog_tp, get_sys_time_usec100);
}


0 comments on commit 13f2d21

Please sign in to comment.