Contiki 3.x
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 "sys/energest.h"
39 #include "dev/uart0.h"
40 #include "dev/watchdog.h"
41 #include "isr_compat.h"
42 
43 static int (*uart0_input_handler)(unsigned char c);
44 
45 static volatile uint8_t transmitting;
46 
47 /*---------------------------------------------------------------------------*/
48 uint8_t
49 uart0_active(void)
50 {
51  return (UCA0STAT & UCBUSY) | transmitting;
52 }
53 /*---------------------------------------------------------------------------*/
54 void
55 uart0_set_input(int (*input)(unsigned char c))
56 {
57  uart0_input_handler = input;
58 }
59 /*---------------------------------------------------------------------------*/
60 void
61 uart0_writeb(unsigned char c)
62 {
64  /* Loop until the transmission buffer is available. */
65  while((UCA0STAT & UCBUSY));
66 
67  /* Transmit the data. */
68  UCA0TXBUF = c;
69 }
70 /*---------------------------------------------------------------------------*/
71 /**
72  * Initalize the RS232 port.
73  *
74  */
75 void
76 uart0_init(unsigned long ubr)
77 {
78  /* RS232 */
79  UCA0CTL1 |= UCSWRST; /* Hold peripheral in reset state */
80  UCA0CTL1 |= UCSSEL_2; /* CLK = SMCLK */
81 
82  ubr = (MSP430_CPU_SPEED / ubr);
83  UCA0BR0 = ubr & 0xff;
84  UCA0BR1 = (ubr >> 8) & 0xff;
85  UCA0MCTL = UCBRS_3; /* Modulation UCBRSx = 3 */
86  P3DIR &= ~0x20; /* P3.5 = USCI_A0 RXD as input */
87  P3DIR |= 0x10; /* P3.4 = USCI_A0 TXD as output */
88  P3SEL |= 0x30; /* P3.4,5 = USCI_A0 TXD/RXD */
89 
90  /*UCA0CTL1 &= ~UCSWRST;*/ /* Initialize USCI state machine */
91 
92  transmitting = 0;
93 
94  /* XXX Clear pending interrupts before enable */
95  UCA0IE &= ~UCRXIFG;
96  UCA0IE &= ~UCTXIFG;
97 
98  UCA0CTL1 &= ~UCSWRST; /* Initialize USCI state machine **before** enabling interrupts */
99  UCA0IE |= UCRXIE; /* Enable UCA0 RX interrupt */
100 }
101 /*---------------------------------------------------------------------------*/
102 ISR(USCI_A0, uart0_rx_interrupt)
103 {
104  uint8_t c;
105 
106  ENERGEST_ON(ENERGEST_TYPE_IRQ);
107  if(UCA0IV == 2) {
108  if(UCA0STAT & UCRXERR) {
109  c = UCA0RXBUF; /* Clear error flags by forcing a dummy read. */
110  } else {
111  c = UCA0RXBUF;
112  if(uart0_input_handler != NULL) {
113  if(uart0_input_handler(c)) {
114  LPM4_EXIT;
115  }
116  }
117  }
118  }
119  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
120 }
121 /*---------------------------------------------------------------------------*/
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:111
#define NULL
The null pointer.
Header file for the energy estimation mechanism
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64