From c04b8fe1c42dd36b384bdc3379734d6c8bfe7996 Mon Sep 17 00:00:00 2001 From: Jérémy Dufour Date: Sat, 14 Mar 2009 23:24:05 +0100 Subject: * digital/io/src: - clean main timer module. --- digital/io/src/Makefile | 2 +- digital/io/src/main.c | 2 +- digital/io/src/main_timer.avr.c | 58 ++++++++++++++++++++++++++++++++++ digital/io/src/main_timer.avr.h | 66 --------------------------------------- digital/io/src/main_timer.h | 69 +++++++++++++++++++++++++++++++++++++++++ digital/io/src/simu.host.h | 8 ----- 6 files changed, 129 insertions(+), 76 deletions(-) create mode 100644 digital/io/src/main_timer.avr.c delete mode 100644 digital/io/src/main_timer.avr.h create mode 100644 digital/io/src/main_timer.h diff --git a/digital/io/src/Makefile b/digital/io/src/Makefile index 37c062a2..ed386944 100644 --- a/digital/io/src/Makefile +++ b/digital/io/src/Makefile @@ -4,7 +4,7 @@ BASE = ../../avr PROGS = io # Sources to compile. io_SOURCES = main.c asserv.c servo.avr.c eeprom.avr.c trap.c sharp.c \ - switch.avr.c chrono.c \ + switch.avr.c chrono.c main_timer.avr.c \ simu.host.c # Modules needed for IO. MODULES = proto uart twi utils adc math/fixed path diff --git a/digital/io/src/main.c b/digital/io/src/main.c index 3203675d..70f21ab9 100644 --- a/digital/io/src/main.c +++ b/digital/io/src/main.c @@ -31,10 +31,10 @@ /* AVR include, non HOST */ #ifndef HOST -# include "main_timer.avr.h" # include "switch.h" /* Manage switches (jack, color selector) */ #endif /* HOST */ +#include "main_timer.h" #include "simu.host.h" #include "asserv.h" /* Functions to control the asserv board */ diff --git a/digital/io/src/main_timer.avr.c b/digital/io/src/main_timer.avr.c new file mode 100644 index 00000000..8f343403 --- /dev/null +++ b/digital/io/src/main_timer.avr.c @@ -0,0 +1,58 @@ +/* main_timer.avr.c */ +/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ + * + * Copyright (C) 2009 Dufour Jérémy + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +#include "common.h" + +#include "main_timer.h" + +#include "modules/utils/utils.h" +#include "io.h" + +void +main_timer_init (void) +{ + /* Fov = F_io / (prescaler * (TOP + 1)) + * TOP = 0xff + * prescaler = 256 + * Tov = 1 / Fov = 4.444 ms */ + /* Note: if you change this, update MT_TC0_*. */ + TCCR0 = regv (FOC0, WGM00, COM01, COM0, WGM01, CS02, CS01, CS00, + 0, 0, 0, 0, 0, 1, 1, 0); +} + +uint8_t +main_timer_wait (void) +{ + /* We have reached overflow. */ + uint8_t count_before_ov = 1; + /* Loop until an overflow of the timer occurs. */ + while (!(TIFR & _BV (TOV0))) + /* We have not reached overflow. */ + count_before_ov = 0; + /* Write 1 to clear overflow. */ + TIFR = _BV (TOV0); + + return count_before_ov; +} diff --git a/digital/io/src/main_timer.avr.h b/digital/io/src/main_timer.avr.h deleted file mode 100644 index 39cf70b5..00000000 --- a/digital/io/src/main_timer.avr.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef main_timer_avr_h -#define main_timer_avr_h -/* main_timer.avr.h */ -/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ - * - * Copyright (C) 2008 Dufour Jérémy - * - * APBTeam: - * Web: http://apbteam.org/ - * Email: team AT apbteam DOT org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * }}} */ - -/** - * @file Main timer module. - * Main timer module is used for the main loop of the io board to ensure it is - * executed only to the defined loop time (at the maximum). It uses the - * timer/counter 0 of the AVR. - */ - -/** - * Initialize the main timer to 4.444 ms. - */ -static inline void -main_timer_init (void) -{ - /* Fov = F_io / (prescaler * (TOP + 1)) - * TOP = 0xff - * prescaler = 256 - * Tov = 1 / Fov = 4.444 ms */ - TCCR0 = regv (FOC0, WGM00, COM01, COM0, WGM01, CS02, CS01, CS00, - 0, 0, 0, 0, 0, 1, 1, 0); -} - -/** - * Wait until the main timer overflows. - */ -static inline uint8_t -main_timer_wait (void) -{ - uint8_t count_before_ov = 0; - /* Loop until an overflow of the timer occurs */ - while (!(TIFR & _BV (TOV0))) - count_before_ov++; - ; - /* Write 1 to clear */ - TIFR = _BV (TOV0); - - return count_before_ov; -} - -#endif /* main_timer_avr_h */ diff --git a/digital/io/src/main_timer.h b/digital/io/src/main_timer.h new file mode 100644 index 00000000..1b69a743 --- /dev/null +++ b/digital/io/src/main_timer.h @@ -0,0 +1,69 @@ +#ifndef main_timer_h +#define main_timer_h +/* main_timer.h */ +/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ + * + * Copyright (C) 2009 Dufour Jérémy + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +/** + * @file Main timer. + * The main timer is responsible for making the main loop executed regularly + * to a specific time (at least). + * + * Sometimes, the main loop can be executed less frequently that we want + * because it takes too much time! This is bad and should be avoided. + * + * The main timer used the timer/counter 0 of the AVR. + */ + +/** + * Configuration of prescaler of timer/counter 0. + */ +#define MT_TC0_PRESCALER 256 +/** + * Configuration of the top of timer/counter 0. + */ +#define MT_TC0_TOP 255 +/** + * Configuration of the period timer/counter 0 (in millisecond). + */ +#define MT_TC0_PERIOD ((1 / AC_FREQ / MT_TC0_PRESCALER * (MT_TC0_TOP + 1)) \ + * 1000) + +/** + * Initialize the main timer to 4.444 ms. + */ +void +main_timer_init (void); + +/** + * Wait until the main timer overflows. + * @return + * - 0 if we are on time (we have not reached overflow before calling this + * function). + * - 1 if we have already reached overflow. + */ +uint8_t +main_timer_wait (void); + +#endif /* main_timer_h */ diff --git a/digital/io/src/simu.host.h b/digital/io/src/simu.host.h index bbd5c218..4203f42e 100644 --- a/digital/io/src/simu.host.h +++ b/digital/io/src/simu.host.h @@ -27,14 +27,6 @@ #ifdef HOST -/** Hooked, initialise the host simulation. */ -void -main_timer_init (void); - -/** Hooked, as before, wait for the next iteration. */ -uint8_t -main_timer_wait (void); - /** Hooked, do nothing. */ void switch_init (void); -- cgit v1.2.3