35 #include <avr/interrupt.h> 
   36 #include <avr/pgmspace.h> 
   38 #include "contiki-conf.h" 
   42 #include "dev/rs232.h" 
   48 #ifndef USART_UCSRC_SEL 
   49 #define USART_UCSRC_SEL 0x00 
   57 #ifdef RS232_CONF_PRINTF_BUFFER_LENGTH 
   58 #define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH 
   60 #if CONTIKI_TARGET_STK500 
   61 #define RS232_PRINTF_BUFFER_LENGTH 64 
   69 #ifdef RS232_CONF_TX_INTERRUPTS 
   70 #define RS232_TX_INTERRUPTS RS232_CONF_TX_INTERRUPTS 
   72 #define RS232_TX_INTERRUPTS 0 
   76 #ifndef ADD_CARRIAGE_RETURN_AFTER_NEWLINE 
   77 #define ADD_CARRIAGE_RETURN_AFTER_NEWLINE 1 
   82 #ifdef RS232_CONF_NUMPORTS 
   83 #define NUMPORTS RS232_CONF_NUMPORTS 
   86 #if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__) 
   90 #error Only two serial ports are defined for this processor! 
   95 #define D_UDRE0M (1 << UDRE0) 
   96 #define D_UBRR0H UBRR0H 
   97 #define D_UBRR0L UBRR0L 
   98 #define D_UCSR0A UCSR0A 
   99 #define D_UCSR0B UCSR0B 
  100 #define D_UCSR0C UCSR0C 
  101 #define D_USART0_RX_vect USART0_RX_vect 
  102 #define D_USART0_TX_vect USART0_TX_vect 
  106 #define D_UDRE1M (1 << UDRE1) 
  107 #define D_UBRR1H UBRR1H 
  108 #define D_UBRR1L UBRR1L 
  109 #define D_UCSR1A UCSR1A 
  110 #define D_UCSR1B UCSR1B 
  111 #define D_UCSR1C UCSR1C 
  112 #define D_USART1_RX_vect USART1_RX_vect 
  113 #define D_USART1_TX_vect USART1_TX_vect 
  118 #elif defined (__AVR_AT90USB1287__) 
  123 #error Only one serial port is defined for this processor! 
  128 #define D_UDRE0M (1 << UDRE1) 
  129 #define D_UBRR0H UBRR1H 
  130 #define D_UBRR0L UBRR1L 
  131 #define D_UCSR0A UCSR1A 
  132 #define D_UCSR0B UCSR1B 
  133 #define D_UCSR0C UCSR1C 
  134 #define D_USART0_RX_vect USART1_RX_vect 
  135 #define D_USART0_TX_vect USART1_TX_vect 
  138 #elif defined (__AVR_ATmega8515__) 
  142 #error Only one serial port is defined for this processor! 
  147 #define D_UDRE0M (1 << UDRE) 
  148 #define D_UBRR0H UBRRH 
  149 #define D_UBRR0L UBRRL 
  150 #define D_UCSR0A UCSRA 
  151 #define D_UCSR0B UCSRB 
  152 #define D_UCSR0C UCSRC 
  153 #define D_USART0_RX_vect USART_RX_vect 
  154 #define D_USART0_TX_vect USART_TX_vect 
  157 #elif defined (__AVR_ATmega328P__) 
  161 #error Only one serial port is defined for this processor! 
  166 #define D_UDRE0M (1 << UDRE0) 
  167 #define D_UBRR0H UBRR0H 
  168 #define D_UBRR0L UBRR0L 
  169 #define D_UCSR0A UCSR0A 
  170 #define D_UCSR0B UCSR0B 
  171 #define D_UCSR0C UCSR0C 
  172 #define D_USART0_RX_vect USART_RX_vect 
  173 #define D_USART0_TX_vect USART_TX_vect 
  176 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) 
  180 #error Only one serial port is defined for this processor! 
  185 #define D_UDRE0M (1 << UDRE) 
  186 #define D_UBRR0H UBRRH 
  187 #define D_UBRR0L UBRRL 
  188 #define D_UCSR0A UCSRA 
  189 #define D_UCSR0B UCSRB 
  190 #define D_UCSR0C UCSRC 
  191 #define D_USART0_RX_vect USART_RXC_vect 
  192 #define D_USART0_TX_vect USART_TXC_vect 
  195 #elif defined (__AVR_ATmega644__) 
  199 #error Only one serial port is defined for this processor! 
  204 #define D_UDRE0M (1 << UDRE0) 
  205 #define D_UBRR0H UBRR0H 
  206 #define D_UBRR0L UBRR0L 
  207 #define D_UCSR0A UCSR0A 
  208 #define D_UCSR0B UCSR0B 
  209 #define D_UCSR0C UCSR0C 
  210 #define D_USART0_RX_vect USART0_RX_vect 
  211 #define D_USART0_TX_vect USART0_TX_vect 
  215 #error Please define the UART registers for your MCU! 
  219 int (* input_handler_0)(
unsigned char);
 
  220 ISR(D_USART0_RX_vect)
 
  224   if (input_handler_0 != 
NULL) input_handler_0(c);
 
  226 #if RS232_TX_INTERRUPTS 
  227 volatile uint8_t txwait_0;
 
  228 ISR(D_USART0_TX_vect)
 
  235 int (* input_handler_1)(
unsigned char);
 
  236 ISR(D_USART1_RX_vect)
 
  240   if (input_handler_1 != 
NULL) input_handler_1(c);
 
  242 #if RS232_TX_INTERRUPTS 
  243 volatile uint8_t txwait_1;
 
  251 int (* input_handler_2)(
unsigned char);
 
  252 ISR(D_USART2_RX_vect)
 
  256   if (input_handler_2 != 
NULL) input_handler_2(c);
 
  258 #if RS232_TX_INTERRUPTS 
  259 volatile uint8_t txwait_2;
 
  271 rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt)
 
  275    D_UBRR0H = (uint8_t)(bd>>8);
 
  276    D_UBRR0L = (uint8_t)bd;
 
  277 #if RS232_TX_INTERRUPTS 
  279    D_UCSR0B =  USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
 
  280                USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
 
  282    D_UCSR0B =  USART_INTERRUPT_RX_COMPLETE | \
 
  283                USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
 
  285    D_UCSR0C = USART_UCSRC_SEL | ffmt;
 
  286    input_handler_0 = 
