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