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. * 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> #include <stdio.h>
@ -50,229 +49,339 @@
#define USART_UCSRC_SEL 0x00 #define USART_UCSRC_SEL 0x00
#endif #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 #ifdef RS232_CONF_PRINTF_BUFFER_LENGTH
#define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH #define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH
#else #else
#if CONTIKI_TARGET_STK500
#define RS232_PRINTF_BUFFER_LENGTH 64 #define RS232_PRINTF_BUFFER_LENGTH 64
#endif #endif
#ifndef ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
#define ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT 1
#endif #endif
typedef struct { /* TX interrupts would allow non-blocking output up to the size of some RAM buffer.
volatile uint8_t * udr; * Since a RAM buffer is not implemented tx interrupts are superfluous and unwanted
volatile uint8_t * ubrrh; * because they block debug prints from within interrupt routines
volatile uint8_t * ubrrl; */
volatile uint8_t * ucsrb; #ifdef RS232_CONF_TX_INTERRUPTS
volatile uint8_t * ucsrc; #define RS232_TX_INTERRUPTS RS232_CONF_TX_INTERRUPTS
volatile uint8_t txwait; #else
int (* input_handler)(unsigned char); #define RS232_TX_INTERRUPTS 0
} rs232_t; #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__) #if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__)
static rs232_t rs232_ports[2] = { #ifndef NUMPORTS
{ // UART0 #define NUMPORTS 2
&UDR0, #elif NUMPORTS > 2
&UBRR0H, #error Only two serial ports are defined for this processor!
&UBRR0L, #endif
&UCSR0B,
&UCSR0C, #if NUMPORTS > 0
0, #define D_UDR0 UDR0
NULL #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__) #elif defined (__AVR_AT90USB1287__)
/* Has only UART1, map it to port 0 */ /* Has only UART1, map it to port 0 */
static rs232_t rs232_ports[1] = { #ifndef NUMPORTS
{ // UART1 #define NUMPORTS 1
&UDR1, #elif NUMPORTS > 1
&UBRR1H, #error Only one serial port is defined for this processor!
&UBRR1L, #endif
&UCSR1B,
&UCSR1C, #if NUMPORTS > 0
0, #define D_UDR0 UDR1
NULL #define D_UDRE0M (1 << UDRE1)
} #define D_UBRR0H UBRR1H
}; #define D_UBRR0L UBRR1L
#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) \ #define D_UCSR0A UCSR1A
|| defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) #define D_UCSR0B UCSR1B
static rs232_t rs232_ports[1] = { #define D_UCSR0C UCSR1C
{ // UART0 #define D_USART0_RX_vect USART1_RX_vect
&UDR, #define D_USART0_TX_vect USART1_TX_vect
&UBRRH, #endif
&UBRRL,
&UCSRB, #elif defined (__AVR_ATmega8515__)
&UCSRC, #ifndef NUMPORTS
0, #define NUMPORTS 1
NULL #elif NUMPORTS > 1
} #error Only one serial port is defined for this processor!
}; #endif
#elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega328P__)
static rs232_t rs232_ports[1] = { #if NUMPORTS > 0
{ // UART0 #define D_UDR0 UDR
&UDR0, #define D_UDRE0M (1 << UDRE)
&UBRR0H, #define D_UBRR0H UBRRH
&UBRR0L, #define D_UBRR0L UBRRL
&UCSR0B, #define D_UCSR0A UCSRA
&UCSR0C, #define D_UCSR0B UCSRB
0, #define D_UCSR0C UCSRC
NULL #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 #else
#error Please define the UART registers for your MCU! #error Please define the UART registers for your MCU!
#endif #endif
#if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__) #if NUMPORTS > 0
/*---------------------------------------------------------------------------*/ int (* input_handler_0)(unsigned char);
ISR(USART0_TX_vect) ISR(D_USART0_RX_vect)
{
rs232_ports[RS232_PORT_0].txwait = 0;
}
/*---------------------------------------------------------------------------*/
ISR(USART0_RX_vect)
{ {
unsigned char c; unsigned char c;
c = D_UDR0;
c = *(rs232_ports[RS232_PORT_0].udr); if (input_handler_0 != NULL) input_handler_0(c);
if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
rs232_ports[RS232_PORT_0].input_handler(c);
} }
} #if RS232_TX_INTERRUPTS
/*---------------------------------------------------------------------------*/ volatile uint8_t txwait_0;
ISR(USART1_TX_vect) 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 #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 void
rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt) rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt)
{ {
*(rs232_ports[port].ubrrh) = (uint8_t)(bd>>8); #if NUMPORTS > 0
*(rs232_ports[port].ubrrl) = (uint8_t)bd; if (port == 0) {
D_UBRR0H = (uint8_t)(bd>>8);
/* D_UBRR0L = (uint8_t)bd;
* - Enable receiver and transmitter, #if RS232_TX_INTERRUPTS
* - Enable interrupts for receiver and transmitter txwait_0 = 0;
*/ D_UCSR0B = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
*(rs232_ports[port].ucsrb) = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE; 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;
/* #if NUMPORTS > 1
* - mode (sync. / async) } else if (port == 1) {
* - Parity D_UBRR1H = (uint8_t)(bd>>8);
* - Stop bits D_UBRR1L = (uint8_t)bd;
* - charater size (9 bits are currently not supported) #if RS232_TX_INTERRUPTS
* - clock polarity txwait_1 = 0;
*/ D_UCSR1B = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
*(rs232_ports[port].ucsrc) = USART_UCSRC_SEL | ffmt; 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;
rs232_ports[port].txwait = 0; #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].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 void
@ -288,7 +397,7 @@ void
rs232_print(uint8_t port, char *buf) rs232_print(uint8_t port, char *buf)
{ {
while(*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=='\n') rs232_send(port, '\r');
if(*buf=='\r') buf++; else rs232_send(port, *buf++); if(*buf=='\r') buf++; else rs232_send(port, *buf++);
#else #else
@ -296,6 +405,8 @@ rs232_print(uint8_t port, char *buf)
#endif #endif
} }
} }
#if RS232_PRINTF_BUFFER_LENGTH
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
rs232_printf(uint8_t port, const char *fmt, ...) rs232_printf(uint8_t port, const char *fmt, ...)
@ -309,20 +420,7 @@ rs232_printf(uint8_t port, const char *fmt, ...)
rs232_print (port, buf); rs232_print (port, buf);
} }
/*---------------------------------------------------------------------------*/ #endif
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;
}
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
slip_arch_writeb(unsigned char c) 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) 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=='\n') rs232_send(stdout_rs232_port, '\r');
if(c!='\r') rs232_send (stdout_rs232_port, c); if(c!='\r') rs232_send (stdout_rs232_port, c);
#else #else