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

Generated on Mon Apr 11 14:23:39 2011 for Contiki 2.5 by  doxygen 1.6.1