Contiki 2.5
uart1x.c
1 /*
2  * Copyright (c) 2010, 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  * @(#)$Id: uart1x.c,v 1.1 2010/08/24 16:23:20 joxe Exp $
30  */
31 
32 /*
33  * Machine dependent MSP430X UART1 code.
34  */
35 #include "contiki.h"
36 #include <stdlib.h>
37 #include "sys/energest.h"
38 #include "dev/uart1.h"
39 #include "dev/watchdog.h"
40 
41 #include "lib/ringbuf.h"
42 
43 static int (*uart1_input_handler)(unsigned char c);
44 
45 static volatile uint8_t transmitting;
46 
47 #ifdef UART1_CONF_TX_WITH_INTERRUPT
48 #define TX_WITH_INTERRUPT UART1_CONF_TX_WITH_INTERRUPT
49 #else /* UART1_CONF_TX_WITH_INTERRUPT */
50 #define TX_WITH_INTERRUPT 1
51 #endif /* UART1_CONF_TX_WITH_INTERRUPT */
52 
53 #if TX_WITH_INTERRUPT
54 #define TXBUFSIZE 64
55 
56 static struct ringbuf txbuf;
57 static uint8_t txbuf_data[TXBUFSIZE];
58 #endif /* TX_WITH_INTERRUPT */
59 
60 /*---------------------------------------------------------------------------*/
61 uint8_t
62 uart1_active(void)
63 {
64  return (UCA0STAT & UCBUSY) | transmitting;
65 }
66 /*---------------------------------------------------------------------------*/
67 void
68 uart1_set_input(int (*input)(unsigned char c))
69 {
70  uart1_input_handler = input;
71 }
72 /*---------------------------------------------------------------------------*/
73 void
74 uart1_writeb(unsigned char c)
75 {
76  /* watchdog_periodic(); */
77 #if TX_WITH_INTERRUPT
78 
79  /* Put the outgoing byte on the transmission buffer. If the buffer
80  is full, we just keep on trying to put the byte into the buffer
81  until it is possible to put it there. */
82  while(ringbuf_put(&txbuf, c) == 0);
83 
84  /* If there is no transmission going, we need to start it by putting
85  the first byte into the UART. */
86  if(transmitting == 0) {
87  transmitting = 1;
88  UCA0TXBUF = ringbuf_get(&txbuf);
89  }
90 
91 #else /* TX_WITH_INTERRUPT */
92 
93  /* Loop until the transmission buffer is available. */
94  while(!(IFG2 & UCA0TXIFG));
95 
96  /* Transmit the data. */
97  UCA0TXBUF = c;
98 #endif /* TX_WITH_INTERRUPT */
99 }
100 /*---------------------------------------------------------------------------*/
101 #if ! WITH_UIP /* If WITH_UIP is defined, putchar() is defined by the SLIP driver */
102 #endif /* ! WITH_UIP */
103 /*---------------------------------------------------------------------------*/
104 /**
105  * Initalize the RS232 port.
106  *
107  */
108 void
109 uart1_init(unsigned long ubr)
110 {
111  /* RS232 */
112  P3SEL |= 0x30; /* P3.4,5 = USCI_A0 TXD/RXD */
113  UCA0CTL1 |= UCSSEL_2; /* CLK = SMCLK */
114  UCA0BR0 = 0x45; /* 8MHz/115200 = 69 = 0x45 */
115  UCA0BR1 = 0x00;
116  UCA0MCTL = UCBRS2; /* Modulation UCBRSx = 4 */
117  UCA0CTL1 &= ~UCSWRST; /* Initialize USCI state machine */
118 
119  transmitting = 0;
120 
121 }
122 /*---------------------------------------------------------------------------*/
123 interrupt(USCIAB1RX_VECTOR)
124 uart1_rx_interrupt(void)
125 {
126  uint8_t c;
127  ENERGEST_ON(ENERGEST_TYPE_IRQ);
128 
129  /* Check status register for receive errors. */
130  if(UCA0STAT & UCRXERR) {
131  c = UCA0RXBUF; /* Clear error flags by forcing a dummy read. */
132  } else {
133  c = UCA0RXBUF;
134  if(uart1_input_handler != NULL) {
135  if(uart1_input_handler(c)) {
136  LPM4_EXIT;
137  }
138  }
139  }
140  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
141 }
142 /*---------------------------------------------------------------------------*/
143 #if TX_WITH_INTERRUPT
144 interrupt(USCIAB1TX_VECTOR)
145 uart1_tx_interrupt(void)
146 {
147  ENERGEST_ON(ENERGEST_TYPE_IRQ);
148  if(IFG2 & UCA0TXIFG) {
149  if(ringbuf_elements(&txbuf) == 0) {
150  transmitting = 0;
151  } else {
152  UCA0TXBUF = ringbuf_get(&txbuf);
153  }
154  }
155 
156  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
157 }
158 #endif /* TX_WITH_INTERRUPT */
159 /*---------------------------------------------------------------------------*/