From 99e387e488e94ab78f50529fdc58b3fad7ec9cae Mon Sep 17 00:00:00 2001 From: Jérémy Dufour Date: Sat, 1 Mar 2008 18:13:13 +0100 Subject: * TWI: modify to have a working test on cards: - work with one byte of data exchanged (master send a byte to slave then read it back); - does not manage when slave is not here (no timeout); - use more proto_ functions rather than uart_ ones; - support reset and unknown functions on both test programs; - update configurations to be compatible with the cards used for the test; - add comments. --- digital/avr/modules/twi/test/Makefile.master | 2 +- digital/avr/modules/twi/test/avrconfig_master.h | 4 +- digital/avr/modules/twi/test/avrconfig_slave.h | 2 +- digital/avr/modules/twi/test/test_twi_master.c | 70 ++++++++++++------------- digital/avr/modules/twi/test/test_twi_sl.c | 39 ++++++++++++-- 5 files changed, 73 insertions(+), 44 deletions(-) (limited to 'digital') diff --git a/digital/avr/modules/twi/test/Makefile.master b/digital/avr/modules/twi/test/Makefile.master index fa4f22ed..63d6baf8 100644 --- a/digital/avr/modules/twi/test/Makefile.master +++ b/digital/avr/modules/twi/test/Makefile.master @@ -11,7 +11,7 @@ TEST_CONFIGFILES = avrconfig_master.h # atmega8, atmega8535, atmega128... -AVR_MCU = atmega8535 +AVR_MCU = atmega128 # -O2 : speed # -Os : size OPTIMIZE = -O2 diff --git a/digital/avr/modules/twi/test/avrconfig_master.h b/digital/avr/modules/twi/test/avrconfig_master.h index 6496886d..bc070738 100644 --- a/digital/avr/modules/twi/test/avrconfig_master.h +++ b/digital/avr/modules/twi/test/avrconfig_master.h @@ -47,10 +47,10 @@ /* uart - UART module. */ /** Select hardware uart for primary uart: 0, 1 or -1 to disable. */ -#define AC_UART0_PORT 0 +#define AC_UART0_PORT 1 /** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, * 115200, 230400, 250000, 500000, 1000000. */ -#define AC_UART0_BAUDRATE 9600 +#define AC_UART0_BAUDRATE 38400 /** Send mode: * - POLLING: no interrupts. * - RING: interrupts, ring buffer. */ diff --git a/digital/avr/modules/twi/test/avrconfig_slave.h b/digital/avr/modules/twi/test/avrconfig_slave.h index 6fdfd9ad..9cbfd33b 100644 --- a/digital/avr/modules/twi/test/avrconfig_slave.h +++ b/digital/avr/modules/twi/test/avrconfig_slave.h @@ -50,7 +50,7 @@ #define AC_UART0_PORT 1 /** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, * 115200, 230400, 250000, 500000, 1000000. */ -#define AC_UART0_BAUDRATE 9600 +#define AC_UART0_BAUDRATE 38400 /** Send mode: * - POLLING: no interrupts. * - RING: interrupts, ring buffer. */ diff --git a/digital/avr/modules/twi/test/test_twi_master.c b/digital/avr/modules/twi/test/test_twi_master.c index 348ac950..6349d995 100644 --- a/digital/avr/modules/twi/test/test_twi_master.c +++ b/digital/avr/modules/twi/test/test_twi_master.c @@ -24,70 +24,70 @@ * }}} */ #include "common.h" - -#include "modules/uart/uart.h" -#include "modules/proto/proto.h" #include "modules/twi/twi.h" +#include "modules/proto/proto.h" +#include "modules/uart/uart.h" #include "modules/utils/utils.h" #include "io.h" -void proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +void +proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) { - uint8_t data[1] = {0x00}; - int8_t c; - switch (cmd) +#define c(cmd, size) (cmd << 8 | size) + switch (c (cmd, size)) { - case 's': - if (size == 2) - { - proto_send ('s', 0, 0); - twi_ms_send (args[0], &args[1], 1); - while (!twi_ms_is_finished ()) - ; - proto_send ('f', 0, 0); - } - else - proto_send ('e', 0, 0); + /* Send one byte to a slave address */ + case c ('s', 2): + /* s destination_addr data */ + twi_ms_send (args[0], &args[1], 1); + while (!twi_ms_is_finished ()) + ; break; - case 'r': - if (size == 2) + /* Read one byte from an address slave */ + case c ('r', 1): + /* c slave_address */ { - uart0_putc ('r'); - c = twi_ms_read (args[0], data, 1); - if (c != 0) - proto_send ('e', 1 , 0); + uint8_t data[1] = {0x00}; + int8_t d = twi_ms_read (args[0], data, 1); + if (d != 0) + proto_send0 ('e'); else { while (!twi_ms_is_finished ()) ; - proto_send ('f', 1, data); + proto_send ('R', 1, data); } } - else - proto_send ('e', 1, 0); break; - case 'z': + /* Reset */ + case c ('z', 0): utils_reset (); break; + /* Error */ default: - proto_send ('e', 1, 0); + proto_send0 ('?'); + return; } + /* Acknowledge what has been done */ + proto_send (cmd, size, args); } int main (void) { - uint8_t c; + /* Enable interruptions */ sei (); + /* Initialize serial port */ uart0_init (); + /* We have successfully boot */ + proto_send0 ('z'); + /* Initialize TWI */ twi_init (0x04); - uart0_putc ('m'); - uart0_putc ('s'); - uart0_putc ('s'); - uart0_putc ('\r'); + /* I am a master */ + proto_send0 ('M'); while (42) { - c = uart0_getc (); + uint8_t c = uart0_getc (); proto_accept (c); } return 0; diff --git a/digital/avr/modules/twi/test/test_twi_sl.c b/digital/avr/modules/twi/test/test_twi_sl.c index 8672a6f7..b6cbc050 100644 --- a/digital/avr/modules/twi/test/test_twi_sl.c +++ b/digital/avr/modules/twi/test/test_twi_sl.c @@ -26,26 +26,55 @@ #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" +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 (void) { - uint8_t data[TWI_SL_RCPT_SIZE]; + /* Enable interruptions */ sei (); + /* Initialize serial port */ uart0_init (); - uart0_putc ('s'); - uart0_putc ('s'); - uart0_putc ('s'); + /* We have successfully boot */ + proto_send0 ('z'); + /* Initialize TWI */ twi_init (0x02); - data[0] = 0; + /* I am a slave */ + proto_send0 ('S'); while (42) { + uint8_t data[TWI_SL_RCPT_SIZE]; + data[0] = 0; + /* Check for data */ if (twi_sl_poll (data, TWI_SL_RCPT_SIZE)) { + /* Receive and store them */ twi_sl_update (data, TWI_SL_RCPT_SIZE); } + if (uart0_poll ()) + proto_accept (uart0_getc ()); } return 0; } -- cgit v1.2.3