From dc1a4f2bdbb5723fd0b5a19aa533211b34c06adb Mon Sep 17 00:00:00 2001 From: schodet Date: Tue, 5 Jul 2005 18:39:06 +0000 Subject: Fix: echo sur l'uart host. Add: mode de compilation de test. Add: erreurs à la termios. Fix: détails... --- n/avr/modules/uart/Makefile | 12 ----- n/avr/modules/uart/Makefile.module | 2 + n/avr/modules/uart/avrconfig.h | 7 ++- n/avr/modules/uart/test/Makefile | 22 +++++++++ n/avr/modules/uart/test/avrconfig_polling.h | 71 ++++++++++++++++++++++++++++ n/avr/modules/uart/test/avrconfig_ring.h | 71 ++++++++++++++++++++++++++++ n/avr/modules/uart/test/avrconfig_twoports.h | 71 ++++++++++++++++++++++++++++ n/avr/modules/uart/test/test_uart.c | 59 +++++++++++++++++++++++ n/avr/modules/uart/test_avrconfig.h | 66 -------------------------- n/avr/modules/uart/test_uart.c | 43 ----------------- n/avr/modules/uart/uart.avr.c | 59 +++++++++++++++-------- n/avr/modules/uart/uart.h | 10 ---- n/avr/modules/uart/uart.host.c | 48 +++++++++++++++---- n/avr/modules/uart/uart.txt | 10 ++-- 14 files changed, 385 insertions(+), 166 deletions(-) create mode 100644 n/avr/modules/uart/test/Makefile create mode 100644 n/avr/modules/uart/test/avrconfig_polling.h create mode 100644 n/avr/modules/uart/test/avrconfig_ring.h create mode 100644 n/avr/modules/uart/test/avrconfig_twoports.h create mode 100644 n/avr/modules/uart/test/test_uart.c delete mode 100644 n/avr/modules/uart/test_avrconfig.h delete mode 100644 n/avr/modules/uart/test_uart.c (limited to 'n/avr') diff --git a/n/avr/modules/uart/Makefile b/n/avr/modules/uart/Makefile index 4546be6..f0f2561 100644 --- a/n/avr/modules/uart/Makefile +++ b/n/avr/modules/uart/Makefile @@ -1,17 +1,5 @@ BASE = ../.. -PROGS = test_uart -test_uart_SOURCES = test_uart.c DOC = uart.html EXTRACTDOC = uart.h avrconfig.h -MODULES = uart -CONFIGFILE = test_avrconfig.h -# atmega8, atmega8535, atmega128... -AVR_MCU = atmega8 -# -O2 : speed -# -Os : size -OPTIMIZE = -O2 - -DEFS = -LIBS = include $(BASE)/make/Makefile.gen diff --git a/n/avr/modules/uart/Makefile.module b/n/avr/modules/uart/Makefile.module index 6473e29..09f3b60 100644 --- a/n/avr/modules/uart/Makefile.module +++ b/n/avr/modules/uart/Makefile.module @@ -1 +1,3 @@ uart_SOURCES = uart0.c uart1.c +EXTRA_CLEAN_FILES += uart0.pts uart1.pts +HOST_LIBS += -lutil diff --git a/n/avr/modules/uart/avrconfig.h b/n/avr/modules/uart/avrconfig.h index a1c0691..0710835 100644 --- a/n/avr/modules/uart/avrconfig.h +++ b/n/avr/modules/uart/avrconfig.h @@ -32,7 +32,7 @@ * 115200, 230400, 250000, 500000, 1000000. */ #define AC_UART0_BAUDRATE 115200 /** Send mode: - * - POLLING: no interrupts; + * - POLLING: no interrupts. * - RING: interrupts, ring buffer. */ #define AC_UART0_SEND_MODE RING /** Recv mode, same as send mode. */ @@ -47,6 +47,10 @@ #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 /** Same thing for secondary port. */ #define AC_UART1_PORT -1 #define AC_UART1_BAUDRATE 115200 @@ -57,5 +61,6 @@ #define AC_UART1_STOP_BITS 1 #define AC_UART1_SEND_BUFFER_SIZE 32 #define AC_UART1_RECV_BUFFER_SIZE 32 +#define AC_UART0_SEND_BUFFER_FULL WAIT #endif /* avrconfig_h */ diff --git a/n/avr/modules/uart/test/Makefile b/n/avr/modules/uart/test/Makefile new file mode 100644 index 0000000..3f162fc --- /dev/null +++ b/n/avr/modules/uart/test/Makefile @@ -0,0 +1,22 @@ +BASE = ../../.. +PROGS = test_uart +test_uart_SOURCES = test_uart.c +DOC = +EXTRACTDOC = +MODULES = uart utils +CONFIGFILE = avrconfig_ring.h +# atmega8, atmega8535, atmega128... +AVR_MCU = atmega8 +# -O2 : speed +# -Os : size +OPTIMIZE = -O2 + +DEFS = +LIBS = + +# Test compilations. +TEST_MCU = atmega8 atmega8535 atmega128 +TEST_CONFIGFILES = avrconfig_ring.h avrconfig_polling.h avrconfig_twoports.h +avrconfig_twoports_MCU = atmega128 + +include $(BASE)/make/Makefile.gen diff --git a/n/avr/modules/uart/test/avrconfig_polling.h b/n/avr/modules/uart/test/avrconfig_polling.h new file mode 100644 index 0000000..f56a2e1 --- /dev/null +++ b/n/avr/modules/uart/test/avrconfig_polling.h @@ -0,0 +1,71 @@ +#ifndef avrconfig_polling_h +#define avrconfig_polling_h +/* avrconfig_polling.h - test_uart config file. */ +/* avr.uart - UART AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * 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 + +/* 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 9600 +/** 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 +/** 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 + +#endif /* avrconfig_polling_h */ diff --git a/n/avr/modules/uart/test/avrconfig_ring.h b/n/avr/modules/uart/test/avrconfig_ring.h new file mode 100644 index 0000000..5d80b62 --- /dev/null +++ b/n/avr/modules/uart/test/avrconfig_ring.h @@ -0,0 +1,71 @@ +#ifndef avrconfig_ring_h +#define avrconfig_ring_h +/* avrconfig_ring.h - test_uart config file. */ +/* avr.uart - UART AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * 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 + +/* 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 +/** 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 + +#endif /* avrconfig_ring_h */ diff --git a/n/avr/modules/uart/test/avrconfig_twoports.h b/n/avr/modules/uart/test/avrconfig_twoports.h new file mode 100644 index 0000000..e9551dd --- /dev/null +++ b/n/avr/modules/uart/test/avrconfig_twoports.h @@ -0,0 +1,71 @@ +#ifndef avrconfig_twoports_h +#define avrconfig_twoports_h +/* avrconfig_twoports.h - test_uart config file. */ +/* avr.uart - UART AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * 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 + +/* 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 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 DROP +/** Same thing for secondary port. */ +#define AC_UART1_PORT 1 +#define AC_UART1_BAUDRATE 115200 +#define AC_UART1_SEND_MODE POLLING +#define AC_UART1_RECV_MODE RING +#define AC_UART1_CHAR_SIZE 8 +#define AC_UART1_PARITY ODD +#define AC_UART1_STOP_BITS 2 +#define AC_UART1_SEND_BUFFER_SIZE 32 +#define AC_UART1_RECV_BUFFER_SIZE 16 +#define AC_UART1_SEND_BUFFER_FULL DROP + +#endif /* avrconfig_twoports_h */ diff --git a/n/avr/modules/uart/test/test_uart.c b/n/avr/modules/uart/test/test_uart.c new file mode 100644 index 0000000..be652f2 --- /dev/null +++ b/n/avr/modules/uart/test/test_uart.c @@ -0,0 +1,59 @@ +/* test_uart.c */ +/* avr.uart - UART AVR module. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * 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/uart/uart.h" +#include "modules/utils/utils.h" +#include "io.h" + +int +main (void) +{ + sei (); + uart0_init (); + uart0_putc ('N'); + uart0_putc ('i'); + uart0_putc ('0'); + uart0_putc ('!'); + uart0_putc ('\r'); +#if AC_UART1_PORT != -1 + uart1_init (); + uart1_putc ('N'); + uart1_putc ('i'); + uart1_putc ('1'); + uart1_putc ('!'); + uart1_putc ('\r'); + while (1) + { + utils_delay (0.1); + if (uart0_poll ()) + uart0_putc (uart0_getc ()); + if (uart1_poll ()) + uart1_putc (uart1_getc ()); + } +#else + while (1) + uart0_putc (uart0_getc ()); +#endif +} diff --git a/n/avr/modules/uart/test_avrconfig.h b/n/avr/modules/uart/test_avrconfig.h deleted file mode 100644 index 6070683..0000000 --- a/n/avr/modules/uart/test_avrconfig.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef test_avrconfig_h -#define test_avrconfig_h -/* test_avrconfig.h - test_uart config file. */ -/* avr.uart - UART AVR module. {{{ - * - * Copyright (C) 2005 Nicolas Schodet - * - * 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 - -/* 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 -/** 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 - -#endif /* test_avrconfig_h */ diff --git a/n/avr/modules/uart/test_uart.c b/n/avr/modules/uart/test_uart.c deleted file mode 100644 index 9ef7e7e..0000000 --- a/n/avr/modules/uart/test_uart.c +++ /dev/null @@ -1,43 +0,0 @@ -/* test_uart.c */ -/* avr.uart - UART AVR module. {{{ - * - * Copyright (C) 2005 Nicolas Schodet - * - * 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 "modules/uart/uart.h" - -//#include - -/* +AutoDec */ -/* -AutoDec */ - -int -main (void) -{ - //sei (); - uart0_init (); - uart0_putc ('N'); - uart0_putc ('i'); - uart0_putc ('!'); - uart0_putc ('\r'); - while (1) - uart0_putc (uart0_getc ()); -} diff --git a/n/avr/modules/uart/uart.avr.c b/n/avr/modules/uart/uart.avr.c index eee9679..2f5c87e 100644 --- a/n/avr/modules/uart/uart.avr.c +++ b/n/avr/modules/uart/uart.avr.c @@ -22,14 +22,14 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * }}} */ +#include "common.h" #include "uart.h" #include "uart_common.h" #if AC_UART (PORT) != -1 -#include -#include +#include "io.h" /* Tested AVR check. */ #if defined (__AVR_ATmega8__) @@ -91,14 +91,16 @@ #endif /* Mode. */ -#define POLLING 0 -#define RING 1 +#define POLLING 1 +#define RING 2 #define SEND_MODE AC_UART (SEND_MODE) #define RECV_MODE AC_UART (RECV_MODE) #if RECV_MODE == POLLING # define RECV_IE 0 -#else +#elif RECV_MODE == RING # define RECV_IE _BV (RXCIE) +#else +# error "uart: bad mode" #endif /* Parity. */ @@ -147,9 +149,16 @@ static uint8_t uart_recv_buffer[RECV_BUFFER_SIZE]; static volatile uint8_t uart_recv_head, uart_recv_tail; #endif -/* Last error code. */ +/* Full buffer politic. */ +#define DROP 1 +#define WAIT 2 +#define SEND_BUFFER_FULL AC_UART (SEND_BUFFER_FULL) +#if SEND_BUFFER_FULL != DROP && SEND_BUFFER_FULL != WAIT +# error "uart: bad full buffer politic" +#endif + +/* Error code. */ #define ERROR_BITS (_BV (FE) | _BV (DOR) | _BV (PE)) -static volatile uint8_t uart_error_code; /* +AutoDec */ /* -AutoDec */ @@ -175,7 +184,6 @@ uart_init (void) uart_send_head = 0; uart_send_tail = 0; #endif - uart_error_code = 0; } /** Read a char. */ @@ -183,12 +191,14 @@ uint8_t uart_getc (void) { #if RECV_MODE == POLLING - uint8_t tmperr; + uint8_t tmperr, c; loop_until_bit_is_set (UCSRA, RXC); tmperr = UCSRA & ERROR_BITS; + c = UDR; if (tmperr) - uart_error_code = tmperr; - return UDR; + return 0xff; + else + return c; #elif RECV_MODE == RING uint8_t tmptail = uart_recv_tail; while (uart_recv_head == tmptail) @@ -204,26 +214,29 @@ void uart_putc (uint8_t c) { #if SEND_MODE == POLLING +# if SEND_BUFFER_FULL == WAIT loop_until_bit_is_set (UCSRA, UDRE); +# else + if (!(UCSRA & _BV (UDRE))) + return; +# endif UDR = c; #elif SEND_MODE == RING uint8_t tmphead; tmphead = (uart_send_head + 1) & SEND_BUFFER_MASK; +# if SEND_BUFFER_FULL == WAIT while (tmphead == uart_send_tail) ; +# else + if (tmphead == uart_send_tail) + return; +# endif uart_send_buffer[tmphead] = c; uart_send_head = tmphead; UCSRB |= _BV (UDRIE); #endif } -/** Retrieve error condition, 0 if no error. */ -uint8_t -uart_error (void) -{ - return uart_error_code; -} - /** Retrieve availlable chars. */ uint8_t uart_poll (void) @@ -244,14 +257,18 @@ SIGNAL (SIG_UART_RECV) uint8_t tmphead; uint8_t tmperr; tmperr = UCSRA & ERROR_BITS; - if (tmperr) - uart_error_code = tmperr; c = UDR; + if (tmperr) + c = 0xff; tmphead = (uart_recv_head + 1) & RECV_BUFFER_MASK; uart_recv_head = tmphead; /* If overflowed, clear the receive buffer. */ if (tmphead == uart_recv_tail) - uart_error_code = 0xff; + { + tmphead = (tmphead + 1) & RECV_BUFFER_MASK; + uart_recv_head = tmphead; + c = 0xff; + } uart_recv_buffer[tmphead] = c; } diff --git a/n/avr/modules/uart/uart.h b/n/avr/modules/uart/uart.h index 09a65d9..f510a01 100644 --- a/n/avr/modules/uart/uart.h +++ b/n/avr/modules/uart/uart.h @@ -25,8 +25,6 @@ * * }}} */ -#include - /** Initialise uart. */ void uart0_init (void); @@ -39,10 +37,6 @@ uart0_getc (void); void uart0_putc (uint8_t c); -/** Retrieve error condition, 0 if no error. */ -uint8_t -uart0_error (void); - /** Retrieve availlable chars. */ uint8_t uart0_poll (void); @@ -59,10 +53,6 @@ uart1_getc (void); void uart1_putc (uint8_t c); -/** Retrieve error condition, 0 if no error. */ -uint8_t -uart1_error (void); - /** Retrieve availlable chars. */ uint8_t uart1_poll (void); diff --git a/n/avr/modules/uart/uart.host.c b/n/avr/modules/uart/uart.host.c index 3bbd112..661401c 100644 --- a/n/avr/modules/uart/uart.host.c +++ b/n/avr/modules/uart/uart.host.c @@ -23,6 +23,7 @@ * * }}} */ #define _GNU_SOURCE +#include "common.h" #include "uart.h" #include "uart_common.h" @@ -31,26 +32,56 @@ #include #include -#include #include #include #include +#include +#include /* Pseudo terminal file descriptor. */ static int uart_pt_fd; +/** Setup a non canonical mode. */ +static void +setup_raw (int fd) +{ + struct termios tc; + tcgetattr (fd, &tc); + tc.c_iflag &= ~(IGNPAR | PARMRK | ISTRIP | IGNBRK | BRKINT | IGNCR | + ICRNL | INLCR | IXON | IXOFF | IXANY | IMAXBEL); + tc.c_iflag |= INPCK; + tc.c_oflag &= ~(OPOST); + tc.c_cflag &= ~(HUPCL | CSTOPB | PARENB | PARODD | CSIZE); + tc.c_cflag |= CS8 | CLOCAL | CREAD; + tc.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN | NOFLSH | TOSTOP); + tc.c_cc[VTIME] = 0; + tc.c_cc[VMIN] = 1; + tcflush (fd, TCIFLUSH); + tcsetattr (fd, TCSANOW, &tc); +} + /** Initialise uart. */ void uart_init (void) { + int slave_fd; + const char *name; +#define STRINGIFY(x) #x +#define FILE_NAME(x) "uart" STRINGIFY(x) ".pts" /* Open and unlock pt. */ - uart_pt_fd = getpt (); - if (uart_pt_fd == -1) + if (openpty (&uart_pt_fd, &slave_fd, 0, 0, 0) == -1 + || grantpt (uart_pt_fd) == -1 + || unlockpt (uart_pt_fd) == -1) assert_perror (errno); - if (grantpt (uart_pt_fd) == -1 || unlockpt (uart_pt_fd) == -1) + /* Make a link to the slave pts. */ + unlink (FILE_NAME (UART_N)); + name = ptsname (uart_pt_fd); + assert (name); + if (symlink (name, FILE_NAME (UART_N)) == -1) assert_perror (errno); - /* Display its name. */ - printf ("uart%d: %s\n", UART_N, ptsname (uart_pt_fd)); + /* Make slave raw. */ + setup_raw (slave_fd); + /* slave_fd is left open. */ } /** Read a char. */ @@ -62,9 +93,9 @@ uart_getc (void) n = read (uart_pt_fd, &c, 1); if (n == -1) assert_perror (errno); - /* This is quite unusual... */ + /* This is a quite unusual behavior... */ if (n == 0) - return 0; + return 0xff; return c; } @@ -72,7 +103,6 @@ uart_getc (void) void uart_putc (uint8_t c) { - int n; write (uart_pt_fd, &c, 1); } diff --git a/n/avr/modules/uart/uart.txt b/n/avr/modules/uart/uart.txt index bdeebfa..b6f1129 100644 --- a/n/avr/modules/uart/uart.txt +++ b/n/avr/modules/uart/uart.txt @@ -5,11 +5,12 @@ Rien de plus simple. Appeler la fonction |uart0_init| au démarrage, puis |uart0_putc| pour envoyer un caractère, |uart0_getc| pour en recevoir. La -fonction |uart0_poll| permet de connaître à l'avance le nombre de caractères +fonction |uart0_poll| permet de connaître à l'avance si des caractère sont disponibles. -Actuellement la fonction |uart_error| permet de détecter les erreurs de -transmission, mais cela va être remplacé par un mécanisme semblable à termios. +En cas d'erreur de réception, la fonction |uart0_getc| renvois 0xff. Ce n'est +pas forcément adapté à une transmission binaire car on ne peut pas distinguer +un 0xff légitime d'une erreur de transmission. On peut configurer deux uarts, dans ce cas les fonctions sont en double. @@ -17,7 +18,8 @@ Comme pour tous les modules, copier la partie concernant l'uart depuis |avrconfig.h|. Dans le cas de la compilation en host, un pseudo-terminal est ouvert pour les -communications, le nom du pseudo-terminal peut être écrit dans un fichier. +communications, un lien vers le pseudo-terminal est crée en |uart0.pts|. Dans +le futur, on pourra peut-être configurer ce fichier par la ligne de commande. * Doc -- cgit v1.2.3