]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/msp430/usci/Msp430UartP.nc
Fix up io pin restoration after Uart peripheral is released.
[tinyos-2.x.git] / tos / chips / msp430 / usci / Msp430UartP.nc
index 72df1df0c1827f63ef14f7836a5927aaa43b3828..605c426960f5d926d10670ec8582a70dc76383f6 100644 (file)
@@ -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,10 +132,14 @@ 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();
     }
   }
@@ -158,16 +170,18 @@ implementation {
     /* FIXME: this can cause an arbitrarily long ISR, if m_slen is large.
      * But depending on timing, we may always only write 1 byte.
      */
-    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);
+    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);
+      }
     }
   }