Contiki 2.5
rs232.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$Id: rs232.c,v 1.9 2009/06/29 12:46:50 nvt-se Exp $
32  */
33 
34 /** \addtogroup esbrs232
35  * @{ */
36 
37 /**
38  * \file
39  * RS232 communication device driver for the MSP430.
40  * \author Adam Dunkels <adam@sics.se>
41  *
42  * This file contains an RS232 device driver for the MSP430 microcontroller.
43  *
44  */
45 #include "contiki.h"
46 #include <string.h>
47 #include "dev/msb430-uart1.h"
48 #include "rs232.h"
49 
50 #ifndef U1IFG
51 #define U1IFG IFG2
52 #endif
53 
54 /*---------------------------------------------------------------------------*/
55 /**
56  * Initalize the RS232 port.
57  *
58  */
59 void
60 rs232_init(void)
61 {
62  rs232_set_speed(RS232_115200);
63 }
64 /*---------------------------------------------------------------------------*/
65 int
66 putchar(int c)
67 {
68  if(uart_get_mode() == UART_MODE_RS232) {
69  /* Loop until the transmission buffer is available. */
70  UART_WAIT_TX();
71  /* Transmit the data. */
72  UART_TX = c;
73  return c;
74  } else {
75  return -1;
76  }
77 }
78 /*---------------------------------------------------------------------------*/
79 void
80 rs232_send(char c)
81 {
82  /* Check if the UART is in RS232 mode before sending.
83  This check can be ommitted if every access to rs232 locks the uart
84  before using it.
85  */
86 
87  putchar(c);
88 }
89 /*---------------------------------------------------------------------------*/
90 void
91 rs232_set_speed(enum rs232_speed speed)
92 {
93  // baud
94  const unsigned char br_table[5][3] = {
95  {0x00, 0x01, 0x00}, // 9600
96  {0x80, 0x00, 0x00}, // 19200
97  {0x40, 0x00, 0x00}, // 38400
98  {0x2a, 0x00, 0x5b}, // 57600
99  {0x15, 0x00, 0x4a} // 115200
100  };
101 
102  uart_set_speed(UART_MODE_RS232, br_table[speed][0],
103  br_table[speed][1], br_table[speed][2]);
104 }
105 /*---------------------------------------------------------------------------*/
106 void
107 rs232_print(char *cptr)
108 {
109  /* lock UART for the print operation */
110  if(uart_lock(UART_MODE_RS232)) {
111  while(*cptr != 0) {
112  rs232_send(*cptr);
113  ++cptr;
114  }
115  uart_unlock(UART_MODE_RS232);
116  }
117 }
118 /*---------------------------------------------------------------------------*/
119 void
120 rs232_set_input(uart_handler_t f)
121 {
122  uart_set_handler(UART_MODE_RS232, f);
123 }
124 /** @} */