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: uart0x.c,v 1.1 2010/08/24 16:23:20 joxe Exp $ 00030 */ 00031 00032 /* 00033 * Machine dependent MSP430X UART0 code. 00034 */ 00035 00036 #include <stdlib.h> 00037 #include <signal.h> 00038 00039 #include "sys/energest.h" 00040 #include "dev/uart0.h" 00041 #include "dev/watchdog.h" 00042 #include "lib/ringbuf.h" 00043 #include "dev/leds.h" 00044 00045 static int (*uart0_input_handler)(unsigned char c); 00046 00047 static volatile uint8_t transmitting; 00048 00049 #ifdef UART0_CONF_TX_WITH_INTERRUPT 00050 #define TX_WITH_INTERRUPT UART0_CONF_TX_WITH_INTERRUPT 00051 #else /* UART0_CONF_TX_WITH_INTERRUPT */ 00052 #define TX_WITH_INTERRUPT 1 00053 #endif /* UART0_CONF_TX_WITH_INTERRUPT */ 00054 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 uart0_active(void) 00066 { 00067 return (UCA0STAT & UCBUSY) | transmitting; 00068 } 00069 /*---------------------------------------------------------------------------*/ 00070 void 00071 uart0_set_input(int (*input)(unsigned char c)) 00072 { 00073 uart0_input_handler = input; 00074 } 00075 /*---------------------------------------------------------------------------*/ 00076 void 00077 uart0_writeb(unsigned char c) 00078 { 00079 watchdog_periodic(); 00080 #if TX_WITH_INTERRUPT 00081 /* Put the outgoing byte on the transmission buffer. If the buffer 00082 is full, we just keep on trying to put the byte into the buffer 00083 until it is possible to put it there. */ 00084 while(ringbuf_put(&txbuf, c) == 0); 00085 00086 /* If there is no transmission going, we need to start it by putting 00087 the first byte into the UART. */ 00088 if(transmitting == 0) { 00089 transmitting = 1; 00090 UCA0TXBUF = ringbuf_get(&txbuf); 00091 } 00092 00093 #else /* TX_WITH_INTERRUPT */ 00094 00095 /* Loop until the transmission buffer is available. */ 00096 /*Enric while((IFG2 & UCA0TXIFG) == 0); */ 00097 while((UCA0STAT & UCBUSY)); 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 uart0_init(unsigned long ubr) 00113 { 00114 /* RS232 */ 00115 UCA0CTL1 |= UCSWRST; /* Hold peripheral in reset state */ 00116 UCA0CTL1 |= UCSSEL_2; /* CLK = SMCLK */ 00117 00118 UCA0BR0 = 0x45; /* 8MHz/115200 = 69 = 0x45 */ 00119 UCA0BR1 = 0x00; 00120 UCA0MCTL = UCBRS_3; /* Modulation UCBRSx = 3 */ 00121 00122 P3DIR &= ~0x20; /* P3.5 = USCI_A0 RXD as input */ 00123 P3DIR |= 0x10; /* P3.4 = USCI_A0 TXD as output */ 00124 P3SEL |= 0x30; /* P3.4,5 = USCI_A0 TXD/RXD */ 00125 /*UCA0CTL1 &= ~UCSWRST;*/ /* Initialize USCI state machine */ 00126 00127 transmitting = 0; 00128 00129 /* XXX Clear pending interrupts before enable */ 00130 IFG2 &= ~UCA0RXIFG; 00131 IFG2 &= ~UCA0TXIFG; 00132 UCA0CTL1 &= ~UCSWRST; /* Initialize USCI state machine **before** enabling interrupts */ 00133 IE2 |= UCA0RXIE; /* Enable UCA0 RX interrupt */ 00134 /* Enable USCI_A0 TX interrupts (if TX_WITH_INTERRUPT enabled) */ 00135 #if TX_WITH_INTERRUPT 00136 ringbuf_init(&txbuf, txbuf_data, sizeof(txbuf_data)); 00137 IE2 |= UCA0TXIE; /* Enable UCA0 TX interrupt */ 00138 #endif /* TX_WITH_INTERRUPT */ 00139 } 00140 /*---------------------------------------------------------------------------*/ 00141 interrupt(USCIAB0RX_VECTOR) 00142 uart0_rx_interrupt(void) 00143 { 00144 uint8_t c; 00145 00146 ENERGEST_ON(ENERGEST_TYPE_IRQ); 00147 leds_toggle(LEDS_RED); 00148 if(UCA0STAT & UCRXERR) { 00149 c = UCA0RXBUF; /* Clear error flags by forcing a dummy read. */ 00150 } else { 00151 c = UCA0RXBUF; 00152 if(uart0_input_handler != NULL) { 00153 if(uart0_input_handler(c)) { 00154 LPM4_EXIT; 00155 } 00156 } 00157 } 00158 ENERGEST_OFF(ENERGEST_TYPE_IRQ); 00159 } 00160 /*---------------------------------------------------------------------------*/ 00161 #if TX_WITH_INTERRUPT 00162 interrupt(USCIAB0TX_VECTOR) 00163 uart0_tx_interrupt(void) 00164 { 00165 ENERGEST_ON(ENERGEST_TYPE_IRQ); 00166 if((IFG2 & UCA0TXIFG)){ 00167 00168 if(ringbuf_elements(&txbuf) == 0) { 00169 transmitting = 0; 00170 } else { 00171 UCA0TXBUF = ringbuf_get(&txbuf); 00172 } 00173 } 00174 00175 /* In a stand-alone app won't work without this. Is the UG misleading? */ 00176 IFG2 &= ~UCA0TXIFG; 00177 00178 ENERGEST_OFF(ENERGEST_TYPE_IRQ); 00179 } 00180 #endif /* TX_WITH_INTERRUPT */ 00181 /*---------------------------------------------------------------------------*/