rs232.c

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * @(#)$Id: rs232.c,v 1.7 2010/10/27 14:51:20 dak664 Exp $
00032  */
00033 
00034 #include <stdio.h>
00035 #include <avr/io.h>
00036 #include <avr/interrupt.h>
00037 #include <avr/pgmspace.h>
00038 
00039 #include "contiki-conf.h"
00040 #include "contiki.h"
00041 
00042 #include "dev/slip.h"
00043 #include "dev/rs232.h"
00044 
00045 #ifdef RS232_CONF_PRINTF_BUFFER_LENGTH
00046 #define RS232_PRINTF_BUFFER_LENGTH RS232_CONF_PRINTF_BUFFER_LENGTH
00047 #else
00048 #define RS232_PRINTF_BUFFER_LENGTH 64
00049 #endif
00050 
00051 #ifndef ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
00052 #define ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT 1
00053 #endif
00054 
00055 #if defined (__AVR_ATmega128__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega128RFA1__)
00056 typedef struct {
00057   volatile uint8_t * UDR;
00058   volatile uint8_t * UBRRH;
00059   volatile uint8_t * UBRRL;
00060   volatile uint8_t * UCSRB;
00061   volatile uint8_t * UCSRC;
00062   volatile uint8_t txwait;
00063   int (* input_handler)(unsigned char);
00064 } rs232_t;
00065 
00066 static rs232_t rs232_ports[2] = {
00067   {   // UART0
00068     &UDR0,
00069     &UBRR0H,
00070     &UBRR0L,
00071     &UCSR0B,
00072     &UCSR0C,
00073     0,
00074     NULL
00075   },
00076 
00077   {  // UART1
00078     &UDR1,
00079     &UBRR1H,
00080     &UBRR1L,
00081     &UCSR1B,
00082     &UCSR1C,
00083     0,
00084     NULL
00085   }
00086 };
00087 /*---------------------------------------------------------------------------*/
00088 ISR(USART0_TX_vect)
00089 {
00090   rs232_ports[RS232_PORT_0].txwait = 0;
00091 }
00092 
00093 /*---------------------------------------------------------------------------*/
00094 ISR(USART0_RX_vect)
00095 {
00096   unsigned char c;
00097 
00098   c = *(rs232_ports[RS232_PORT_0].UDR);
00099 
00100   if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
00101     rs232_ports[RS232_PORT_0].input_handler(c);
00102   }
00103 }
00104 /*---------------------------------------------------------------------------*/
00105 ISR(USART1_TX_vect)
00106 {
00107   rs232_ports[RS232_PORT_1].txwait = 0;
00108 }
00109 
00110 /*---------------------------------------------------------------------------*/
00111 ISR(USART1_RX_vect)
00112 {
00113   unsigned char c;
00114 
00115   c = *(rs232_ports[RS232_PORT_1].UDR);
00116 
00117   if(rs232_ports[RS232_PORT_1].input_handler != NULL) {
00118     rs232_ports[RS232_PORT_1].input_handler(c);
00119   }
00120 }
00121 
00122 #elif defined (__AVR_AT90USB1287__)
00123 /* Has only UART1, map it to port 0 */
00124 typedef struct {
00125   volatile uint8_t * UDR;
00126   volatile uint8_t * UBRRH;
00127   volatile uint8_t * UBRRL;
00128   volatile uint8_t * UCSRB;
00129   volatile uint8_t * UCSRC;
00130   volatile uint8_t txwait;
00131   int (* input_handler)(unsigned char);
00132 } rs232_t;
00133 
00134 static rs232_t rs232_ports[1] = {
00135   {  // UART1
00136     &UDR1,
00137     &UBRR1H,
00138     &UBRR1L,
00139     &UCSR1B,
00140     &UCSR1C,
00141     0,
00142     NULL
00143   }
00144 };
00145 /*---------------------------------------------------------------------------*/
00146 ISR(USART1_TX_vect)
00147 {
00148   rs232_ports[RS232_PORT_0].txwait = 0;
00149 }
00150 
00151 /*---------------------------------------------------------------------------*/
00152 ISR(USART1_RX_vect)
00153 {
00154   unsigned char c;
00155 
00156   c = *(rs232_ports[RS232_PORT_0].UDR);
00157 
00158   if(rs232_ports[RS232_PORT_0].input_handler != NULL) {
00159     rs232_ports[RS232_PORT_0].input_handler(c);
00160   }
00161 }
00162 #else
00163 #error Please define the UART registers for your MCU!
00164 #endif
00165 
00166 /*---------------------------------------------------------------------------*/
00167 void
00168 rs232_init (uint8_t port, uint8_t bd, uint8_t ffmt)
00169 {
00170   *(rs232_ports[port].UBRRH) = (uint8_t)(bd>>8);
00171   *(rs232_ports[port].UBRRL) = (uint8_t)bd;
00172 
00173   /*
00174    * - Enable receiver and transmitter,
00175    * - Enable interrupts for receiver and transmitter
00176    */
00177   *(rs232_ports[port].UCSRB) = USART_INTERRUPT_RX_COMPLETE | USART_INTERRUPT_TX_COMPLETE | \
00178     USART_RECEIVER_ENABLE | USART_TRANSMITTER_ENABLE;
00179 
00180   /*
00181    * - mode (sync. / async)
00182    * - Parity
00183    * - Stop bits
00184    * - charater size (9 bits are currently not supported)
00185    * - clock polarity
00186    */
00187   *(rs232_ports[port].UCSRC) = ffmt;
00188 
00189   rs232_ports[port].txwait = 0;
00190 
00191   rs232_ports[port].input_handler = NULL;
00192 }
00193 
00194 void
00195 rs232_print_p(uint8_t port, prog_char *buf)
00196 {
00197   while(pgm_read_byte(buf)) {
00198     rs232_send(port, pgm_read_byte(buf));
00199     ++buf;
00200   }
00201 }
00202 /*---------------------------------------------------------------------------*/
00203 void
00204 rs232_print(uint8_t port, char *buf)
00205 {
00206   while(*buf) {
00207 #if ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
00208     if(*buf=='\n') rs232_send(port, '\r');
00209         if(*buf=='\r') buf++; else rs232_send(port, *buf++);
00210 #else
00211     rs232_send(port, *buf++);
00212 #endif
00213   }
00214 }
00215 /*---------------------------------------------------------------------------*/
00216 void
00217 rs232_printf(uint8_t port, const char *fmt, ...)
00218 {
00219   va_list ap;
00220   static char buf[RS232_PRINTF_BUFFER_LENGTH];
00221 
00222   va_start (ap, fmt);
00223   vsnprintf (buf, RS232_PRINTF_BUFFER_LENGTH, fmt, ap);
00224   va_end(ap);
00225 
00226   rs232_print (port, buf);
00227 }
00228 /*---------------------------------------------------------------------------*/
00229 void
00230 rs232_send(uint8_t port, unsigned char c)
00231 {
00232   rs232_ports[port].txwait = 1;
00233   *(rs232_ports[port].UDR) = c;
00234   while(rs232_ports[port].txwait);
00235 }
00236 /*---------------------------------------------------------------------------*/
00237 void
00238 rs232_set_input(uint8_t port, int (*f)(unsigned char))
00239 {
00240   rs232_ports[port].input_handler = f;
00241 }
00242 /*---------------------------------------------------------------------------*/
00243 void
00244 slip_arch_writeb(unsigned char c)
00245 {
00246   rs232_send(SLIP_PORT, c);
00247 }
00248 /*---------------------------------------------------------------------------*/
00249 int rs232_stdout_putchar(char c, FILE *stream);
00250 static uint8_t stdout_rs232_port=RS232_PORT_0;
00251 static FILE rs232_stdout = FDEV_SETUP_STREAM(rs232_stdout_putchar,
00252                                              NULL,
00253                                              _FDEV_SETUP_WRITE);
00254 
00255 int rs232_stdout_putchar(char c, FILE *stream)
00256 {
00257 #if ADD_CARRAGE_RETURNS_TO_SERIAL_OUTPUT
00258   if(c=='\n') rs232_send(stdout_rs232_port, '\r');
00259   if(c!='\r') rs232_send (stdout_rs232_port, c);
00260 #else
00261   rs232_send (stdout_rs232_port, c);
00262 #endif
00263   return 0;
00264 }
00265 /*---------------------------------------------------------------------------*/
00266 void rs232_redirect_stdout (uint8_t port) {
00267   stdout_rs232_port = port;
00268   stdout = &rs232_stdout;
00269 }

Generated on Mon Apr 11 14:23:36 2011 for Contiki 2.5 by  doxygen 1.6.1