]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/msp430/usci/Msp430UartP.nc
Clean up pin configuration for peripheral code. Here's the best practice
[tinyos-2.x.git] / tos / chips / msp430 / usci / Msp430UartP.nc
index 17fbfd1983ac18b25dcc7b554b3f0ab8cee01edb..11bf8da9889e86fdc58000d275bbe54c4ebf91d5 100644 (file)
@@ -53,35 +53,13 @@ generic module Msp430UartP() {
   }
 }
 implementation {
-  #define saveBits(pin, pos, dir, out, ren) { \
-               if (call pin.isOutput()) \
-                       dir |= (1 << pos); \
-               if (call pin.getOut()) \
-                       out |= (1 << pos); \
-               if (call pin.isRen()) \
-                       ren |= (1 << pos); \
-       }
-
-  #define restoreBits(pin, pos, dir, out, ren) { \
-               if (ren & (1 << pos)) \
-                       call pin.enableRen(); \
-               else \
-                       call pin.disableRen(); \
-               if (out & (1 << pos)) \
-                       call pin.set(); \
-               else \
-                       call pin.clr(); \
-               if (dir & (1 << pos)) \
-                       call pin.makeOutput(); \
-               else \
-                       call pin.makeInput(); \
-       }
-
-  /* Pin IO configuration storage for later restoration */
-  uint8_t m_dir;
-  uint8_t m_out;
-  uint8_t m_ren;
+  enum {
+    /* Bit positions in m_pins */
+    PINS_RXD = 1,
+    PINS_TXD
+  };
 
+  uint8_t m_pins;
   uint8_t* m_sobuf;    /* Original buffer ptr from UartStream.send() */
   uint8_t m_solen;     /* Original buffer len from UartStream.send() */
   uint8_t* m_sbuf;     /* Position of next char to send */
@@ -115,36 +93,16 @@ implementation {
       else
        call Registers.clrStat(UCLISTEN);
 
-      /* Save pin IO configuration */
-      m_dir = m_out = m_ren = 0;
-      saveBits(RXD, 0, m_dir, m_out, m_ren);
-      saveBits(TXD, 1, m_dir, m_out, m_ren);
-
-      /* Configure RX pin for UART use */
-      call RXD.makeInput();
-      if (config->ren & USCI_REN_RX) {
-       if (config->ren & USCI_REN_RX_PULLUP)
-         call RXD.set();
-       else
-         call RXD.clr();
-       call RXD.enableRen();
+      /* Configure pins for UART, saving prior pin states */
+      m_pins = 0;
+      if (call RXD.isIOFunc()) {
+       m_pins |= (1 << PINS_RXD);
+       call RXD.selectModuleFunc();
       }
-      call RXD.selectModuleFunc();
-
-#if 0 /* pull-ups don't make sense on TXD, since it doesn't appear that
-       * enabling an open-drain emulation mode via USCI is not possible.
-       */
-
-      /* Configure TX pin for UART use */
-      if (config->ren & USCI_REN_TX) {
-       if (config->ren & USCI_REN_TX_PULLUP)
-         call TXD.set();
-       else
-         call TXD.clr();
-       call TXD.enableRen();
+      if (call TXD.isIOFunc()) {
+       m_pins |= (1 << PINS_TXD);
+       call TXD.selectModuleFunc();
       }
-      call TXD.selectModuleFunc();
-#endif
 
       /* Clear interrupts; we'll add them as needed */
       call Registers.clrIeRx();
@@ -167,13 +125,11 @@ implementation {
       call Registers.clrIfgRx();
       call Registers.clrIfgTx();
 
-      /* Restore pins to their preconfigured state */
-#if 0
-      restoreBits(RXD, 0, m_dir, m_out, m_ren);
-      restoreBits(TXD, 0, m_dir, m_out, m_ren);
-#endif
-      call RXD.selectIOFunc();
-      call TXD.selectIOFunc();
+      /* Restore pins to their pre-configure state */
+      if (m_pins & PINS_RXD)
+       call RXD.selectIOFunc();
+      if (m_pins & PINS_TXD)
+       call TXD.selectIOFunc();
     }
   }