Contiki 2.6

rs232.c

Go to the documentation of this file.
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.3 2007/08/07 11:06:14 nifi Exp $
00032  */
00033 
00034 /** \addtogroup esbrs232
00035  * @{ */
00036 
00037 /**
00038  * \file
00039  * RS232 communication device driver for the MSP430.
00040  * \author Adam Dunkels <adam@sics.se>
00041  *
00042  * This file contains an RS232 device driver for the MSP430 microcontroller.
00043  *
00044  */
00045 
00046 #include "contiki.h"
00047 #include <string.h>
00048 
00049 #include "contiki-esb.h"
00050 #include "isr_compat.h"
00051 
00052 static int (* input_handler)(unsigned char) = NULL;
00053 
00054 /*---------------------------------------------------------------------------*/
00055 ISR(UART1RX, rs232_rx_usart1)
00056 {
00057   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00058   /* Check status register for receive errors. - before reading RXBUF since
00059      it clears the error and interrupt flags */
00060   if(!(URCTL1 & RXERR) && input_handler != NULL) {
00061 
00062     if(input_handler(RXBUF1)) {
00063       LPM4_EXIT;
00064     }
00065   } else {
00066     /* Else read out the char to clear the I-flags, etc. */
00067     RXBUF1;
00068   }
00069   ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00070 }
00071 /*---------------------------------------------------------------------------*/
00072 /**
00073  * Initalize the RS232 port.
00074  *
00075  */
00076 void
00077 rs232_init(void)
00078 {
00079 
00080   /* RS232 */
00081   UCTL1 = CHAR;                         /* 8-bit character */
00082   UTCTL1 = SSEL1;                       /* UCLK = MCLK */
00083 
00084   rs232_set_speed(RS232_57600);
00085 
00086   input_handler = NULL;
00087 
00088   ME2 |= (UTXE1 | URXE1);                 /* Enable USART1 TXD/RXD */
00089   IE2 |= URXIE1;                        /* Enable USART1 RX interrupt  */
00090 }
00091 /*---------------------------------------------------------------------------*/
00092 void
00093 rs232_send(char c)
00094 {
00095 
00096   ENERGEST_ON(ENERGEST_TYPE_SERIAL);
00097   /* Loop until the transmission buffer is available. */
00098   while((IFG2 & UTXIFG1) == 0) {
00099   }
00100 
00101   /* Transmit the data. */
00102   TXBUF1 = c;
00103   ENERGEST_OFF(ENERGEST_TYPE_SERIAL);
00104 }
00105 /*---------------------------------------------------------------------------*/
00106 void
00107 rs232_set_speed(unsigned char speed)
00108 {
00109   if(speed == RS232_19200) {
00110     /* Set RS232 to 19200 */
00111     UBR01 = 0x80;                         /* 2,457MHz/19200 = 128 -> 0x80 */
00112     UBR11 = 0x00;                         /* */
00113     UMCTL1 = 0x00;                        /* no modulation  */
00114   } else if(speed == RS232_38400) {
00115     /* Set RS232 to 38400 */
00116     UBR01 = 0x40;                         /* 2,457MHz/38400 = 64 -> 0x40 */
00117     UBR11 = 0x00;                         /* */
00118     UMCTL1 = 0x00;                        /* no modulation  */
00119   } else if(speed == RS232_57600) {
00120     UBR01 = 0x2a;                         /* 2,457MHz/57600 = 42.7 -> 0x2A */
00121     UBR11 = 0x00;                         /* */
00122     UMCTL1 = 0x5b;                        /* */
00123   } else if(speed == RS232_115200) {
00124     UBR01 = 0x15;                         /* 2,457MHz/115200 = 21.4 -> 0x15 */
00125     UBR11 = 0x00;                         /* */
00126     UMCTL1 = 0x4a;                        /* */
00127   } else {
00128     rs232_set_speed(RS232_57600);
00129   }
00130 
00131 }
00132 /*---------------------------------------------------------------------------*/
00133 void
00134 rs232_print(char *cptr)
00135 {
00136   while(*cptr != 0) {
00137     rs232_send(*cptr);
00138     ++cptr;
00139   }
00140 }
00141 /*---------------------------------------------------------------------------*/
00142 void
00143 rs232_set_input(int (*f)(unsigned char))
00144 {
00145   input_handler = f;
00146 }
00147 /*---------------------------------------------------------------------------*/
00148 void
00149 slip_arch_writeb(unsigned char c)
00150 {
00151   rs232_send(c);
00152 }
00153 /*---------------------------------------------------------------------------*/
00154 /** @} */