From 32de814d56c82ceca168155e070fefad5a51ca1c Mon Sep 17 00:00:00 2001 From: schodet Date: Thu, 24 Jun 2004 23:05:13 +0000 Subject: Initial revision --- n/avr/rs232/Makefile | 73 ++++++++++++++++++++++++++++++ n/avr/rs232/README | 1 + n/avr/rs232/avrconfig.h | 45 +++++++++++++++++++ n/avr/rs232/rs232.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++ n/avr/rs232/rs232.h | 44 ++++++++++++++++++ n/avr/rs232/test_rs232.c | 35 +++++++++++++++ 6 files changed, 313 insertions(+) create mode 100644 n/avr/rs232/Makefile create mode 100644 n/avr/rs232/README create mode 100644 n/avr/rs232/avrconfig.h create mode 100644 n/avr/rs232/rs232.c create mode 100644 n/avr/rs232/rs232.h create mode 100644 n/avr/rs232/test_rs232.c diff --git a/n/avr/rs232/Makefile b/n/avr/rs232/Makefile new file mode 100644 index 0000000..8aec9f8 --- /dev/null +++ b/n/avr/rs232/Makefile @@ -0,0 +1,73 @@ +PRG = test_rs232 +OBJ = test_rs232.o rs232.o +# atmega8, atmega8535, atmega128... +MCU_TARGET = atmega8 +OPTIMIZE = -O2 + +DEFS = +LIBS = + +# You should not have to change anything below here. + +CC = avr-gcc + +CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) +LDFLAGS = -Wl,-Map,$(PRG).map + +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump + +all: elf lst hex + +elf: $(PRG).elf +lst: $(PRG).lst + +$(PRG).elf: $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + +%.lst: %.elf + $(OBJDUMP) -h -S $< > $@ + +clean: + rm -f *.o $(PRG).elf *.bak + rm -f *.lst *.map $(EXTRA_CLEAN_FILES) + rm -f $(PRG).hex $(PRG).bin $(PRG).srec $(PRG)_eeprom.hex $(PRG)_eeprom.bin $(PRG)_eeprom.srec + +# Rules for building the doc. + +doc: + +# Rules for building the .text rom images. + +text: hex bin srec + +hex: $(PRG).hex +bin: $(PRG).bin +srec: $(PRG).srec + +%.hex: %.elf + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + +%.srec: %.elf + $(OBJCOPY) -j .text -j .data -O srec $< $@ + +%.bin: %.elf + $(OBJCOPY) -j .text -j .data -O binary $< $@ + +# Rules for building the .eeprom rom images. + +eeprom: ehex ebin esrec + +ehex: $(PRG)_eeprom.hex +ebin: $(PRG)_eeprom.bin +esrec: $(PRG)_eeprom.srec + +%_eeprom.hex: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%_eeprom.srec: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@ + +%_eeprom.bin: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ + diff --git a/n/avr/rs232/README b/n/avr/rs232/README new file mode 100644 index 0000000..0e0e85e --- /dev/null +++ b/n/avr/rs232/README @@ -0,0 +1 @@ +n.avr.rs232 - AVR RS232 Module. diff --git a/n/avr/rs232/avrconfig.h b/n/avr/rs232/avrconfig.h new file mode 100644 index 0000000..deee7db --- /dev/null +++ b/n/avr/rs232/avrconfig.h @@ -0,0 +1,45 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h - config file for avr projects. */ +/* 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: + * }}} */ + +/* global */ +/* AVR Frequency : 1000000 1843200 2000000 3686400 4000000 7372800 8000000 + * 11059200 14745600 16000000 18432000 20000000 */ +#define AC_FREQ 14745600 + +/* rs232 - RS232 Module. */ +/* Baudrate : 2400 4800 9600 14400 19200 28800 38400 57600 76800 115200 230400 + * 250000 500000 1000000 */ +#define AC_RS232_BAUDRATE 115200 +/* Choose between interrupt (1) or polling (0). */ +#define AC_RS232_INTERRUPT 0 +/* Parity : ODD, EVEN, NONE. */ +#define AC_RS232_PARITY ODD +/* Stop bits : 1, 2. */ +#define AC_RS232_STOP_BITS 2 +/* Character size : 5, 6, 7, 8, 9. */ +#define AC_RS232_CHAR_SIZE 8 + +#endif /* avrconfig_h */ 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: + * }}} */ +#include "rs232.h" + +#include + +/* 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; +} + diff --git a/n/avr/rs232/rs232.h b/n/avr/rs232/rs232.h new file mode 100644 index 0000000..120a920 --- /dev/null +++ b/n/avr/rs232/rs232.h @@ -0,0 +1,44 @@ +#ifndef rs232_h +#define rs232_h +/* rs232.h */ +/* 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: + * }}} */ +#include "avrconfig.h" + +/* +AutoDec */ + +/* Initialise rs232. */ +void +rs232_init (void); + +/* Read a char. */ +unsigned char +rs232_getc (void); + +/* Write a char. */ +void +rs232_putc (unsigned char c); + +/* -AutoDec */ + +#endif /* rs232_h */ diff --git a/n/avr/rs232/test_rs232.c b/n/avr/rs232/test_rs232.c new file mode 100644 index 0000000..89e0979 --- /dev/null +++ b/n/avr/rs232/test_rs232.c @@ -0,0 +1,35 @@ +/* test_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: + * }}} */ +#include "rs232.h" + +/* +AutoDec */ +/* -AutoDec */ + +int +main (void) +{ + rs232_init (); + while (1) + rs232_putc (rs232_getc ()); +} -- cgit v1.2.3