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.3 2007/08/07 11:06:14 nifi 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 
46 #include "contiki.h"
47 #include <string.h>
48 
49 #include "contiki-esb.h"
50 
51 static int (* input_handler)(unsigned char) = NULL;
52 
53 /*---------------------------------------------------------------------------*/
54 interrupt(UART1RX_VECTOR)
55  rs232_rx_usart1(void)
56 {
57  ENERGEST_ON(ENERGEST_TYPE_IRQ);
58  /* Check status register for receive errors. - before reading RXBUF since
59  it clears the error and interrupt flags */
60  if(!(URCTL1 & RXERR) && input_handler != NULL) {
61 
62  if(input_handler(RXBUF1)) {
63  LPM4_EXIT;
64  }
65  } else {
66  /* Else read out the char to clear the I-flags, etc. */
67  RXBUF1;
68  }
69  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
70 }
71 /*---------------------------------------------------------------------------*/
72 /**
73  * Initalize the RS232 port.
74  *
75  */
76 void
77 rs232_init(void)
78 {
79 
80  /* RS232 */
81  UCTL1 = CHAR; /* 8-bit character */
82  UTCTL1 = SSEL1; /* UCLK = MCLK */
83 
84  rs232_set_speed(RS232_57600);
85 
86  input_handler = NULL;
87 
88  ME2 |= (UTXE1 | URXE1); /* Enable USART1 TXD/RXD */
89  IE2 |= URXIE1; /* Enable USART1 RX interrupt */
90 }
91 /*---------------------------------------------------------------------------*/
92 void
93 rs232_send(char c)
94 {
95 
96  ENERGEST_ON(ENERGEST_TYPE_SERIAL);
97  /* Loop until the transmission buffer is available. */
98  while((IFG2 & UTXIFG1) == 0) {
99  }
100 
101  /* Transmit the data. */
102  TXBUF1 = c;
103  ENERGEST_OFF(ENERGEST_TYPE_SERIAL);
104 }
105 /*---------------------------------------------------------------------------*/
106 void
107 rs232_set_speed(unsigned char speed)
108 {
109  if(speed == RS232_19200) {
110  /* Set RS232 to 19200 */
111  UBR01 = 0x80; /* 2,457MHz/19200 = 128 -> 0x80 */
112  UBR11 = 0x00; /* */
113  UMCTL1 = 0x00; /* no modulation */
114  } else if(speed == RS232_38400) {
115  /* Set RS232 to 38400 */
116  UBR01 = 0x40; /* 2,457MHz/38400 = 64 -> 0x40 */
117  UBR11 = 0x00; /* */
118  UMCTL1 = 0x00; /* no modulation */
119  } else if(speed == RS232_57600) {
120  UBR01 = 0x2a; /* 2,457MHz/57600 = 42.7 -> 0x2A */
121  UBR11 = 0x00; /* */
122  UMCTL1 = 0x5b; /* */
123  } else if(speed == RS232_115200) {
124  UBR01 = 0x15; /* 2,457MHz/115200 = 21.4 -> 0x15 */
125  UBR11 = 0x00; /* */
126  UMCTL1 = 0x4a; /* */
127  } else {
128  rs232_set_speed(RS232_57600);
129  }
130 
131 }
132 /*---------------------------------------------------------------------------*/
133 void
134 rs232_print(char *cptr)
135 {
136  while(*cptr != 0) {
137  rs232_send(*cptr);
138  ++cptr;
139  }
140 }
141 /*---------------------------------------------------------------------------*/
142 void
143 rs232_set_input(int (*f)(unsigned char))
144 {
145  input_handler = f;
146 }
147 /*---------------------------------------------------------------------------*/
148 void
149 slip_arch_writeb(unsigned char c)
150 {
151  rs232_send(c);
152 }
153 /*---------------------------------------------------------------------------*/
154 /** @} */