NULL;
 
  289  } 
else if (port == 1) {
 
  290    D_UBRR1H = (uint8_t)(bd>>8);
 
  291    D_UBRR1L = (uint8_t)bd;
 
  292 #if RS232_TX_INTERRUPTS 
  294    D_UCSR1B =  USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
 
  295                USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
 
  297    D_UCSR1B =  USART_INTERRUPT_RX_COMPLETE | \
 
  298                USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
 
  300    D_UCSR1C = USART_UCSRC_SEL | ffmt;
 
  301    input_handler_1 = 
NULL;
 
  304  } 
else if (port == 2) {
 
  305    D_UBRR2H = (uint8_t)(bd>>8);
 
  306    D_UBRR2L = (uint8_t)bd;
 
  307 #if RS232_TX_INTERRUPTS 
  309    D_UCSR2B =  USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
 
  310                USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
 
  312    D_UCSR2B =  USART_INTERRUPT_RX_COMPLETE | \
 
  313                USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
 
  315    D_UCSR2C = USART_UCSRC_SEL | ffmt;
 
  316    input_handler_2 = 
NULL;
 
  327 #if RS232_TX_INTERRUPTS 
  335   } 
else if (port == 1) {
 
  340   } 
else if (port == 2) {
 
  352     while (!(D_UCSR0A & D_UDRE0M));
 
  355   } 
else if (port == 1) {
 
  356     while (!(D_UCSR1A & D_UDRE1M));
 
  359   } 
else if (port == 2) {
 
  360     while (!(D_UCSR2A & D_UDRE2M));
 
  376   } 
else if (port == 1) {
 
  379   } 
else if (port == 2) {
 
  392 #if ADD_CARRIAGE_RETURN_AFTER_NEWLINE 
  394         if(*buf==
'\r') buf++; 
else rs232_send(port, *buf++);
 
  401 #if RS232_PRINTF_BUFFER_LENGTH 
  404 rs232_printf(uint8_t port, 
const char *fmt, ...)
 
  407   static char buf[RS232_PRINTF_BUFFER_LENGTH];
 
  410   vsnprintf (buf, RS232_PRINTF_BUFFER_LENGTH, fmt, ap);
 
  423 int rs232_stdout_putchar(
char c, FILE *stream);
 
  424 static uint8_t stdout_rs232_port=RS232_PORT_0;
 
  425 static FILE rs232_stdout = FDEV_SETUP_STREAM(rs232_stdout_putchar,
 
  429 int rs232_stdout_putchar(
char c, FILE *stream)
 
  431 #if ADD_CARRIAGE_RETURN_AFTER_NEWLINE 
  432   if(c==
'\n') 
rs232_send(stdout_rs232_port, 
'\r');
 
  433   if(c!=
'\r') 
rs232_send (stdout_rs232_port, c);
 
  440 void rs232_redirect_stdout (uint8_t port) {
 
  441   stdout_rs232_port = port;
 
  442   stdout = &rs232_stdout;
 
void rs232_init(void)
Initialize the RS232 module. 
void slip_arch_writeb(unsigned char c)
Copyright (c) 2014, Analog Devices, Inc. 
void rs232_set_input(int(*f)(unsigned char))
Set an input handler for incoming RS232 data. 
#define NULL
The null pointer. 
void rs232_print(char *text)
Print a text string on RS232. 
void rs232_send(uint8_t port, unsigned char c)
Print a character on RS232.