Contiki-Inga 3.x
uart1.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/uart1.h"
40 #include "dev/watchdog.h"
41 #include "isr_compat.h"
42 
43 static int (*uart1_input_handler)(unsigned char c);
44 
45 static volatile uint8_t transmitting;
46 
47 /*---------------------------------------------------------------------------*/
48 uint8_t
49 uart1_active(void)
50 {
51  return (UCA1STAT & UCBUSY) | transmitting;
52 }
53 /*---------------------------------------------------------------------------*/
54 void
55 uart1_set_input(int (*input)(unsigned char c))
56 {
57  uart1_input_handler = input;
58 }
59 /*---------------------------------------------------------------------------*/
60 void
61 uart1_writeb(unsigned char c)
62 {
64  /* Loop until the transmission buffer is available. */
65  while((UCA1STAT & UCBUSY));
66 
67  /* Transmit the data. */
68  UCA1TXBUF = c;
69 }
70 /*---------------------------------------------------------------------------*/
71 /**
72  * Initalize the RS232 port.
73  *
74  */
75 void
76 uart1_init(unsigned long ubr)
77 {
78  /* RS232 */
79  UCA1CTL1 |= UCSWRST; /* Hold peripheral in reset state */
80  UCA1CTL1 |= UCSSEL_2; /* CLK = SMCLK */
81 
82  ubr = (MSP430_CPU_SPEED / ubr);
83  UCA1BR0 = ubr & 0xff;
84  UCA1BR1 = (ubr >> 8) & 0xff;
85  /* UCA1MCTL |= UCBRS_2 + UCBRF_0; // Modulation UCBRFx=0 */
86  UCA1MCTL = UCBRS_3; /* Modulation UCBRSx = 3 */
87 
88  P4DIR |= BIT5;
89  P4OUT |= BIT5 ;
90  P5SEL |= BIT6|BIT7; // P5.6,7 = USCI_A1 TXD/RXD
91 
92  P4SEL |= BIT7;
93  P4DIR |= BIT7;
94 
95  /*UCA1CTL1 &= ~UCSWRST;*/ /* Initialize USCI state machine */
96 
97  transmitting = 0;
98 
99  /* XXX Clear pending interrupts before enable */
100  UCA1IE &= ~UCRXIFG;
101  UCA1IE &= ~UCTXIFG;
102 
103  UCA1CTL1 &= ~UCSWRST; /* Initialize USCI state machine **before** enabling interrupts */
104  UCA1IE |= UCRXIE; /* Enable UCA1 RX interrupt */
105 }
106 /*---------------------------------------------------------------------------*/
107 ISR(USCI_A1, uart1_rx_interrupt)
108 {
109  uint8_t c;
110 
111  ENERGEST_ON(ENERGEST_TYPE_IRQ);
112  if (UCA1IV == 2) {
113  if(UCA1STAT & UCRXERR) {
114  c = UCA1RXBUF; /* Clear error flags by forcing a dummy read. */
115  } else {
116  c = UCA1RXBUF;
117  if(uart1_input_handler != NULL) {
118  if(uart1_input_handler(c)) {
119  LPM4_EXIT;
120  }
121  }
122  }
123  }
124  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
125 }
126 /*---------------------------------------------------------------------------*/