From b8d4b6293bbc4ee12e0102db7858bb07d0ac57b8 Mon Sep 17 00:00:00 2001 From: Maxime Hadjinlian Date: Sat, 28 May 2011 20:06:13 +0200 Subject: digital/io-serial/src/codebar: initial codebar code --- digital/io-serial/src/codebar/Makefile | 16 +++ digital/io-serial/src/codebar/avrconfig.h | 110 +++++++++++++++++++ digital/io-serial/src/codebar/codebar.c | 171 ++++++++++++++++++++++++++++++ digital/io-serial/src/codebar/codebar.h | 39 +++++++ 4 files changed, 336 insertions(+) create mode 100644 digital/io-serial/src/codebar/Makefile create mode 100644 digital/io-serial/src/codebar/avrconfig.h create mode 100644 digital/io-serial/src/codebar/codebar.c create mode 100644 digital/io-serial/src/codebar/codebar.h (limited to 'digital/io-serial') diff --git a/digital/io-serial/src/codebar/Makefile b/digital/io-serial/src/codebar/Makefile new file mode 100644 index 00000000..a62147a3 --- /dev/null +++ b/digital/io-serial/src/codebar/Makefile @@ -0,0 +1,16 @@ +BASE = ../../../avr +AVR_PROGS = codebar_reader +codebar_reader_SOURCES = codebar.c timer.avr.c +MODULES = proto twi uart utils +AI_MODULES = utils +CONFIGFILE = avrconfig.h +AVR_MCU = atmega164p +# -O2 : speed +# -Os : size +OPTIMIZE = -Os + +vpath %.c $(AI_MODULES:%=../../../ai/src/%) +vpath %.h $(AI_MODULES:%=../../../ai/src/%) +INCLUDES += $(AI_MODULES:%=-I../../../ai/src/%) + +include $(BASE)/make/Makefile.gen diff --git a/digital/io-serial/src/codebar/avrconfig.h b/digital/io-serial/src/codebar/avrconfig.h new file mode 100644 index 00000000..6acd0e81 --- /dev/null +++ b/digital/io-serial/src/codebar/avrconfig.h @@ -0,0 +1,110 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h - Codebar configuration template. */ +/* io-serial.codebar - Codebar AVR module. {{{ + * + * Copyright (C) 2011 Nicolas Schodet + * + * Robot APB Team/Efrei 2011. + * 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 + +/* uart - UART module. */ +/** Select hardware uart for primary uart: 0, 1 or -1 to disable. */ +#define AC_UART0_PORT 0 +/** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, + * 115200, 230400, 250000, 500000, 1000000. */ +#define AC_UART0_BAUDRATE 115200 +/** 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 PTS +/** 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 + +/* 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 + +/* proto - Protocol module. */ +/** Maximum argument size. */ +#define AC_PROTO_ARGS_MAX_SIZE 12 +/** 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/io-serial/src/codebar/codebar.c b/digital/io-serial/src/codebar/codebar.c new file mode 100644 index 00000000..056508c3 --- /dev/null +++ b/digital/io-serial/src/codebar/codebar.c @@ -0,0 +1,171 @@ +/* codebar.c */ +/* codebar - Codebar Reader. {{{ + * + * Copyright (C) 2011 + * + * 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 +#include +#include + +#include "common.h" +#include "codebar.h" +#include "modules/twi/twi.h" +#include "modules/proto/proto.h" +#include "modules/uart/uart.h" +#include "modules/utils/utils.h" +#include "modules/utils/byte.h" +#include "modules/utils/crc.h" +#include "timer.h" + +/* from robospierre/element.h file */ +#define ELEMENT_UNKOWN 0 +#define ELEMENT_PAWN 1 +#define ELEMENT_QUEEN 2 +#define ELEMENT_KING 4 + +#define KING "KING" +#define QUEEN "QUEEN" +#define PAWN "PAWN" + +#define STRING_MAX 10 + +struct status_t +{ + uint16_t age1; + uint8_t piece1; + uint16_t age2; + uint8_t piece2; +}; + +void proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +{ + /* nothing here*/ +} + +uint8_t string_to_element(char* data) +{ + if (strlen(data) == 5) + return ELEMENT_QUEEN; + if (strcmp(data, KING) == 0) + return ELEMENT_KING; + if (strcmp(data, PAWN) == 0) + return ELEMENT_PAWN; + return ELEMENT_UNKOWN; +} + +char* read_string(int uart_port) +{ + static int cnt_char_u0 = 0; + static char buffer_u0[STRING_MAX] = {'\0'}; + static int cnt_char_u1 = 0; + static char buffer_u1[STRING_MAX] = {'\0'}; + + char c; + + if (uart_port == 0 && uart0_poll ()) + { + while ( uart0_poll() && (c = uart0_getc()) != '\r') + { + buffer_u0[cnt_char_u0] = c; + cnt_char_u0++; + } + buffer_u0[cnt_char_u0+1] = '\0'; + cnt_char_u0 = 0; + return buffer_u0; + } + else if (uart_port == 1 && uart1_poll()) + { + while ( uart1_poll() && (c = uart1_getc()) != '\r') + { + buffer_u1[cnt_char_u1] = c; + cnt_char_u1++; + } + buffer_u1[cnt_char_u1+1] = '\0'; + cnt_char_u1 = 0; + return buffer_u1; + } + else + { + return NULL; + } +} + +int +main (int argc, char **argv) +{ + char* buffer; + struct status_t status; + + status.age1 = 0; + status.piece1 = ELEMENT_UNKOWN; + status.age2 = 0; + status.piece2 = ELEMENT_UNKOWN; + + timer_init (); + avr_init (argc, argv); + sei (); + uart0_init (); + uart1_init (); + /* We have successfully boot. */ + proto_send0 ('z'); + /* Initialize TWI. */ + twi_init (0x04); + /* I am a slave. */ + proto_send0 ('S'); + + while (1) + { + /* Wait until next cycle. */ + timer_wait (); + if (status.age1 < (uint16_t) -1) + status.age1 ++; + if (status.age2 < (uint16_t) -1) + status.age2 ++; + + if ((buffer = read_string(0)) != NULL) + { + status.piece1 = string_to_element(buffer); + } + + if ((buffer = read_string(1)) != NULL) + { + status.piece2 = string_to_element(buffer); + } + + uint8_t status_with_crc[8]; + uint8_t *status_twi = &status_with_crc[1]; + status_twi[0] = v16_to_v8 (status.age1, 0); + status_twi[1] = v16_to_v8 (status.age1, 1); + status_twi[2] = status.piece1; + status_twi[3] = v16_to_v8 (status.age2, 0); + status_twi[4] = v16_to_v8 (status.age2, 1); + status_twi[5] = status.piece2; + status_twi[6] = 42; + status_twi[7] = 32; + /* Compute CRC. */ + status_with_crc[0] = crc_compute (&status_with_crc[1], sizeof (status_with_crc) - 1); + twi_slave_update (status_with_crc, sizeof (status_with_crc)); + + } + return 0; +} diff --git a/digital/io-serial/src/codebar/codebar.h b/digital/io-serial/src/codebar/codebar.h new file mode 100644 index 00000000..5b84f898 --- /dev/null +++ b/digital/io-serial/src/codebar/codebar.h @@ -0,0 +1,39 @@ +#ifndef codebar_h +#define codebar_h +/* codebar.h */ +/* {{{ + * + * Copyright (C) 2011 + * + * Robot APB Team/Efrei 2011 + * 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. + * + * }}} */ + +/* Read a string from UART0 and return NULL or the string read. */ +char* read_string(int uart_port); + +/* Take a string and check it againt the list of valid element + * as defined in io-hub/src/robospierre/element.h and return the + * u8 value of this element. + */ +uint8_t string_to_element(char* data); + +void proto_callback (uint8_t cmd, uint8_t size, uint8_t *args); + +#endif /* codebar_h */ -- cgit v1.2.3