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 
55 #if TX_WITH_INTERRUPT
56 #define TXBUFSIZE 64
57 
58 static struct ringbuf txbuf;
59 static uint8_t txbuf_data[TXBUFSIZE];
60 #endif /* TX_WITH_INTERRUPT */
61 
62 /*---------------------------------------------------------------------------*/
63 uint8_t
64 uart0_active(void)
65 {
66  return (UCA0STAT & UCBUSY) | transmitting;
67 }
68 /*---------------------------------------------------------------------------*/
69 void
70 uart0_set_input(int (*input)(unsigned char c))
71 {
72  uart0_input_handler = input;
73 }
74 /*---------------------------------------------------------------------------*/
75 void
76 uart0_writeb(unsigned char c)
77 {
79 #if TX_WITH_INTERRUPT
80  /* Put the outgoing byte on the transmission buffer. If the buffer
81  is full, we just keep on trying to put the byte into the buffer
82  until it is possible to put it there. */
83  while(ringbuf_put(&txbuf, c) == 0);
84 
85  /* If there is no transmission going, we need to start it by putting
86  the first byte into the UART. */
87  if(transmitting == 0) {
88  transmitting = 1;
89  UCA0TXBUF = ringbuf_get(&txbuf);
90  }
91 
92 #else /* TX_WITH_INTERRUPT */
93 
94  /* Loop until the transmission buffer is available. */
95  /*Enric while((IFG2 & UCA0TXIFG) == 0); */
96  while((UCA0STAT & UCBUSY));
97 
98  /* Transmit the data. */
99  UCA0TXBUF = c;
100 #endif /* TX_WITH_INTERRUPT */
101 }
102 /*---------------------------------------------------------------------------*/
103 #if ! WITH_UIP /* If WITH_UIP is defined, putchar() is defined by the SLIP driver */
104 #endif /* ! WITH_UIP */
105 /*---------------------------------------------------------------------------*/
106 /**
107  * Initalize the RS232 port.
108  *
109  */
110 void
111 uart0_init(unsigned long ubr)
112 {
113  /* RS232 */
114  UCA0CTL1 |= UCSWRST; /* Hold peripheral in reset state */
115  UCA0CTL1 |= UCSSEL_2; /* CLK = SMCLK */
116 
117  UCA0BR0 = ubr; /* 8MHz/115200 = 69 = 0x45 */
118  UCA0BR1 = 0x00;
119  UCA0MCTL = UCBRS_3; /* Modulation UCBRSx = 3 */
120 
121  P3DIR &= ~0x20; /* P3.5 = USCI_A0 RXD as input */
122  P3DIR |= 0x10; /* P3.4 = USCI_A0 TXD as output */
123  P3SEL |= 0x30; /* P3.4,5 = USCI_A0 TXD/RXD */
124  /*UCA0CTL1 &= ~UCSWRST;*/ /* Initialize USCI state machine */
125 
126  transmitting = 0;
127 
128  /* XXX Clear pending interrupts before enable */
129  IFG2 &= ~UCA0RXIFG;
130  IFG2 &= ~UCA0TXIFG;
131  UCA0CTL1 &= ~UCSWRST; /* Initialize USCI state machine **before** enabling interrupts */
132  IE2 |= UCA0RXIE; /* Enable UCA0 RX interrupt */
133  /* Enable USCI_A0 TX interrupts (if TX_WITH_INTERRUPT enabled) */
134 #if TX_WITH_INTERRUPT
135  ringbuf_init(&txbuf, txbuf_data, sizeof(txbuf_data));
136  IE2 |= UCA0TXIE; /* Enable UCA0 TX interrupt */
137 #endif /* TX_WITH_INTERRUPT */
138 }
139 /*---------------------------------------------------------------------------*/
140 ISR(USCIAB0RX, uart0_rx_interrupt)
141 {
142  uint8_t c;
143 
144  ENERGEST_ON(ENERGEST_TYPE_IRQ);
145  if(UCA0STAT & UCRXERR) {
146  c = UCA0RXBUF; /* Clear error flags by forcing a dummy read. */
147  } else {
148  c = UCA0RXBUF;
149  if(uart0_input_handler != NULL) {
150  if(uart0_input_handler(c)) {
151  LPM4_EXIT;
152  }
153  }
154  }
155  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
156 }
157 /*---------------------------------------------------------------------------*/
158 #if TX_WITH_INTERRUPT
159 ISR(USCIAB0TX, uart0_tx_interrupt)
160 {
161  ENERGEST_ON(ENERGEST_TYPE_IRQ);
162  if((IFG2 & UCA0TXIFG)){
163 
164  if(ringbuf_elements(&txbuf) == 0) {
165  transmitting = 0;
166  } else {
167  UCA0TXBUF = ringbuf_get(&txbuf);
168  }
169  }
170 
171  /* In a stand-alone app won't work without this. Is the UG misleading? */
172  IFG2 &= ~UCA0TXIFG;
173 
174  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
175 }
176 #endif /* TX_WITH_INTERRUPT */
177 /*---------------------------------------------------------------------------*/
Header file for the ring buffer library
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:111
Structure that holds the state of a ring buffer.
Definition: ringbuf.h:68
#define NULL
The null pointer.
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
Definition: ringbuf.c:72
void ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
Initialize a ring buffer.
Definition: ringbuf.c:43
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:102
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.
Definition: ringbuf.c:52