Access uart registers directly instead of through intermediate addresses in a RAM struct.

Add a platform define to reduce the number of ports supported by the cpu.
This commit is contained in:
David Kopf 2012-01-21 13:49:58 -05:00
parent aceb36c66b
commit 7ca141b457

View File

@ -28,7 +28,6 @@
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: rs232.c,v 1.7 2010/10/27 14:51:20 dak664 Exp $
*/
#include <stdio.h>
@ -50,229 +49,339 @@
#define USART_UCSRC_SEL 0x00
#endif
/* Currently only the STK500 platform uses a static RAM buffer for printfs.
* Others print a character at a time using the gcc string pointer.
* gcc may not strip the unused buffer, even though it is statically
* allocated in an unused routine.
*/
#ifdef RS232_CONF_PRINTF_BUFFER_LENGTH
#define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH
#else
#if CONTIKI_TARGET_STK500
#define RS232_PRINTF_BUFFER_LENGTH 64
#endif
#ifndef ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
#define ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT 1
#endif
typedef struct {
volatile uint8_t * udr;
volatile uint8_t * ubrrh;
volatile uint8_t * ubrrl;
volatile uint8_t * ucsrb;
volatile uint8_t * ucsrc;
volatile uint8_t txwait;
int (* input_handler)(unsigned char);
} rs232_t;
/* TX interrupts would allow non-blocking output up to the size of some RAM buffer.
* Since a RAM buffer is not implemented tx interrupts are superfluous and unwanted
* because they block debug prints from within interrupt routines
*/
#ifdef RS232_CONF_TX_INTERRUPTS
#define RS232_TX_INTERRUPTS RS232_CONF_TX_INTERRUPTS
#else
#define RS232_TX_INTERRUPTS 0
#endif
/* Insert a carriage return after a line feed. This is the default. */
#ifndef ADD_CARRIAGE_RETURN_AFTER_NEWLINE
#define ADD_CARRIAGE_RETURN_AFTER_NEWLINE 1
#endif
/* Reducing NUMPORTS from the default will harmlessly disable usage of those ports */
/* Two ports take 400 bytes flash and 4 bytes RAM. */
#ifdef RS232_CONF_NUMPORTS
#define NUMPORTS RS232_CONF_NUMPORTS
#endif
#if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__)
static rs232_t rs232_ports[2] = {
{ // UART0
&UDR0,
&UBRR0H,
&UBRR0L,
&UCSR0B,
&UCSR0C,
0,
NULL
},
#ifndef NUMPORTS
#define NUMPORTS 2
#elif NUMPORTS > 2
#error Only two serial ports are defined for this processor!
#endif
#if NUMPORTS > 0
#define D_UDR0 UDR0
#define D_UDRE0M (1 << UDRE0)
#define D_UBRR0H UBRR0H
#define D_UBRR0L UBRR0L
#define D_UCSR0A UCSR0A
#define D_UCSR0B UCSR0B
#define D_UCSR0C UCSR0C
#define D_USART0_RX_vect USART0_RX_vect
#define D_USART0_TX_vect USART0_TX_vect
#if NUMPORTS > 1
#define D_UDR1 UDR1
#define D_UDRE1M (1 << UDRE1)
#define D_UBRR1H UBRR1H
#define D_UBRR1L UBRR1L
#define D_UCSR1A UCSR1A
#define D_UCSR1B UCSR1B
#define D_UCSR1C UCSR1C
#define D_USART1_RX_vect USART1_RX_vect
#define D_USART1_TX_vect USART1_TX_vect
#endif
#endif
{ // UART1
&UDR1,
&UBRR1H,
&UBRR1L,
&UCSR1B,
&UCSR1C,
0,
NULL
}
};
#elif defined (__AVR_AT90USB1287__)
/* Has only UART1, map it to port 0 */
static rs232_t rs232_ports[1] = {
{ // UART1
&UDR1,
&UBRR1H,
&UBRR1L,
&UCSR1B,
&UCSR1C,
0,
NULL
}
};
#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) \
|| defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
static rs232_t rs232_ports[1] = {
{ // UART0
&UDR,
&UBRRH,
&UBRRL,
&UCSRB,
&UCSRC,
0,
NULL
}
};
#elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega328P__)
static rs232_t rs232_ports[1] = {
{ // UART0
&UDR0,
&UBRR0H,
&UBRR0L,
&UCSR0B,
&UCSR0C,
0,
NULL
}
};
#ifndef NUMPORTS
#define NUMPORTS 1
#elif NUMPORTS > 1
#error Only one serial port is defined for this processor!
#endif
#if NUMPORTS > 0
#define D_UDR0 UDR1
#define D_UDRE0M (1 << UDRE1)
#define D_UBRR0H UBRR1H
#define D_UBRR0L UBRR1L
#define D_UCSR0A UCSR1A
#define D_UCSR0B UCSR1B
#define D_UCSR0C UCSR1C
#define D_USART0_RX_vect USART1_RX_vect
#define D_USART0_TX_vect USART1_TX_vect
#endif
#elif defined (__AVR_ATmega8515__)
#ifndef NUMPORTS
#define NUMPORTS 1
#elif NUMPORTS > 1
#error Only one serial port is defined for this processor!
#endif
#if NUMPORTS > 0
#define D_UDR0 UDR
#define D_UDRE0M (1 << UDRE)
#define D_UBRR0H UBRRH
#define D_UBRR0L UBRRL
#define D_UCSR0A UCSRA
#define D_UCSR0B UCSRB
#define D_UCSR0C UCSRC
#define D_USART0_RX_vect USART_RX_vect
#define D_USART0_TX_vect USART_TX_vect
#endif
#elif defined (__AVR_ATmega328P__)
#ifndef NUMPORTS
#define NUMPORTS 1
#elif NUMPORTS > 1
#error Only one serial port is defined for this processor!
#endif
#if NUMPORTS > 0
#define D_UDR0 UDR0
#define D_UDRE0M (1 << UDRE0)
#define D_UBRR0H UBRR0H
#define D_UBRR0L UBRR0L
#define D_UCSR0A UCSR0A
#define D_UCSR0B UCSR0B
#define D_UCSR0C UCSR0C
#define D_USART0_RX_vect USART_RX_vect
#define D_USART0_TX_vect USART_TX_vect
#endif
#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
#ifndef NUMPORTS
#define NUMPORTS 1
#elif NUMPORTS > 1
#error Only one serial port is defined for this processor!
#endif
#if NUMPORTS > 0
#define D_UDR0 UDR
#define D_UDRE0M (1 << UDRE)
#define D_UBRR0H UBRRH
#define D_UBRR0L UBRRL
#define D_UCSR0A UCSRA
#define D_UCSR0B UCSRB
#define D_UCSR0C UCSRC
#define D_USART0_RX_vect USART_RXC_vect
#define D_USART0_TX_vect USART_TXC_vect
#endif
#elif defined (__AVR_ATmega644__)
#ifndef NUMPORTS
#define NUMPORTS 1
#elif NUMPORTS > 1
#error Only one serial port is defined for this processor!
#endif
#if NUMPORTS > 0
#define D_UDR0 UDR0
#define D_UDRE0M (1 << UDRE0)
#define D_UBRR0H UBRR0H
#define D_UBRR0L UBRR0L
#define D_UCSR0A UCSR0A
#define D_UCSR0B UCSR0B
#define D_UCSR0C UCSR0C
#define D_USART0_RX_vect USART0_RX_vect
#define D_USART0_TX_vect USART0_TX_vect
#endif
#else
#error Please define the UART registers for your MCU!
#endif
#if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__)
/*---------------------------------------------------------------------------*/
ISR(USART0_TX_vect)
{
rs232_ports[RS232_PORT_0].txwait = 0;
}
/*---------------------------------------------------------------------------*/
ISR(USART0_RX_vect)
#if NUMPORTS > 0
int (* input_handler_0)(unsigned char);
ISR(D_USART0_RX_vect)
{
unsigned char c;
c = *(rs232_ports[RS232_PORT_0].udr);
if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
rs232_ports[RS232_PORT_0].input_handler(c);
}
c = D_UDR0;
if (input_handler_0 != NULL) input_handler_0(c);
}
/*---------------------------------------------------------------------------*/
ISR(USART1_TX_vect)
#if RS232_TX_INTERRUPTS
volatile uint8_t txwait_0;
ISR(D_USART0_TX_vect)
{
rs232_ports[RS232_PORT_1].txwait = 0;
txwait_0 = 0;
}
/*---------------------------------------------------------------------------*/
ISR(USART1_RX_vect)
{
unsigned char c;
c = *(rs232_ports[RS232_PORT_1].udr);
if(rs232_ports[RS232_PORT_1].input_handler != NULL) {
rs232_ports[RS232_PORT_1].input_handler(c);
}
}
#elif defined (__AVR_ATmega644__)
/*---------------------------------------------------------------------------*/
ISR(USART0_TX_vect)
{
rs232_ports[RS232_PORT_0].txwait = 0;
}
/*---------------------------------------------------------------------------*/
ISR(USART0_RX_vect)
{
unsigned char c;
c = *(rs232_ports[RS232_PORT_0].udr);
if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
rs232_ports[RS232_PORT_0].input_handler(c);
}
}
#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
/*---------------------------------------------------------------------------*/
ISR(USART_TXC_vect)
{
rs232_ports[RS232_PORT_0].txwait = 0;
}
/*---------------------------------------------------------------------------*/
ISR(USART_RXC_vect)
{
unsigned char c;
c = *(rs232_ports[RS232_PORT_0].udr);
if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
rs232_ports[RS232_PORT_0].input_handler(c);
}
}
#elif defined (__AVR_ATmega8515__) || defined (__AVR_ATmega328P__)
/*---------------------------------------------------------------------------*/
ISR(USART_TX_vect)
{
rs232_ports[RS232_PORT_0].txwait = 0;
}
/*---------------------------------------------------------------------------*/
ISR(USART_RX_vect)
{
unsigned char c;
c = *(rs232_ports[RS232_PORT_0].udr);
if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
rs232_ports[RS232_PORT_0].input_handler(c);
}
}
#elif defined (__AVR_AT90USB1287__)
/*---------------------------------------------------------------------*/
ISR(USART1_TX_vect)
{
rs232_ports[RS232_PORT_0].txwait = 0;
}
/*---------------------------------------------------------------------------*/
ISR(USART1_RX_vect)
{
unsigned char c;
c = *(rs232_ports[RS232_PORT_0].udr);
if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
rs232_ports[RS232_PORT_0].input_handler(c);
}
}
#else
#error Please define the interrupt vectors for your MCU!
#endif
#if NUMPORTS > 1
int (* input_handler_1)(unsigned char);
ISR(D_USART1_RX_vect)
{
unsigned char c;
c = D_UDR1;
if (input_handler_1 != NULL) input_handler_1(c);
}
#if RS232_TX_INTERRUPTS
volatile uint8_t txwait_1;
ISR(USART1_TX_vect)
{
txwait_1 = 0;
}
#endif
#if NUMPORTS > 2
int (* input_handler_2)(unsigned char);
ISR(D_USART2_RX_vect)
{
unsigned char c;
c = D_UDR2;
if (input_handler_2 != NULL) input_handler_2(c);
}
#if RS232_TX_INTERRUPTS
volatile uint8_t txwait_2;
ISR(USART2_TX_vect)
{
txwait_2= 0;
}
#endif
#endif
#endif
#endif
/*---------------------------------------------------------------------------*/
void
rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt)
{
*(rs232_ports[port].ubrrh) = (uint8_t)(bd>>8);
*(rs232_ports[port].ubrrl) = (uint8_t)bd;
#if NUMPORTS > 0
if (port == 0) {
D_UBRR0H = (uint8_t)(bd>>8);
D_UBRR0L = (uint8_t)bd;
#if RS232_TX_INTERRUPTS
txwait_0 = 0;
D_UCSR0B = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
#else
D_UCSR0B = USART_INTERRUPT_RX_COMPLETE | \
USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
#endif
D_UCSR0C = USART_UCSRC_SEL | ffmt;
input_handler_0 = NULL;
/*
* - Enable receiver and transmitter,
* - Enable interrupts for receiver and transmitter
*/
*(rs232_ports[port].ucsrb) = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
#if NUMPORTS > 1
} else if (port == 1) {
D_UBRR1H = (uint8_t)(bd>>8);
D_UBRR1L = (uint8_t)bd;
#if RS232_TX_INTERRUPTS
txwait_1 = 0;
D_UCSR1B = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
#else
D_UCSR1B = USART_INTERRUPT_RX_COMPLETE | \
USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
#endif
D_UCSR1C = USART_UCSRC_SEL | ffmt;
input_handler_1 = NULL;
/*
* - mode (sync. / async)
* - Parity
* - Stop bits
* - charater size (9 bits are currently not supported)
* - clock polarity
*/
*(rs232_ports[port].ucsrc) = USART_UCSRC_SEL | ffmt;
#if NUMPORTS > 2
} else if (port == 2) {
D_UBRR2H = (uint8_t)(bd>>8);
D_UBRR2L = (uint8_t)bd;
#if RS232_TX_INTERRUPTS
txwait_2 = 0;
D_UCSR2B = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
#else
D_UCSR2B = USART_INTERRUPT_RX_COMPLETE | \
USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
#endif
D_UCSR2C = USART_UCSRC_SEL | ffmt;
input_handler_2 = NULL;
#endif
#endif
}
#endif /* NUMPORTS > 0 */
}
rs232_ports[port].txwait = 0;
rs232_ports[port].input_handler = NULL;
/*---------------------------------------------------------------------------*/
void
rs232_send(uint8_t port, unsigned char c)
{
#if RS232_TX_INTERRUPTS
/* Output character and block until it is transmitted */
#if NUMPORTS > 0
if (port == 0 ) {
txwait_0 = 1;
D_UDR0 = c;
while (txwait_0);
#if NUMPORTS > 1
} else if (port == 1) {
txwait_1 = 1;
D_UDR1 = c;
while (txwait_1);
#if NUMPORTS > 2
} else if (port == 2) {
txwait_2 = 1;
D_UDR2 = c;
while (txwait_2);
#endif
#endif
}
#endif
#else /* RS232_TX_INTERRUPTS */
/* Block until tx ready and output character */
#if NUMPORTS > 0
if (port == 0 ) {
while (!(D_UCSR0A & D_UDRE0M));
D_UDR0 = c;
#if NUMPORTS > 1
} else if (port == 1) {
while (!(D_UCSR1A & D_UDRE1M));
D_UDR1 = c;
#if NUMPORTS > 2
} else if (port == 2) {
while (!(D_UCSR2A & D_UDRE2M));
D_UDR2 = c;
#endif
#endif
}
#endif
#endif /* RS232_TX_INTERRUPTS */
}
/*---------------------------------------------------------------------------*/
void
rs232_set_input(uint8_t port, int (*f)(unsigned char))
{
#if NUMPORTS > 0
if (port == 0) {
input_handler_0 = f;
#if NUMPORTS > 1
} else if (port == 1) {
input_handler_1 = f;
#if NUMPORTS > 2
} else if (port == 2) {
input_handler_2 = f;
#endif
#endif
}
#endif
}
void
@ -288,7 +397,7 @@ void
rs232_print(uint8_t port, char *buf)
{
while(*buf) {
#if ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
#if ADD_CARRIAGE_RETURN_AFTER_NEWLINE
if(*buf=='\n') rs232_send(port, '\r');
if(*buf=='\r') buf++; else rs232_send(port, *buf++);
#else
@ -296,6 +405,8 @@ rs232_print(uint8_t port, char *buf)
#endif
}
}
#if RS232_PRINTF_BUFFER_LENGTH
/*---------------------------------------------------------------------------*/
void
rs232_printf(uint8_t port, const char *fmt, ...)
@ -309,20 +420,7 @@ rs232_printf(uint8_t port, const char *fmt, ...)
rs232_print (port, buf);
}
/*---------------------------------------------------------------------------*/
void
rs232_send(uint8_t port, unsigned char c)
{
rs232_ports[port].txwait = 1;
*(rs232_ports[port].udr) = c;
while(rs232_ports[port].txwait);
}
/*---------------------------------------------------------------------------*/
void
rs232_set_input(uint8_t port, int (*f)(unsigned char))
{
rs232_ports[port].input_handler = f;
}
#endif
/*---------------------------------------------------------------------------*/
void
slip_arch_writeb(unsigned char c)
@ -338,7 +436,7 @@ static FILE rs232_stdout = FDEV_SETUP_STREAM(rs232_stdout_putchar,
int rs232_stdout_putchar(char c, FILE *stream)
{
#if ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
#if ADD_CARRIAGE_RETURN_AFTER_NEWLINE
if(c=='\n') rs232_send(stdout_rs232_port, '\r');
if(c!='\r') rs232_send (stdout_rs232_port, c);
#else