Contiki 2.6

uart1.c

00001 /*
00002  * Copyright (c) 2011, Swedish Institute of Computer Science
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Yet another machine dependent MSP430X UART0 code.
00032  * IF2, etc. can not be used here... need to abstract to some macros
00033  * later.
00034  */
00035 
00036 #include "contiki.h"
00037 #include <stdlib.h>
00038 #include "sys/energest.h"
00039 #include "dev/uart1.h"
00040 #include "dev/watchdog.h"
00041 #include "isr_compat.h"
00042 
00043 static int (*uart1_input_handler)(unsigned char c);
00044 
00045 static volatile uint8_t transmitting;
00046 
00047 /*---------------------------------------------------------------------------*/
00048 uint8_t
00049 uart1_active(void)
00050 {
00051   return (UCA1STAT & UCBUSY) | transmitting;
00052 }
00053 /*---------------------------------------------------------------------------*/
00054 void
00055 uart1_set_input(int (*input)(unsigned char c))
00056 {
00057   uart1_input_handler = input;
00058 }
00059 /*---------------------------------------------------------------------------*/
00060 void
00061 uart1_writeb(unsigned char c)
00062 {
00063   watchdog_periodic();
00064   /* Loop until the transmission buffer is available. */
00065   while((UCA1STAT & UCBUSY));
00066 
00067   /* Transmit the data. */
00068   UCA1TXBUF = c;
00069 }
00070 /*---------------------------------------------------------------------------*/
00071 /**
00072  * Initalize the RS232 port.
00073  *
00074  */
00075 void
00076 uart1_init(unsigned long ubr)
00077 {
00078   /* RS232 */
00079   UCA1CTL1 |= UCSWRST;            /* Hold peripheral in reset state */
00080   UCA1CTL1 |= UCSSEL_2;           /* CLK = SMCLK */
00081 
00082   ubr = (MSP430_CPU_SPEED / ubr);
00083   UCA1BR0 = ubr & 0xff;
00084   UCA1BR1 = (ubr >> 8) & 0xff;
00085   /* UCA1MCTL |= UCBRS_2 + UCBRF_0;            // Modulation UCBRFx=0 */
00086   UCA1MCTL = UCBRS_3;             /* Modulation UCBRSx = 3 */
00087 
00088   P4DIR |= BIT5;
00089   P4OUT |= BIT5 ;
00090   P5SEL |= BIT6|BIT7;  // P5.6,7 = USCI_A1 TXD/RXD
00091 
00092   P4SEL |= BIT7;
00093   P4DIR |= BIT7;
00094 
00095   /*UCA1CTL1 &= ~UCSWRST;*/       /* Initialize USCI state machine */
00096 
00097   transmitting = 0;
00098 
00099   /* XXX Clear pending interrupts before enable */
00100   UCA1IE &= ~UCRXIFG;
00101   UCA1IE &= ~UCTXIFG;
00102 
00103   UCA1CTL1 &= ~UCSWRST;                   /* Initialize USCI state machine **before** enabling interrupts */
00104   UCA1IE |= UCRXIE;                        /* Enable UCA1 RX interrupt */
00105 }
00106 /*---------------------------------------------------------------------------*/
00107 ISR(USCI_A1, uart1_rx_interrupt)
00108 {
00109   uint8_t c;
00110 
00111   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00112   if (UCA1IV == 2) {
00113     if(UCA1STAT & UCRXERR) {
00114       c = UCA1RXBUF;   /* Clear error flags by forcing a dummy read. */
00115     } else {
00116       c = UCA1RXBUF;
00117       if(uart1_input_handler != NULL) {
00118         if(uart1_input_handler(c)) {
00119           LPM4_EXIT;
00120         }
00121       }
00122     }
00123   }
00124   ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00125 }
00126 /*---------------------------------------------------------------------------*/