]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/msp430/usci/Msp430SpiP.nc
Remove the NO_REN_ON_SPI define.
[tinyos-2.x.git] / tos / chips / msp430 / usci / Msp430SpiP.nc
index e35d54fa9ccd7d8847aac6b7accb9c705ad52984..9f3fd177fabddc6079ed4374425502d34de5a476 100644 (file)
@@ -32,7 +32,7 @@
  *
  * TODO: Implement error checking via UCxxSTAT
  *
- * @author R. Steve McKown <smckown@gmail.com>
+ * @author R. Steve McKown <rsmckown@gmail.com>
  */
  
 generic module Msp430SpiP(uint16_t blockSize) {
@@ -57,10 +57,11 @@ implementation {
     BLOCKSIZE_DEFAULT = 64,
 
     /* Bit positions in m_pins */
-    PINS_STE = 1,
+    PINS_STE = 0,
     PINS_SOMI,
     PINS_SIMO,
     PINS_CLK,
+    PINS_RENADDR,      /* This gets added to store the PxREN bit */
   };
 
   uint8_t m_pins;
@@ -107,6 +108,24 @@ implementation {
 
       /* Configure pins for SPI, saving prior pin states */
       m_pins = 0;
+      /* - First save off and disable PxREN bits */
+      if (is4pin() && call STE.isRen()) {
+       m_pins |= (1 << (PINS_STE + PINS_RENADDR));
+       call STE.disableRen();
+      }
+      if (call SOMI.isRen()) {
+       m_pins |= (1 << (PINS_SOMI + PINS_RENADDR));
+       call SOMI.disableRen();
+      }
+      if (call SIMO.isRen()) {
+       m_pins |= (1 << (PINS_SIMO + PINS_RENADDR));
+       call SIMO.disableRen();
+      }
+      if (call CLK.isRen()) {
+       m_pins |= (1 << (PINS_CLK + PINS_RENADDR));
+       call CLK.disableRen();
+      }
+      /* - Then save off IOFunc state and enable ModuleFunc */
       if (is4pin() && call STE.isIOFunc()) {
        m_pins |= (1 << PINS_STE);
        call STE.selectModuleFunc();
@@ -137,21 +156,31 @@ implementation {
   {
     atomic {
       /* Disable the device */
-      call Registers.setCtl1(UCSYNC);
+      call Registers.setCtl1(UCSWRST);
 
       /* Clear interrupts and interrupt flags.  We only used Rx */
       call Registers.clrIeRx();
       call Registers.clrIfgRx();
 
       /* Restore pins to their pre-configure state */
-      if (is4pin() && (m_pins & PINS_STE))
+      /* - First restore IOFunc states */
+      if (is4pin() && (m_pins & (1 << PINS_STE)))
        call STE.selectIOFunc();
-      if (m_pins & PINS_SIMO)
+      if (m_pins & (1 << PINS_SIMO))
        call SIMO.selectIOFunc();
-      if (m_pins & PINS_SOMI)
+      if (m_pins & (1 << PINS_SOMI))
        call SOMI.selectIOFunc();
-      if (m_pins & PINS_CLK)
+      if (m_pins & (1 << PINS_CLK))
        call CLK.selectIOFunc();
+      /* - Then restore PxREN bits */
+      if (is4pin() && (m_pins & (1 << (PINS_STE + PINS_RENADDR))))
+       call STE.enableRen();
+      if (m_pins & (1 << (PINS_SIMO + PINS_RENADDR)))
+       call SIMO.enableRen();
+      if (m_pins & (1 << (PINS_SOMI + PINS_RENADDR)))
+       call SOMI.enableRen();
+      if (m_pins & (1 << (PINS_CLK + PINS_RENADDR)))
+       call CLK.enableRen();
     }
   }
 
@@ -160,9 +189,9 @@ implementation {
     if (isBusy())
       return 0;
     else {
-      while (!call Registers.getIfgTx());
+      while (!call Registers.getIfgTx() && !call Registers.getCtl1(UCSWRST));
       call Registers.setTxbuf(byte);
-      while(!call Registers.getIfgRx());
+      while(!call Registers.getIfgRx() && !call Registers.getCtl1(UCSWRST));
       return call Registers.getRxbuf();
     }
   }
@@ -180,14 +209,14 @@ implementation {
 
       if (end > m_len)
        end = m_len;
-      call Registers.setTxbuf((m_txBuf) ? m_txBuf[m_pos] : 0);
+      call Registers.setTxbuf(m_txBuf ? m_txBuf[m_pos] : 0);
       while (++m_pos < end) {
-       while (!call Registers.getIfgRx());
+       while (!call Registers.getIfgRx() && !call Registers.getCtl1(UCSWRST));
+       tmp = call Registers.getRxbuf();
        if (m_rxBuf)
          m_rxBuf[m_pos - 1] = call Registers.getRxbuf();
-       else
-         tmp = call Registers.getRxbuf();
-       call Registers.setTxbuf((m_txBuf) ? m_txBuf[m_pos] : 0);
+       while (!call Registers.getIfgTx() && !call Registers.getCtl1(UCSWRST));
+       call Registers.setTxbuf(m_txBuf ? m_txBuf[m_pos] : 0);
       }
     }
   }
@@ -215,17 +244,16 @@ implementation {
   task void signalSendDone()
   {
     atomic {
-      signal SpiPacket.sendDone(m_txBuf, m_rxBuf, m_len, SUCCESS);
+      uint16_t len = m_len;
       m_len = 0;
+      signal SpiPacket.sendDone(m_txBuf, m_rxBuf, len, SUCCESS);
     }
   }
 
   async event void Interrupts.rx(uint8_t byte)
   {
     if (m_rxBuf)
-      m_rxBuf[m_pos - 1] = call Registers.getRxbuf();
-    else
-      call Registers.getRxbuf();
+      m_rxBuf[m_pos - 1] = byte;
 
     if (m_pos < m_len)
       sendData();