From db212811293f91e0f1147eb57e5f6d5df2db10ef Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 29 Nov 2010 23:07:46 +0100 Subject: digital/avr/modules/twi: cleanup TWI module, refs #29, closes #90 - use contexts - changed interface - merge test programs - share polling mechanism - prepare place for other drivers - add callback system - use english comments --- digital/avr/modules/twi/test/Makefile | 21 --- digital/avr/modules/twi/test/Makefile.master | 25 --- digital/avr/modules/twi/test/Makefile.slave | 24 --- digital/avr/modules/twi/test/avrconfig_master.h | 97 ----------- digital/avr/modules/twi/test/avrconfig_slave.h | 97 ----------- digital/avr/modules/twi/test/master/Makefile | 14 ++ digital/avr/modules/twi/test/master/avrconfig.h | 103 +++++++++++ digital/avr/modules/twi/test/slave/Makefile | 14 ++ digital/avr/modules/twi/test/slave/avrconfig.h | 110 ++++++++++++ digital/avr/modules/twi/test/test_twi.c | 202 +++++++++++++++++++++ digital/avr/modules/twi/test/test_twi.py | 26 +++ digital/avr/modules/twi/test/test_twi_host.py | 29 --- digital/avr/modules/twi/test/test_twi_master.c | 223 ------------------------ digital/avr/modules/twi/test/test_twi_sl.c | 90 ---------- 14 files changed, 469 insertions(+), 606 deletions(-) delete mode 100644 digital/avr/modules/twi/test/Makefile delete mode 100644 digital/avr/modules/twi/test/Makefile.master delete mode 100644 digital/avr/modules/twi/test/Makefile.slave delete mode 100644 digital/avr/modules/twi/test/avrconfig_master.h delete mode 100644 digital/avr/modules/twi/test/avrconfig_slave.h create mode 100644 digital/avr/modules/twi/test/master/Makefile create mode 100644 digital/avr/modules/twi/test/master/avrconfig.h create mode 100644 digital/avr/modules/twi/test/slave/Makefile create mode 100644 digital/avr/modules/twi/test/slave/avrconfig.h create mode 100644 digital/avr/modules/twi/test/test_twi.c create mode 100644 digital/avr/modules/twi/test/test_twi.py delete mode 100644 digital/avr/modules/twi/test/test_twi_host.py delete mode 100644 digital/avr/modules/twi/test/test_twi_master.c delete mode 100644 digital/avr/modules/twi/test/test_twi_sl.c (limited to 'digital/avr/modules/twi/test') diff --git a/digital/avr/modules/twi/test/Makefile b/digital/avr/modules/twi/test/Makefile deleted file mode 100644 index 1af68de3..00000000 --- a/digital/avr/modules/twi/test/Makefile +++ /dev/null @@ -1,21 +0,0 @@ -# Quick dirty makefile to help you build the test program for twi. -# It is required to clean before building each program because they share the -# same object with different defines. -# Files are copied in *.keep.hex. -all: subclean - # Slave - $(MAKE) -f Makefile.slave - cp test_twi_sl.hex test_twi_sl.keep.hex - $(MAKE) -f Makefile.slave clean - # Master - $(MAKE) -f Makefile.master - cp test_twi_master.hex test_twi_master.keep.hex - $(MAKE) -f Makefile.master clean - -subclean: - # General cleaning - $(MAKE) -f Makefile.slave clean - $(MAKE) -f Makefile.master clean - -clean: subclean - rm -f test_twi_master.keep.hex test_twi_sl.keep.hex diff --git a/digital/avr/modules/twi/test/Makefile.master b/digital/avr/modules/twi/test/Makefile.master deleted file mode 100644 index 55a762c0..00000000 --- a/digital/avr/modules/twi/test/Makefile.master +++ /dev/null @@ -1,25 +0,0 @@ -BASE = ../../.. -PROGS = test_twi_master -test_twi_master_SOURCES = test_twi_master.c -DOC = -EXTRACTDOC = -MODULES = twi uart proto math/random utils - - -CONFIGFILE = avrconfig_master.h -TEST_CONFIGFILES = avrconfig_master.h - - -# atmega8, atmega8535, atmega128... -AVR_MCU = atmega128 -# -O2 : speed -# -Os : size -OPTIMIZE = -O2 - -DEFS = -LIBS = - -# Test compilations. -TEST_MCU = atmega8535 atmega128 - -include $(BASE)/make/Makefile.gen diff --git a/digital/avr/modules/twi/test/Makefile.slave b/digital/avr/modules/twi/test/Makefile.slave deleted file mode 100644 index 35f84bcd..00000000 --- a/digital/avr/modules/twi/test/Makefile.slave +++ /dev/null @@ -1,24 +0,0 @@ -BASE = ../../.. -PROGS = test_twi_sl -test_twi_sl_SOURCES = test_twi_sl.c -DOC = -EXTRACTDOC = -MODULES = twi uart proto utils - - -CONFIGFILE = avrconfig_slave.h -TEST_CONFIGFILES = avrconfig_slave.h - -# atmega8, atmega8535, atmega128... -AVR_MCU = atmega128 -# -O2 : speed -# -Os : size -OPTIMIZE = -O2 - -DEFS = -LIBS = - -# Test compilations. -TEST_MCU = atmega8535 atmega128 - -include $(BASE)/make/Makefile.gen diff --git a/digital/avr/modules/twi/test/avrconfig_master.h b/digital/avr/modules/twi/test/avrconfig_master.h deleted file mode 100644 index a65f872f..00000000 --- a/digital/avr/modules/twi/test/avrconfig_master.h +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef avrconfig_h -#define avrconfig_h -/* avrconfig.h */ -/* avr.twi - TWI AVR module. {{{ - * - * Copyright (C) 2005 Demonchy Clément - * - * Robot APB Team/Efrei 2006. - * Web: http://assos.efrei.fr/robot/ - * Email: robot AT efrei DOT fr - * - * 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 - -/* twi - TWI module. */ -/** Activate master part. */ -#define AC_TWI_MASTER_ENABLE 1 -/** Activate slave part. */ -#define AC_TWI_SLAVE_ENABLE 0 -/** Slave recv buffer size. */ -#define AC_TWI_SL_RECV_BUFFER_SIZE 16 -/** Slave send buffer size. */ -#define AC_TWI_SL_SEND_BUFFER_SIZE 16 - -/* proto - Protocol module. */ -/** Maximum argument size. */ -#define AC_PROTO_ARGS_MAX_SIZE 16 -/** 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 - -/* 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 POLLING -/** Recv mode, same as send mode. */ -#define AC_UART0_RECV_MODE POLLING -/** 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 WAIT -/** 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 STDIO - - -#endif /* avrconfig_h */ diff --git a/digital/avr/modules/twi/test/avrconfig_slave.h b/digital/avr/modules/twi/test/avrconfig_slave.h deleted file mode 100644 index 2635810d..00000000 --- a/digital/avr/modules/twi/test/avrconfig_slave.h +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef avrconfig_h -#define avrconfig_h -/* avrconfig.h */ -/* avr.twi - TWI AVR module. {{{ - * - * Copyright (C) 2005 Demonchy Clément - * - * Robot APB Team/Efrei 2006. - * Web: http://assos.efrei.fr/robot/ - * Email: robot AT efrei DOT fr - * - * 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 - -/* twi - TWI module. */ -/** Activate master part. */ -#define AC_TWI_MASTER_ENABLE 0 -/** Activate slave part. */ -#define AC_TWI_SLAVE_ENABLE 1 -/** Slave recv buffer size. */ -#define AC_TWI_SL_RECV_BUFFER_SIZE 16 -/** Slave send buffer size. */ -#define AC_TWI_SL_SEND_BUFFER_SIZE 16 - -/* 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 - -/* 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 POLLING -/** Recv mode, same as send mode. */ -#define AC_UART0_RECV_MODE POLLING -/** 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 WAIT -/** 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 STDIO - - -#endif /* avrconfig_h */ diff --git a/digital/avr/modules/twi/test/master/Makefile b/digital/avr/modules/twi/test/master/Makefile new file mode 100644 index 00000000..59e36f55 --- /dev/null +++ b/digital/avr/modules/twi/test/master/Makefile @@ -0,0 +1,14 @@ +BASE = ../../../.. +PROGS = test_twi_master +test_twi_master_SOURCES = test_twi.c +MODULES = twi uart proto math/random utils +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +AVR_MCU = atmega128 +# -O2 : speed +# -Os : size +OPTIMIZE = -Os + +vpath %.c .. + +include $(BASE)/make/Makefile.gen diff --git a/digital/avr/modules/twi/test/master/avrconfig.h b/digital/avr/modules/twi/test/master/avrconfig.h new file mode 100644 index 00000000..c3b572a3 --- /dev/null +++ b/digital/avr/modules/twi/test/master/avrconfig.h @@ -0,0 +1,103 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h */ +/* avr.twi - TWI AVR module. {{{ + * + * 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. + * + * }}} */ + +/* utils */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 14745600 + +/* 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 WAIT +/** 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 32 +/** 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 + +/* twi - TWI module. */ +/** Driver to implement TWI: HARD, SOFT, or USI. */ +#define AC_TWI_DRIVER HARD +/** Do not use interrupts. */ +#define AC_TWI_NO_INTERRUPT 0 +/** TWI frequency, should really be 100 kHz. */ +#define AC_TWI_FREQ 100000 +/** Enable slave part. */ +#define AC_TWI_SLAVE_ENABLE 0 +/** Enable master part. */ +#define AC_TWI_MASTER_ENABLE 1 +/** Master transfer completion callback, optionally defined by the user, called + * at end of master transfer. */ +#undef AC_TWI_MASTER_DONE +/** Use internal pull up. */ +#define AC_TWI_PULL_UP 0 + +#endif /* avrconfig_h */ diff --git a/digital/avr/modules/twi/test/slave/Makefile b/digital/avr/modules/twi/test/slave/Makefile new file mode 100644 index 00000000..fbabd11d --- /dev/null +++ b/digital/avr/modules/twi/test/slave/Makefile @@ -0,0 +1,14 @@ +BASE = ../../../.. +PROGS = test_twi_slave +test_twi_slave_SOURCES = test_twi.c +MODULES = twi uart proto math/random utils +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +AVR_MCU = atmega128 +# -O2 : speed +# -Os : size +OPTIMIZE = -Os + +vpath %.c .. + +include $(BASE)/make/Makefile.gen diff --git a/digital/avr/modules/twi/test/slave/avrconfig.h b/digital/avr/modules/twi/test/slave/avrconfig.h new file mode 100644 index 00000000..3d01c881 --- /dev/null +++ b/digital/avr/modules/twi/test/slave/avrconfig.h @@ -0,0 +1,110 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h */ +/* avr.twi - TWI AVR module. {{{ + * + * 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. + * + * }}} */ + +/* utils */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 14745600 + +/* 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 WAIT +/** 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 32 +/** 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 + +/* twi - TWI module. */ +/** Driver to implement TWI: HARD, SOFT, or USI. */ +#define AC_TWI_DRIVER HARD +/** Do not use interrupts. */ +#define AC_TWI_NO_INTERRUPT 0 +/** TWI frequency, should really be 100 kHz. */ +#define AC_TWI_FREQ 100000 +/** Enable slave part. */ +#define AC_TWI_SLAVE_ENABLE 1 +/** Enable master part. */ +#define AC_TWI_MASTER_ENABLE 0 +/** Use polled slave mode: received data is stored in a buffer which can be + * polled using twi_slave_poll. */ +#define AC_TWI_SLAVE_POLLED 1 +/** Slave reception callback to be defined by the user when not in polled + * mode. */ +#undef AC_TWI_SLAVE_RECV +/** Use internal pull up. */ +#define AC_TWI_PULL_UP 0 +/** Slave reception buffer size. */ +#define AC_TWI_SLAVE_RECV_BUFFER_SIZE 16 +/** Slave transmission buffer size. */ +#define AC_TWI_SLAVE_SEND_BUFFER_SIZE 16 + +#endif /* avrconfig_h */ diff --git a/digital/avr/modules/twi/test/test_twi.c b/digital/avr/modules/twi/test/test_twi.c new file mode 100644 index 00000000..f3656b20 --- /dev/null +++ b/digital/avr/modules/twi/test/test_twi.c @@ -0,0 +1,202 @@ +/* test_twi.c */ +/* avr.twi - TWI AVR module. {{{ + * + * Copyright (C) 2005 Clément Demonchy + * 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 "modules/twi/twi.h" +#include "modules/proto/proto.h" +#include "modules/uart/uart.h" +#include "modules/utils/utils.h" +#include "modules/math/random/random.h" +#include "io.h" + +#ifdef HOST +# include "modules/host/mex.h" +#endif + +void +proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +{ +#define c(cmd, size) (cmd << 8 | size) + switch (c (cmd, size)) + { + case c ('z', 0): + /* Reset. */ + utils_reset (); + break; +#if AC_TWI_MASTER_ENABLE + case c ('r', 2): + /* Read n bytes from a slave. + * - b: slave address. + * - b: nb of bytes. */ + { + uint8_t data[args[1]]; + twi_master_recv (args[0], data, args[1]); + uint8_t r = twi_master_wait (); + proto_send ('R', r, data); + } + break; + case c ('t', 3): + /* Test for sending and receiving multiple data. + * It sends a random number of bytes to the slave and then read back + * from it the same number, checking the data are the same as the sent + * ones. + * - slave address. + * - maximum nb of bytes. + * - number of iterations to try on. + */ + { + int error = 0; + uint8_t max_byte_number = args[1]; + uint8_t compt = args[2]; + while (compt-- != 0 && !error) + { + /* Maximum number of bytes of the message */ + /* Get a random number */ + uint32_t random = random_u32 (); + /* Extract a number under max_byte_number */ + uint8_t byte_number = random % max_byte_number + 1; + /* Table which contains the random bytes */ + uint8_t random_bytes[max_byte_number]; + uint8_t i, ret; + for (i = 0; i < byte_number; i++) + { + uint8_t mod = i % 4; + /* Regenerate a new 32 bit random number every 4 bytes */ + if ((mod == 0) && (i != 0)) + random = random_u32 (); + /* Get the part of the random number we need */ + random_bytes[i] = (random >> (mod * 8)) & 0xFF; + } + /* Send data */ + twi_master_send (args[0], random_bytes, byte_number); + ret = twi_master_wait (); + /* Check return */ + if (ret != byte_number) + { + /* Error */ + proto_send3b ('S', compt, ret, byte_number); + error = 1; + break; + } +#ifdef HOST + /* Give time to slave to copy data. */ + mex_node_wait_date (mex_node_date () + 2); +#endif + /* Received buffer */ + uint8_t received_buffer[max_byte_number]; + /* Receive data */ + twi_master_recv (args[0], received_buffer, byte_number); + ret = twi_master_wait (); + /* Check return */ + if (ret != byte_number) + { + /* Error */ + proto_send3b ('R', compt, ret, byte_number); + error = 1; + break; + } + for (i = 0; i < byte_number; i++) + { + if (received_buffer[i] != random_bytes[i]) + { + /* Error */ + proto_send3b ('C', compt, i, byte_number); + error = 1; + break; + } + } + } + } + break; +#endif /* AC_TWI_MASTER_ENABLE */ + default: + /* Variable number of arguments. */ +#if AC_TWI_MASTER_ENABLE + if (cmd == 's') + { + /* Send n bytes to a slave. + * - b: slave address. + * - xb: x data. */ + twi_master_send (args[0], &args[1], size - 1); + uint8_t r = twi_master_wait (); + proto_send1b ('S', r); + } + else +#endif /* AC_TWI_MASTER_ENABLE */ + { + /* Error */ + proto_send0 ('?'); + return; + } + break; + } + /* Acknowledge what has been done */ + proto_send (cmd, size, args); +} + +int +main (int argc, char **argv) +{ + avr_init (argc, argv); +#ifdef HOST + mex_node_connect (); +#endif + /* Enable interruptions. */ + sei (); + /* Initialize serial port. */ + uart0_init (); + /* We have successfully boot. */ + proto_send0 ('z'); + /* Initialize TWI. */ + twi_init (0x04); +#if AC_TWI_MASTER_ENABLE + /* I am a master. */ + proto_send0 ('M'); +#endif +#if AC_TWI_SLAVE_ENABLE + /* I am a slave. */ + proto_send0 ('S'); +#endif + while (42) + { +#ifdef HOST + mex_node_wait_date (mex_node_date () + 1); +#endif +#if AC_TWI_SLAVE_ENABLE + uint8_t data[AC_TWI_SLAVE_RECV_BUFFER_SIZE]; + uint8_t data_len; + /* Check for data. */ + data_len = twi_slave_poll (data, AC_TWI_SLAVE_RECV_BUFFER_SIZE); + if (data_len) + { + /* Echo them back.*/ + twi_slave_update (data, data_len); + } +#endif + while (uart0_poll ()) + proto_accept (uart0_getc ()); + } +} + diff --git a/digital/avr/modules/twi/test/test_twi.py b/digital/avr/modules/twi/test/test_twi.py new file mode 100644 index 00000000..3b227f5b --- /dev/null +++ b/digital/avr/modules/twi/test/test_twi.py @@ -0,0 +1,26 @@ +import os, signal, time + +from mex.hub import Hub +from mex.msg import Msg +from mex.node import Node +from utils.forked import Forked + +h = Hub (min_clients = 3) +fh = Forked (h.wait) + +n = Node () +def nf (): + while True: + time.sleep (1) + n.wait (n.date + 1000) +fn = Forked (nf) + +slave = os.popen ('./slave/test_twi_slave.host', 'w') + +try: + os.system ('./master/test_twi_master.host') +finally: + fn.kill () + fh.kill () + slave.close () + time.sleep (1) diff --git a/digital/avr/modules/twi/test/test_twi_host.py b/digital/avr/modules/twi/test/test_twi_host.py deleted file mode 100644 index 94cd9628..00000000 --- a/digital/avr/modules/twi/test/test_twi_host.py +++ /dev/null @@ -1,29 +0,0 @@ -import os, signal, time - -from mex.hub import Hub -from mex.msg import Msg -from mex.node import Node -from utils.forked import Forked - -def log (x): - print x - -h = Hub (min_clients = 3, log = log) -fh = Forked (h.wait) - -n = Node () -def nf (): - while True: - time.sleep (1) - n.wait (n.date + 1) -fn = Forked (nf) - -slave = os.popen ('./test_twi_sl.host', 'w') - -try: - os.system ('./test_twi_master.host') -finally: - fn.kill () - fh.kill () - slave.close () - time.sleep (1) diff --git a/digital/avr/modules/twi/test/test_twi_master.c b/digital/avr/modules/twi/test/test_twi_master.c deleted file mode 100644 index ff3a570a..00000000 --- a/digital/avr/modules/twi/test/test_twi_master.c +++ /dev/null @@ -1,223 +0,0 @@ -/* test_twi_master.c */ -/* avr.twi.master - TWI master module. {{{ - * - * Copyright (C) 2005 Clément Demonchy - * - * Robot APB Team/Efrei 2006. - * Web: http://assos.efrei.fr/robot/ - * Email: robot AT efrei DOT fr - * - * 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/twi/twi.h" -#include "modules/proto/proto.h" -#include "modules/uart/uart.h" -#include "modules/utils/utils.h" -#include "modules/math/random/random.h" -#include "io.h" - -#ifdef HOST -# include "modules/host/mex.h" -#endif - -void -proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) -{ -#define c(cmd, size) (cmd << 8 | size) - switch (c (cmd, size)) - { - case c ('s', 2): - /* Send one byte to a slave address. - * 2 parameters: - * - slave address; - * - data. - */ - twi_ms_send (args[0], &args[1], 1); - while (!twi_ms_is_finished ()) - ; - break; - case c ('r', 1): - /* Read one byte from an address slave - * 1 parameter: - * - slave address. - */ - { - uint8_t data = 0x00; - int8_t d = twi_ms_read (args[0], &data, 1); - if (d != 0) - proto_send0 ('e'); - else - { - while (!twi_ms_is_finished ()) - ; - proto_send1b ('R', data); - } - } - break; - case c ('r', 2): - /* Read n bytes from an address slave - * 2 parameter: - * - slave address. - * - nb of bytes. - */ - { - uint8_t data[16]; - int8_t d = twi_ms_read (args[0], data, args[1]); - if (d != 0) - proto_send0 ('e'); - else - { - while (!twi_ms_is_finished ()) - ; - proto_send ('R', args[1], data); - } - } - break; - case c ('t', 2): - /* Test for sending and receiving multiple data. - * It sends a random number of bytes to the slave and then read back - * from it the same number, checking the data are the same as the sent - * ones. - * 2 parameters: - * - slave address; - * - number of iterations to try on. - */ - { - int error = 0; - uint8_t compt = args[1]; - while (compt-- != 0) - { - /* Maximum number of bytes of the message */ - static const uint8_t max_byte_number = - AC_TWI_SL_RECV_BUFFER_SIZE; - /* Get a random number */ - uint32_t random = random_u32 (); - /* Extract a number under max_byte_number */ - uint8_t byte_number = random % max_byte_number + 1; - /* Table which contains the random bytes */ - uint8_t random_bytes[max_byte_number]; - uint8_t i; - for (i = 0; i < byte_number; i++) - { - uint8_t mod = i % 4; - /* Regenerate a new 32 bit random number every 4 bytes */ - if ((mod == 0) && (i != 0)) - random = random_u32 (); - /* Get the part of the random number we need */ - random_bytes[i] = (random >> (mod * 8)) & 0xFF; - } - /* Send data */ - twi_ms_send (args[0], random_bytes, byte_number); - while (!twi_ms_is_finished ()) - ; -#ifdef HOST - /* Give time to slave to copy data. */ - mex_node_wait_date (mex_node_date () + 2); -#endif - - /* Received buffer */ - uint8_t received_buffer[max_byte_number]; - /* Receive data */ - int8_t ret = twi_ms_read (args[0], received_buffer, - byte_number); - /* Check return */ - if (ret != 0) - { - /* Error */ - proto_send0 ('e'); - error = 1; - break; - } - while (!twi_ms_is_finished ()) - ; - for (i = 0; i < byte_number; i++) - { - if (received_buffer[i] != random_bytes[i]) - { - error = 1; - break; - } - } - if (error != 0) - break; - } - /* Check for error */ - if (error == 0) - { - proto_send1b ('T', args[1]); - } - } - break; - case c ('z', 0): - /* Reset */ - utils_reset (); - break; - default: - /* Variable number of arguments. */ - if (cmd == 's') - { - /* Send n bytes to a slave address. - * 1+n parameters: - * - slave address; - * - n x data. - */ - twi_ms_send (args[0], &args[1], size - 1); - while (!twi_ms_is_finished ()) - ; - } - else - { - /* Error */ - proto_send0 ('?'); - return; - } - break; - } - /* Acknowledge what has been done */ - proto_send (cmd, size, args); -} - -int -main (int argc, char **argv) -{ - avr_init (argc, argv); -#ifdef HOST - mex_node_connect (); -#endif - /* Enable interruptions */ - sei (); - /* Initialize serial port */ - uart0_init (); - /* We have successfully boot */ - proto_send0 ('z'); - /* Initialize TWI */ - twi_init (0x04); - /* I am a master */ - proto_send0 ('M'); - while (42) - { -#ifdef HOST - mex_node_wait_date (mex_node_date () + 1); - while (uart0_poll ()) - proto_accept (uart0_getc ()); -#else - proto_accept (uart0_getc ()); -#endif - } - return 0; -} diff --git a/digital/avr/modules/twi/test/test_twi_sl.c b/digital/avr/modules/twi/test/test_twi_sl.c deleted file mode 100644 index e52d914f..00000000 --- a/digital/avr/modules/twi/test/test_twi_sl.c +++ /dev/null @@ -1,90 +0,0 @@ -/* test_twi_slave.c */ -/* avr.twi.slave - TWI slave module. {{{ - * - * Copyright (C) 2005 Clément Demonchy - * - * Robot APB Team/Efrei 2006. - * Web: http://assos.efrei.fr/robot/ - * Email: robot AT efrei DOT fr - * - * 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/twi/twi.h" -#include "modules/proto/proto.h" -#include "modules/uart/uart.h" -#include "modules/utils/utils.h" -#include "io.h" - -#ifdef HOST -# include "modules/host/mex.h" -#endif - -void -proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) -{ -#define c(cmd, size) (cmd << 8 | size) - switch (c (cmd, size)) - { - /* Reset */ - case 'z': - utils_reset (); - break; - /* Error */ - default: - proto_send0 ('?'); - return; - } - /* Acknowledge what has been done */ - proto_send (cmd, size, args); -} - -int -main (int argc, char **argv) -{ - avr_init (argc, argv); -#ifdef HOST - mex_node_connect (); -#endif - /* Enable interruptions */ - sei (); - /* Initialize serial port */ - uart0_init (); - /* We have successfully boot */ - proto_send0 ('z'); - /* Initialize TWI */ - twi_init (0x02); - /* I am a slave */ - proto_send0 ('S'); - while (42) - { -#ifdef HOST - mex_node_wait_date (mex_node_date () + 1); -#endif - uint8_t data[AC_TWI_SL_RECV_BUFFER_SIZE]; - data[0] = 0; - /* Check for data */ - if (twi_sl_poll (data, AC_TWI_SL_RECV_BUFFER_SIZE)) - { - /* Receive and store them */ - twi_sl_update (data, AC_TWI_SL_RECV_BUFFER_SIZE); - } - while (uart0_poll ()) - proto_accept (uart0_getc ()); - } - return 0; -} -- cgit v1.2.3