Contiki 2.6

uart1.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  * Machine dependent MSP430X UART1 code.
00034  */
00035 #include "contiki.h"
00036 #include <stdlib.h>
00037 #include "sys/energest.h"
00038 #include "dev/uart1.h"
00039 #include "dev/watchdog.h"
00040 #include "lib/ringbuf.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 #ifdef UART1_CONF_TX_WITH_INTERRUPT
00048 #define TX_WITH_INTERRUPT UART1_CONF_TX_WITH_INTERRUPT
00049 #else /* UART1_CONF_TX_WITH_INTERRUPT */
00050 #define TX_WITH_INTERRUPT 1
00051 #endif /* UART1_CONF_TX_WITH_INTERRUPT */
00052 
00053 #if TX_WITH_INTERRUPT
00054 #define TXBUFSIZE 64
00055 
00056 static struct ringbuf txbuf;
00057 static uint8_t txbuf_data[TXBUFSIZE];
00058 #endif /* TX_WITH_INTERRUPT */
00059 
00060 /*---------------------------------------------------------------------------*/
00061 uint8_t
00062 uart1_active(void)
00063 {
00064   return (UCA0STAT & UCBUSY) | transmitting;
00065 }
00066 /*---------------------------------------------------------------------------*/
00067 void
00068 uart1_set_input(int (*input)(unsigned char c))
00069 {
00070   uart1_input_handler = input;
00071 }
00072 /*---------------------------------------------------------------------------*/
00073 void
00074 uart1_writeb(unsigned char c)
00075 {
00076   /* watchdog_periodic(); */
00077 #if TX_WITH_INTERRUPT
00078 
00079   /* Put the outgoing byte on the transmission buffer. If the buffer
00080      is full, we just keep on trying to put the byte into the buffer
00081      until it is possible to put it there. */
00082   while(ringbuf_put(&txbuf, c) == 0);
00083 
00084   /* If there is no transmission going, we need to start it by putting
00085      the first byte into the UART. */
00086   if(transmitting == 0) {
00087     transmitting = 1;
00088     UCA0TXBUF = ringbuf_get(&txbuf);
00089   }
00090 
00091 #else /* TX_WITH_INTERRUPT */
00092 
00093   /* Loop until the transmission buffer is available. */
00094   while(!(IFG2 & UCA0TXIFG));
00095 
00096   /* Transmit the data. */
00097   UCA0TXBUF = c;
00098 #endif /* TX_WITH_INTERRUPT */
00099 }
00100 /*---------------------------------------------------------------------------*/
00101 #if ! WITH_UIP /* If WITH_UIP is defined, putchar() is defined by the SLIP driver */
00102 #endif /* ! WITH_UIP */
00103 /*---------------------------------------------------------------------------*/
00104 /**
00105  * Initalize the RS232 port.
00106  *
00107  */
00108 void
00109 uart1_init(unsigned long ubr)
00110 {
00111   /* RS232 */
00112   P3SEL |= 0x30;                            /* P3.4,5 = USCI_A0 TXD/RXD */
00113   UCA0CTL1 |= UCSSEL_2;                     /* CLK = SMCLK */
00114   UCA0BR0 = 0x45;                           /* 8MHz/115200 = 69 = 0x45 */
00115   UCA0BR1 = 0x00;
00116   UCA0MCTL = UCBRS2;                        /* Modulation UCBRSx = 4 */
00117   UCA0CTL1 &= ~UCSWRST;                     /* Initialize USCI state machine */
00118 
00119   transmitting = 0;
00120  
00121 }
00122 /*---------------------------------------------------------------------------*/
00123 ISR(USCIAB1RX, uart1_rx_interrupt)
00124 {
00125   uint8_t c;
00126   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00127 
00128   /* Check status register for receive errors. */
00129   if(UCA0STAT & UCRXERR) {
00130     c = UCA0RXBUF;   /* Clear error flags by forcing a dummy read. */
00131   } else {
00132     c = UCA0RXBUF;
00133     if(uart1_input_handler != NULL) {
00134       if(uart1_input_handler(c)) {
00135         LPM4_EXIT;
00136       }
00137     }
00138   }
00139   ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00140 }
00141 /*---------------------------------------------------------------------------*/
00142 #if TX_WITH_INTERRUPT
00143 ISR(USCIAB1TX, uart1_tx_interrupt)
00144 {
00145   ENERGEST_ON(ENERGEST_TYPE_IRQ);
00146   if(IFG2 & UCA0TXIFG) {
00147     if(ringbuf_elements(&txbuf) == 0) {
00148       transmitting = 0;
00149     } else {
00150       UCA0TXBUF = ringbuf_get(&txbuf);
00151     }
00152   }
00153   
00154   ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00155 }
00156 #endif /* TX_WITH_INTERRUPT */
00157 /*---------------------------------------------------------------------------*/