Enable radio stats in sensor cgi as default
[contiki-2.x.git] / cpu / avr / dev / rs232.c
blobd848656055aed2418f6b49830e399136522c639f
1 /*
2 * Copyright (c) 2005, Swedish Institute of Computer Science
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
27 * SUCH DAMAGE.
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 $
34 #include <stdio.h>
35 #include <avr/io.h>
36 #include <avr/interrupt.h>
37 #include <avr/pgmspace.h>
39 #include "contiki-conf.h"
40 #include "contiki.h"
42 #include "dev/slip.h"
43 #include "dev/rs232.h"
45 #ifdef RS232_CONF_PRINTF_BUFFER_LENGTH
46 #define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH
47 #else
48 #define RS232_PRINTF_BUFFER_LENGTH 64
49 #endif
51 #if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__)
52 typedef struct {
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);
60 } rs232_t;
62 static rs232_t rs232_ports[2] = {
63 { // UART0
64 &UDR0,
65 &UBRR0H,
66 &UBRR0L,
67 &UCSR0B,
68 &UCSR0C,
70 NULL
73 { // UART1
74 &UDR1,
75 &UBRR1H,
76 &UBRR1L,
77 &UCSR1B,
78 &UCSR1C,
80 NULL
83 #else
84 #error Please define the UART registers for your MCU!
85 #endif
87 /*---------------------------------------------------------------------------*/
88 ISR(USART0_TX_vect)
90 rs232_ports[RS232_PORT_0].txwait = 0;
93 /*---------------------------------------------------------------------------*/
94 ISR(USART0_RX_vect)
96 unsigned char c;
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 /*---------------------------------------------------------------------------*/
106 ISR(USART1_TX_vect)
108 rs232_ports[RS232_PORT_1].txwait = 0;
111 /*---------------------------------------------------------------------------*/
112 ISR(USART1_RX_vect)
114 unsigned char c;
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 /*---------------------------------------------------------------------------*/
124 void
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)
139 * - Parity
140 * - Stop bits
141 * - charater size (9 bits are currently not supported)
142 * - clock polarity
144 *(rs232_ports[port].UCSRC) = ffmt;
146 rs232_ports[port].txwait = 0;
148 rs232_ports[port].input_handler = NULL;
151 void
152 rs232_print_p(uint8_t port, prog_char *buf)
154 while(pgm_read_byte(buf)) {
155 rs232_send(port, pgm_read_byte(buf));
156 ++buf;
159 /*---------------------------------------------------------------------------*/
160 void
161 rs232_print(uint8_t port, char *buf)
163 while(*buf) {
164 rs232_send(port, *buf++);
167 /*---------------------------------------------------------------------------*/
168 void
169 rs232_printf(uint8_t port, const char *fmt, ...)
171 va_list ap;
172 static char buf[RS232_PRINTF_BUFFER_LENGTH];
174 va_start (ap, fmt);
175 vsnprintf (buf, RS232_PRINTF_BUFFER_LENGTH, fmt, ap);
176 va_end(ap);
178 rs232_print (port, buf);
180 /*---------------------------------------------------------------------------*/
181 void
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 /*---------------------------------------------------------------------------*/
189 void
190 rs232_set_input(uint8_t port, int (*f)(unsigned char))
192 rs232_ports[port].input_handler = f;
194 /*---------------------------------------------------------------------------*/
195 void
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,
204 NULL,
205 _FDEV_SETUP_WRITE);
207 int rs232_stdout_putchar(char c, FILE *stream)
209 rs232_send (stdout_rs232_port, c);
210 return 0;
212 /*---------------------------------------------------------------------------*/
213 void rs232_redirect_stdout (uint8_t port) {
214 stdout_rs232_port = port;
215 stdout = &rs232_stdout;