summaryrefslogtreecommitdiff
path: root/n/avr/rs232/rs232.c
diff options
context:
space:
mode:
Diffstat (limited to 'n/avr/rs232/rs232.c')
-rw-r--r--n/avr/rs232/rs232.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/n/avr/rs232/rs232.c b/n/avr/rs232/rs232.c
new file mode 100644
index 0000000..a3984f7
--- /dev/null
+++ b/n/avr/rs232/rs232.c
@@ -0,0 +1,115 @@
+/* rs232.c */
+/* n.avr.rs232 - AVR RS232 Module. {{{
+ *
+ * Copyright (C) 2004 Nicolas Schodet
+ *
+ * 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.
+ *
+ * Contact :
+ * Web: http://perso.efrei.fr/~schodet/
+ * Email: <contact@ni.fr.eu.org>
+ * }}} */
+#include "rs232.h"
+
+#include <avr/io.h>
+
+/* Tested AVR check. */
+#if defined (__AVR_ATmega8__)
+# define SELECTOR _BV (URSEL)
+#elif defined (__AVR_ATmega8535__)
+# define SELECTOR _BV (URSEL)
+#elif defined (__AVR_ATmega128__)
+# define UBRRH UBRR0H
+# define UBRRL UBRR0L
+# define UCSRA UCSR0A
+# define UCSRB UCSR0B
+# define UCSRC UCSR0C
+# define UDR UDR0
+# define SELECTOR 0
+#else
+# warning "rs232: not tested on this chip."
+#endif
+
+/* Unimplemented features. */
+#if AC_RS232_INTERRUPT
+# error "rs232: interrupts to be implemented."
+#endif
+
+#if AC_RS232_CHAR_SIZE != 8
+# error "rs232: char size != 8 not implemented."
+#endif
+
+/* Baud rate error check. */
+#define UBRR_VAL (AC_FREQ / 16 / AC_RS232_BAUDRATE - 1)
+#define BAUDRATE (AC_FREQ / 16 / (UBRR_VAL + 1))
+
+#if BAUDRATE - AC_RS232_BAUDRATE > 2
+#warning "rs232: baud rate error > 2."
+#elif BAUDRATE - AC_RS232_BAUDRATE > 1
+#warning "rs232: baud rate error > 1."
+#elif BAUDRATE - AC_RS232_BAUDRATE > 0
+#warning "rs232: baud rate error > 0."
+#endif
+
+/* Parity. */
+#define ODD (_BV (UPM1) | _BV (UPM0))
+#define EVEN _BV (UPM1)
+#define NONE 0
+#define PARITY AC_RS232_PARITY
+
+/* Stop bits. */
+#if AC_RS232_STOP_BITS == 1
+#define STOP_BITS 0
+#elif AC_RS232_STOP_BITS == 2
+#define STOP_BITS _BV (USBS)
+#else
+#error "rs232: bad stop bits value."
+#endif
+
+/* Character size. */
+#define CHAR_SIZE (_BV (UCSZ1) | _BV (UCSZ0))
+
+/* +AutoDec */
+/* -AutoDec */
+
+/* Initialise rs232. */
+void
+rs232_init (void)
+{
+ /* Set baud rate. */
+ UBRRH = UBRR_VAL >> 8;
+ UBRRL = UBRR_VAL & 0xff;
+ UCSRA = 0;
+ /* Set format and enable rs232. */
+ UCSRC = SELECTOR | PARITY | STOP_BITS | CHAR_SIZE;
+ UCSRB = _BV (RXEN) | _BV (TXEN);
+}
+
+/* Read a char. */
+unsigned char
+rs232_getc (void)
+{
+ loop_until_bit_is_set (UCSRA, RXC);
+ return UDR;
+}
+
+/* Write a char. */
+void
+rs232_putc (unsigned char c)
+{
+ loop_until_bit_is_set (UCSRA, UDRE);
+ UDR = c;
+}
+