]> 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 49dc1f1bac92445a9579b14ba43c63fa796e8c91..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.
  *
  * (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.
  *
- * @author R. Steve McKown <smckown@gmail.com>
+ * TODO: Implement blocking like in Msp430UartSpiP.nc.
+ *       Implement error checking via UCAxSTAT.
+ *
+ * @author R. Steve McKown <rsmckown@gmail.com>
  */
+
 generic module Msp430UartP() {
   provides {
     interface UartStream;
@@ -50,42 +53,24 @@ 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); \
-       }
+  enum {
+    /* Bit positions in m_pins */
+    PINS_RXD = 0,
+    PINS_TXD
+  };
 
-  #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(); \
-       }
+  uint8_t m_pins;
+  uint8_t* m_sobuf;    /* Original buffer ptr from UartStream.send() */
+  uint16_t m_solen;    /* Original buffer len from UartStream.send() */
+  uint8_t* m_sbuf;     /* Position of next char to send */
+  uint16_t m_slen;     /* Len of chars in m_sbuf to send */
+  bool m_rxie;         /* Set if rxie has been enabled to UartStream.receive() */
+  uint8_t* m_robuf;    /* Original receive buffer */
+  uint16_t m_rolen;    /* Original (maximum) receive len */
+  uint8_t* m_rbuf;     /* Position of next byte in which to receive a char */
+  uint16_t m_rlen;     /* Remaining length in receive buffer */
 
