Contiki 3.x
uart1x.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  * Yet another machine dependent MSP430X UART1 code.
33  * IF2, etc. can not be used here... need to abstract to some macros
34  * later.
35  */
36 
37 #include "contiki.h"
38 #include <stdlib.h>
39 
40 #include "sys/energest.h"
41 #include "dev/uart1.h"
42 #include "dev/watchdog.h"
43 #include "lib/ringbuf.h"
44 #include "dev/leds.h"
45 #include "isr_compat.h"
46 
47 static int (*uart1_input_handler)(unsigned char c);
48 
49 static volatile uint8_t transmitting;
50 
51 /*---------------------------------------------------------------------------*/
52 uint8_t
53 uart1_active(void)
54 {
55  return (UCA1STAT & UCBUSY) | transmitting;
56 }
57 /*---------------------------------------------------------------------------*/
58 void
59 uart1_set_input(int (*input)(unsigned char c))
60 {
61  uart1_input_handler = input;
62 }
63 /*---------------------------------------------------------------------------*/
64 void
65 uart1_writeb(unsigned char c)
66 {
68  /* Loop until the transmission buffer is available. */
69  while((UCA1STAT & UCBUSY));
70 
71  /* Transmit the data. */
72  UCA1TXBUF = c;
73 }
74 /*---------------------------------------------------------------------------*/
75 #if ! WITH_UIP /* If WITH_UIP is defined, putchar() is defined by the SLIP driver */
76 #endif /* ! WITH_UIP */
77 /*---------------------------------------------------------------------------*/
78 /**
79  * Initalize the RS232 port.
80  *
81  */
82 void
83 uart1_init(unsigned long ubr)
84 {
85  /* RS232 */
86  UCA1CTL1 |= UCSWRST; /* Hold peripheral in reset state */
87  UCA1CTL1 |= UCSSEL_2; /* CLK = SMCLK */
88 
89  /* UCA1BR0 = 0x45; /\* 8MHz/115200 = 69 = 0x45 *\/ */
90  UCA1BR0 = ubr & 0xff; //0x45; /* tested... */
91  /* UCA1BR0 = 9; */
92  UCA1BR1 = ubr >> 8;
93  UCA1MCTL = UCBRS_3; /* Modulation UCBRSx = 3 */
94  P5DIR &= ~0x80; /* P5.7 = USCI_A1 RXD as input */
95  P5DIR |= 0x40; /* P5.6 = USCI_A1 TXD as output */
96  P5SEL |= 0xc0; /* P5.6,7 = USCI_A1 TXD/RXD */
97 
98  /*UCA1CTL1 &= ~UCSWRST;*/ /* Initialize USCI state machine */
99 
100  transmitting = 0;
101 
102  /* XXX Clear pending interrupts before enable */
103  UCA1IE &= ~UCRXIFG;
104  UCA1IE &= ~UCTXIFG;
105 
106  UCA1CTL1 &= ~UCSWRST; /* Initialize USCI state machine **before** enabling interrupts */
107  UCA1IE |= UCRXIE; /* Enable UCA1 RX interrupt */
108 }
109 /*---------------------------------------------------------------------------*/
110 ISR(USCI_A1, uart1_rx_interrupt)
111 {
112  uint8_t c;
113 
114  ENERGEST_ON(ENERGEST_TYPE_IRQ);
115  /*leds_toggle(LEDS_ALL);*/
116  if(UCA1IV == 2) {
117  if(UCA1STAT & UCRXERR) {
118  c = UCA1RXBUF; /* Clear error flags by forcing a dummy read. */
119  } else {
120  c = UCA1RXBUF;
121  if(uart1_input_handler != NULL) {
122  if(uart1_input_handler(c)) {
123  LPM4_EXIT;
124  }
125  }
126  }
127  }
128  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
129 }
130 /*---------------------------------------------------------------------------*/
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart1.c:143
Header file for the ring buffer library
#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