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.1 2006/06/18 07:49:33 adamdunkels 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 /* 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 LPM_AWAKE(); 00064 } 00065 } else { 00066 /* Else read out the char to clear the I-flags, etc. */ 00067 RXBUF1; 00068 } 00069 } 00070 /*---------------------------------------------------------------------------*/ 00071 /** 00072 * Initalize the RS232 port. 00073 * 00074 */ 00075 void 00076 rs232_init(void) 00077 { 00078 00079 /* RS232 */ 00080 UCTL1 = CHAR; /* 8-bit character */ 00081 UTCTL1 = SSEL1; /* UCLK = MCLK */ 00082 00083 rs232_set_speed(RS232_57600); 00084 00085 input_handler = NULL; 00086 00087 ME2 |= (UTXE1 | URXE1); /* Enable USART1 TXD/RXD */ 00088 IE2 |= URXIE1; /* Enable USART1 RX interrupt */ 00089 } 00090 /*---------------------------------------------------------------------------*/ 00091 void 00092 rs232_send(char c) 00093 { 00094 /* Loop until the transmission buffer is available. */ 00095 while ((IFG2 & UTXIFG1) == 0); 00096 00097 /* Transmit the data. */ 00098 TXBUF1 = c; 00099 } 00100 /*---------------------------------------------------------------------------*/ 00101 void 00102 rs232_set_speed(unsigned char speed) 00103 { 00104 if(speed == RS232_19200) { 00105 /* Set RS232 to 19200 */ 00106 UBR01 = 0x80; /* 2,457MHz/19200 = 128 -> 0x80 */ 00107 UBR11 = 0x00; /* */ 00108 UMCTL1 = 0x00; /* no modulation */ 00109 } else if(speed == RS232_38400) { 00110 /* Set RS232 to 38400 */ 00111 UBR01 = 0x40; /* 2,457MHz/38400 = 64 -> 0x40 */ 00112 UBR11 = 0x00; /* */ 00113 UMCTL1 = 0x00; /* no modulation */ 00114 } else if(speed == RS232_57600) { 00115 UBR01 = 0x2a; /* 2,457MHz/57600 = 42.7 -> 0x2A */ 00116 UBR11 = 0x00; /* */ 00117 UMCTL1 = 0x5b; /* */ 00118 } else if(speed == RS232_115200) { 00119 UBR01 = 0x15; /* 2,457MHz/115200 = 21.4 -> 0x15 */ 00120 UBR11 = 0x00; /* */ 00121 UMCTL1 = 0x4a; /* */ 00122 } else { 00123 rs232_set_speed(RS232_57600); 00124 } 00125 00126 } 00127 /*---------------------------------------------------------------------------*/ 00128 void 00129 rs232_print(char *cptr) 00130 { 00131 while(*cptr != 0) { 00132 rs232_send(*cptr); 00133 ++cptr; 00134 } 00135 } 00136 /*---------------------------------------------------------------------------*/ 00137 void 00138 rs232_set_input(int (*f)(unsigned char)) 00139 { 00140 input_handler = f; 00141 } 00142 /*---------------------------------------------------------------------------*/ 00143 void 00144 slip_arch_writeb(unsigned char c) 00145 { 00146 rs232_send(c); 00147 } 00148 /*---------------------------------------------------------------------------*/ 00149 /** @} */