Contiki 2.5
uart0x.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: uart0x.c,v 1.1 2010/08/24 16:23:20 joxe Exp $
30  */
31 
32 /*
33  * Machine dependent MSP430X UART0 code.
34  */
35 
36 #include <stdlib.h>
37 
38 #include "contiki.h"
39 #include "sys/energest.h"
40 #include "dev/uart0.h"
41 #include "dev/watchdog.h"
42 #include "lib/ringbuf.h"
43 #include "dev/leds.h"
44 
45 static int (*uart0_input_handler)(unsigned char c);
46 
47 static volatile uint8_t transmitting;
48 
49 #ifdef UART0_CONF_TX_WITH_INTERRUPT
50 #define TX_WITH_INTERRUPT UART0_CONF_TX_WITH_INTERRUPT
51 #else /* UART0_CONF_TX_WITH_INTERRUPT */
52 #define TX_WITH_INTERRUPT 1
53 #endif /* UART0_CONF_TX_WITH_INTERRUPT */
54 
55 
56 #if TX_WITH_INTERRUPT
57 #define TXBUFSIZE 64
58 
59 static struct ringbuf txbuf;
60 static uint8_t txbuf_data[TXBUFSIZE];
61 #endif /* TX_WITH_INTERRUPT */
62 
63 /*---------------------------------------------------------------------------*/
64 uint8_t
65 uart0_active(void)
66 {
67  return (UCA0STAT & UCBUSY) | transmitting;
68 }
69 /*---------------------------------------------------------------------------*/
70 void
71 uart0_set_input(int (*input)(unsigned char c))
72 {
73  uart0_input_handler = input;
74 }
75 /*---------------------------------------------------------------------------*/
76 void
77 uart0_writeb(unsigned char c)
78 {
79  watchdog_periodic();
80 #if TX_WITH_INTERRUPT
81  /* Put the outgoing byte on the transmission buffer. If the buffer
82  is full, we just keep on trying to put the byte into the buffer
83  until it is possible to put it there. */
84  while(ringbuf_put(&txbuf, c) == 0);
85 
86  /* If there is no transmission going, we need to start it by putting
87  the first byte into the UART. */
88  if(transmitting == 0) {
89  transmitting = 1;
90  UCA0TXBUF = ringbuf_get(&txbuf);
91  }
92 
93 #else /* TX_WITH_INTERRUPT */
94 
95  /* Loop until the transmission buffer is available. */
96  /*Enric while((IFG2 & UCA0TXIFG) == 0); */
97  while((UCA0STAT & UCBUSY));
98 
99  /* Transmit the data. */
100  UCA0TXBUF = c;
101 #endif /* TX_WITH_INTERRUPT */
102 }
103 /*---------------------------------------------------------------------------*/
104 #if ! WITH_UIP /* If WITH_UIP is defined, putchar() is defined by the SLIP driver */
105 #endif /* ! WITH_UIP */
106 /*---------------------------------------------------------------------------*/
107 /**
108  * Initalize the RS232 port.
109  *
110  */
111 void
112 uart0_init(unsigned long ubr)
113 {
114  /* RS232 */
115  UCA0CTL1 |= UCSWRST; /* Hold peripheral in reset state */
116  UCA0CTL1 |= UCSSEL_2; /* CLK = SMCLK */
117 
118  UCA0BR0 = 0x45; /* 8MHz/115200 = 69 = 0x45 */
119  UCA0BR1 = 0x00;
120  UCA0MCTL = UCBRS_3; /* Modulation UCBRSx = 3 */
121 
122  P3DIR &= ~0x20; /* P3.5 = USCI_A0 RXD as input */
123  P3DIR |= 0x10; /* P3.4 = USCI_A0 TXD as output */
124  P3SEL |= 0x30; /* P3.4,5 = USCI_A0 TXD/RXD */
125  /*UCA0CTL1 &= ~UCSWRST;*/ /* Initialize USCI state machine */
126 
127  transmitting = 0;
128 
129  /* XXX Clear pending interrupts before enable */
130  IFG2 &= ~UCA0RXIFG;
131  IFG2 &= ~UCA0TXIFG;
132  UCA0CTL1 &= ~UCSWRST; /* Initialize USCI state machine **before** enabling interrupts */
133  IE2 |= UCA0RXIE; /* Enable UCA0 RX interrupt */
134  /* Enable USCI_A0 TX interrupts (if TX_WITH_INTERRUPT enabled) */
135 #if TX_WITH_INTERRUPT
136  ringbuf_init(&txbuf, txbuf_data, sizeof(txbuf_data));
137  IE2 |= UCA0TXIE; /* Enable UCA0 TX interrupt */
138 #endif /* TX_WITH_INTERRUPT */
139 }
140 /*---------------------------------------------------------------------------*/
141 
142 #ifdef __IAR_SYSTEMS_ICC__
143 #pragma vector=USCIAB0RX_VECTOR
144 __interrupt void
145 #else
146 interrupt(USCIAB0RX_VECTOR)
147 #endif
148 uart0_rx_interrupt(void)
149 {
150  uint8_t c;
151 
152  ENERGEST_ON(ENERGEST_TYPE_IRQ);
153  leds_toggle(LEDS_RED);
154  if(UCA0STAT & UCRXERR) {
155  c = UCA0RXBUF; /* Clear error flags by forcing a dummy read. */
156  } else {
157  c = UCA0RXBUF;
158  if(uart0_input_handler != NULL) {
159  if(uart0_input_handler(c)) {
160  LPM4_EXIT;
161  }
162  }
163  }
164  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
165 }
166 /*---------------------------------------------------------------------------*/
167 #if TX_WITH_INTERRUPT
168 #ifdef __IAR_SYSTEMS_ICC__
169 #pragma vector=USCIAB0TX_VECTOR
170 __interrupt void
171 #else
172 interrupt(USCIAB0TX_VECTOR)
173 #endif
174 uart0_tx_interrupt(void)
175 {
176  ENERGEST_ON(ENERGEST_TYPE_IRQ);
177  if((IFG2 & UCA0TXIFG)){
178 
179  if(ringbuf_elements(&txbuf) == 0) {
180  transmitting = 0;
181  } else {
182  UCA0TXBUF = ringbuf_get(&txbuf);
183  }
184  }
185 
186  /* In a stand-alone app won't work without this. Is the UG misleading? */
187  IFG2 &= ~UCA0TXIFG;
188 
189  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
190 }
191 #endif /* TX_WITH_INTERRUPT */
192 /*---------------------------------------------------------------------------*/