Contiki 2.6

uart1x.c

00001 /*
00002  * Copyright (c) 2010, 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  * @(#)$Id: uart1x.c,v 1.1 2010/08/24 16:23:20 joxe Exp $
00030  */
00031 
00032 /*
00033  * Yet another machine dependent MSP430X UART1 code.
00034  * IF2, etc. can not be used here... need to abstract to some macros
00035  * later.
00036  */
00037 
00038 #include "contiki.h"
00039 #include <stdlib.h>
00040 
00041 #include "sys/energest.h"
00042 #include "dev/uart1.h"
00043 #include "dev/watchdog.h"
00044 #include "lib/ringbuf.h"
00045 #include "dev/leds.h"
00046 #include "isr_compat.h"
00047 
00048 static int (*uart1_input_handler)(unsigned char c);
00049 
00050 static volatile uint8_t transmitting;
00051 
00052 /*---------------------------------------------------------------------------*/
00053 uint8_t
00054 uart1_active(void)
00055 {
00056   return (UCA1STAT & UCBUSY) | transmitting;
00057 }
00058 /*---------------------------------------------------------------------------*/
00059 void
00060 uart1_set_input(int (*input)(unsigned char c))
00061 {
00062   uart1_input_handler = input;
00063 }
00064 /*---------------------------------------------------------------------------*/
00065 void
00066 uart1_writeb(unsigned char c)
00067 {
00068   watchdog_periodic();
00069   /* Loop until the transmission buffer is available. */
00070   while((UCA1STAT & UCBUSY));
00071 
00072   /* Transmit the data. */
00073   UCA1TXBUF = c;
00074 }
00075 /*---------------------------------------------------------------------------*/
00076 #if ! WITH_UIP /* If WITH_UIP is defined, putchar() is defined by the SLIP driver */
00077 #endif /* ! WITH_UIP */
00078 /*---------------------------------------------------------------------------*/
00079 /**
00080  * Initalize the RS232 port.
00081  *
00082  */
00083 void
00084 uart1_init(unsigned long ubr)
00085 {
00086   /* RS232 */
00087   UCA1CTL1 |= UCSWRST;            /* Hold peripheral in reset state */
00088   UCA1CTL1 |= UCSSEL_2;           /* CLK = SMCLK */
00089 
00090   /* UCA1BR0 = 0x45;                 /\* 8MHz/115200 = 69 = 0x45 *\/ */
00091   UCA1BR0 = ubr & 0xff; //0x45; /* tested... */
00092   /* UCA1BR0 = 9; */
00093   UCA1BR1 = ubr >> 8;
00094   UCA1MCTL = UCBRS_3;             /* Modulation UCBRSx = 3 */
00095   P5DIR &= ~0x80;                 /* P5.7 = USCI_A1 RXD as input */
00096   P5DIR |= 0x40;                  /* P5.6 = USCI_A1 TXD as output */
00097   P5SEL |= 0xc0;                  /* P5.6,7 = USCI_A1 TXD/RXD */
00098 
00099   /*UCA1CTL1 &= ~UCSWRST;*/       /* Initialize USCI state machine */
00100 
00101   transmitting = 0;
00102 
00103   /* XXX Clear pending interrupts before enable */
00104   UCA1IE &= ~UCRXIFG;
00105   UCA1IE &= ~UCTXIFG;
00106 
00107   UCA1CTL1 &= ~UCSWRST;                   /* Initialize USCI state machine **before** enabling interrupts */
00108   UCA1IE |= UCRXIE;                        /* Enable UCA1 RX interrupt */
00109 }
00110 /*---------------------------------------------------------------------------*/
00111 ISR(USCI_A1, uart1_rx_interrupt)
00112 {
00113   uint8_t c;
00114 
00115   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00116   /*leds_toggle(LEDS_ALL);*/
00117   if(UCA1IV == 2) {
00118     if(UCA1STAT & UCRXERR) {
00119       c = UCA1RXBUF;   /* Clear error flags by forcing a dummy read. */
00120     } else {
00121       c = UCA1RXBUF;
00122       if(uart1_input_handler != NULL) {
00123         if(uart1_input_handler(c)) {
00124           LPM4_EXIT;
00125         }
00126       }
00127     }
00128   }
00129   ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00130 }
00131 /*---------------------------------------------------------------------------*/