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