X-Git-Url: https://oss.titaniummirror.com/gitweb/?a=blobdiff_plain;f=tos%2Fchips%2Fmsp430%2Fusci%2FMsp430UartP.nc;h=605c426960f5d926d10670ec8582a70dc76383f6;hb=2d252cfb688eee6ff8aa13bef1efb1627cb866f8;hp=17fbfd1983ac18b25dcc7b554b3f0ab8cee01edb;hpb=97db9cf16631f784eea3fc7a3ec925f09bc65c08;p=tinyos-2.x.git diff --git a/tos/chips/msp430/usci/Msp430UartP.nc b/tos/chips/msp430/usci/Msp430UartP.nc index 17fbfd19..605c4269 100644 --- a/tos/chips/msp430/usci/Msp430UartP.nc +++ b/tos/chips/msp430/usci/Msp430UartP.nc @@ -26,16 +26,16 @@ * (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. * * TODO: Implement blocking like in Msp430UartSpiP.nc. * Implement error checking via UCAxSTAT. * - * @author R. Steve McKown + * @author R. Steve McKown */ - + generic module Msp430UartP() { provides { interface UartStream; @@ -53,44 +53,22 @@ 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 = 0, + 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() */ + uint16_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 */ + 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 */ - uint8_t m_rolen; /* Original (maximum) receive len */ + uint16_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 */ + uint16_t m_rlen; /* Remaining length in receive buffer */ sfrb(MYBRX, 219U); @@ -115,36 +93,20 @@ 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 + + /* Reset important state variables */ + m_robuf = 0; + m_sobuf = 0; /* Clear interrupts; we'll add them as needed */ call Registers.clrIeRx(); @@ -152,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(); } } @@ -159,30 +125,28 @@ 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, m_dir, m_out, m_ren); - restoreBits(TXD, 0, m_dir, m_out, m_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 (m_sobuf) - return FAIL; while (!call Registers.getIfgTx()); call Registers.setTxbuf(byte); return SUCCESS; @@ -194,29 +158,30 @@ implementation { atomic { if (m_sobuf || !buf || !len) return FAIL; - m_sobuf = buf; - m_solen = len; + m_sbuf = m_sobuf = buf; + m_slen = 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 (m_slen && call Registers.getIfgTx()) { - call Registers.setTxbuf(*m_sbuf); - if (--m_slen) - m_sbuf++; - } - if (m_slen == 0 && m_sobuf) { - call Registers.clrIeTx(); - call Registers.clrIfgTx(); - m_sobuf = 0; - signal UartStream.sendDone(m_sobuf, m_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); + } } } @@ -281,30 +246,31 @@ implementation { async event void Interrupts.rx(uint8_t byte) { - atomic { - if (m_robuf) { - /* receive() takes precedence if active */ - while (m_rlen && call Registers.getIfgRx()) { - *m_rbuf = byte; - if (--m_rlen) - m_rbuf++; - } - if (m_rlen == 0 && m_robuf) { - if (m_rxie) { - call Registers.clrIeRx(); - call Registers.clrIfgRx(); - } - m_robuf = 0; - signal UartStream.receiveDone(m_robuf, m_rolen, SUCCESS); + if (m_robuf) { + /* receive() takes precedence if active */ + /* 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 (m_rlen == 0 && m_robuf) { + if (m_rxie) { + call Registers.clrIeRx(); + call Registers.clrIfgRx(); } - } else - signal UartStream.receivedByte(byte); - } + m_robuf = 0; + signal UartStream.receiveDone(m_robuf, m_rolen, SUCCESS); + } + } else + signal UartStream.receivedByte(byte); } 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_3, /* clock uart from SMCLK */ brx: UBRX_1MHZ_115200,