Contiki 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 #ifdef UART1_CONF_RX_WITH_DMA
48 #define RX_WITH_DMA UART1_CONF_RX_WITH_DMA
49 #else /* UART1_CONF_RX_WITH_DMA */
50 #define RX_WITH_DMA 1
51 #endif /* UART1_CONF_RX_WITH_DMA */
52 
53 #if RX_WITH_DMA
54 #define RXBUFSIZE 128
55 
56 static uint8_t rxbuf[RXBUFSIZE];
57 static uint16_t last_size;
58 static struct ctimer rxdma_timer;
59 
60 static void
61 handle_rxdma_timer(void *ptr)
62 {
63  uint16_t size;
64  size = DMA0SZ; /* Note: loop requires that size is less or eq to RXBUFSIZE */
65  while(last_size != size) {
66  uart1_input_handler((unsigned char)rxbuf[RXBUFSIZE - last_size]);
67  last_size--;
68  if(last_size == 0) {
69  last_size = RXBUFSIZE;
70  }
71  }
72 
73  ctimer_reset(&rxdma_timer);
74 }
75 #endif /* RX_WITH_DMA */
76 
77 /*---------------------------------------------------------------------------*/
78 uint8_t
79 uart1_active(void)
80 {
81  return (UCA1STAT & UCBUSY) | transmitting;
82 }
83 /*---------------------------------------------------------------------------*/
84 void
85 uart1_set_input(int (*input)(unsigned char c))
86 {
87 #if RX_WITH_DMA /* This needs to be called after ctimer process is started */
88  ctimer_set(&rxdma_timer, CLOCK_SECOND / 64, handle_rxdma_timer, NULL);
89 #endif
90  uart1_input_handler = input;
91 }
92 /*---------------------------------------------------------------------------*/
93 void
94 uart1_writeb(unsigned char c)
95 {
97  /* Loop until the transmission buffer is available. */
98  while((UCA1STAT & UCBUSY));
99 
100  /* Transmit the data. */
101  UCA1TXBUF = c;
102 }
103 /*---------------------------------------------------------------------------*/
104 /**
105  * Initalize the RS232 port.
106  *
107  */
108 void
109 uart1_init(unsigned long ubr)
110 {
111  /* RS232 */
112  UCA1CTL1 |= UCSWRST; /* Hold peripheral in reset state */
113  UCA1CTL1 |= UCSSEL_2; /* CLK = SMCLK */
114 
115  ubr = (MSP430_CPU_SPEED / ubr);
116  UCA1BR0 = ubr & 0xff;
117  UCA1BR1 = (ubr >> 8) & 0xff;
118  /* UCA1MCTL |= UCBRS_2 + UCBRF_0; // Modulation UCBRFx=0 */
119  UCA1MCTL = UCBRS_3; /* Modulation UCBRSx = 3 */
120 
121  P4DIR |= BIT5;
122  P4OUT |= BIT5;
123  P5SEL |= BIT6 | BIT7; /* P5.6,7 = USCI_A1 TXD/RXD */
124 
125  P4SEL |= BIT7;
126  P4DIR |= BIT7;
127 
128  /*UCA1CTL1 &= ~UCSWRST;*/ /* Initialize USCI state machine */
129 
130  transmitting = 0;
131 
132  /* XXX Clear pending interrupts before enable */
133  UCA1IE &= ~UCRXIFG;
134  UCA1IE &= ~UCTXIFG;
135 
136  UCA1CTL1 &= ~UCSWRST; /* Initialize USCI state machine **before** enabling interrupts */
137  UCA1IE |= UCRXIE; /* Enable UCA1 RX interrupt */
138 
139 #if RX_WITH_DMA
140  UCA1IE &= ~UCRXIE; /* disable USART1 RX interrupt */
141  /* UART1_RX trigger */
142  DMACTL0 = DMA0TSEL_20;
143 
144  /* source address = RXBUF1 */
145  DMA0SA = (unsigned int)&UCA1RXBUF;
146  DMA0DA = (unsigned int)&rxbuf;
147  DMA0SZ = RXBUFSIZE;
148  last_size = RXBUFSIZE;
149  DMA0CTL = DMADT_4 + DMASBDB + DMADSTINCR_3 + DMAEN + DMAREQ;
150 
151  msp430_add_lpm_req(MSP430_REQUIRE_LPM1);
152 #endif /* RX_WITH_DMA */
153 }
154 /*---------------------------------------------------------------------------*/
155 #if !RX_WITH_DMA
156 ISR(USCI_A1, uart1_rx_interrupt)
157 {
158  uint8_t c;
159 
160  ENERGEST_ON(ENERGEST_TYPE_IRQ);
161  if(UCA1IV == 2) {
162  if(UCA1STAT & UCRXERR) {
163  c = UCA1RXBUF; /* Clear error flags by forcing a dummy read. */
164  } else {
165  c = UCA1RXBUF;
166  if(uart1_input_handler != NULL) {
167  if(uart1_input_handler(c)) {
168  LPM4_EXIT;
169  }
170  }
171  }
172  }
173  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
174 }
175 #endif /* !RX_WITH_DMA */
176 /*---------------------------------------------------------------------------*/
void uart1_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart1.c:131
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1503
Header file for the energy estimation mechanism
#define NULL
The null pointer.
static uint8_t transmitting(void)
Check the RF's TX status.
Definition: ieee-mode.c:267
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition: ctimer.c:125
tcirc_buf rxbuf
The RX circular buffer, for storing characters from serial port.
Definition: uart.c:56