]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
Clean up pin configuration for peripheral code. Here's the best practice
authorsmckown <smckown@4bc1554a-c7f2-4f65-a403-e0be01f0239c>
Fri, 12 Sep 2008 22:11:15 +0000 (22:11 +0000)
committerR. Steve McKown <rsmckown@gmail.com>
Tue, 1 Dec 2009 03:00:58 +0000 (20:00 -0700)
for platforms.  MotePlatformC defines pins, using also the new PxREN feature,
such that the peripheral pins are inputs with pull up/down's, or outputs, as
required to meet the electrical characteristics when the peripheral is not on.
In the peripheral's implementation of ResourceConfigure, it sets module
function on configure() and sets back to IO funtion on unconfigure(), but
only if the pin was set to IO mode when configure() was called.  This latter
condition allows a MotePlatformC to set peripheral mode on for pins that will
always be used for peripheral function, and the modes won't switch around.

tos/chips/msp430/usci/Msp430SpiP.nc
tos/chips/msp430/usci/Msp430UartP.nc
tos/platforms/tmicore/MotePlatformC.nc

index 5a23279c4689d8a1e76a625d40f9216c4e03b3dd..e35d54fa9ccd7d8847aac6b7accb9c705ad52984 100644 (file)
@@ -54,38 +54,16 @@ generic module Msp430SpiP(uint16_t blockSize) {
 }
 implementation {
   enum {
-    BLOCKSIZE_DEFAULT = 64
-  };
-
-  #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); \
-       }
+    BLOCKSIZE_DEFAULT = 64,
 
-  #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;
+    /* Bit positions in m_pins */
+    PINS_STE = 1,
+    PINS_SOMI,
+    PINS_SIMO,
+    PINS_CLK,
+  };
 
+  uint8_t m_pins;
   uint8_t* m_txBuf;
   uint8_t* m_rxBuf;
   uint16_t m_len;
@@ -127,59 +105,24 @@ implementation {
       else
        call Registers.clrStat(UCLISTEN);
 
-      /* Save pin IO configuration */
-      m_dir = m_out = m_ren = 0;
-      if (is4pin())
-       saveBits(STE, 0, m_dir, m_out, m_ren);
-      saveBits(SIMO, 1, m_dir, m_out, m_ren);
-      saveBits(SOMI, 2, m_dir, m_out, m_ren);
-      saveBits(CLK, 3, m_dir, m_out, m_ren);
-
-      /* Configure pins for SPI use */
-      if (is4pin()) {
-#if 0 /* Unsure if REN on STE is a valid configuration */
-       /* Configure STE pin for SPI use */
-       if (config->ren & USCI_REN_STE) {
-         if (config->ren & USCI_REN_STE_PULLUP)
-           call STE.set();
-         else
-           call STE.clr();
-         call STE.enableRen();
-       }
-#endif
+      /* Configure pins for SPI, saving prior pin states */
+      m_pins = 0;
+      if (is4pin() && call STE.isIOFunc()) {
+       m_pins |= (1 << PINS_STE);
        call STE.selectModuleFunc();
       }
-      call SOMI.makeInput();
-      if (config->ren & USCI_REN_SOMI) {
-       if (config->ren & USCI_REN_SOMI_PULLUP)
-         call SOMI.set();
-       else
-         call SOMI.clr();
-       call SOMI.enableRen();
+      if (call SOMI.isIOFunc()) {
+       m_pins |= (1 << PINS_SOMI);
+       call SOMI.selectModuleFunc();
       }
-      call SOMI.selectModuleFunc();
-#if 0 /* Unsure if REN on SIMO is a valid configuration */
-      /* Configure SIMO pin for SPI use */
-      if (config->ren & USCI_REN_SIMO) {
-       if (config->ren & USCI_REN_SIMO_PULLUP)
-         call SIMO.set();
-       else
-         call SIMO.clr();
-       call SIMO.enableRen();
+      if (call SIMO.isIOFunc()) {
+       m_pins |= (1 << PINS_SIMO);
+       call SIMO.selectModuleFunc();
       }
-#endif
-      call SIMO.selectModuleFunc();
-#if 0 /* Unsure if REN on CLK is a valid configuration */
-      /* Configure CLK pin for SPI use */
-      if (config->ren & USCI_REN_CLK) {
-       if (config->ren & USCI_REN_CLK_PULLUP)
-         call CLK.set();
-       else
-         call CLK.clr();
-       call CLK.enableRen();
+      if (call CLK.isIOFunc()) {
+       m_pins |= (1 << PINS_CLK);
+       call CLK.selectModuleFunc();
       }
-#endif
-      call CLK.selectModuleFunc();
 
       /* Clear interrupts; we'll add them as needed */
       call Registers.clrIeRx();
@@ -200,19 +143,15 @@ implementation {
       call Registers.clrIeRx();
       call Registers.clrIfgRx();
 
-      /* Restore pins to their preconfigured state */
-#if 0
-      if (is4pin())
-       restoreBits(STE, 0, m_dir, m_out, m_ren);
-      restoreBits(SIMO, 1, m_dir, m_out, m_ren);
-      restoreBits(SOMI, 1, m_dir, m_out, m_ren);
-      restoreBits(CLK, 1, m_dir, m_out, m_ren);
-#endif
-      if (is4pin())
+      /* Restore pins to their pre-configure state */
+      if (is4pin() && (m_pins & PINS_STE))
        call STE.selectIOFunc();
-      call SIMO.selectIOFunc();
-      call SOMI.selectIOFunc();
-      call CLK.selectIOFunc();
+      if (m_pins & PINS_SIMO)
+       call SIMO.selectIOFunc();
+      if (m_pins & PINS_SOMI)
+       call SOMI.selectIOFunc();
+      if (m_pins & PINS_CLK)
+       call CLK.selectIOFunc();
     }
   }
 
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();
     }
   }
 
index bdf8d366e5297ec2e573fd3fec0d9377b30f7123..c4eb5028a3b5894305aa35e81cb3fa2d85161af8 100644 (file)
@@ -59,10 +59,10 @@ implementation {
        P2REN = 0xc8;   /* 1 1 0 0 1 0 0 0 */
 
        /* Port 3: 7:UC_RX, 6:UC_TX, 5:-, 4:-, 3:UC_SCK, 2:UC_SOMI, 1:UC_SIMO, 0:- */
-       P3SEL = 0xce;   /* 1 1 0 0 1 1 1 0 */
-       P3OUT = 0x7b;   /* 0 1 1 1 1 0 1 1 */
+       P3SEL = 0x00;   /* 0 0 0 0 0 0 0 0 */
+       P3OUT = 0xff;   /* 1 1 1 1 1 1 1 1 */
        P3DIR = 0x4a;   /* 0 1 0 0 1 0 1 0 */
-       P3REN = 0x31;   /* 0 0 1 1 0 0 0 1 */
+       P3REN = 0xb5;   /* 1 0 1 1 0 1 0 1 */
 
        /* Port 4: 7:-, 6:LED2n, 5:LED1n, 4:-, 3:-, 2:-, 1:-, 0:- */
        P4SEL = 0;      /* 0 0 0 0 0 0 0 0 */