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.9 2009/06/29 12:46:50 nvt-se 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 #include "dev/msb430-uart1.h"
00050 #include "rs232.h"
00051 
00052 #ifndef U1IFG
00053 #define U1IFG   IFG2
00054 #endif
00055 
00056 /*---------------------------------------------------------------------------*/
00057 /**
00058  * Initalize the RS232 port.
00059  *
00060  */
00061 void
00062 rs232_init(void)
00063 {
00064   rs232_set_speed(RS232_115200);
00065 }
00066 /*---------------------------------------------------------------------------*/
00067 int
00068 putchar(int c)
00069 {
00070   if(uart_get_mode() == UART_MODE_RS232) {
00071     /* Loop until the transmission buffer is available. */
00072     UART_WAIT_TX();
00073     /* Transmit the data. */
00074     UART_TX = c;
00075     return c;
00076   } else {
00077     return -1;
00078   }
00079 }
00080 /*---------------------------------------------------------------------------*/
00081 void
00082 rs232_send(char c)
00083 {
00084   /* Check if the UART is in RS232 mode before sending.
00085      This check can be ommitted if every access to rs232 locks the uart
00086      before using it.
00087   */
00088 
00089   putchar(c);
00090 }
00091 /*---------------------------------------------------------------------------*/
00092 void
00093 rs232_set_speed(enum rs232_speed speed)
00094 {
00095   // baud
00096   const unsigned char br_table[5][3] = {
00097         {0x00, 0x01, 0x00},             // 9600
00098         {0x80, 0x00, 0x00},             // 19200
00099         {0x40, 0x00, 0x00},             // 38400
00100         {0x2a, 0x00, 0x5b},             // 57600
00101         {0x15, 0x00, 0x4a}              // 115200
00102   };
00103 
00104   uart_set_speed(UART_MODE_RS232, br_table[speed][0], 
00105         br_table[speed][1], br_table[speed][2]);
00106 }
00107 /*---------------------------------------------------------------------------*/
00108 void
00109 rs232_print(char *cptr)
00110 {
00111   /* lock UART for the print operation */
00112   if(uart_lock(UART_MODE_RS232)) {
00113     while(*cptr != 0) {
00114       rs232_send(*cptr);
00115       ++cptr;
00116     }
00117     uart_unlock(UART_MODE_RS232);
00118   }
00119 }
00120 /*---------------------------------------------------------------------------*/
00121 void
00122 rs232_set_input(uart_handler_t f)
00123 {
00124   uart_set_handler(UART_MODE_RS232, f);
00125 }
00126 /** @} */

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