Contiki 3.x
uart0.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  */
30 
31 /*
32  * Machine dependent MSP430X UART0 code.
33  */
34 
35 #include <stdlib.h>
36 
37 #include "contiki.h"
38 #include "sys/energest.h"
39 #include "dev/uart0.h"
40 #include "dev/watchdog.h"
41 #include "lib/ringbuf.h"
42 #include "isr_compat.h"
43 
44 static int (*uart0_input_handler)(unsigned char c);
45 
46 static volatile uint8_t transmitting;
47 
48 #ifdef UART0_CONF_TX_WITH_INTERRUPT
49 #define TX_WITH_INTERRUPT UART0_CONF_TX_WITH_INTERRUPT
50 #else /* UART0_CONF_TX_WITH_INTERRUPT */
51 #define TX_WITH_INTERRUPT 1
52 #endif /* UART0_CONF_TX_WITH_INTERRUPT */
53 
54 #ifdef UART0_CONF_RX_WITH_DMA
55 #define RX_WITH_DMA UART0_CONF_RX_WITH_DMA
56 #else /* UART0_CONF_RX_WITH_DMA */
57 #define RX_WITH_DMA 1
58 #endif /* UART0_CONF_RX_WITH_DMA */
59 
60 #if TX_WITH_INTERRUPT
61 #define TXBUFSIZE 64
62 
63 static struct ringbuf txbuf;
64 static uint8_t txbuf_data[TXBUFSIZE];
65 #endif /* TX_WITH_INTERRUPT */
66 
67 #if RX_WITH_DMA
68 #define RXBUFSIZE 128
69 
70 static uint8_t rxbuf[RXBUFSIZE];
71 static uint16_t last_size;
72 static struct ctimer rxdma_timer;
73 
74 static void
75 handle_rxdma_timer(void *ptr)
76 {
77  uint16_t size;
78  size = DMA0SZ; /* Note: loop requires that size is less or eq to RXBUFSIZE */
79  while(last_size != size) {
80  uart0_input_handler((unsigned char)rxbuf[RXBUFSIZE - last_size]);
81  last_size--;
82  if(last_size == 0) {
83  last_size = RXBUFSIZE;
84  }
85  }
86 
87  ctimer_reset(&rxdma_timer);
88 }
89 #endif /* RX_WITH_DMA */
90 
91 /*---------------------------------------------------------------------------*/
92 uint8_t
93 uart0_active(void)
94 {
95  return (UCA0STAT & UCBUSY) | transmitting;
96 }
97 /*---------------------------------------------------------------------------*/
98 void
99 uart0_set_input(int (*input)(unsigned char c))
100 {
101 #if RX_WITH_DMA /* This needs to be called after ctimer process is started */
102  ctimer_set(&rxdma_timer, CLOCK_SECOND / 64, handle_rxdma_timer, NULL);
103 #endif
104  uart0_input_handler = input;
105 }
106 /*---------------------------------------------------------------------------*/
107 void
108 uart0_writeb(unsigned char c)
109 {
111 #if TX_WITH_INTERRUPT
112  /* Put the outgoing byte on the transmission buffer. If the buffer
113  is full, we just keep on trying to put the byte into the buffer
114  until it is possible to put it there. */
115  while(ringbuf_put(&txbuf, c) == 0);
116 
117  /* If there is no transmission going, we need to start it by putting
118  the first byte into the UART. */
119  if(transmitting == 0) {
120  transmitting = 1;
121  UCA0TXBUF = ringbuf_get(&txbuf);
122  }
123 
124 #else /* TX_WITH_INTERRUPT */
125 
126  /* Loop until the transmission buffer is available. */
127  /*Enric while((IFG2 & UCA0TXIFG) == 0); */
128  while((UCA0STAT & UCBUSY));
129 
130  /* Transmit the data. */
131  UCA0TXBUF = c;
132 #endif /* TX_WITH_INTERRUPT */
133 }
134 /*---------------------------------------------------------------------------*/
135 #if !NETSTACK_CONF_WITH_IPV4 /* If NETSTACK_CONF_WITH_IPV4 is defined, putchar() is defined by the SLIP driver */
136 #endif /* ! NETSTACK_CONF_WITH_IPV4 */
137 /*---------------------------------------------------------------------------*/
138 /**
139  * Initalize the RS232 port.
140  *
141  */
142 void
143 uart0_init(unsigned long ubr)
144 {
145  /* RS232 */
146  UCA0CTL1 |= UCSWRST; /* Hold peripheral in reset state */
147  UCA0CTL1 |= UCSSEL_2; /* CLK = SMCLK */
148 
149  UCA0BR0 = ubr; /* 8MHz/115200 = 69 = 0x45 */
150  UCA0BR1 = 0x00;
151  UCA0MCTL = UCBRS_3; /* Modulation UCBRSx = 3 */
152 
153  P3DIR &= ~0x20; /* P3.5 = USCI_A0 RXD as input */
154  P3DIR |= 0x10; /* P3.4 = USCI_A0 TXD as output */
155  P3SEL |= 0x30; /* P3.4,5 = USCI_A0 TXD/RXD */
156  /*UCA0CTL1 &= ~UCSWRST;*/ /* Initialize USCI state machine */
157 
158  transmitting = 0;
159 
160  /* XXX Clear pending interrupts before enable */
161  IFG2 &= ~UCA0RXIFG;
162  IFG2 &= ~UCA0TXIFG;
163  UCA0CTL1 &= ~UCSWRST; /* Initialize USCI state machine **before** enabling interrupts */
164  IE2 |= UCA0RXIE; /* Enable UCA0 RX interrupt */
165  /* Enable USCI_A0 TX interrupts (if TX_WITH_INTERRUPT enabled) */
166 #if TX_WITH_INTERRUPT
167  ringbuf_init(&txbuf, txbuf_data, sizeof(txbuf_data));
168  IE2 |= UCA0TXIE; /* Enable UCA0 TX interrupt */
169 #endif /* TX_WITH_INTERRUPT */
170 
171 #if RX_WITH_DMA
172  IE2 &= ~UCA0RXIE; /* disable USART0 RX interrupt */
173  /* UART0_RX trigger */
174  DMACTL0 = DMA0TSEL_3;
175 
176  /* source address = UCA0RXBUF */
177  DMA0SA = (unsigned int)&UCA0RXBUF;
178  DMA0DA = (unsigned int)&rxbuf;
179  DMA0SZ = RXBUFSIZE;
180  last_size = RXBUFSIZE;
181  DMA0CTL = DMADT_4 + DMASBDB + DMADSTINCR_3 + DMAEN + DMAREQ;
182 
183  msp430_add_lpm_req(MSP430_REQUIRE_LPM1);
184 #endif /* RX_WITH_DMA */
185 }
186 /*---------------------------------------------------------------------------*/
187 #if !RX_WITH_DMA
188 ISR(USCIAB0RX, uart0_rx_interrupt)
189 {
190  uint8_t c;
191 
192  ENERGEST_ON(ENERGEST_TYPE_IRQ);
193  if(UCA0STAT & UCRXERR) {
194  c = UCA0RXBUF; /* Clear error flags by forcing a dummy read. */
195  } else {
196  c = UCA0RXBUF;
197  if(uart0_input_handler != NULL) {
198  if(uart0_input_handler(c)) {
199  LPM4_EXIT;
200  }
201  }
202  }
203  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
204 }
205 #endif /* !RX_WITH_DMA */
206 /*---------------------------------------------------------------------------*/
207 #if TX_WITH_INTERRUPT
208 ISR(USCIAB0TX, uart0_tx_interrupt)
209 {
210  ENERGEST_ON(ENERGEST_TYPE_IRQ);
211  if((IFG2 & UCA0TXIFG)) {
212 
213  if(ringbuf_elements(&txbuf) == 0) {
214  transmitting = 0;
215  } else {
216  UCA0TXBUF = ringbuf_get(&txbuf);
217  }
218  }
219 
220  /* In a stand-alone app won't work without this. Is the UG misleading? */
221  IFG2 &= ~UCA0TXIFG;
222 
223  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
224 }
225 #endif /* TX_WITH_INTERRUPT */
226 /*---------------------------------------------------------------------------*/
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
Definition: ringbuf.c:80
void uart0_init(unsigned long ubr)
Initialize the RS232 port.
Definition: uart0.c:104
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.
Definition: ringbuf.c:53
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1503
Header file for the energy estimation mechanism
int ringbuf_elements(struct ringbuf *r)
Get the number of elements currently in the ring buffer.
Definition: ringbuf.c:119
#define NULL
The null pointer.
static uint8_t transmitting(void)
Check the RF's TX status.
Definition: ieee-mode.c:267
void ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
Initialize a ring buffer.
Definition: ringbuf.c:44
Structure that holds the state of a ring buffer.
Definition: ringbuf.h:68
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void uart0_set_input(int(*input)(unsigned char c))
Definition: uart0.c:84
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition: ctimer.c:125
tcirc_buf rxbuf
The RX circular buffer, for storing characters from serial port.
Definition: uart.c:56
Header file for the ring buffer library