- Fix/enhence support for RS232 interface on AVR (ATMega128):

* support for multiple ports: (extended interface with 'port' parameter)
 * new function: rs232_redirect_stdout that allows you to redirect stdout
   to a serial port
- In order to implement support for other MCUs, adopt a copy of
   rs232_atmega128.h
This commit is contained in:
barner 2006-12-22 17:00:45 +00:00
parent 86f37e7c1b
commit 45cbcde797
3 changed files with 319 additions and 51 deletions

View file

@ -28,103 +28,190 @@
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: rs232.c,v 1.1 2006/06/17 22:41:21 adamdunkels Exp $
* @(#)$Id: rs232.c,v 1.2 2006/12/22 17:00:45 barner Exp $
*/
#include <stdio.h>
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "contiki-conf.h"
#include "contiki.h"
#include "dev/slip.h"
#include "dev/serial.h"
#include "dev/rs232.h"
static volatile unsigned char txwait;
/*static unsigned char slipmode;*/
#ifdef RS232_CONF_PRINTF_BUFFER_LENGTH
#define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH
#else
#define RS232_PRINTF_BUFFER_LENGTH 64
#endif
static int (* input_handler)(unsigned char) = NULL;
#if MCU == atmega128
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;
static rs232_t rs232_ports[2] = {
{ // UART0
&UDR0,
&UBRR0H,
&UBRR0L,
&UCSR0B,
&UCSR0C,
0,
NULL
},
{ // UART1
&UDR1,
&UBRR1H,
&UBRR1L,
&UCSR1B,
&UCSR1C,
0,
NULL
}
};
#else
#error Please define the UART registers for your MCU!
#endif
/*---------------------------------------------------------------------------*/
SIGNAL(SIG_UART0_TRANS)
{
txwait = 0;
rs232_ports[RS232_PORT_0].txwait = 0;
}
/*---------------------------------------------------------------------------*/
SIGNAL(SIG_UART0_RECV)
{
unsigned char c;
c = UDR0;
c = *(rs232_ports[RS232_PORT_0].UDR);
if(input_handler != NULL) {
input_handler(c);
if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
rs232_ports[RS232_PORT_0].input_handler(c);
}
/* if(slipmode) {
slip_input_byte(c);
} else {
serial_input_byte(c);
}*/
}
/*---------------------------------------------------------------------------*/
SIGNAL(SIG_UART1_TRANS)
{
rs232_ports[RS232_PORT_1].txwait = 0;
}
/*---------------------------------------------------------------------------*/
SIGNAL(SIG_UART1_RECV)
{
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);
}
}
/*---------------------------------------------------------------------------*/
void
rs232_init(void)
rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt)
{
/* Enable transmit. */
UCSR0B = _BV(RXCIE) | _BV(TXCIE) | _BV(RXEN) | _BV(TXEN);
/* Set baud rate (23 =~ 38400) */
UBRR0H = 0;
UBRR0L = 23;
*(rs232_ports[port].UBRRH) = (uint8_t)(bd>>8);
*(rs232_ports[port].UBRRL) = (uint8_t)bd;
/* slipmode = 0;*/
txwait = 0;
/*
* - 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;
input_handler = NULL;
/*
* - mode (sync. / async)
* - Parity
* - Stop bits
* - charater size (9 bits are currently not supported)
* - clock polarity
*/
*(rs232_ports[port].UCSRC) = ffmt;
rs232_ports[port].txwait = 0;
rs232_ports[port].input_handler = NULL;
}
/*---------------------------------------------------------------------------*/
/*void
rs232_init_slip(void)
{
rs232_init();
slipmode = 1;
}*/
/*---------------------------------------------------------------------------*/
void
rs232_print_p(prog_char *buf)
rs232_print_p(uint8_t port, prog_char *buf)
{
while(pgm_read_byte(buf)) {
rs232_send(pgm_read_byte(buf));
rs232_send(port, pgm_read_byte(buf));
++buf;
}
}
/*---------------------------------------------------------------------------*/
void
rs232_print(char *buf)
rs232_print(uint8_t port, char *buf)
{
while(*buf) {
rs232_send(*buf++);
rs232_send(port, *buf++);
}
}
/*---------------------------------------------------------------------------*/
void
rs232_send(unsigned char c)
rs232_printf(uint8_t port, const char *fmt, ...)
{
txwait = 1;
UDR0 = c;
while(txwait);
va_list ap;
static char buf[RS232_PRINTF_BUFFER_LENGTH];
va_start (ap, fmt);
vsnprintf (buf, RS232_PRINTF_BUFFER_LENGTH, fmt, ap);
va_end(ap);
rs232_print (port, buf);
}
/*---------------------------------------------------------------------------*/
void
rs232_set_input(int (*f)(unsigned char))
rs232_send(uint8_t port, unsigned char c)
{
input_handler = f;
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
slip_arch_writeb(unsigned char c)
{
rs232_send(c);
rs232_send(SLIP_PORT, c);
}
/*---------------------------------------------------------------------------*/
int rs232_stdout_putchar(char c, FILE *stream);
static uint8_t stdout_rs232_port=RS232_PORT_0;
static FILE rs232_stdout = FDEV_SETUP_STREAM(rs232_stdout_putchar,
NULL,
_FDEV_SETUP_WRITE);
int rs232_stdout_putchar(char c, FILE *stream)
{
rs232_send (stdout_rs232_port, c);
return 0;
}
/*---------------------------------------------------------------------------*/
void rs232_redirect_stdout (uint8_t port) {
stdout_rs232_port = port;
stdout = &rs232_stdout;
}