Contiki 3.x
rs232.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
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.
16  *
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.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 #include <stdio.h>
34 #include <avr/io.h>
35 #include <avr/interrupt.h>
36 #include <avr/pgmspace.h>
37 
38 #include "contiki-conf.h"
39 #include "contiki.h"
40 
41 #include "dev/slip.h"
42 #include "dev/rs232.h"
43 
44 /*ATmega32 and smaller have UBRRH/UCSRC at the same I/O address.
45  *USART_UCSRC_SEL (bit7) selects writing to UBRHH(0) or UCSRC(1).
46  *It is OR'd in below so if not defined we can just set it to zero.
47  */
48 #ifndef USART_UCSRC_SEL
49 #define USART_UCSRC_SEL 0x00
50 #endif
51 
52 /* Currently only the STK500 platform uses a static RAM buffer for printfs.
53  * Others print a character at a time using the gcc string pointer.
54  * gcc may not strip the unused buffer, even though it is statically
55  * allocated in an unused routine.
56  */
57 #ifdef RS232_CONF_PRINTF_BUFFER_LENGTH
58 #define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH
59 #else
60 #if CONTIKI_TARGET_STK500
61 #define RS232_PRINTF_BUFFER_LENGTH 64
62 #endif
63 #endif
64 
65 /* TX interrupts would allow non-blocking output up to the size of some RAM buffer.
66  * Since a RAM buffer is not implemented tx interrupts are superfluous and unwanted
67  * because they block debug prints from within interrupt routines
68  */
69 #ifdef RS232_CONF_TX_INTERRUPTS
70 #define RS232_TX_INTERRUPTS RS232_CONF_TX_INTERRUPTS
71 #else
72 #define RS232_TX_INTERRUPTS 0
73 #endif
74 
75 /* Insert a carriage return after a line feed. This is the default. */
76 #ifndef ADD_CARRIAGE_RETURN_AFTER_NEWLINE
77 #define ADD_CARRIAGE_RETURN_AFTER_NEWLINE 1
78 #endif
79 
80 /* Reducing NUMPORTS from the default will harmlessly disable usage of those ports */
81 /* Two ports take 400 bytes flash and 4 bytes RAM. */
82 #ifdef RS232_CONF_NUMPORTS
83 #define NUMPORTS RS232_CONF_NUMPORTS
84 #endif
85 
86 #if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__)
87 #ifndef NUMPORTS
88 #define NUMPORTS 2
89 #elif NUMPORTS > 2
90 #error Only two serial ports are defined for this processor!
91 #endif
92 
93 #if NUMPORTS > 0
94 #define D_UDR0 UDR0
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
103 
104 #if NUMPORTS > 1
105 #define D_UDR1 UDR1
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
114 #endif
115 
116 #endif
117 
118 #elif defined (__AVR_AT90USB1287__)
119 /* Has only UART1, map it to port 0 */
120 #ifndef NUMPORTS
121 #define NUMPORTS 1
122 #elif NUMPORTS > 1
123 #error Only one serial port is defined for this processor!
124 #endif
125 
126 #if NUMPORTS > 0
127 #define D_UDR0 UDR1
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
136 #endif
137 
138 #elif defined (__AVR_ATmega8515__)
139 #ifndef NUMPORTS
140 #define NUMPORTS 1
141 #elif NUMPORTS > 1
142 #error Only one serial port is defined for this processor!
143 #endif
144 
145 #if NUMPORTS > 0
146 #define D_UDR0 UDR
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
155 #endif
156 
157 #elif defined (__AVR_ATmega328P__)
158 #ifndef NUMPORTS
159 #define NUMPORTS 1
160 #elif NUMPORTS > 1
161 #error Only one serial port is defined for this processor!
162 #endif
163 
164 #if NUMPORTS > 0
165 #define D_UDR0 UDR0
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
174 #endif
175 
176 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
177 #ifndef NUMPORTS
178 #define NUMPORTS 1
179 #elif NUMPORTS > 1
180 #error Only one serial port is defined for this processor!
181 #endif
182 
183 #if NUMPORTS > 0
184 #define D_UDR0 UDR
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
193 #endif
194 
195 #elif defined (__AVR_ATmega644__)
196 #ifndef NUMPORTS
197 #define NUMPORTS 1
198 #elif NUMPORTS > 1
199 #error Only one serial port is defined for this processor!
200 #endif
201 
202 #if NUMPORTS > 0
203 #define D_UDR0 UDR0
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
212 #endif
213 
214 #else
215 #error Please define the UART registers for your MCU!
216 #endif
217 
218 #if NUMPORTS > 0
219 int (* input_handler_0)(unsigned char);
220 ISR(D_USART0_RX_vect)
221 {
222  unsigned char c;
223  c = D_UDR0;
224  if (input_handler_0 != NULL) input_handler_0(c);
225 }
226 #if RS232_TX_INTERRUPTS
227 volatile uint8_t txwait_0;
228 ISR(D_USART0_TX_vect)
229 {
230  txwait_0 = 0;
231 }
232 #endif
233 
234 #if NUMPORTS > 1
235 int (* input_handler_1)(unsigned char);
236 ISR(D_USART1_RX_vect)
237 {
238  unsigned char c;
239  c = D_UDR1;
240  if (input_handler_1 != NULL) input_handler_1(c);
241 }
242 #if RS232_TX_INTERRUPTS
243 volatile uint8_t txwait_1;
244 ISR(USART1_TX_vect)
245 {
246  txwait_1 = 0;
247 }
248 #endif
249 
250 #if NUMPORTS > 2
251 int (* input_handler_2)(unsigned char);
252 ISR(D_USART2_RX_vect)
253 {
254  unsigned char c;
255  c = D_UDR2;
256  if (input_handler_2 != NULL) input_handler_2(c);
257 }
258 #if RS232_TX_INTERRUPTS
259 volatile uint8_t txwait_2;
260 ISR(USART2_TX_vect)
261 {
262  txwait_2= 0;
263 }
264 #endif
265 #endif
266 
267 #endif
268 #endif
269 /*---------------------------------------------------------------------------*/
270 void
271 rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt)
272 {
273 #if NUMPORTS > 0
274  if (port == 0) {
275  D_UBRR0H = (uint8_t)(bd>>8);
276  D_UBRR0L = (uint8_t)bd;
277 #if RS232_TX_INTERRUPTS
278  txwait_0 = 0;
279  D_UCSR0B = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
280  USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
281 #else
282  D_UCSR0B = USART_INTERRUPT_RX_COMPLETE | \
283  USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
284 #endif
285  D_UCSR0C = USART_UCSRC_SEL | ffmt;
286  input_handler_0 = NULL;
287 
288 #if NUMPORTS > 1
289  } else if (port == 1) {
290  D_UBRR1H = (uint8_t)(bd>>8);
291  D_UBRR1L = (uint8_t)bd;
292 #if RS232_TX_INTERRUPTS
293  txwait_1 = 0;
294  D_UCSR1B = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
295  USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
296 #else
297  D_UCSR1B = USART_INTERRUPT_RX_COMPLETE | \
298  USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
299 #endif
300  D_UCSR1C = USART_UCSRC_SEL | ffmt;
301  input_handler_1 = NULL;
302 
303 #if NUMPORTS > 2
304  } else if (port == 2) {
305  D_UBRR2H = (uint8_t)(bd>>8);
306  D_UBRR2L = (uint8_t)bd;
307 #if RS232_TX_INTERRUPTS
308  txwait_2 = 0;
309  D_UCSR2B = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
310  USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
311 #else
312  D_UCSR2B = USART_INTERRUPT_RX_COMPLETE | \
313  USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
314 #endif
315  D_UCSR2C = USART_UCSRC_SEL | ffmt;
316  input_handler_2 = NULL;
317 #endif
318 #endif
319  }
320 #endif /* NUMPORTS > 0 */
321 }
322 
323 /*---------------------------------------------------------------------------*/
324 void
325 rs232_send(uint8_t port, unsigned char c)
326 {
327 #if RS232_TX_INTERRUPTS
328  /* Output character and block until it is transmitted */
329 #if NUMPORTS > 0
330  if (port == 0 ) {
331  txwait_0 = 1;
332  D_UDR0 = c;
333  while (txwait_0);
334 #if NUMPORTS > 1
335  } else if (port == 1) {
336  txwait_1 = 1;
337  D_UDR1 = c;
338  while (txwait_1);
339 #if NUMPORTS > 2
340  } else if (port == 2) {
341  txwait_2 = 1;
342  D_UDR2 = c;
343  while (txwait_2);
344 #endif
345 #endif
346  }
347 #endif
348 #else /* RS232_TX_INTERRUPTS */
349  /* Block until tx ready and output character */
350 #if NUMPORTS > 0
351  if (port == 0 ) {
352  while (!(D_UCSR0A & D_UDRE0M));
353  D_UDR0 = c;
354 #if NUMPORTS > 1
355  } else if (port == 1) {
356  while (!(D_UCSR1A & D_UDRE1M));
357  D_UDR1 = c;
358 #if NUMPORTS > 2
359  } else if (port == 2) {
360  while (!(D_UCSR2A & D_UDRE2M));
361  D_UDR2 = c;
362 #endif
363 #endif
364  }
365 #endif
366 #endif /* RS232_TX_INTERRUPTS */
367 }
368 /*---------------------------------------------------------------------------*/
369 void
370 rs232_set_input(uint8_t port, int (*f)(unsigned char))
371 {
372 #if NUMPORTS > 0
373  if (port == 0) {
374  input_handler_0 = f;
375 #if NUMPORTS > 1
376  } else if (port == 1) {
377  input_handler_1 = f;
378 #if NUMPORTS > 2
379  } else if (port == 2) {
380  input_handler_2 = f;
381 #endif
382 #endif
383  }
384 #endif
385 }
386 
387 /*---------------------------------------------------------------------------*/
388 void
389 rs232_print(uint8_t port, char *buf)
390 {
391  while(*buf) {
392 #if ADD_CARRIAGE_RETURN_AFTER_NEWLINE
393  if(*buf=='\n') rs232_send(port, '\r');
394  if(*buf=='\r') buf++; else rs232_send(port, *buf++);
395 #else
396  rs232_send(port, *buf++);
397 #endif
398  }
399 }
400 
401 #if RS232_PRINTF_BUFFER_LENGTH
402 /*---------------------------------------------------------------------------*/
403 void
404 rs232_printf(uint8_t port, const char *fmt, ...)
405 {
406  va_list ap;
407  static char buf[RS232_PRINTF_BUFFER_LENGTH];
408 
409  va_start (ap, fmt);
410  vsnprintf (buf, RS232_PRINTF_BUFFER_LENGTH, fmt, ap);
411  va_end(ap);
412 
413  rs232_print (port, buf);
414 }
415 #endif
416 /*---------------------------------------------------------------------------*/
417 void
418 slip_arch_writeb(unsigned char c)
419 {
420  rs232_send(SLIP_PORT, c);
421 }
422 /*---------------------------------------------------------------------------*/
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,
426  NULL,
427  _FDEV_SETUP_WRITE);
428 
429 int rs232_stdout_putchar(char c, FILE *stream)
430 {
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);
434 #else
435  rs232_send (stdout_rs232_port, c);
436 #endif
437  return 0;
438 }
439 /*---------------------------------------------------------------------------*/
440 void rs232_redirect_stdout (uint8_t port) {
441  stdout_rs232_port = port;
442  stdout = &rs232_stdout;
443 }
void rs232_init(void)
Initialize the RS232 module.
Definition: rs232.c:50
void slip_arch_writeb(unsigned char c)
Copyright (c) 2014, Analog Devices, Inc.
Definition: slip-arch.c:46
void rs232_set_input(int(*f)(unsigned char))
Set an input handler for incoming RS232 data.
Definition: rs232.c:55
#define NULL
The null pointer.
void rs232_print(char *text)
Print a text string on RS232.
Definition: rs232.c:65
void rs232_send(uint8_t port, unsigned char c)
Print a character on RS232.
Definition: rs232.c:325