summaryrefslogtreecommitdiff
path: root/digital
diff options
context:
space:
mode:
authorJérémy Dufour2009-03-14 23:24:05 +0100
committerJérémy Dufour2009-03-14 23:24:05 +0100
commitc04b8fe1c42dd36b384bdc3379734d6c8bfe7996 (patch)
tree6c039231081556f4f7d5f5be23f918708e48c59a /digital
parentb0c2503c9a9d11c119d8f551a76381c6038e253b (diff)
* digital/io/src:
- clean main timer module.
Diffstat (limited to 'digital')
-rw-r--r--digital/io/src/Makefile2
-rw-r--r--digital/io/src/main.c2
-rw-r--r--digital/io/src/main_timer.avr.c (renamed from digital/io/src/main_timer.avr.h)42
-rw-r--r--digital/io/src/main_timer.h69
-rw-r--r--digital/io/src/simu.host.h8
5 files changed, 88 insertions, 35 deletions
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.h b/digital/io/src/main_timer.avr.c
index 39cf70b5..8f343403 100644
--- a/digital/io/src/main_timer.avr.h
+++ b/digital/io/src/main_timer.avr.c
@@ -1,9 +1,7 @@
-#ifndef main_timer_avr_h
-#define main_timer_avr_h
-/* main_timer.avr.h */
+/* main_timer.avr.c */
/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
*
- * Copyright (C) 2008 Dufour Jérémy
+ * Copyright (C) 2009 Dufour Jérémy
*
* APBTeam:
* Web: http://apbteam.org/
@@ -25,42 +23,36 @@
*
* }}} */
-/**
- * @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.
- */
+#include "common.h"
-/**
- * Initialize the main timer to 4.444 ms.
- */
-static inline void
+#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);
}
-/**
- * Wait until the main timer overflows.
- */
-static inline uint8_t
+uint8_t
main_timer_wait (void)
{
- uint8_t count_before_ov = 0;
- /* Loop until an overflow of the timer occurs */
+ /* We have reached overflow. */
+ uint8_t count_before_ov = 1;
+ /* Loop until an overflow of the timer occurs. */
while (!(TIFR & _BV (TOV0)))
- count_before_ov++;
- ;
- /* Write 1 to clear */
+ /* We have not reached overflow. */
+ count_before_ov = 0;
+ /* Write 1 to clear overflow. */
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);