]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/msp430/usci/Msp430UartP.nc
Added SPI support. Now working using the loopback feature (UCLISTEN bit).
[tinyos-2.x.git] / tos / chips / msp430 / usci / Msp430UartP.nc
index 355820460eb6af580b6b211cb13c160596f1cbcd..17fbfd1983ac18b25dcc7b554b3f0ab8cee01edb 100644 (file)
@@ -30,6 +30,9 @@
 /**
  * Uart implementation using a USCI device.
  *
+ * TODO: Implement blocking like in Msp430UartSpiP.nc.
+ *       Implement error checking via UCAxSTAT.
+ *
  * @author R. Steve McKown <smckown@gmail.com>
  */
  
@@ -74,18 +77,22 @@ implementation {
                        call pin.makeInput(); \
        }
 
-  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 */
+  /* Pin IO configuration storage for later restoration */
+  uint8_t m_dir;
+  uint8_t m_out;
+  uint8_t m_ren;
+
+  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 */
+  uint8_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 */
+  uint8_t m_rolen;     /* Original (maximum) receive len */
+  uint8_t* m_rbuf;     /* Position of next byte in which to receive a char */
+  uint8_t m_rlen;      /* Remaining length in receive buffer */
+
+  sfrb(MYBRX, 219U);
 
   async command void ResourceConfigure.configure()
   {
@@ -103,12 +110,15 @@ implementation {
       call Registers.assignIrtctl(config->irtctl);
       call Registers.assignIrrctl(config->irrctl);
       call Registers.assignAbctl(config->abctl);
-      call Registers.clrStat(UCLISTEN);
+      if (config->uclisten)
+       call Registers.setStat(UCLISTEN);
+      else
+       call Registers.clrStat(UCLISTEN);
 
-      /* Save pin IO states */
-      dir = out = ren = 0;
-      saveBits(RXD, 0, dir, out, ren);
-      saveBits(TXD, 1, dir, out, ren);
+      /* 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();
@@ -122,7 +132,7 @@ implementation {
       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.
+       * enabling an open-drain emulation mode via USCI is not possible.
        */
 
       /* Configure TX pin for UART use */
@@ -159,8 +169,8 @@ implementation {
 
       /* Restore pins to their preconfigured state */
 #if 0
-      restoreBits(RXD, 0, dir, out, ren);
-      restoreBits(TXD, 0, dir, out, ren);
+      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();
@@ -171,9 +181,9 @@ implementation {
   {
     /* FIXME: race with UartStream.send() */
     atomic {
-      if (sobuf)
+      if (m_sobuf)
        return FAIL;
-      while (call Registers.getStat(UCBUSY));
+      while (!call Registers.getIfgTx());
       call Registers.setTxbuf(byte);
       return SUCCESS;
     }
@@ -181,40 +191,42 @@ 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_sobuf = buf;
+      m_solen = len;
+      call Registers.setIeTx();
+      call Registers.setTxbuf(*m_sobuf);
+      m_slen = m_solen - 1;
+      if (m_slen)
+       m_sbuf = m_sobuf + 1;
+      return SUCCESS;
+    }
   }
 
   async event void Interrupts.tx()
   {
-    while (slen && call Registers.getIfgTx()) {
-      call Registers.setTxbuf(*sbuf);
-      if (--slen)
-       sbuf++;
+    while (m_slen && call Registers.getIfgTx()) {
+      call Registers.setTxbuf(*m_sbuf);
+      if (--m_slen)
+       m_sbuf++;
     }
-    if (slen == 0 && sobuf) {
+    if (m_slen == 0 && m_sobuf) {
       call Registers.clrIeTx();
       call Registers.clrIfgTx();
-      sobuf = 0;
-      signal UartStream.sendDone(sobuf, solen, SUCCESS);
+      m_sobuf = 0;
+      signal UartStream.sendDone(m_sobuf, m_solen, SUCCESS);
     }
   }
 
   async command error_t UartStream.enableReceiveInterrupt()
   {
     atomic {
-      if (!robuf)
+      if (!m_robuf)
        call Registers.clrIfgRx();
       call Registers.setIeRx();
-      rxie = FALSE;
+      m_rxie = FALSE;
       return SUCCESS;
     }
   }
@@ -222,11 +234,11 @@ implementation {
   async command error_t UartStream.disableReceiveInterrupt()
   {
     atomic {
-      if (!robuf) {
+      if (!m_robuf) {
        call Registers.clrIeRx();
        call Registers.clrIfgRx();
       } else
-       rxie = TRUE;
+       m_rxie = TRUE;
       return SUCCESS;
     }
   }
@@ -237,7 +249,7 @@ implementation {
       uint16_t t;
 
       /* FIXME: race with UartStream.receive() */
-      if (robuf || !byte)
+      if (m_robuf || !byte)
        return FAIL;
       /* TODO: implement timeout, byte-time units.  For now, 1-2 sec */
       t = TBR;
@@ -254,36 +266,36 @@ implementation {
   async command error_t UartStream.receive(uint8_t* buf, uint16_t len)
   {
     atomic {
-      if (robuf || !buf || !len)
+      if (m_robuf || !buf || !len)
        return FAIL;
-      robuf = rbuf = buf;
-      rolen = rlen = len;
+      m_robuf = m_rbuf = buf;
+      m_rolen = m_rlen = len;
       if (!call Registers.getIeRx()) {
        call Registers.clrIfgRx();
        call Registers.setIeRx();
-       rxie = TRUE;
+       m_rxie = TRUE;
       } else
-       rxie = FALSE;
+       m_rxie = FALSE;
     }
   }
 
   async event void Interrupts.rx(uint8_t byte)
   {
     atomic {
-      if (robuf) {
+      if (m_robuf) {
        /* receive() takes precedence if active */
-       while (rlen && call Registers.getIfgRx()) {
-         *rbuf = byte;
-         if (--rlen)
-           rbuf++;
+       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);
@@ -294,12 +306,13 @@ implementation {
   {
     const static msp430_usci_uart_t def = { 
       ctl0: UCMODE_0,          /* async, lsb first, 8N1 */
-      ctl1: UCSWRST|UCSSEL_3,  /* clock uart from SMCLK */
+      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
     };