Skip to content

Commit

Permalink
[sys_time] replace SYS_TIME_RESOLUTION with SYS_TIME_FREQUENCY
Browse files Browse the repository at this point in the history
  • Loading branch information
flixr committed Feb 26, 2013
1 parent 523e2da commit 70fc079
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 26 deletions.
6 changes: 1 addition & 5 deletions sw/airborne/arch/lpc21/mcu_periph/sys_time_arch.c
Expand Up @@ -105,11 +105,7 @@
void sys_time_arch_init( void ) {
sys_time.cpu_ticks_per_sec = PCLK / T0_PCLK_DIV;
/* cpu ticks per desired sys_time timer step */
sys_time.resolution_cpu_ticks = (uint32_t)(sys_time.resolution_sec * sys_time.cpu_ticks_per_sec + 0.5);

/* set final sys_time resolution in seconds from resolution in cpu_ticks */
sys_time.resolution_sec = (float)sys_time.resolution_cpu_ticks / sys_time.cpu_ticks_per_sec;
sys_time.ticks_per_sec = (uint32_t)(sys_time.cpu_ticks_per_sec / sys_time.resolution_cpu_ticks + 0.5);
sys_time.resolution_cpu_ticks = (uint32_t)(sys_time.resolution * sys_time.cpu_ticks_per_sec + 0.5);

/* setup Timer 0 to count forever */
/* reset & disable timer 0 */
Expand Down
2 changes: 1 addition & 1 deletion sw/airborne/arch/sim/mcu_periph/sys_time_arch.c
Expand Up @@ -31,7 +31,7 @@
void sys_time_arch_init( void ) {
// simulate 1us cpu ticks
sys_time.cpu_ticks_per_sec = 1e6;
sys_time.resolution_cpu_ticks = (uint32_t)(sys_time.resolution_sec * sys_time.cpu_ticks_per_sec + 0.5);
sys_time.resolution_cpu_ticks = (uint32_t)(sys_time.resolution * sys_time.cpu_ticks_per_sec + 0.5);
}

void sys_tick_handler( void ) {
Expand Down
6 changes: 1 addition & 5 deletions sw/airborne/arch/stm32/mcu_periph/sys_time_arch.c
Expand Up @@ -44,11 +44,7 @@ void sys_time_arch_init( void ) {
sys_time.cpu_ticks_per_sec = AHB_CLK;

/* cpu ticks per desired sys_time timer step */
sys_time.resolution_cpu_ticks = (uint32_t)(sys_time.resolution_sec * sys_time.cpu_ticks_per_sec + 0.5);

/* set final sys_time resolution in seconds from resolution in cpu_ticks */
sys_time.resolution_sec = (float)sys_time.resolution_cpu_ticks / sys_time.cpu_ticks_per_sec;
sys_time.ticks_per_sec = (uint32_t)(sys_time.cpu_ticks_per_sec / sys_time.resolution_cpu_ticks + 0.5);
sys_time.resolution_cpu_ticks = (uint32_t)(sys_time.resolution * sys_time.cpu_ticks_per_sec + 0.5);

/* The timer interrupt is activated on the transition from 1 to 0,
* therefore it activates every n+1 clock ticks.
Expand Down
8 changes: 4 additions & 4 deletions sw/airborne/mcu_periph/sys_time.c
Expand Up @@ -30,9 +30,9 @@
#include "mcu_periph/sys_time.h"
#include "mcu.h"

struct sys_time sys_time;
PRINT_CONFIG_VAR(SYS_TIME_FREQUENCY)

PRINT_CONFIG_VAR(SYS_TIME_RESOLUTION)
struct sys_time sys_time;

int sys_time_register_timer(float duration, sys_time_cb cb) {

Expand Down Expand Up @@ -72,8 +72,8 @@ void sys_time_init( void ) {
sys_time.nb_sec_rem = 0;
sys_time.nb_tick = 0;

sys_time.resolution_sec = SYS_TIME_RESOLUTION;
sys_time.ticks_per_sec = 1.0 / sys_time.resolution_sec;
sys_time.ticks_per_sec = SYS_TIME_FREQUENCY;
sys_time.resolution = 1.0 / sys_time.ticks_per_sec;

for (unsigned int i=0; i<SYS_TIME_NB_TIMER; i++) {
sys_time.timer[i].in_use = FALSE;
Expand Down
18 changes: 11 additions & 7 deletions sw/airborne/mcu_periph/sys_time.h
Expand Up @@ -40,12 +40,16 @@
#define SYS_TIME_NB_TIMER 8
#endif

/** system time resolution in seconds */
#ifndef SYS_TIME_RESOLUTION

/**
* (Default) sys_time timer frequency in Hz.
* sys_time.resolution is set from this define.
*/
#ifndef SYS_TIME_FREQUENCY
#if defined PERIODIC_FREQUENCY
#define SYS_TIME_RESOLUTION ( 1./(2*PERIODIC_FREQUENCY) )
#define SYS_TIME_FREQUENCY (2 * PERIODIC_FREQUENCY)
#else
#define SYS_TIME_RESOLUTION ( 1./1000. )
#define SYS_TIME_FREQUENCY 1000
#endif
#endif

Expand All @@ -67,9 +71,9 @@ struct sys_time {
volatile uint32_t nb_tick; ///< SYS_TIME_TICKS since startup
struct sys_time_timer timer[SYS_TIME_NB_TIMER];

float resolution_sec; ///< sys_time_timer resolution in seconds
float resolution; ///< sys_time_timer resolution in seconds
uint32_t ticks_per_sec; ///< sys_time ticks per second (SYS_TIME_FREQUENCY)
uint32_t resolution_cpu_ticks; ///< sys_time_timer resolution in cpu ticks
uint32_t ticks_per_sec; ///< sys_time ticks per second
uint32_t cpu_ticks_per_sec; ///< cpu ticks per second
};

Expand Down Expand Up @@ -137,7 +141,7 @@ static inline uint32_t sys_time_ticks_of_usec(uint32_t usec) {
}

static inline float sec_of_sys_time_ticks(uint32_t ticks) {
return (float)ticks * sys_time.resolution_sec;
return (float)ticks * sys_time.resolution;
}

static inline uint32_t msec_of_sys_time_ticks(uint32_t ticks) {
Expand Down
2 changes: 1 addition & 1 deletion sw/airborne/subsystems/gps.h
Expand Up @@ -135,7 +135,7 @@ extern struct GpsTimeSync gps_time_sync;

/**
* Convert time in sys_time ticks to GPS time of week.
* The resolution depends on #SYS_TIME_RESOLUTION
* The resolution is sys_time.resolution
* @return GPS tow in ms
*/
extern uint32_t gps_tow_from_sys_ticks(uint32_t sys_ticks);
Expand Down
2 changes: 1 addition & 1 deletion sw/simulator/nps/nps_main.c
Expand Up @@ -36,7 +36,7 @@
#include "nps_flightgear.h"

#include "mcu_periph/sys_time.h"
#define SIM_DT (SYS_TIME_RESOLUTION)
#define SIM_DT (1./SYS_TIME_FREQUENCY)
#define DISPLAY_DT (1./30.)
#define HOST_TIMEOUT_MS 40
#define HOST_TIME_FACTOR 1.
Expand Down
2 changes: 1 addition & 1 deletion sw/simulator/sim_ac_jsbsim.c
Expand Up @@ -78,7 +78,7 @@ static void sim_init(void) {
// main AP init (feed the sensors once before ?)
sim_autopilot_init();

printf("sys_time resolution: %f\n", SYS_TIME_RESOLUTION);
printf("sys_time frequency: %f\n", SYS_TIME_FREQUENCY);
printf("sys_time period in msec: %d\n", SYSTIME_PERIOD);

}
Expand Down
2 changes: 1 addition & 1 deletion sw/simulator/sim_ac_jsbsim.h
Expand Up @@ -42,7 +42,7 @@
#endif
#define DT (JSBSIM_PERIOD*1e-3) ///< JSBSim timestep in seconds

#define SYSTIME_PERIOD ((uint32_t)(SYS_TIME_RESOLUTION * 1000)) ///< in msec
#define SYSTIME_PERIOD ((uint32_t)(1000. / SYS_TIME_FREQUENCY)) ///< in msec

#define RAD2DEG 57.29578
#define FT2M 0.3048
Expand Down

0 comments on commit 70fc079

Please sign in to comment.