]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/msp430/usci/Msp430UartP.nc
Fix TMI copyright attributions
[tinyos-2.x.git] / tos / chips / msp430 / usci / Msp430UartP.nc
index 1b595bfdbd9af40369beceb8128264f6f6620183..ca6cd52ce628e0f8aa0034a3c5595065efec51e2 100644 (file)
@@ -10,7 +10,7 @@
  * - Redistributions in binary form must reproduce the above copyright
  *   notice, this list of conditions and the following disclaimer in the
  *   documentation and/or other materials provided with the distribution.
- * - Neither the name of the Technische Universität Berlin nor the names
+ * - Neither the name of the Titanium Mirror, Inc. nor the names
  *   of its contributors may be used to endorse or promote products derived
  *   from this software without specific prior written permission.
  *
@@ -26,7 +26,7 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+
 /**
  * Uart implementation using a USCI device.
  *
@@ -35,7 +35,7 @@
  *
  * @author R. Steve McKown <rsmckown@gmail.com>
  */
+
 generic module Msp430UartP() {
   provides {
     interface UartStream;
@@ -55,7 +55,7 @@ generic module Msp430UartP() {
 implementation {
   enum {
     /* Bit positions in m_pins */
-    PINS_RXD = 1,
+    PINS_RXD = 0,
     PINS_TXD
   };
 
@@ -104,12 +104,20 @@ implementation {
        call TXD.selectModuleFunc();
       }
 
+      /* Reset important state variables */
+      m_robuf = 0;
+      m_sobuf = 0;
+
       /* Clear interrupts; we'll add them as needed */
       call Registers.clrIeRx();
       call Registers.clrIeTx();
 
       /* Enable the device */
       call Registers.clrCtl1(UCSWRST);
+
+      /* TOS convention is for receive interrupts on by default. */
+      call Registers.clrIfgRx();
+      call Registers.setIeRx();
     }
   }
 
@@ -124,20 +132,21 @@ implementation {
       call Registers.clrIeTx();
       call Registers.clrIfgRx();
 
+      /* Reset important state variables */
+      m_robuf = 0;
+      m_sobuf = 0;
+
       /* Restore pins to their pre-configure state */
-      if (m_pins & PINS_RXD)
+      if (m_pins & (1 << PINS_RXD))
        call RXD.selectIOFunc();
-      if (m_pins & PINS_TXD)
+      if (m_pins & (1 << PINS_TXD))
        call TXD.selectIOFunc();
     }
   }
 
   async command error_t UartByte.send(uint8_t byte)
   {
-    /* FIXME: race with UartStream.send() */
     atomic {
-      if (m_sobuf)
-       return FAIL;
       while (!call Registers.getIfgTx());
       call Registers.setTxbuf(byte);
       return SUCCESS;
@@ -149,29 +158,30 @@ implementation {
     atomic {
       if (m_sobuf || !buf || !len)
        return FAIL;
-      m_sobuf = buf;
-      m_solen = len;
-      while (!call Registers.getIfgTx());
+      m_sbuf = m_sobuf = buf;
+      m_slen = m_solen = len;
       call Registers.setIeTx();
-      call Registers.setTxbuf(*m_sobuf);
-      m_slen = m_solen - 1;
-      if (m_slen)
-       m_sbuf = m_sobuf + 1;
       return SUCCESS;
     }
   }
 
   async event void Interrupts.tx()
   {
-    while (m_slen && call Registers.getIfgTx()) {
-      call Registers.setTxbuf(*m_sbuf);
-      if (--m_slen)
-       m_sbuf++;
-    }
-    if (m_slen == 0 && m_sobuf) {
-      call Registers.clrIeTx();
-      m_sobuf = 0;
-      signal UartStream.sendDone(m_sobuf, m_solen, SUCCESS);
+    /* FIXME: this can cause an arbitrarily long ISR, if m_slen is large.
+     * But depending on timing, we may always only write 1 byte.
+     */
+    if (m_sobuf) {
+      while (!call Registers.getIfgTx()); /* in case interleaved UB.send */
+      while (m_slen && call Registers.getIfgTx()) {
+       call Registers.setTxbuf(*m_sbuf);
+       if (--m_slen)
+         m_sbuf++;
+      }
+      if (m_slen == 0) {
+       call Registers.clrIeTx();
+       m_sobuf = 0;
+       signal UartStream.sendDone(m_sobuf, m_solen, SUCCESS);
+      }
     }
   }
 
@@ -236,30 +246,31 @@ implementation {
 
   async event void Interrupts.rx(uint8_t byte)
   {
-    atomic {
-      if (m_robuf) {
-       /* receive() takes precedence if active */
-       while (m_rlen && call Registers.getIfgRx()) {
-         *m_rbuf = byte;
-         if (--m_rlen)
-           m_rbuf++;
-       }
-       if (m_rlen == 0 && m_robuf) {
-         if (m_rxie) {
-           call Registers.clrIeRx();
-           call Registers.clrIfgRx();
-         }
-         m_robuf = 0;
-         signal UartStream.receiveDone(m_robuf, m_rolen, SUCCESS);
+    if (m_robuf) {
+      /* receive() takes precedence if active */
+      /* FIXME: an arbitrarily long ISR may occur if m_rlen is large.
+       * But depending on timing, we may always only read 1 byte.
+       */
+      while (m_rlen && call Registers.getIfgRx()) {
+       *m_rbuf = byte;
+       if (--m_rlen)
+         m_rbuf++;
+      }
+      if (m_rlen == 0 && m_robuf) {
+       if (m_rxie) {
+         call Registers.clrIeRx();
+         call Registers.clrIfgRx();
        }
-      } else
-       signal UartStream.receivedByte(byte);
-    }
+       m_robuf = 0;
+       signal UartStream.receiveDone(m_robuf, m_rolen, SUCCESS);
+      }
+    } else
+      signal UartStream.receivedByte(byte);
   }
 
   default async command const msp430_usci_uart_t* Configure.get()
   {
-    const static msp430_usci_uart_t def = { 
+    const static msp430_usci_uart_t def = {
       ctl0: UCMODE_0,          /* async, lsb first, 8N1 */
       ctl1: UCSWRST | UCSSEL_3,        /* clock uart from SMCLK */
       brx: UBRX_1MHZ_115200,
@@ -278,7 +289,7 @@ implementation {
   async event void Interrupts.i2cStop() {}
   async event void Interrupts.i2cCal() {}
   async event void Interrupts.brk() {}
-  async event void Interrupts.i2cNak() {}
+  async event void Interrupts.i2cNack() {}
   async event void Counter.overflow() {}
 
   default async event void UartStream.sendDone( uint8_t* buf, uint16_t len,