-  uint8_t dir; /* Pin state storage to allow for proper unconfiguration */
-  uint8_t out;
-  uint8_t ren;
-  uint8_t* sobuf; /* Original buffer ptr from UartStream.send() */
-  uint8_t solen;       /* Original buffer len from UartStream.send() */
-  uint8_t* sbuf; /* Position of next char to send */
-  uint8_t slen;        /* Len of chars in sbuf to send */
-  bool rxie;   /* Set if rxie has been enabled to UartStream.receive() */
-  uint8_t* robuf; /* Original receive buffer */
-  uint8_t rolen; /* Original (maximum) receive len */
-  uint8_t* rbuf; /* Position of next byte in which to receive a char */
-  uint8_t rlen; /* Remaining length in receive buffer */
+  sfrb(MYBRX, 219U);
 
   async command void ResourceConfigure.configure()
   {
@@ -95,46 +80,33 @@ implementation {
       call Registers.setCtl1(UCSWRST);
 
       /* Configure USCI registers */
-      call Registers.assignCtl0(config->ctl0);
-      call Registers.assignCtl1(config->ctl1|UCSWRST);
+      call Registers.assignCtl0(config->ctl0 & ~UCSYNC);
+      call Registers.assignCtl1(config->ctl1 | UCSWRST);
       call Registers.assignBr0(config->brx & 0xff);
       call Registers.assignBr1(config->brx >> 8);
       call Registers.assignMctl(config->mctl);
       call Registers.assignIrtctl(config->irtctl);
       call Registers.assignIrrctl(config->irrctl);
       call Registers.assignAbctl(config->abctl);
-      call Registers.clrStat(UCLISTEN);
-
-      /* Save pin IO states */
-      dir = out = ren = 0;
-      saveBits(RXD, 0, dir, out, ren);
-      saveBits(TXD, 1, dir, out, ren);
+      if (config->uclisten)
+       call Registers.setStat(UCLISTEN);
+      else
+       call Registers.clrStat(UCLISTEN);
 
-      /* 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 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
+
+      /* Reset important state variables */
+      m_robuf = 0;
+      m_sobuf = 0;
 
       /* Clear interrupts; we'll add them as needed */
       call Registers.clrIeRx();
@@ -142,6 +114,10 @@ implementation {
 
       /* Enable the device */
       call Registers.clrCtl1(UCSWRST);
+
+      /* TOS convention is for receive interrupts on by default. */
+      call Registers.clrIfgRx();
+      call Registers.setIeRx();
     }
   }
 
@@ -149,31 +125,29 @@ implementation {
   {
     atomic {
       /* Disable the device */
-      call Registers.setCtl1(UCSYNC);
+      call Registers.setCtl1(UCSWRST);
 
       /* Clear interrupts and interrupt flags */
       call Registers.clrIeRx();
       call Registers.clrIeTx();
       call Registers.clrIfgRx();
-      call Registers.clrIfgTx();
 
-      /* Restore pins to their preconfigured state */
-#if 0
-      restoreBits(RXD, 0, dir, out, ren);
-      restoreBits(TXD, 0, dir, out, ren);
-#endif
-      call RXD.selectIOFunc();
-      call TXD.selectIOFunc();
+      /* Reset important state variables */
+      m_robuf = 0;
+      m_sobuf = 0;
+
+      /* Restore pins to their pre-configure state */
+      if (m_pins & (1 << PINS_RXD))
+       call RXD.selectIOFunc();
+      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 (sobuf)
-       return FAIL;
-      while (call Registers.getStat(UCBUSY));
+      while (!call Registers.getIfgTx());
       call Registers.setTxbuf(byte);
       return SUCCESS;
     }
@@ -181,100 +155,114 @@ implementation {
 
   async command error_t UartStream.send(uint8_t* buf, uint16_t len)
   {
-    if (sobuf || !buf || !len)
-      return FAIL;
-    sobuf = buf;
-    solen = len;
-    call Registers.setIeTx();
-    call Registers.setTxbuf(*sobuf);
-    slen = solen - 1;
-    if (slen)
-      sbuf = sobuf + 1;
-    return SUCCESS;
+    atomic {
+      if (m_sobuf || !buf || !len)
+       return FAIL;
+      m_sbuf = m_sobuf = buf;
+      m_slen = m_solen = len;
+      call Registers.setIeTx();
+      return SUCCESS;
+    }
   }
 
   async event void Interrupts.tx()
   {
-    while (slen && call Registers.getIfgTx()) {
-      call Registers.setTxbuf(*sbuf);
-      if (--slen)
-       sbuf++;
-    }
-    if (slen == 0 && sobuf) {
-      call Registers.clrIeTx();
-      call Registers.clrIfgTx();
-      sobuf = 0;
-      signal UartStream.sendDone(sobuf, 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);
+      }
     }
   }
 
   async command error_t UartStream.enableReceiveInterrupt()
   {
-    if (!robuf)
-      call Registers.clrIfgRx();
-    call Registers.setIeRx();
-    rxie = FALSE;
-    return SUCCESS;
+    atomic {
+      if (!m_robuf)
+       call Registers.clrIfgRx();
+      call Registers.setIeRx();
+      m_rxie = FALSE;
+      return SUCCESS;
+    }
   }
 
   async command error_t UartStream.disableReceiveInterrupt()
   {
-    if (!robuf) {
-      call Registers.clrIeRx();
-      call Registers.clrIfgRx();
-    } else
-      rxie = TRUE;
-    return SUCCESS;
+    atomic {
+      if (!m_robuf) {
+       call Registers.clrIeRx();
+       call Registers.clrIfgRx();
+      } else
+       m_rxie = TRUE;
+      return SUCCESS;
+    }
   }
 
   async command error_t UartByte.receive(uint8_t* byte, uint8_t timeout)
   {
-    uint16_t t;
+    atomic {
+      uint16_t t;
 
-    /* FIXME: race with UartStream.receive() */
-    if (robuf || !byte)
-      return FAIL;
-    /* TODO: implement timeout, byte-time units.  For now, 1-2 sec */
-    t = TBR;
-    while (t < TBR) {
-      if (call Registers.getIfgRx()) {
-       *byte = call Registers.getRxbuf();
-       return SUCCESS;
+      /* FIXME: race with UartStream.receive() */
+      if (m_robuf || !byte)
+       return FAIL;
+      /* TODO: implement timeout, byte-time units.  For now, 1-2 sec */
+      t = TBR;
+      while (t < TBR) {
+       if (call Registers.getIfgRx()) {
+         *byte = call Registers.getRxbuf();
+         return SUCCESS;
+       }
       }
+      return FAIL;
     }
-    return FAIL;
   }
 
   async command error_t UartStream.receive(uint8_t* buf, uint16_t len)
   {
-    if (robuf || !buf || !len)
-      return FAIL;
-    robuf = rbuf = buf;
-    rolen = rlen = len;
-    if (!call Registers.getIeRx()) {
-      call Registers.clrIfgRx();
-      call Registers.setIeRx();
-      rxie = TRUE;
-    } else
-      rxie = FALSE;
+    atomic {
+      if (m_robuf || !buf || !len)
+       return FAIL;
+      m_robuf = m_rbuf = buf;
+      m_rolen = m_rlen = len;
+      if (!call Registers.getIeRx()) {
+       call Registers.clrIfgRx();
+       call Registers.setIeRx();
+       m_rxie = TRUE;
+      } else
+       m_rxie = FALSE;
+    }
   }
 
   async event void Interrupts.rx(uint8_t byte)
   {
-    if (robuf) {
+    if (m_robuf) {
       /* receive() takes precedence if active */
-      while (rlen && call Registers.getIfgRx()) {
-       *rbuf = byte;
-       if (--rlen)
-         rbuf++;
+      /* 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 (rlen == 0 && robuf) {
-       if (rxie) {
+      if (m_rlen == 0 && m_robuf) {
+       if (m_rxie) {
          call Registers.clrIeRx();
          call Registers.clrIfgRx();
        }
-       robuf = 0;
-       signal UartStream.receiveDone(robuf, rolen, SUCCESS);
+       m_robuf = 0;
+       signal UartStream.receiveDone(m_robuf, m_rolen, SUCCESS);
       }
     } else
       signal UartStream.receivedByte(byte);
@@ -282,14 +270,15 @@ implementation {
 
   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_1,  /* clock uart from SMCLK */
-      brx: UBRX_32768HZ_9600,
-      mctl: UMCTL_32768HZ_9600,
+      ctl1: UCSWRST | UCSSEL_3,        /* clock uart from SMCLK */
+      brx: UBRX_1MHZ_115200,
+      mctl: UMCTL_1MHZ_115200,
       irtctl: 0,
       irrctl: 0,
       abctl: 0,
+      uclisten: FALSE,
       ren: USCI_REN_NONE
     };
 
@@ -300,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,