Contiki-NG
uart0.c
1 /*
2  * Copyright (c) 2011, 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  * Yet another machine dependent MSP430X UART0 code.
32  * IF2, etc. can not be used here... need to abstract to some macros
33  * later.
34  */
35 
36 #include "contiki.h"
37 #include <stdlib.h>
38 #include "dev/uart0.h"
39 #include "dev/watchdog.h"
40 #include "isr_compat.h"
41 
42 static int (*uart0_input_handler)(unsigned char c);
43 
44 static volatile uint8_t transmitting;
45 
46 /*---------------------------------------------------------------------------*/
47 uint8_t
48 uart0_active(void)
49 {
50  return (UCA0STAT & UCBUSY) | transmitting;
51 }
52 /*---------------------------------------------------------------------------*/
53 void
54 uart0_set_input(int (*input)(unsigned char c))
55 {
56  uart0_input_handler = input;
57 }
58 /*---------------------------------------------------------------------------*/
59 void
60 uart0_writeb(unsigned char c)
61 {
63  /* Loop until the transmission buffer is available. */
64  while((UCA0STAT & UCBUSY));
65 
66  /* Transmit the data. */
67  UCA0TXBUF = c;
68 }
69 /*---------------------------------------------------------------------------*/
70 /**
71  * Initalize the RS232 port.
72  *
73  */
74 void
75 uart0_init(unsigned long ubr)
76 {
77  /* RS232 */
78  UCA0CTL1 |= UCSWRST; /* Hold peripheral in reset state */
79  UCA0CTL1 |= UCSSEL_2; /* CLK = SMCLK */
80 
81  ubr = (MSP430_CPU_SPEED / ubr);
82  UCA0BR0 = ubr & 0xff;
83  UCA0BR1 = (ubr >> 8) & 0xff;
84  UCA0MCTL = UCBRS_3; /* Modulation UCBRSx = 3 */
85  P3DIR &= ~0x20; /* P3.5 = USCI_A0 RXD as input */
86  P3DIR |= 0x10; /* P3.4 = USCI_A0 TXD as output */
87  P3SEL |= 0x30; /* P3.4,5 = USCI_A0 TXD/RXD */
88 
89  /*UCA0CTL1 &= ~UCSWRST;*/ /* Initialize USCI state machine */
90 
91  transmitting = 0;
92 
93  /* XXX Clear pending interrupts before enable */
94  UCA0IE &= ~UCRXIFG;
95  UCA0IE &= ~UCTXIFG;
96 
97  UCA0CTL1 &= ~UCSWRST; /* Initialize USCI state machine **before** enabling interrupts */
98  UCA0IE |= UCRXIE; /* Enable UCA0 RX interrupt */
99 }
100 /*---------------------------------------------------------------------------*/
101 ISR(USCI_A0, uart0_rx_interrupt)
102 {
103  uint8_t c;
104 
105  if(UCA0IV == 2) {
106  if(UCA0STAT & UCRXERR) {
107  c = UCA0RXBUF; /* Clear error flags by forcing a dummy read. */
108  } else {
109  c = UCA0RXBUF;
110  if(uart0_input_handler != NULL) {
111  if(uart0_input_handler(c)) {
112  LPM4_EXIT;
113  }
114  }
115  }
116  }
117 }
118 /*---------------------------------------------------------------------------*/
static uint8_t transmitting(void)
Check the RF&#39;s TX status.
Definition: ieee-mode.c:285
void uart0_init(unsigned long ubr)
Initialize the RS232 port.
Definition: uart0.c:100
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1847