2 * Copyright (c) 2005, Swedish Institute of Computer Science
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file is part of the Contiki operating system.
31 * @(#)$Id: rs232.c,v 1.5 2009/03/17 20:32:22 adamdunkels Exp $
36 #include <avr/interrupt.h>
37 #include <avr/pgmspace.h>
39 #include "contiki-conf.h"
43 #include "dev/rs232.h"
45 #ifdef RS232_CONF_PRINTF_BUFFER_LENGTH
46 #define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH
48 #define RS232_PRINTF_BUFFER_LENGTH 64
51 #if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__)
53 volatile uint8_t * UDR
;
54 volatile uint8_t * UBRRH
;
55 volatile uint8_t * UBRRL
;
56 volatile uint8_t * UCSRB
;
57 volatile uint8_t * UCSRC
;
58 volatile uint8_t txwait
;
59 int (* input_handler
)(unsigned char);
62 static rs232_t rs232_ports
[2] = {
84 #error Please define the UART registers for your MCU!
87 /*---------------------------------------------------------------------------*/
90 rs232_ports
[RS232_PORT_0
].txwait
= 0;
93 /*---------------------------------------------------------------------------*/
98 c
= *(rs232_ports
[RS232_PORT_0
].UDR
);
100 if(rs232_ports
[RS232_PORT_0
].input_handler
!= NULL
) {
101 rs232_ports
[RS232_PORT_0
].input_handler(c
);
105 /*---------------------------------------------------------------------------*/
108 rs232_ports
[RS232_PORT_1
].txwait
= 0;
111 /*---------------------------------------------------------------------------*/
116 c
= *(rs232_ports
[RS232_PORT_1
].UDR
);
118 if(rs232_ports
[RS232_PORT_1
].input_handler
!= NULL
) {
119 rs232_ports
[RS232_PORT_1
].input_handler(c
);
123 /*---------------------------------------------------------------------------*/
125 rs232_init (uint8_t port
, uint8_t bd
, uint8_t ffmt
)
127 *(rs232_ports
[port
].UBRRH
) = (uint8_t)(bd
>>8);
128 *(rs232_ports
[port
].UBRRL
) = (uint8_t)bd
;
131 * - Enable receiver and transmitter,
132 * - Enable interrupts for receiver and transmitter
134 *(rs232_ports
[port
].UCSRB
) = USART_INTERRUPT_RX_COMPLETE
| USART_INTERRUPT_TX_COMPLETE
| \
135 USART_RECEIVER_ENABLE
| USART_TRANSMITTER_ENABLE
;
138 * - mode (sync. / async)
141 * - charater size (9 bits are currently not supported)
144 *(rs232_ports
[port
].UCSRC
) = ffmt
;
146 rs232_ports
[port
].txwait
= 0;
148 rs232_ports
[port
].input_handler
= NULL
;
152 rs232_print_p(uint8_t port
, prog_char
*buf
)
154 while(pgm_read_byte(buf
)) {
155 rs232_send(port
, pgm_read_byte(buf
));
159 /*---------------------------------------------------------------------------*/
161 rs232_print(uint8_t port
, char *buf
)
164 rs232_send(port
, *buf
++);
167 /*---------------------------------------------------------------------------*/
169 rs232_printf(uint8_t port
, const char *fmt
, ...)
172 static char buf
[RS232_PRINTF_BUFFER_LENGTH
];
175 vsnprintf (buf
, RS232_PRINTF_BUFFER_LENGTH
, fmt
, ap
);
178 rs232_print (port
, buf
);
180 /*---------------------------------------------------------------------------*/
182 rs232_send(uint8_t port
, unsigned char c
)
184 rs232_ports
[port
].txwait
= 1;
185 *(rs232_ports
[port
].UDR
) = c
;
186 while(rs232_ports
[port
].txwait
);
188 /*---------------------------------------------------------------------------*/
190 rs232_set_input(uint8_t port
, int (*f
)(unsigned char))
192 rs232_ports
[port
].input_handler
= f
;
194 /*---------------------------------------------------------------------------*/
196 slip_arch_writeb(unsigned char c
)
198 rs232_send(SLIP_PORT
, c
);
200 /*---------------------------------------------------------------------------*/
201 int rs232_stdout_putchar(char c
, FILE *stream
);
202 static uint8_t stdout_rs232_port
=RS232_PORT_0
;
203 static FILE rs232_stdout
= FDEV_SETUP_STREAM(rs232_stdout_putchar
,
207 int rs232_stdout_putchar(char c
, FILE *stream
)
209 rs232_send (stdout_rs232_port
, c
);
212 /*---------------------------------------------------------------------------*/
213 void rs232_redirect_stdout (uint8_t port
) {
214 stdout_rs232_port
= port
;
215 stdout
= &rs232_stdout
;