sys_time: specify duration in seconds instead of SYS_TIME_TICKS when registering or updating a timer

This commit is contained in:
Felix Ruess
2012-01-27 21:52:52 +01:00
parent 5f511e0eee
commit 7412e35a04
16 changed files with 46 additions and 28 deletions
@@ -70,7 +70,7 @@ endif
ifndef PERIODIC_FREQUENCY
PERIODIC_FREQUENCY = 60
endif
$(TARGET).CFLAGS += -DPERIODIC_TASK_PERIOD='CPU_TICKS_OF_SEC((1./$(PERIODIC_FREQUENCY).))' -DPERIODIC_FREQUENCY=$(PERIODIC_FREQUENCY)
$(TARGET).CFLAGS += -DPERIODIC_FREQUENCY=$(PERIODIC_FREQUENCY)
$(TARGET).srcs += mcu_periph/sys_time.c $(SRC_ARCH)/mcu_periph/sys_time_arch.c
#
+1 -1
View File
@@ -528,7 +528,7 @@ void periodic_task_ap( void ) {
void init_ap( void ) {
#ifndef SINGLE_MCU /** init done in main_fbw in single MCU */
mcu_init();
sys_time_register_timer(SYS_TIME_TIMER_S(1./PERIODIC_FREQUENCY), NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
#endif /* SINGLE_MCU */
/************* Sensors initialization ***************/
+1 -1
View File
@@ -61,7 +61,7 @@ void init_fbw( void ) {
mcu_init();
sys_time_register_timer(SYS_TIME_TIMER_S(1./PERIODIC_FREQUENCY), NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
electrical_init();
@@ -41,7 +41,7 @@ int main( void ) {
static inline void main_init( void ) {
mcu_init();
sys_time_register_timer(PERIODIC_TASK_PERIOD, NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
mb_tacho_init();
#if defined USE_TWI_CONTROLLER
@@ -35,7 +35,7 @@ int main( void ) {
static inline void main_init( void ) {
mcu_init();
sys_time_register_timer(PERIODIC_TASK_PERIOD, NULL);
sys_time_register_timer(1./PERIODIC_FREQUENCY, NULL);
main_init_tacho();
mcu_int_enable();
}
+1 -1
View File
@@ -87,7 +87,7 @@ STATIC_INLINE void main_init( void ) {
mcu_init();
sys_time_register_timer(SYS_TIME_TIMER_S(1./PERIODIC_FREQUENCY), NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
electrical_init();
+1 -1
View File
@@ -18,7 +18,7 @@ int main( void ) {
static inline void main_init( void ) {
mcu_init();
sys_time_register_timer(PERIODIC_TASK_PERIOD, NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
}
static inline void main_periodic_task( void ) {
+1 -1
View File
@@ -20,7 +20,7 @@ int main( void ) {
static inline void main_init( void ) {
mcu_init();
sys_time_register_timer(PERIODIC_TASK_PERIOD, NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
uart0_init_tx();
mcu_int_enable();
}
+1 -1
View File
@@ -21,7 +21,7 @@ int main( void ) {
static inline void main_init( void ) {
mcu_init();
sys_time_register_timer(PERIODIC_TASK_PERIOD, NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
uart0_init_tx();
mcu_int_enable();
}
+1 -1
View File
@@ -25,7 +25,7 @@ int main( void ) {
static inline void main_init( void ) {
mcu_init();
sys_time_register_timer(PERIODIC_TASK_PERIOD, NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
uart0_init_tx();
mcu_int_enable();
}
+1 -1
View File
@@ -22,7 +22,7 @@ int main( void ) {
static inline void main_init( void ) {
mcu_init();
sys_time_register_timer(PERIODIC_TASK_PERIOD, NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
usb_serial_init();
mcu_int_enable();
}
+4 -2
View File
@@ -31,6 +31,7 @@
#include "mcu.h"
#include "mcu_periph/uart.h"
#include "mcu_periph/sys_time.h"
#include "led.h"
static inline void main_init( void );
static inline void main_periodic( void );
@@ -40,15 +41,16 @@ int main(void) {
main_init();
while (1) {
if (sys_time_periodic())
if (sys_time_check_and_ack_timer(0))
main_periodic();
}
return 0;
}
static inline void main_init( void ) {
mcu_init();
sys_time_init();
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
}
static inline void main_periodic( void ) {
+3 -2
View File
@@ -53,7 +53,7 @@ void Delay(__IO uint32_t nCount) {
int main(void) {
mcu_init();
sys_time_register_timer(SYS_TIME_TIMER_S(1./512.), NULL);
sys_time_register_timer((1./PERIODIC_FREQUENCY), NULL);
/* init RCC */
RCC_APB2PeriphClockCmd(A_PERIPH , ENABLE);
@@ -83,7 +83,8 @@ int main(void) {
if (sys_time_check_and_ack_timer(0))
main_periodic();
main_event();
};
}
return 0;
}
+6 -6
View File
@@ -32,15 +32,15 @@
struct sys_time sys_time;
uint8_t sys_time_register_timer(uint32_t duration, sys_time_cb cb) {
uint8_t sys_time_register_timer(float duration, sys_time_cb cb) {
uint32_t start_time = sys_time.nb_tick;
for (int i = 0; i< SYS_TIME_NB_TIMER; i++) {
if (!sys_time.timer[i].in_use) {
sys_time.timer[i].cb = cb;
sys_time.timer[i].elapsed = FALSE;
sys_time.timer[i].end_time = start_time + duration;
sys_time.timer[i].duration = duration;
sys_time.timer[i].end_time = start_time + SYS_TIME_TICKS_OF_SEC(duration);
sys_time.timer[i].duration = SYS_TIME_TICKS_OF_SEC(duration);
sys_time.timer[i].in_use = TRUE;
return i;
}
@@ -59,10 +59,10 @@ void sys_time_cancel_timer(uint8_t id) {
}
// FIXME: race condition ??
void sys_time_update_timer(uint8_t id, uint32_t duration) {
void sys_time_update_timer(uint8_t id, float duration) {
mcu_int_disable();
sys_time.timer[id].end_time -= (sys_time.timer[id].duration - duration);
sys_time.timer[id].duration = duration;
sys_time.timer[id].end_time -= (sys_time.timer[id].duration - SYS_TIME_TICKS_OF_SEC(duration));
sys_time.timer[id].duration = SYS_TIME_TICKS_OF_SEC(duration);
mcu_int_enable();
}
+19 -4
View File
@@ -64,9 +64,26 @@ extern struct sys_time sys_time;
#define cpu_time_ticks sys_time.nb_sec_rem
extern void sys_time_init(void);
extern uint8_t sys_time_register_timer(uint32_t duration, sys_time_cb cb);
/**
* Register a new system timer.
* @param duration Duration in seconds until the timer elapses.
* @param cb Callback function that is called from the ISR when timer elapses, or NULL
*/
extern uint8_t sys_time_register_timer(float duration, sys_time_cb cb);
/**
* Cancel a system timer by id.
* @param id Timer id.
*/
extern void sys_time_cancel_timer(uint8_t id);
extern void sys_time_update_timer(uint8_t id, uint32_t duration);
/**
* Update the duration until a timer elapses.
* @param id Timer id
* @param duration Duration in seconds until the timer elapses.
*/
extern void sys_time_update_timer(uint8_t id, float duration);
static inline bool_t sys_time_check_and_ack_timer( uint8_t id ) {
if (sys_time.timer[id].elapsed) {
@@ -94,8 +111,6 @@ static inline bool_t sys_time_check_and_ack_timer( uint8_t id ) {
#endif
#define SYS_TIME_RESOLUTION_CPU_TICKS CPU_TICKS_OF_SEC(SYS_TIME_RESOLUTION)
#define SYS_TIME_TIMER_S(_s) ((_s)/SYS_TIME_RESOLUTION)
#define SYS_TIME_TICKS_OF_SEC(s) (uint32_t)((s) / SYS_TIME_RESOLUTION + 0.5)
#define SYS_TIME_TICKS_OF_USEC(us) SYS_TIME_TICKS_OF_SEC((us) * 1e-6)
#define SYS_TIME_TICKS_OF_NSEC(ns) SYS_TIME_TICKS_OF_SEC((ns) * 1e-9)
+3 -3
View File
@@ -11,9 +11,9 @@ static inline void main_event( void );
int main(void) {
mcu_init();
unsigned int tmr_02 = sys_time_register_timer(SYS_TIME_TIMER_S(0.2), NULL);
unsigned int tmr_03 = sys_time_register_timer(SYS_TIME_TIMER_S(0.3), NULL);
sys_time_register_timer(SYS_TIME_TIMER_S(0.5), main_periodic_05);
unsigned int tmr_02 = sys_time_register_timer(0.2, NULL);
unsigned int tmr_03 = sys_time_register_timer(0.3, NULL);
sys_time_register_timer(0.5, main_periodic_05);
while(1) {
if (sys_time_check_and_ack_timer(tmr_02))