36 #include <avr/interrupt.h>
37 #include <avr/pgmspace.h>
39 #include "contiki-conf.h"
43 #include "dev/rs232.h"
49 #ifndef USART_UCSRC_SEL
50 #define USART_UCSRC_SEL 0x00
53 #ifdef RS232_CONF_PRINTF_BUFFER_LENGTH
54 #define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH
56 #define RS232_PRINTF_BUFFER_LENGTH 64
59 #ifndef ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
60 #define ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT 1
64 volatile uint8_t * udr;
65 volatile uint8_t * ubrrh;
66 volatile uint8_t * ubrrl;
67 volatile uint8_t * ucsra;
68 volatile uint8_t * ucsrb;
69 volatile uint8_t * ucsrc;
70 volatile uint8_t txwait;
71 int (* input_handler)(
unsigned char);
74 #if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__)
75 static rs232_t rs232_ports[2] = {
98 #elif defined (__AVR_AT90USB1287__)
100 static rs232_t rs232_ports[1] = {
111 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) \
112 || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
113 static rs232_t rs232_ports[1] = {
124 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega328P__)
125 static rs232_t rs232_ports[1] = {
137 #error Please define the UART registers for your MCU!
140 #if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__)
144 rs232_ports[RS232_PORT_0].txwait = 0;
152 c = *(rs232_ports[RS232_PORT_0].udr);
154 if(rs232_ports[RS232_PORT_0].input_handler !=
NULL) {
155 rs232_ports[RS232_PORT_0].input_handler(c);
161 rs232_ports[RS232_PORT_1].txwait = 0;
169 c = *(rs232_ports[RS232_PORT_1].udr);
171 if(rs232_ports[RS232_PORT_1].input_handler !=
NULL) {
172 rs232_ports[RS232_PORT_1].input_handler(c);
176 #elif defined (__AVR_ATmega644__)
180 rs232_ports[RS232_PORT_0].txwait = 0;
187 c = *(rs232_ports[RS232_PORT_0].udr);
189 if(rs232_ports[RS232_PORT_0].input_handler !=
NULL) {
190 rs232_ports[RS232_PORT_0].input_handler(c);
193 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
197 rs232_ports[RS232_PORT_0].txwait = 0;
205 c = *(rs232_ports[RS232_PORT_0].udr);
207 if(rs232_ports[RS232_PORT_0].input_handler !=
NULL) {
208 rs232_ports[RS232_PORT_0].input_handler(c);
212 #elif defined (__AVR_ATmega8515__) || defined (__AVR_ATmega328P__)
216 rs232_ports[RS232_PORT_0].txwait = 0;
224 c = *(rs232_ports[RS232_PORT_0].udr);
226 if(rs232_ports[RS232_PORT_0].input_handler !=
NULL) {
227 rs232_ports[RS232_PORT_0].input_handler(c);
231 #elif defined (__AVR_AT90USB1287__)
235 rs232_ports[RS232_PORT_0].txwait = 0;
243 c = *(rs232_ports[RS232_PORT_0].udr);
245 if(rs232_ports[RS232_PORT_0].input_handler !=
NULL) {
246 rs232_ports[RS232_PORT_0].input_handler(c);
250 #error Please define the interrupt vectors for your MCU!
255 rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt)
257 *(rs232_ports[port].ubrrh) = (uint8_t)(bd>>8);
258 *(rs232_ports[port].ubrrl) = (uint8_t)bd;
264 *(rs232_ports[port].ucsrb) = USART_INTERRUPT_RX_COMPLETE | \
265 USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
274 *(rs232_ports[port].ucsrc) = USART_UCSRC_SEL | ffmt;
276 rs232_ports[port].txwait = 0;
278 rs232_ports[port].input_handler =
NULL;
282 rs232_print_p(uint8_t port,
char *buf)
284 while(pgm_read_byte(buf)) {
294 #if ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
296 if(*buf==
'\r') buf++;
else rs232_send(port, *buf++);
304 rs232_printf(uint8_t port,
const char *fmt, ...)
307 static char buf[RS232_PRINTF_BUFFER_LENGTH];
310 vsnprintf (buf, RS232_PRINTF_BUFFER_LENGTH, fmt, ap);
319 while (!(*(rs232_ports[port].ucsra) & (1 << UDRE1))) {
321 *(rs232_ports[port].udr) = c;
327 rs232_ports[port].input_handler = f;
331 slip_arch_writeb(
unsigned char c)
336 int rs232_stdout_putchar(
char c, FILE *stream);
337 static uint8_t stdout_rs232_port=RS232_PORT_0;
338 static FILE rs232_stdout = FDEV_SETUP_STREAM(rs232_stdout_putchar,
342 int rs232_stdout_putchar(
char c, FILE *stream)
344 #if ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
345 if(c==
'\n')
rs232_send(stdout_rs232_port,
'\r');
346 if(c!=
'\r')
rs232_send (stdout_rs232_port, c);
353 void rs232_redirect_stdout (uint8_t port) {
354 stdout_rs232_port = port;
355 stdout = &rs232_stdout;