Contiki 2.5
uart1.c
1 /*
2  * Copyright (c) 2006, 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: uart1.c,v 1.24 2011/01/19 20:44:20 joxe Exp $
30  */
31 
32 /*
33  * Machine dependent MSP430 UART1 code.
34  */
35 
36 #include <stdlib.h>
37 #include "contiki.h"
38 #include "sys/energest.h"
39 #include "dev/uart1.h"
40 #include "dev/watchdog.h"
41 #include "sys/ctimer.h"
42 #include "lib/ringbuf.h"
43 
44 static int (*uart1_input_handler)(unsigned char c);
45 static volatile uint8_t rx_in_progress;
46 
47 static volatile uint8_t transmitting;
48 
49 #ifdef UART1_CONF_TX_WITH_INTERRUPT
50 #define TX_WITH_INTERRUPT UART1_CONF_TX_WITH_INTERRUPT
51 #else /* UART1_CONF_TX_WITH_INTERRUPT */
52 #define TX_WITH_INTERRUPT 0
53 #endif /* UART1_CONF_TX_WITH_INTERRUPT */
54 
55 #ifdef UART1_CONF_RX_WITH_DMA
56 #define RX_WITH_DMA UART1_CONF_RX_WITH_DMA
57 #else /* UART1_CONF_RX_WITH_DMA */
58 #define RX_WITH_DMA 0
59 #endif /* UART1_CONF_RX_WITH_DMA */
60 
61 #if RX_WITH_DMA
62 #warning RX_WITH_DMA ENABLED - WILL NOT WORK WITH MSPSIM / COOJA!
63 #endif /* RX_WITH_DMA */
64 
65 #if TX_WITH_INTERRUPT
66 #define TXBUFSIZE 128
67 
68 static struct ringbuf txbuf;
69 static uint8_t txbuf_data[TXBUFSIZE];
70 #endif /* TX_WITH_INTERRUPT */
71 
72 #if RX_WITH_DMA
73 #define RXBUFSIZE 128
74 
75 static uint8_t rxbuf[RXBUFSIZE];
76 static uint16_t last_size;
77 static struct ctimer rxdma_timer;
78 
79 static void
80 handle_rxdma_timer(void *ptr)
81 {
82  uint16_t size;
83  size = DMA0SZ; /* Note: loop requires that size is less or eq to RXBUFSIZE */
84  while(last_size != size) {
85 /* printf("read: %c [%d,%d]\n", (unsigned char)rxbuf[RXBUFSIZE - last_size], */
86 /* last_size, size); */
87  uart1_input_handler((unsigned char)rxbuf[RXBUFSIZE - last_size]);
88  last_size--;
89  if(last_size == 0) last_size = RXBUFSIZE;
90  }
91 
92  ctimer_reset(&rxdma_timer);
93 }
94 #endif /* RX_WITH_DMA */
95 
96 /*---------------------------------------------------------------------------*/
97 uint8_t
98 uart1_active(void)
99 {
100  return ((~ UTCTL1) & TXEPT) | rx_in_progress | transmitting;
101 }
102 /*---------------------------------------------------------------------------*/
103 void
104 uart1_set_input(int (*input)(unsigned char c))
105 {
106 #if RX_WITH_DMA /* This needs to be called after ctimer process is started */
107  ctimer_set(&rxdma_timer, CLOCK_SECOND/64, handle_rxdma_timer, NULL);
108 #endif
109  uart1_input_handler = input;
110 }
111 /*---------------------------------------------------------------------------*/
112 void
113 uart1_writeb(unsigned char c)
114 {
115  watchdog_periodic();
116 #if TX_WITH_INTERRUPT
117 
118  /* Put the outgoing byte on the transmission buffer. If the buffer
119  is full, we just keep on trying to put the byte into the buffer
120  until it is possible to put it there. */
121  while(ringbuf_put(&txbuf, c) == 0);
122 
123  /* If there is no transmission going, we need to start it by putting
124  the first byte into the UART. */
125  if(transmitting == 0) {
126  transmitting = 1;
127 
128  /* Loop until the transmission buffer is available. */
129  /*while((IFG2 & UTXIFG1) == 0);*/
130  TXBUF1 = ringbuf_get(&txbuf);
131  }
132 
133 #else /* TX_WITH_INTERRUPT */
134 
135  /* Loop until the transmission buffer is available. */
136  while((IFG2 & UTXIFG1) == 0);
137 
138  /* Transmit the data. */
139  TXBUF1 = c;
140 #endif /* TX_WITH_INTERRUPT */
141 }
142 /*---------------------------------------------------------------------------*/
143 /**
144  * Initalize the RS232 port.
145  *
146  */
147 void
148 uart1_init(unsigned long ubr)
149 {
150  /* RS232 */
151  P3DIR &= ~0x80; /* Select P37 for input (UART1RX) */
152  P3DIR |= 0x40; /* Select P36 for output (UART1TX) */
153  P3SEL |= 0xC0; /* Select P36,P37 for UART1{TX,RX} */
154 
155  UCTL1 = SWRST | CHAR; /* 8-bit character, UART mode */
156 
157 #if 0
158  U1RCTL &= ~URXEIE; /* even erroneous characters trigger interrupts */
159 #endif
160 
161  UTCTL1 = SSEL1; /* UCLK = MCLK */
162 
163  UBR01 = ubr;
164  UBR11 = ubr >> 8;
165  /*
166  * UMCTL1 values calculated using
167  * http://mspgcc.sourceforge.net/baudrate.html
168  */
169  switch(ubr) {
170 
171 #if F_CPU == 3900000ul
172 
173  case UART1_BAUD2UBR(115200ul):
174  UMCTL1 = 0xF7;
175  break;
176  case UART1_BAUD2UBR(57600ul):
177  UMCTL1 = 0xED;
178  break;
179  case UART1_BAUD2UBR(38400ul):
180  UMCTL1 = 0xD6;
181  break;
182  case UART1_BAUD2UBR(19200ul):
183  UMCTL1 = 0x08;
184  break;
185  case UART1_BAUD2UBR(9600ul):
186  UMCTL1 = 0x22;
187  break;
188 
189 #elif F_CPU == 2457600ul
190 
191  case UART1_BAUD2UBR(115200ul):
192  UMCTL1 = 0x4A;
193  break;
194  case UART1_BAUD2UBR(57600ul):
195  UMCTL1 = 0x5B;
196  break;
197  default:
198  /* 9600, 19200, 38400 don't require any correction */
199  UMCTL1 = 0x00;
200 
201 #else
202 
203 #error Unsupported CPU speed in uart1.c
204 
205 #endif
206  }
207 
208  ME2 &= ~USPIE1; /* USART1 SPI module disable */
209  ME2 |= (UTXE1 | URXE1); /* Enable USART1 TXD/RXD */
210 
211  UCTL1 &= ~SWRST;
212 
213  /* XXX Clear pending interrupts before enable!!! */
214  IFG2 &= ~URXIFG1;
215  U1TCTL |= URXSE;
216 
217  rx_in_progress = 0;
218 
219  transmitting = 0;
220 
221  IE2 |= URXIE1; /* Enable USART1 RX interrupt */
222 #if TX_WITH_INTERRUPT
223  ringbuf_init(&txbuf, txbuf_data, sizeof(txbuf_data));
224  IE2 |= UTXIE1; /* Enable USART1 TX interrupt */
225 #endif /* TX_WITH_INTERRUPT */
226 
227 #if RX_WITH_DMA
228  IE2 &= ~URXIE1; /* disable USART1 RX interrupt */
229  /* UART1_RX trigger */
230  DMACTL0 = DMA0TSEL_9;
231 
232  /* source address = RXBUF1 */
233  DMA0SA = (unsigned int) &RXBUF1;
234  DMA0DA = (unsigned int) &rxbuf;
235  DMA0SZ = RXBUFSIZE;
236  last_size = RXBUFSIZE;
237  DMA0CTL = DMADT_4 + DMASBDB + DMADSTINCR_3 + DMAEN + DMAREQ;// DMAIE;
238 
239  msp430_add_lpm_req(MSP430_REQUIRE_LPM1);
240 #endif /* RX_WITH_DMA */
241 
242 }
243 
244 
245 /*---------------------------------------------------------------------------*/
246 #if !RX_WITH_DMA
247 #ifdef __IAR_SYSTEMS_ICC__
248 #pragma vector=UART1RX_VECTOR
249 __interrupt void
250 #else
251 interrupt(UART1RX_VECTOR)
252 #endif
253 uart1_rx_interrupt(void)
254 {
255  uint8_t c;
256  ENERGEST_ON(ENERGEST_TYPE_IRQ);
257 
258  if(!(URXIFG1 & IFG2)) {
259  /* Edge detect if IFG not set? */
260  U1TCTL &= ~URXSE; /* Clear the URXS signal */
261  U1TCTL |= URXSE; /* Re-enable URXS - needed here?*/
262  rx_in_progress = 1;
263  LPM4_EXIT;
264  } else {
265  rx_in_progress = 0;
266  /* Check status register for receive errors. */
267  if(URCTL1 & RXERR) {
268  c = RXBUF1; /* Clear error flags by forcing a dummy read. */
269  } else {
270  c = RXBUF1;
271  if(uart1_input_handler != NULL) {
272  if(uart1_input_handler(c)) {
273  LPM4_EXIT;
274  }
275  }
276  }
277  }
278 
279  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
280 }
281 #endif /* !RX_WITH_DMA */
282 /*---------------------------------------------------------------------------*/
283 #if TX_WITH_INTERRUPT
284 interrupt(UART1TX_VECTOR)
285 uart1_tx_interrupt(void)
286 {
287  ENERGEST_ON(ENERGEST_TYPE_IRQ);
288 
289  if(ringbuf_elements(&txbuf) == 0) {
290  transmitting = 0;
291  } else {
292  TXBUF1 = ringbuf_get(&txbuf);
293  }
294 
295  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
296 }
297 #endif /* TX_WITH_INTERRUPT */
298 /*---------------------------------------------------------------------------*/