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

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