diff --git a/conf/autopilot/subsystems/fixedwing/autopilot.makefile b/conf/autopilot/subsystems/fixedwing/autopilot.makefile index b3a2ae5f62..3a398196d5 100644 --- a/conf/autopilot/subsystems/fixedwing/autopilot.makefile +++ b/conf/autopilot/subsystems/fixedwing/autopilot.makefile @@ -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 # diff --git a/sw/airborne/firmwares/fixedwing/main_ap.c b/sw/airborne/firmwares/fixedwing/main_ap.c index 2a613b27da..7decc8025d 100644 --- a/sw/airborne/firmwares/fixedwing/main_ap.c +++ b/sw/airborne/firmwares/fixedwing/main_ap.c @@ -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 ***************/ diff --git a/sw/airborne/firmwares/fixedwing/main_fbw.c b/sw/airborne/firmwares/fixedwing/main_fbw.c index 914bb01951..23555723af 100644 --- a/sw/airborne/firmwares/fixedwing/main_fbw.c +++ b/sw/airborne/firmwares/fixedwing/main_fbw.c @@ -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(); diff --git a/sw/airborne/firmwares/motor_bench/main_motor_bench.c b/sw/airborne/firmwares/motor_bench/main_motor_bench.c index 44e4999e35..3b3afe9f38 100644 --- a/sw/airborne/firmwares/motor_bench/main_motor_bench.c +++ b/sw/airborne/firmwares/motor_bench/main_motor_bench.c @@ -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 diff --git a/sw/airborne/firmwares/motor_bench/main_turntable.c b/sw/airborne/firmwares/motor_bench/main_turntable.c index 0b5ad71a2d..576a9583f1 100644 --- a/sw/airborne/firmwares/motor_bench/main_turntable.c +++ b/sw/airborne/firmwares/motor_bench/main_turntable.c @@ -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(); } diff --git a/sw/airborne/firmwares/rotorcraft/main.c b/sw/airborne/firmwares/rotorcraft/main.c index 3893773dd3..26f0336067 100644 --- a/sw/airborne/firmwares/rotorcraft/main.c +++ b/sw/airborne/firmwares/rotorcraft/main.c @@ -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(); diff --git a/sw/airborne/firmwares/tutorial/main_demo2.c b/sw/airborne/firmwares/tutorial/main_demo2.c index e69b857d7d..23950fb208 100644 --- a/sw/airborne/firmwares/tutorial/main_demo2.c +++ b/sw/airborne/firmwares/tutorial/main_demo2.c @@ -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 ) { diff --git a/sw/airborne/firmwares/tutorial/main_demo3.c b/sw/airborne/firmwares/tutorial/main_demo3.c index a2eb80c7f1..47527879d3 100644 --- a/sw/airborne/firmwares/tutorial/main_demo3.c +++ b/sw/airborne/firmwares/tutorial/main_demo3.c @@ -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(); } diff --git a/sw/airborne/firmwares/tutorial/main_demo4.c b/sw/airborne/firmwares/tutorial/main_demo4.c index 5194eef92a..b0a91d0d8d 100644 --- a/sw/airborne/firmwares/tutorial/main_demo4.c +++ b/sw/airborne/firmwares/tutorial/main_demo4.c @@ -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(); } diff --git a/sw/airborne/firmwares/tutorial/main_demo5.c b/sw/airborne/firmwares/tutorial/main_demo5.c index eab0c4c31a..fe85cd36e0 100644 --- a/sw/airborne/firmwares/tutorial/main_demo5.c +++ b/sw/airborne/firmwares/tutorial/main_demo5.c @@ -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(); } diff --git a/sw/airborne/firmwares/tutorial/main_demo6.c b/sw/airborne/firmwares/tutorial/main_demo6.c index 8f950e8c7f..7703f13f1c 100644 --- a/sw/airborne/firmwares/tutorial/main_demo6.c +++ b/sw/airborne/firmwares/tutorial/main_demo6.c @@ -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(); } diff --git a/sw/airborne/lisa/test_uart_lisam.c b/sw/airborne/lisa/test_uart_lisam.c index 664f3efe83..5ef4c0dad9 100644 --- a/sw/airborne/lisa/test_uart_lisam.c +++ b/sw/airborne/lisa/test_uart_lisam.c @@ -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 ) { diff --git a/sw/airborne/lisa/tunnel_hw.c b/sw/airborne/lisa/tunnel_hw.c index 0cd55fd838..6f9df43608 100644 --- a/sw/airborne/lisa/tunnel_hw.c +++ b/sw/airborne/lisa/tunnel_hw.c @@ -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; } diff --git a/sw/airborne/mcu_periph/sys_time.c b/sw/airborne/mcu_periph/sys_time.c index 9e5b4e49fa..944c4b2784 100644 --- a/sw/airborne/mcu_periph/sys_time.c +++ b/sw/airborne/mcu_periph/sys_time.c @@ -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(); } diff --git a/sw/airborne/mcu_periph/sys_time.h b/sw/airborne/mcu_periph/sys_time.h index 3d0865b914..2c0993175d 100644 --- a/sw/airborne/mcu_periph/sys_time.h +++ b/sw/airborne/mcu_periph/sys_time.h @@ -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) diff --git a/sw/airborne/test/mcu_periph/test_sys_time.c b/sw/airborne/test/mcu_periph/test_sys_time.c index ec1b48d8bc..95147a13cc 100644 --- a/sw/airborne/test/mcu_periph/test_sys_time.c +++ b/sw/airborne/test/mcu_periph/test_sys_time.c @@ -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))