summaryrefslogtreecommitdiffhomepage
path: root/digital/io-hub
diff options
context:
space:
mode:
authorNicolas Schodet2011-05-06 01:55:49 +0200
committerNicolas Schodet2011-05-06 01:55:49 +0200
commitb4a96cca3985cba6079d1b7a16f30aa43432a9b3 (patch)
tree266aca5a8ce118a3934507ba5495a3d009588437 /digital/io-hub
parentff7505c0a9d6b5332db32ae9b23326bc466f0b01 (diff)
digital/io-hub: add PWM
Diffstat (limited to 'digital/io-hub')
-rw-r--r--digital/io-hub/src/common/pwm.avr.c133
-rw-r--r--digital/io-hub/src/common/pwm.h60
-rw-r--r--digital/io-hub/src/common/pwm.host.c109
-rw-r--r--digital/io-hub/src/robospierre/Makefile5
-rw-r--r--digital/io-hub/src/robospierre/avrconfig.h8
-rw-r--r--digital/io-hub/src/robospierre/main.c21
6 files changed, 335 insertions, 1 deletions
diff --git a/digital/io-hub/src/common/pwm.avr.c b/digital/io-hub/src/common/pwm.avr.c
new file mode 100644
index 00000000..4b6ad896
--- /dev/null
+++ b/digital/io-hub/src/common/pwm.avr.c
@@ -0,0 +1,133 @@
+/* pwm.avr.c */
+/* io-hub - Modular Input/Output. {{{
+ *
+ * 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.
+ *
+ * }}} */
+#include "common.h"
+#include "pwm.h"
+
+#include "modules/utils/utils.h"
+#include "preproc.h"
+
+/** PWM context. */
+struct pwm_t
+{
+ /** Output compare register. */
+ volatile uint16_t *ocr;
+ /** Direction port. */
+ volatile uint8_t *dir_port;
+ /** Direction bit value. */
+ uint8_t dir_bv;
+ /** Remaining time before timer elapse, 0 for disabled. */
+ uint16_t time;
+ /** Value to be used after timer elapse. */
+ int16_t value_rest;
+};
+
+/** Array of PWM contexts. */
+#define PWM(timer, pwm, pwm_io_port, pwm_io_n, dir_io_port, dir_io_n) \
+{ &PREPROC_PASTE (OCR, timer, pwm), \
+ &IO_PORT_ (dir_io_port, dir_io_n), IO_BV_ (dir_io_port, dir_io_n), \
+ 0, 0 },
+struct pwm_t pwm[] = {
+ AC_IOHUB_PWM
+};
+#undef PWM
+
+void
+pwm_init (void)
+{
+ /* Fast PWM, TOP = 0x3ff, with positive logic.
+ f_IO without prescaler.
+ Fpwm = f_IO / (prescaler * (1 + TOP)). */
+ uint8_t timer_com_1 = 0, timer_com_3 = 0;
+#define PWM(timer, pwm, pwm_io_port, pwm_io_n, dir_io_port, dir_io_n) \
+ do { \
+ PREPROC_PASTE (timer_com_, timer) |= \
+ _BV (PREPROC_PASTE (COM, timer, pwm, 1)); \
+ IO_DDR_ (pwm_io_port, pwm_io_n) |= IO_BV_ (pwm_io_port, pwm_io_n); \
+ IO_DDR_ (dir_io_port, dir_io_n) |= IO_BV_ (dir_io_port, dir_io_n); \
+ } while (0);
+ AC_IOHUB_PWM
+#undef PWM
+ if (timer_com_1)
+ {
+ TCCR1A = timer_com_1 |
+ regv (COM1A1, COM1A0, COM1B1, COM1B0, COM1C1, COM1C0, WGM11, WGM10,
+ 0, 0, 0, 0, 0, 0, 1, 1);
+ TCCR1B = regv (ICNC1, ICES1, 5, WGM13, WGM12, CS12, CS11, CS10,
+ 0, 0, 0, 0, 1, 0, 0, 1);
+ }
+ if (timer_com_3)
+ {
+ TCCR3A = timer_com_3 |
+ regv (COM3A1, COM3A0, COM3B1, COM3B0, COM3C1, COM3C0, WGM31, WGM30,
+ 0, 0, 0, 0, 0, 0, 1, 1);
+ TCCR3B = regv (ICNC3, ICES3, 5, WGM33, WGM32, CS32, CS31, CS30,
+ 0, 0, 0, 0, 1, 0, 0, 1);
+ }
+}
+
+void
+pwm_update (void)
+{
+ uint8_t i;
+ for (i = 0; i < UTILS_COUNT (pwm); i++)
+ {
+ if (pwm[i].time)
+ {
+ pwm[i].time--;
+ if (pwm[i].time == 0)
+ pwm_set (i, pwm[i].value_rest);
+ }
+ }
+}
+
+void
+pwm_set (uint8_t index, int16_t value)
+{
+ assert (index < UTILS_COUNT (pwm));
+ assert (value <= PWM_MAX && value >= -PWM_MAX);
+ pwm[index].time = 0;
+ if (value >= 0)
+ {
+ *pwm[index].dir_port &= ~pwm[index].dir_bv;
+ *pwm[index].ocr = value;
+ }
+ else
+ {
+ *pwm[index].dir_port |= pwm[index].dir_bv;
+ *pwm[index].ocr = -value;
+ }
+}
+
+void
+pwm_set_timed (uint8_t index, int16_t value, uint16_t time, int16_t value_rest)
+{
+ assert (index < UTILS_COUNT (pwm));
+ assert (value <= PWM_MAX && value >= -PWM_MAX);
+ assert (value_rest <= PWM_MAX && value_rest >= -PWM_MAX);
+ pwm_set (index, value);
+ pwm[index].time = time;
+ pwm[index].value_rest = value_rest;
+}
+
diff --git a/digital/io-hub/src/common/pwm.h b/digital/io-hub/src/common/pwm.h
new file mode 100644
index 00000000..aff84b39
--- /dev/null
+++ b/digital/io-hub/src/common/pwm.h
@@ -0,0 +1,60 @@
+#ifndef pwm_h
+#define pwm_h
+/* pwm.h */
+/* io-hub - Modular Input/Output. {{{
+ *
+ * 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.
+ *
+ * }}} */
+
+/** Handle PWM output with direction. Also handle timed PWM setting to apply
+ * a PWM value for a given number of update, then switch to a resting value.
+ *
+ * AC_IOHUB_PWM should be set to a list of space separated PWM macro call:
+ *
+ * PWM (timer, pwm, pwm_io_port, pwm_io_n, dir_io_port, dir_io_n)
+ *
+ * For example:
+ *
+ * PWM (1, A, B, 1, B, 2) => use output compare A on timer 1, PWM output on
+ * B1, direction on B2. */
+
+/** Maximum positive value. */
+#define PWM_MAX 0x3ff
+
+/** Initialise PWM module. */
+void
+pwm_init (void);
+
+/** Update PWM module (handle timed PWM). */
+void
+pwm_update (void);
+
+/** Set PWM value for given PWM index. */
+void
+pwm_set (uint8_t index, int16_t value);
+
+/** Set PWM value for given PWM index for a given time. After time elapsed,
+ * use a rest value. */
+void
+pwm_set_timed (uint8_t index, int16_t value, uint16_t time, int16_t value_rest);
+
+#endif /* pwm_h */
diff --git a/digital/io-hub/src/common/pwm.host.c b/digital/io-hub/src/common/pwm.host.c
new file mode 100644
index 00000000..f528522a
--- /dev/null
+++ b/digital/io-hub/src/common/pwm.host.c
@@ -0,0 +1,109 @@
+/* pwm.host.c */
+/* io-hub - Modular Input/Output. {{{
+ *
+ * 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.
+ *
+ * }}} */
+#include "common.h"
+#include "pwm.h"
+
+#include "modules/utils/utils.h"
+#include "modules/host/host.h"
+#include "modules/host/mex.h"
+
+/** PWM context. */
+struct pwm_t
+{
+ /** Current value. */
+ int16_t value;
+ /** Remaining time before timer elapse, 0 for disabled. */
+ uint16_t time;
+ /** Value to be used after timer elapse. */
+ int16_t value_rest;
+};
+
+/** Array of PWM contexts. */
+#define PWM(timer, pwm, pwm_io_port, pwm_io_n, dir_io_port, dir_io_n) \
+ { 0, 0, 0 },
+struct pwm_t pwm[] = {
+ AC_IOHUB_PWM
+};
+#undef PWM
+
+/** Message type. */
+uint8_t pwm_mtype;
+
+/** Message has to be sent. */
+uint8_t pwm_changed;
+
+void
+pwm_init (void)
+{
+ const char *mex_instance = host_get_instance ("io-hub0", 0);
+ pwm_mtype = mex_node_reservef ("%s:pwm", mex_instance);
+}
+
+void
+pwm_update (void)
+{
+ uint8_t i;
+ /* Update timed PWM. */
+ for (i = 0; i < UTILS_COUNT (pwm); i++)
+ {
+ if (pwm[i].time)
+ {
+ pwm[i].time--;
+ if (pwm[i].time == 0)
+ pwm_set (i, pwm[i].value_rest);
+ }
+ }
+ /* Send change if needed. */
+ if (pwm_changed)
+ {
+ mex_msg_t *m = mex_msg_new (pwm_mtype);
+ for (i = 0; i < UTILS_COUNT (pwm); i++)
+ mex_msg_push (m, "h", pwm[i].value);
+ mex_node_send (m);
+ pwm_changed = 0;
+ }
+}
+
+void
+pwm_set (uint8_t index, int16_t value)
+{
+ assert (index < UTILS_COUNT (pwm));
+ assert (value <= PWM_MAX && value >= -PWM_MAX);
+ pwm[index].time = 0;
+ pwm[index].value = value;
+ pwm_changed = 1;
+}
+
+void
+pwm_set_timed (uint8_t index, int16_t value, uint16_t time, int16_t value_rest)
+{
+ assert (index < UTILS_COUNT (pwm));
+ assert (value <= PWM_MAX && value >= -PWM_MAX);
+ assert (value_rest <= PWM_MAX && value_rest >= -PWM_MAX);
+ pwm_set (index, value);
+ pwm[index].time = time;
+ pwm[index].value_rest = value_rest;
+}
+
diff --git a/digital/io-hub/src/robospierre/Makefile b/digital/io-hub/src/robospierre/Makefile
index 768fe9a4..9d0035fe 100644
--- a/digital/io-hub/src/robospierre/Makefile
+++ b/digital/io-hub/src/robospierre/Makefile
@@ -4,6 +4,7 @@ BASE = ../../../avr
PROGS = io_hub
# Sources to compile.
io_hub_SOURCES = main.c \
+ pwm.avr.c pwm.host.c \
twi_master.c asserv.c mimot.c \
chrono.c timer.avr.c simu.host.c
# Modules needed for IO.
@@ -17,8 +18,10 @@ AVR_MCU = at90usb1287
OPTIMIZE = -O2
HOST_LIBS = -lm
+vpath %.c ../common
+vpath %.h ../common
vpath %.c $(AI_MODULES:%=../../../ai/src/%)
vpath %.h $(AI_MODULES:%=../../../ai/src/%)
-INCLUDES += -I. $(AI_MODULES:%=-I../../../ai/src/%)
+INCLUDES += -I. -I../common $(AI_MODULES:%=-I../../../ai/src/%)
include $(BASE)/make/Makefile.gen
diff --git a/digital/io-hub/src/robospierre/avrconfig.h b/digital/io-hub/src/robospierre/avrconfig.h
index 11635646..0fc2f11d 100644
--- a/digital/io-hub/src/robospierre/avrconfig.h
+++ b/digital/io-hub/src/robospierre/avrconfig.h
@@ -103,5 +103,13 @@
/* io-hub - io/ai board. */
/** TWI address of the io board. */
#define AC_IO_TWI_ADDRESS 10
+/** PWM setting. */
+#define AC_IOHUB_PWM \
+ PWM (1, A, B, 5, D, 4) \
+ PWM (1, B, B, 6, D, 5) \
+ PWM (1, C, B, 7, D, 6) \
+ PWM (3, A, C, 6, C, 7) \
+ PWM (3, B, C, 5, C, 3) \
+ PWM (3, C, C, 4, C, 2)
#endif /* avrconfig_h */
diff --git a/digital/io-hub/src/robospierre/main.c b/digital/io-hub/src/robospierre/main.c
index 5a987240..c0937615 100644
--- a/digital/io-hub/src/robospierre/main.c
+++ b/digital/io-hub/src/robospierre/main.c
@@ -36,6 +36,8 @@
#include "mimot.h"
#include "twi_master.h"
+#include "pwm.h"
+
#include "io.h"
/** Our color. */
@@ -58,6 +60,8 @@ main_init (void)
asserv_init ();
mimot_init ();
twi_master_init ();
+ /* IO modules. */
+ pwm_init ();
/* Initialization done. */
proto_send0 ('z');
}
@@ -88,6 +92,8 @@ main_loop (void)
/* Handle commands from UART. */
while (uart0_poll ())
proto_accept (uart0_getc ());
+ /* Update IO modules. */
+ pwm_update ();
/* Only manage events if slaves are synchronised. */
if (twi_master_sync ())
main_event_to_fsm ();
@@ -116,6 +122,21 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
/* Reset */
utils_reset ();
break;
+ case c ('w', 4):
+ /* Set PWM.
+ * - 1w: index.
+ * - 1w: value. */
+ pwm_set (v8_to_v16 (args[0], args[1]), v8_to_v16 (args[2], args[3]));
+ break;
+ case c ('w', 8):
+ /* Set timed PWM.
+ * - 1w: index.
+ * - 1w: value.
+ * - 1w: time.
+ * - 1w: rest value. */
+ pwm_set_timed (v8_to_v16 (args[0], args[1]), v8_to_v16 (args[2], args[3]),
+ v8_to_v16 (args[4], args[5]), v8_to_v16 (args[6], args[7]));
+ break;
/* Stats commands.
* - b: interval between stats. */
case c ('A', 1):