summaryrefslogtreecommitdiff
path: root/digital/avr
diff options
context:
space:
mode:
authorNicolas Schodet2012-03-21 00:46:03 +0100
committerNicolas Schodet2012-03-21 18:56:51 +0100
commit1e7c8a8a9811c45091e6c471482086113f059f40 (patch)
treed7faa80dd162799c394ec23925cc53b6dbff008e /digital/avr
parent0feb542a8cf7630a5d442387727414f5251aad83 (diff)
parentdcb79f383269440ec2c5a54b6d7792fbb0110d5a (diff)
Merge branch 'master' into efrei-robotique
Conflicts: digital/mimot/src/dirty/aux.c digital/mimot/src/dirty/aux.h digital/mimot/src/dirty/contacts.h digital/mimot/src/dirty/main.c digital/mimot/src/dirty/models.host.c digital/mimot/src/dirty/twi_proto.c digital/mimot/tools/mimot/init.py
Diffstat (limited to 'digital/avr')
-rw-r--r--digital/avr/doc/fuses.txt60
-rw-r--r--digital/avr/make/Makefile.avr7
-rw-r--r--digital/avr/modules/adc/adc.avr.c6
-rw-r--r--digital/avr/modules/devices/servo/Makefile.module1
-rw-r--r--digital/avr/modules/devices/servo/README23
-rw-r--r--digital/avr/modules/devices/servo/avrconfig.h33
-rw-r--r--digital/avr/modules/devices/servo/servo.avr.c193
-rw-r--r--digital/avr/modules/devices/servo/servo.h96
-rw-r--r--digital/avr/modules/devices/servo/servo_pos.c60
-rw-r--r--digital/avr/modules/devices/servo/servo_pos.h73
-rw-r--r--digital/avr/modules/devices/servo/test/Makefile12
-rw-r--r--digital/avr/modules/devices/servo/test/avrconfig.h91
-rw-r--r--digital/avr/modules/devices/servo/test/test_servo.c86
-rw-r--r--digital/avr/modules/devices/usdist/Makefile.module1
-rw-r--r--digital/avr/modules/devices/usdist/README23
-rw-r--r--digital/avr/modules/devices/usdist/avrconfig.h40
-rw-r--r--digital/avr/modules/devices/usdist/usdist.c122
-rw-r--r--digital/avr/modules/devices/usdist/usdist.h58
-rw-r--r--digital/avr/modules/twi/twi_hard.avr.c1
-rw-r--r--digital/avr/modules/uart/uart.avr.c1
20 files changed, 959 insertions, 28 deletions
diff --git a/digital/avr/doc/fuses.txt b/digital/avr/doc/fuses.txt
index 3f353fcd..1345c0ef 100644
--- a/digital/avr/doc/fuses.txt
+++ b/digital/avr/doc/fuses.txt
@@ -19,36 +19,44 @@ m16 0xff 0xc1 --
m32 0xff 0xc1 --
m64 0xff 0xc1 0xff
m128 0xff 0xc1 0xff
+usb128 0xff 0xd1 0xf3
====== ===== ===== =====
This correspond to ('!' means unprogrammed, i.e. '1'):
-=== === === === ==== =======
-m8 m16 m32 m64 m128 Meaning
-=== === === === ==== =======
-l7 l7 l7 l7 l7 !BODLEVEL (BOD trigger level)
-l6 l6 l6 l6 l6 !BODEN (BOD enabled)
-l5 l5 l5 l5 l5 !SUT1 (start up time)
-l4 l4 l4 l4 l4 !SUT0 (start up time)
-l3 l3 l3 l3 l3 !CKSEL3 (clock source select)
-l2 l2 l2 l2 l2 !CKSEL2 (clock source select)
-l1 l1 l1 l1 l1 !CKSEL1 (clock source select)
-l0 l0 l0 l0 l0 !CKSEL0 (clock source select)
---- --- --- --- ---- -------
-h7 .. .. .. .. !RSTDISBL (no reset pin disabled)
-.. h7 h7 h7 h7 !OCDEN (OCD enabled)
-h6 .. .. .. .. !WDTON (no watchdog always on)
-.. h6 h6 h6 h6 !JTAGEN (JTAG enabled)
-h5 h5 h5 h5 h5 SPIEN (spi programming enabled)
-h4 h4 h4 h4 h4 CKOPT (clock option)
-h3 h3 h3 h3 h3 EESAVE (eeprom preserved on chip erase)
-h2 h2 h2 h2 h2 BOOTSZ1 (boot size)
-h1 h1 h1 h1 h1 BOOTSZ0 (boot size)
-h0 h0 h0 h0 h0 !BOOTRST (use boot reset vector)
---- --- --- --- ---- -------
-.. .. .. e1 e1 !M103C (ATmega103 compatibility)
-.. .. .. e0 e0 !WDTON (watchdog always on)
-=== === === === ==== =======
+=== === === === ==== ====== =======
+m8 m16 m32 m64 m128 usb128 Meaning
+=== === === === ==== ====== =======
+l7 l7 l7 l7 l7 .. !BODLEVEL (BOD trigger level)
+l6 l6 l6 l6 l6 .. !BODEN (BOD enabled)
+.. .. .. .. .. l7 !CKDIV8 (divide clock by 8)
+.. .. .. .. .. l6 !CKOUT (clock output)
+l5 l5 l5 l5 l5 l5 !SUT1 (start up time)
+l4 l4 l4 l4 l4 l4 !SUT0 (start up time)
+l3 l3 l3 l3 l3 l3 !CKSEL3 (clock source select)
+l2 l2 l2 l2 l2 l2 !CKSEL2 (clock source select)
+l1 l1 l1 l1 l1 l1 !CKSEL1 (clock source select)
+l0 l0 l0 l0 l0 l0 !CKSEL0 (clock source select)
+--- --- --- --- ---- ------ -------
+h7 .. .. .. .. .. !RSTDISBL (no reset pin disabled)
+.. h7 h7 h7 h7 h7 !OCDEN (OCD enabled)
+h6 .. .. .. .. .. !WDTON (no watchdog always on)
+.. h6 h6 h6 h6 h6 !JTAGEN (JTAG enabled)
+h5 h5 h5 h5 h5 h5 SPIEN (spi programming enabled)
+h4 h4 h4 h4 h4 .. CKOPT (clock option)
+.. .. .. .. .. h4 !WDTON (watchdog always on)
+h3 h3 h3 h3 h3 h3 EESAVE (eeprom preserved on chip erase)
+h2 h2 h2 h2 h2 h2 BOOTSZ1 (boot size)
+h1 h1 h1 h1 h1 h1 BOOTSZ0 (boot size)
+h0 h0 h0 h0 h0 h0 !BOOTRST (use boot reset vector)
+--- --- --- --- ---- ------ -------
+.. .. .. e1 e1 !M103C (ATmega103 compatibility)
+.. .. .. e0 e0 !WDTON (watchdog always on)
+.. .. .. .. .. e3 HWBE (hardware boot enable)
+.. .. .. .. .. e2 BODLEVEL2 (brown-out detector select)
+.. .. .. .. .. e1 !BODLEVEL1 (brown-out detector select)
+.. .. .. .. .. e0 !BODLEVEL0 (brown-out detector select)
+=== === === === ==== ====== =======
- CKSEL321 = 111, CKOPT = 0 corresponds to external crystal, more than 1 MHz.
- CKSEL0 = 1, SUT10 = 11 corresponds to crystal, slowly rising power, this is
diff --git a/digital/avr/make/Makefile.avr b/digital/avr/make/Makefile.avr
index bffca25a..1fe5ff31 100644
--- a/digital/avr/make/Makefile.avr
+++ b/digital/avr/make/Makefile.avr
@@ -11,6 +11,7 @@ AVR_LDLIBS := $(LDLIBS) $(AVR_LIBS)
AVR_CC := avr-gcc
AVR_OBJCOPY := avr-objcopy
AVR_OBJDUMP := avr-objdump
+AVR_SIZE := avr-size --mcu=$(AVR_MCU) -C
AVR_COMPILE.c = $(AVR_CC) $(AVR_CFLAGS) $(AVR_CPPFLAGS) -c
ifdef L
AVR_COMPILE.c += -Wa,-adhlns=$(@:%.avr.o=%.c.avr.lst)
@@ -20,7 +21,7 @@ AVR_LINK.o := $(AVR_CC) $(AVR_CFLAGS) $(AVR_LDFLAGS)
# Main rules.
-avr: elf lst hex
+avr: elf lst hex size
simu: simuelf
@@ -90,6 +91,7 @@ text: hex
hex: $(AVR_PROGS:%=%.hex)
bin: $(AVR_PROGS:%=%.bin)
srec: $(AVR_PROGS:%=%.srec)
+size: $(AVR_PROGS:%=%.size)
%.hex: %.avr.elf
$(AVR_OBJCOPY) -j .text -j .data -O ihex $< $@
@@ -100,6 +102,9 @@ srec: $(AVR_PROGS:%=%.srec)
%.bin: %.avr.elf
$(AVR_OBJCOPY) -j .text -j .data -O binary $< $@
+%.size: %.avr.elf
+ $(AVR_SIZE) $< | grep Full
+
# Rules for building the .eeprom rom images.
EEPROMS := $(AVR_PROGS:%=%_eeprom.hex) $(AVR_PROGS:%=%_eeprom.bin) \
diff --git a/digital/avr/modules/adc/adc.avr.c b/digital/avr/modules/adc/adc.avr.c
index 3bfdefa9..96392a86 100644
--- a/digital/avr/modules/adc/adc.avr.c
+++ b/digital/avr/modules/adc/adc.avr.c
@@ -32,6 +32,10 @@
#elif defined (__AVR_ATmega128__)
#elif defined (__AVR_ATmega64__)
#elif defined (__AVR_ATmega16__)
+# elif defined (__AVR_AT90USB646__)
+# elif defined (__AVR_AT90USB647__)
+# elif defined (__AVR_AT90USB1286__)
+# elif defined (__AVR_AT90USB1287__)
#else
# warning "adc: not tested on this chip."
#endif
@@ -48,7 +52,7 @@
* 11: Internal 2.56V Voltage Reference with external capacitor
* at AREF pin. */
#define ADMUX_CFG (regv (REFS1, REFS0, ADLAR, MUX4, MUX3, MUX2, MUX1, MUX0, \
- 1, 1, 0, 0, 0, 0, 0, 0))
+ 0, 1, 0, 0, 0, 0, 0, 0))
#define ADCSR_CFG (regv (ADEN, ADSC, ADFR, ADIF, ADIE, ADPS2, ADPS1, ADPS0, \
1, 0, 0, 1, 0, 0, 0, 0))
#define ADCSR_CFG_115200 7
diff --git a/digital/avr/modules/devices/servo/Makefile.module b/digital/avr/modules/devices/servo/Makefile.module
new file mode 100644
index 00000000..bf82ffac
--- /dev/null
+++ b/digital/avr/modules/devices/servo/Makefile.module
@@ -0,0 +1 @@
+devices_servo_SOURCES = servo_pos.c servo.avr.c
diff --git a/digital/avr/modules/devices/servo/README b/digital/avr/modules/devices/servo/README
new file mode 100644
index 00000000..4981de17
--- /dev/null
+++ b/digital/avr/modules/devices/servo/README
@@ -0,0 +1,23 @@
+avr.devices.servo - Servo AVR module.
+
+Servo module for AVR. See modules README for more details about AVR modules.
+
+Copyright (C) 2011 Maxime Hadjinlian
+
+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.
diff --git a/digital/avr/modules/devices/servo/avrconfig.h b/digital/avr/modules/devices/servo/avrconfig.h
new file mode 100644
index 00000000..d192598a
--- /dev/null
+++ b/digital/avr/modules/devices/servo/avrconfig.h
@@ -0,0 +1,33 @@
+#ifndef avrconfig_h
+#define avrconfig_h
+/* avrconfig.h */
+/* avr.devices.servo - Servo AVR module. {{{
+ *
+ * Copyright (C) 2011 Maxime Hadjinlian
+ *
+ * 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.
+ *
+ * }}} */
+
+/* servo - Servo module. */
+/** All servos are currently connected to the same port. */
+#define AC_SERVO_PORT PORTA
+#define AC_SERVO_DDR DDRA
+
+#endif /* avrconfig_h */
diff --git a/digital/avr/modules/devices/servo/servo.avr.c b/digital/avr/modules/devices/servo/servo.avr.c
new file mode 100644
index 00000000..fc5adc78
--- /dev/null
+++ b/digital/avr/modules/devices/servo/servo.avr.c
@@ -0,0 +1,193 @@
+/* servo.avr.c */
+/* avr.devices.servo - Servo AVR module. {{{
+ *
+ * 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.
+ *
+ * }}} */
+#include "common.h"
+#include "servo.h"
+
+#include "modules/utils/utils.h" /* regv, set_bit */
+#include "modules/utils/byte.h" /* v16_to_v8 */
+#include "io.h" /* General defines of registers */
+
+/**
+ * @defgroup ServoConfig Servo module configuration variables and defines.
+ * @{
+ */
+
+/**
+ * TOP of the timer/counter.
+ */
+#define SERVO_TCNT_TOP 0xFF
+
+/**
+ * Number of TIC the timer/counter need to do to make a whole cycle of servo
+ * update.
+ * It does not depend on the servo motors we manage but on the time we want a
+ * whole cycle to last.
+ * The formula used is:
+ * time_of_a_cycle * AVR_frequency / timer_counter_prescaler
+ * We want a time of 20ms (20/1000).
+ * See @a servo_init to know the prescaler value of the timer/counter.
+ */
+static const uint16_t servo_tic_cycle_ = AC_FREQ / 256 * 20 / 1000;
+
+/** @} */
+
+/**
+ * @defgroup ServoPrivate Servo module private variables and functions
+ * declarations
+ * @{
+ */
+
+/**
+ * Identifier of the servo we are currently updating.
+ * Note: -1 is a special value used by the servo module system to update the
+ * low state of all the servos.
+ */
+volatile int8_t servo_updating_id_;
+
+/**
+ * A table for the time spent by each servo in high state.
+ */
+volatile uint8_t servo_position_[SERVO_NUMBER];
+
+/**
+ * Overflow of timer/counter 2 handler.
+ */
+SIGNAL (SIG_OVERFLOW2);
+
+/** @} */
+
+/* Initialize servo module. */
+void
+servo_init (void)
+{
+ /* Set-up all the pins of the servo to out direction */
+ AC_SERVO_DDR = 0xff;
+ /* All pins are at low state by default */
+
+ /* Set-up the timer/counter 2:
+ - prescaler 256 => 4.44 ms TOP */
+ TCCR2 = regv (FOC2, WGM20, COM21, COM20, WGM21, CS22, CS21, CS20,
+ 0, 0, 0, 0, 0, 1, 0, 0);
+
+ /* The state machine start with the first servo */
+ servo_updating_id_ = 0;
+
+ /* Enable overflow interrupt */
+ set_bit (TIMSK, TOIE2);
+
+ /* By default, servo init disable all servo. */
+ uint8_t i;
+ for (i = 0; i < SERVO_NUMBER; i++)
+ servo_set_position (i, 0);
+}
+
+/* Set the duration of the input signal at the high state of a servo. */
+void
+servo_set_position (uint8_t servo, uint8_t position)
+{
+ uint8_t filtered = position;
+ if (filtered != 0)
+ UTILS_BOUND (filtered, SERVO_HIGH_TIME_MIN, SERVO_HIGH_TIME_MAX);
+ /* Sanity check */
+ if (servo < SERVO_NUMBER)
+ /* Set new desired position (high value time) */
+ servo_position_[servo] = filtered;
+}
+
+/* Get the duration of the servo's input signal at high state. */
+uint8_t
+servo_get_position (uint8_t servo)
+{
+ /* Sanity check */
+ if (servo < SERVO_NUMBER)
+ return servo_position_[servo];
+ return 0;
+}
+
+/* Overflow of timer/counter 2 handler. */
+SIGNAL (SIG_OVERFLOW2)
+{
+ /* Overflow count (used when we wait in the lower state).
+ -1 is used for the first count where we wait less than a complete
+ overflow */
+ static int8_t servo_overflow_count = -1;
+ /* Time spent by each servo motor at high state during a whole cycle */
+ static uint16_t servo_position_cycle = servo_tic_cycle_;
+
+ /* State machine actions */
+ if (servo_updating_id_ >= 0)
+ {
+ /* Servos motor high state mode */
+
+ /* Set to low state the previous servo motor pin if needed (not for
+ * the first one) */
+ if (servo_updating_id_ != 0)
+ AC_SERVO_PORT &= ~_BV (servo_updating_id_ - 1);
+ /* Set to high state the current servo motor pin, unless is zero */
+ if (servo_position_[servo_updating_id_])
+ set_bit (AC_SERVO_PORT, servo_updating_id_);
+ /* Plan next timer overflow to the TOP minus the current configuration
+ * of the servo motor */
+ TCNT2 = SERVO_TCNT_TOP - servo_position_[servo_updating_id_];
+ /* Update the time spent at high state by all servo motors for this
+ * cycle */
+ servo_position_cycle += servo_position_[servo_updating_id_];
+ /* Update the identifier of the current servo motor (and manage when
+ * we are at the last one) */
+ if (++servo_updating_id_ == SERVO_NUMBER)
+ servo_updating_id_ = -1;
+ }
+ else
+ {
+ /* Sleeping time mode */
+
+ /* Is it the first we are in this mode? */
+ if (servo_overflow_count == -1)
+ {
+ /* Set to low state the previous servo motor pin */
+ AC_SERVO_PORT &= ~_BV (SERVO_NUMBER - 1);
+ /* Number of full overflow (from 0 to SERVO_TCNT_TOP) we need to
+ * wait (division by SERVO_TCNT_TOP or >> 8) */
+ servo_overflow_count = servo_position_cycle >> 8;
+ /* Restart the counter from remaining TIC that are left and can
+ * not be used to make a full overflow */
+ TCNT2 = SERVO_TCNT_TOP - v16_to_v8 (servo_position_cycle, 0);
+ }
+ else
+ {
+ /* We just have an overflow, are we at the last one needed? The -1
+ * is normal: we do not count the first overflow of the sleeping
+ * mode because it is not a full one */
+ if (--servo_overflow_count == -1)
+ {
+ /* Restart with first servo motor */
+ servo_updating_id_ = 0;
+ /* Re-initialize the counter of time spent by each servo motor
+ * at high state */
+ servo_position_cycle = servo_tic_cycle_;
+ }
+ }
+ }
+}
diff --git a/digital/avr/modules/devices/servo/servo.h b/digital/avr/modules/devices/servo/servo.h
new file mode 100644
index 00000000..59d35907
--- /dev/null
+++ b/digital/avr/modules/devices/servo/servo.h
@@ -0,0 +1,96 @@
+#ifndef servo_h
+#define servo_h
+/* servo.h */
+/* avr.devices.servo - Servo AVR module. {{{
+ *
+ * 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.
+ *
+ * }}} */
+#include "common.h" // uint8_t
+
+/**
+ * @file Module to control servo motors.
+ * This module contains low-level functions to control the servo motors. If
+ * you want to control the traps to store the balls at a specific place, have
+ * a look at the trap module.
+ * It uses the timer/counter 2 (8-bit).
+ *
+ * Servo motors can be controlled by the time the input signal spend at its
+ * high state. For example, if the signal sent to the servo motor only spend
+ * 1ms at the high state, it will have a 0° angle position. If the signal
+ * stays for 1.5ms at high state, it will have a 90° angle position.
+ * To manage all servo motors in a "one time shot", we need to use the
+ * timer/counter 2 of the ATmega128 and its overflow.
+ * We setup the timer/counter to the value of its overflow minus the time the
+ * input signal of the servo need to spend at high state ; we put the input
+ * signal of this servo motor to the high value. When the timer overflows, we
+ * put it back to the low state. We go to the next servo motors and do the
+ * same algorithm. When the all servos motor have been taken care of, we
+ * set-up the timer to overflow a certain number of times to wait before
+ * restarting the whole cycle.
+ *
+ * All servos are connected to AC_SERVO_PORT defined in avrconfig.h
+ */
+
+/**
+ * Number of servos motor managed by this module.
+ * If you change it, you _must_ update the key of the eeprom module!
+ */
+#define SERVO_NUMBER 8
+
+/**
+ * Minimum high time for servos.
+ */
+#define SERVO_HIGH_TIME_MIN 0x24
+
+/**
+ * Maximum high time for servos.
+ */
+#define SERVO_HIGH_TIME_MAX 0x88
+
+/**
+ * Initialize servo module.
+ * This functions put the pins of the servos motor in the right direction,
+ * initialize the timer/counter 2 and some internals stuff.
+ */
+void
+servo_init (void);
+
+/**
+ * Set the duration of the input signal of a servo at high state
+ * and thus its position.
+ * @param servo the servo to change the position.
+ * @param position the duration while the input signal will be at the
+ * high state. The servo will then move to a position.
+ * A zero will let the servo floating.
+ */
+void
+servo_set_position (uint8_t servo, uint8_t position);
+
+/**
+ * Get the duration of the servo's input signal at high state.
+ * @param servo the servo to get the position of.
+ * @return the current position of the servo.
+ */
+uint8_t
+servo_get_position (uint8_t servo);
+
+#endif /* servo_h */
diff --git a/digital/avr/modules/devices/servo/servo_pos.c b/digital/avr/modules/devices/servo/servo_pos.c
new file mode 100644
index 00000000..6308ceaf
--- /dev/null
+++ b/digital/avr/modules/devices/servo/servo_pos.c
@@ -0,0 +1,60 @@
+/* servo_pos.c */
+/* avr.devices.servo - Servo AVR module. {{{
+ *
+ * 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 "servo_pos.h"
+
+uint8_t servo_pos_high_time[SERVO_NUMBER][SERVO_POS_NUMBER];
+
+void
+servo_pos_init (void)
+{
+ /* Initialize under layer, servo module. */
+ servo_init ();
+}
+
+void
+servo_pos_set_high_time (uint8_t servo_id,
+ uint8_t high_times[SERVO_POS_NUMBER])
+{
+ uint8_t i;
+ /* If servo exists. */
+ if (servo_id < SERVO_NUMBER)
+ {
+ /* For each position. */
+ for (i = 0; i < SERVO_POS_NUMBER; i++)
+ servo_pos_high_time[servo_id][i] = high_times[i];
+ }
+}
+
+/**
+ * Move a servo to a specific position.
+ * @param servo_id the id of the servo to move.
+ * @param position the position identifier where to move the servo.
+ */
+void
+servo_pos_move_to (uint8_t servo_id, uint8_t position)
+{
+ servo_set_position (servo_id, servo_pos_high_time[servo_id][position]);
+}
diff --git a/digital/avr/modules/devices/servo/servo_pos.h b/digital/avr/modules/devices/servo/servo_pos.h
new file mode 100644
index 00000000..e173ab6d
--- /dev/null
+++ b/digital/avr/modules/devices/servo/servo_pos.h
@@ -0,0 +1,73 @@
+#ifndef servo_pos_h
+#define servo_pos_h
+/* servo.pos.h */
+/* avr.devices.servo - Servo AVR module. {{{
+ *
+ * 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.
+ *
+ * }}} */
+
+/**
+ * Module to control two positions of the servo motor.
+ * It's an higher interface that uses the servo module but remember the
+ * positions (for example, two positions, open and close) that they can take.
+ */
+
+#include "servo.h"
+
+/**
+ * Number of positions the servo can take.
+ * @warning if you change this define, you must update the key value in the
+ * EEPROM module. Otherwise, everything will fail!
+ */
+#define SERVO_POS_NUMBER 2
+
+/**
+ * Table to store the correspondence between the positions and the high time.
+ */
+extern uint8_t servo_pos_high_time[SERVO_NUMBER][SERVO_POS_NUMBER];
+
+/**
+ * Initialize the servo with positions module.
+ * @note it also initialize the servo module for you.
+ */
+void
+servo_pos_init (void);
+
+/**
+ * Set the high times of a servo for positions.
+ * @param servo_id the ID of the servo.
+ * @param high_times the different high times for the positions of the
+ * servo.
+ */
+void
+servo_pos_set_high_time (uint8_t servo_id,
+ uint8_t high_times[SERVO_POS_NUMBER]);
+
+/**
+ * Move a servo to a specific position.
+ * @param servo_id the id of the servo to move.
+ * @param position the position identifier where to move the servo.
+ */
+void
+servo_pos_move_to (uint8_t servo_id, uint8_t position);
+
+#endif /* servo_pos_h */
diff --git a/digital/avr/modules/devices/servo/test/Makefile b/digital/avr/modules/devices/servo/test/Makefile
new file mode 100644
index 00000000..a0ad639f
--- /dev/null
+++ b/digital/avr/modules/devices/servo/test/Makefile
@@ -0,0 +1,12 @@
+BASE = ../../../..
+AVR_PROGS = test_servo
+test_servo_SOURCES = test_servo.c
+MODULES = proto devices/servo uart utils
+CONFIGFILE = avrconfig.h
+# atmega8, atmega8535, atmega128...
+AVR_MCU = atmega128
+# -O2 : speed
+# -Os : size
+OPTIMIZE = -O2
+
+include $(BASE)/make/Makefile.gen
diff --git a/digital/avr/modules/devices/servo/test/avrconfig.h b/digital/avr/modules/devices/servo/test/avrconfig.h
new file mode 100644
index 00000000..6ee81392
--- /dev/null
+++ b/digital/avr/modules/devices/servo/test/avrconfig.h
@@ -0,0 +1,91 @@
+#ifndef avrconfig_h
+#define avrconfig_h
+/* avrconfig.h */
+/* avr.devices.servo - Servo AVR module. {{{
+ *
+ * Copyright (C) 2011 Maxime Hadjinlian
+ *
+ * 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.
+ *
+ * }}} */
+
+/* global */
+/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800,
+ * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */
+#define AC_FREQ 14745600
+
+/* servo - Servo module. */
+/** All servos are currently connected to the same port. */
+#define AC_SERVO_PORT PORTA
+#define AC_SERVO_DDR DDRA
+
+/* uart - UART module. */
+/** Select hardware uart for primary uart: 0, 1 or -1 to disable. */
+#define AC_UART0_PORT 1
+/** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800,
+ * 115200, 230400, 250000, 500000, 1000000. */
+#define AC_UART0_BAUDRATE 38400
+/** Send mode:
+ * - POLLING: no interrupts.
+ * - RING: interrupts, ring buffer. */
+#define AC_UART0_SEND_MODE RING
+/** Recv mode, same as send mode. */
+#define AC_UART0_RECV_MODE RING
+/** Character size: 5, 6, 7, 8, 9 (only 8 implemented). */
+#define AC_UART0_CHAR_SIZE 8
+/** Parity : ODD, EVEN, NONE. */
+#define AC_UART0_PARITY EVEN
+/** Stop bits : 1, 2. */
+#define AC_UART0_STOP_BITS 1
+/** Send buffer size, should be power of 2 for RING mode. */
+#define AC_UART0_SEND_BUFFER_SIZE 32
+/** Recv buffer size, should be power of 2 for RING mode. */
+#define AC_UART0_RECV_BUFFER_SIZE 32
+/** If the send buffer is full when putc:
+ * - DROP: drop the new byte.
+ * - WAIT: wait until there is room in the send buffer. */
+#define AC_UART0_SEND_BUFFER_FULL DROP
+/** In HOST compilation:
+ * - STDIO: use stdin/out.
+ * - PTS: use pseudo terminal. */
+#define AC_UART0_HOST_DRIVER STDIO
+/** Same thing for secondary port. */
+#define AC_UART1_PORT -1
+#define AC_UART1_BAUDRATE 115200
+#define AC_UART1_SEND_MODE RING
+#define AC_UART1_RECV_MODE RING
+#define AC_UART1_CHAR_SIZE 8
+#define AC_UART1_PARITY EVEN
+#define AC_UART1_STOP_BITS 1
+#define AC_UART1_SEND_BUFFER_SIZE 32
+#define AC_UART1_RECV_BUFFER_SIZE 32
+#define AC_UART1_SEND_BUFFER_FULL WAIT
+#define AC_UART1_HOST_DRIVER PTS
+
+/* proto - Protocol module. */
+/** Maximum argument size. */
+#define AC_PROTO_ARGS_MAX_SIZE 8
+/** Callback function name. */
+#define AC_PROTO_CALLBACK proto_callback
+/** Putchar function name. */
+#define AC_PROTO_PUTC uart0_putc
+/** Support for quote parameter. */
+#define AC_PROTO_QUOTE 1
+
+#endif /* avrconfig_h */
diff --git a/digital/avr/modules/devices/servo/test/test_servo.c b/digital/avr/modules/devices/servo/test/test_servo.c
new file mode 100644
index 00000000..924fae1c
--- /dev/null
+++ b/digital/avr/modules/devices/servo/test/test_servo.c
@@ -0,0 +1,86 @@
+/* test_servo.c. */
+/* avr.servo - Servo AVR module. {{{
+ *
+ * Copyright (C) 2011 Maxime Hadjinlian
+ *
+ * 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 "modules/uart/uart.h"
+#include "modules/proto/proto.h"
+#include "modules/devices/servo/servo.h"
+#include "modules/utils/utils.h"
+#include "modules/utils/byte.h"
+#include "io.h"
+
+/* proto_callback is called by proto
+ * when a command is complete and valid.
+ * proto act as an intelligent buffer for us.
+*/
+void
+proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
+{
+ /* This macro combine command and size in one integer. */
+#define c(cmd, size) (cmd << 8 | size)
+ switch (c (cmd, size))
+ {
+ case c ('z', 0):
+ /* This should be generaly implemented. */
+ utils_reset ();
+ break;
+ case c ('s', 3):
+ /* We are looking to receive a command in the form !sxxyyyy
+ * where xx is the servo id and yyyy is the command itself.
+ * For the size, please read digital/avr/modules/proto/proto.txt
+ */
+ servo_set_position (args[0], v8_to_v16 (args[1], args[2]));
+ break;
+ default:
+ /* This is to handle default commands, return an error. */
+ proto_send0 ('?');
+ return;
+ }
+ /* When no error acknoledge. */
+ proto_send (cmd, size, args);
+#undef c
+}
+
+int
+main (int argc, char **argv)
+{
+ avr_init (argc, argv);
+ sei ();
+ uart0_init ();
+ servo_init ();
+
+ /* This command should be generaly sent on reset. */
+ proto_send0 ('z');
+ /* This is to accept commands. */
+ while (1)
+ {
+ uint8_t c = uart0_getc ();
+ proto_accept (c);
+ /* once the command is complete according to
+ * proto's state machine, proto_callback will
+ * be called with the full command.
+ */
+ }
+}
diff --git a/digital/avr/modules/devices/usdist/Makefile.module b/digital/avr/modules/devices/usdist/Makefile.module
new file mode 100644
index 00000000..61691300
--- /dev/null
+++ b/digital/avr/modules/devices/usdist/Makefile.module
@@ -0,0 +1 @@
+devices_usdist_SOURCES = usdist.c
diff --git a/digital/avr/modules/devices/usdist/README b/digital/avr/modules/devices/usdist/README
new file mode 100644
index 00000000..5ddb8aa6
--- /dev/null
+++ b/digital/avr/modules/devices/usdist/README
@@ -0,0 +1,23 @@
+usdist - Analog US distance sensor support.
+
+Used with Sensopart UT20-700.
+
+Copyright (C) 2011 Nicolas Schodet
+
+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.
diff --git a/digital/avr/modules/devices/usdist/avrconfig.h b/digital/avr/modules/devices/usdist/avrconfig.h
new file mode 100644
index 00000000..2a908794
--- /dev/null
+++ b/digital/avr/modules/devices/usdist/avrconfig.h
@@ -0,0 +1,40 @@
+#ifndef avrconfig_h
+#define avrconfig_h
+/* avrconfig.h - TWI module configuration template. */
+/* usdist - Analog US distance sensor support. {{{
+ *
+ * Copyright (C) 2011 Nicolas Schodet
+ *
+ * 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.
+ *
+ * }}} */
+
+/* usdist - Analog US distance sensor. */
+/** Number of sensors. */
+#define AC_USDIST_NB 4
+/** Measuring period, in number of update call. */
+#define AC_USDIST_PERIOD 2
+/** List of space separated sensor definition, see usdist.h. */
+#define AC_USDIST_SENSORS \
+ USDIST_SENSOR (0, G, 3) \
+ USDIST_SENSOR (1, G, 1) \
+ USDIST_SENSOR (2, C, 7) \
+ USDIST_SENSOR (3, D, 4)
+
+#endif /* avrconfig_h */
diff --git a/digital/avr/modules/devices/usdist/usdist.c b/digital/avr/modules/devices/usdist/usdist.c
new file mode 100644
index 00000000..75c7899b
--- /dev/null
+++ b/digital/avr/modules/devices/usdist/usdist.c
@@ -0,0 +1,122 @@
+/* usdist.c */
+/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
+ *
+ * Copyright (C) 2010 Nicolas Schodet
+ *
+ * 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 "usdist.h"
+
+#include "modules/adc/adc.h"
+#include "modules/utils/utils.h"
+#include "io.h"
+
+/* This include is to be defined by user program. */
+#include "simu.host.h"
+
+uint16_t usdist_mm[AC_USDIST_NB];
+
+/** Describe a sensor. */
+struct usdist_sensor_t
+{
+ /** ADC number. */
+ uint8_t adc;
+ /** Sync PORT. */
+ volatile uint8_t *sync_port;
+ /** Sync DDR. */
+ volatile uint8_t *sync_ddr;
+ /** Sync bit mask. */
+ uint8_t sync_bv;
+};
+
+/** Define sensors configuration. */
+#define USDIST_SENSOR(adc, p, n) \
+ { adc, &IO_PORT_ (p, n), &IO_DDR_ (p, n), IO_BV_ (p, n) },
+struct usdist_sensor_t usdist_sensors[AC_USDIST_NB] = {
+ AC_USDIST_SENSORS
+};
+
+void
+usdist_init (void)
+{
+ uint8_t i;
+ adc_init ();
+ for (i = 0; i < AC_USDIST_NB; i++)
+ {
+ usdist_mm[i] = 0xffff;
+ *usdist_sensors[i].sync_port &= ~usdist_sensors[i].sync_bv;
+ *usdist_sensors[i].sync_ddr |= usdist_sensors[i].sync_bv;
+ }
+}
+
+uint8_t
+usdist_update (void)
+{
+ /* Current measuring sensor. */
+ static uint8_t current;
+ /* Number of cycles until the measure ends. */
+ static uint8_t wait;
+ /* Set to one once first cycle is done. */
+ static uint8_t init;
+ /* Time to measure? */
+ if (wait == 0)
+ {
+ if (init)
+ {
+ /* Stop sensor. */
+ *usdist_sensors[current].sync_port &=
+ ~usdist_sensors[current].sync_bv;
+ /* Read ADC value. */
+ adc_start (usdist_sensors[current].adc);
+ while (!adc_checkf ())
+ ;
+ uint16_t v = adc_read ();
+ /* Our sensors return a value between 1 and 5 V proportional to
+ * the distance between calibrated values. Ignore faulty sensors. */
+ if (v <= 1024 / 5 / 4)
+ usdist_mm[current] = 0xffff;
+ else if (v <= 1024 / 5)
+ usdist_mm[current] = USDIST_MM_MIN;
+ else
+ usdist_mm[current] = USDIST_MM_MIN
+ + ((uint32_t) (v - 1024 / 5)
+ * (USDIST_MM_MAX - USDIST_MM_MIN)
+ / (4 * 1024 / 5));
+ if (usdist_mm[current] >= USDIST_MM_TOO_FAR)
+ usdist_mm[current] = 0xffff;
+ /* Next. */
+ current = (current + 1) % AC_USDIST_NB;
+ }
+ init = 1;
+ /* Prepare next measure. */
+ *usdist_sensors[current].sync_port |=
+ usdist_sensors[current].sync_bv;
+ wait = AC_USDIST_PERIOD;
+ /* New mesure done. */
+ return 1;
+ }
+ else
+ {
+ wait--;
+ return 0;
+ }
+}
+
diff --git a/digital/avr/modules/devices/usdist/usdist.h b/digital/avr/modules/devices/usdist/usdist.h
new file mode 100644
index 00000000..37683091
--- /dev/null
+++ b/digital/avr/modules/devices/usdist/usdist.h
@@ -0,0 +1,58 @@
+#ifndef usdist_h
+#define usdist_h
+/* usdist.h */
+/* usdist - Analog US distance sensor support. {{{
+ *
+ * Copyright (C) 2011 Nicolas Schodet
+ *
+ * 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.
+ *
+ * }}} */
+
+/*
+ * Manage ultrasonic distance sensors using analog to digital converter.
+ *
+ * AC_USDIST_SENSORS should be set to a list of space separated USDIST_SENSOR
+ * macro calls:
+ *
+ * USDIST_SENSOR (adc_number, synchro_io_port, synchro_io_n)
+ */
+
+/** Minimal calibrated distance. */
+#define USDIST_MM_MIN 100
+
+/** Maximal calibrated distance. */
+#define USDIST_MM_MAX 700
+
+/** Distance considered as too far to be true. */
+#define USDIST_MM_TOO_FAR 650
+
+/** Array containing the last measures in millimeters. */
+extern uint16_t usdist_mm[AC_USDIST_NB];
+
+/** Initialise module. */
+void
+usdist_init (void);
+
+/** To be called every cycle to update sensor measures.
+ * - returns: non zero if sensor value has been updated. */
+uint8_t
+usdist_update (void);
+
+#endif /* usdist_h */
diff --git a/digital/avr/modules/twi/twi_hard.avr.c b/digital/avr/modules/twi/twi_hard.avr.c
index 8bd18d34..71483ecb 100644
--- a/digital/avr/modules/twi/twi_hard.avr.c
+++ b/digital/avr/modules/twi/twi_hard.avr.c
@@ -56,6 +56,7 @@
# if defined (__AVR_ATmega32__)
# elif defined (__AVR_ATmega64__)
# elif defined (__AVR_ATmega128__)
+# elif defined (__AVR_ATmega164P__)
# elif defined (__AVR_AT90USB646__)
# elif defined (__AVR_AT90USB647__)
# elif defined (__AVR_AT90USB1286__)
diff --git a/digital/avr/modules/uart/uart.avr.c b/digital/avr/modules/uart/uart.avr.c
index 689866ae..0b0ab107 100644
--- a/digital/avr/modules/uart/uart.avr.c
+++ b/digital/avr/modules/uart/uart.avr.c
@@ -35,6 +35,7 @@
#if defined (__AVR_ATmega8__)
#elif defined (__AVR_ATmega8535__)
#elif defined (__AVR_ATmega16__)
+#elif defined (__AVR_ATmega164P__)
#elif defined (__AVR_ATmega32__)
#elif defined (__AVR_ATmega64__)
#elif defined (__AVR_ATmega128__)