]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/msp430/usart/Msp430UartP.nc
Update to include paramaterization of the UartStream interface as well as push Resour...
[tinyos-2.x.git] / tos / chips / msp430 / usart / Msp430UartP.nc
index c08d3fc00960babf62133af1a9897b4e0db26b5f..ca408466913a96e4d9335bd4c487bed0e060a5e2 100644 (file)
@@ -32,6 +32,7 @@
 /**
  * @author Jonathan Hui <jhui@archrock.com>
  * @author Vlado Handziski <handzisk@tkn.tu-berlin.de>
+ * @author Eric B. Decker <cire831@gmail.com>
  * @version $Revision$ $Date$
  */
 
@@ -41,14 +42,13 @@ generic module Msp430UartP() {
 
   provides interface Resource[ uint8_t id ];
   provides interface ResourceConfigure[ uint8_t id ];
-  provides interface Msp430UartControl as UartControl[ uint8_t id ];
-  provides interface UartStream;
-  provides interface UartByte;
+  provides interface UartStream[ uint8_t id ];
+  provides interface UartByte[ uint8_t id ];
   
   uses interface Resource as UsartResource[ uint8_t id ];
   uses interface Msp430UartConfigure[ uint8_t id ];
   uses interface HplMsp430Usart as Usart;
-  uses interface HplMsp430UsartInterrupts as UsartInterrupts;
+  uses interface HplMsp430UsartInterrupts as UsartInterrupts[ uint8_t id ];
   uses interface Counter<T32khz,uint16_t>;
   uses interface Leds;
 
@@ -60,6 +60,7 @@ implementation {
   norace uint16_t m_tx_len, m_rx_len;
   norace uint16_t m_tx_pos, m_rx_pos;
   norace uint8_t m_byte_time;
+  norace uint8_t current_owner;
   
   async command error_t Resource.immediateRequest[ uint8_t id ]() {
     return call UsartResource.immediateRequest[ id ]();
@@ -74,57 +75,50 @@ implementation {
   }
 
   async command error_t Resource.release[ uint8_t id ]() {
+    if (call UsartResource.isOwner[id]() == FALSE)
+      return FAIL;
     if ( m_rx_buf || m_tx_buf )
       return EBUSY;
     return call UsartResource.release[ id ]();
   }
 
   async command void ResourceConfigure.configure[ uint8_t id ]() {
-    call UartControl.setModeDuplex[id]();
+    msp430_uart_union_config_t* config = call Msp430UartConfigure.getConfig[id]();
+    m_byte_time = config->uartConfig.ubr / 2;
+    call Usart.setModeUart(config);
+    call Usart.enableIntr();
   }
 
   async command void ResourceConfigure.unconfigure[ uint8_t id ]() {
+    call Usart.resetUsart(TRUE);
     call Usart.disableIntr();
     call Usart.disableUart();
+
+    /* leave the usart in reset */
+    //call Usart.resetUsart(FALSE); // this shouldn't be called.
   }
 
   event void UsartResource.granted[ uint8_t id ]() {
     signal Resource.granted[ id ]();
   }
-
-  async command void UartControl.setModeRx[ uint8_t id ]() {
-    msp430_uart_config_t* config = call Msp430UartConfigure.getConfig[id]();
-    m_byte_time = config->ubr / 2;
-    call Usart.setModeUartRx(config);
-    call Usart.clrIntr();
-    call Usart.enableRxIntr();
-  }
-  
-  async command void UartControl.setModeTx[ uint8_t id ]() {
-    call Usart.setModeUartTx(call Msp430UartConfigure.getConfig[id]());
-    call Usart.clrIntr();
-    call Usart.enableTxIntr();
-  }
-  
-  async command void UartControl.setModeDuplex[ uint8_t id ]() {
-    msp430_uart_config_t* config = call Msp430UartConfigure.getConfig[id]();
-    m_byte_time = config->ubr / 2;
-    call Usart.setModeUart(config);
-    call Usart.clrIntr();
-    call Usart.enableIntr();
-  }
   
-  async command error_t UartStream.enableReceiveInterrupt() {
+  async command error_t UartStream.enableReceiveInterrupt[ uint8_t id ]() {
+    if (call UsartResource.isOwner[id]() == FALSE)
+      return FAIL;
     call Usart.enableRxIntr();
     return SUCCESS;
   }
   
-  async command error_t UartStream.disableReceiveInterrupt() {
+  async command error_t UartStream.disableReceiveInterrupt[ uint8_t id ]() {
+    if (call UsartResource.isOwner[id]() == FALSE)
+      return FAIL;
     call Usart.disableRxIntr();
     return SUCCESS;
   }
 
-  async command error_t UartStream.receive( uint8_t* buf, uint16_t len ) {
+  async command error_t UartStream.receive[ uint8_t id ]( uint8_t* buf, uint16_t len ) {
+    if (call UsartResource.isOwner[id]() == FALSE)
+      return FAIL;
     if ( len == 0 )
       return FAIL;
     atomic {
@@ -137,21 +131,22 @@ implementation {
     return SUCCESS;
   }
   
-  async event void UsartInterrupts.rxDone( uint8_t data ) {
+  async event void UsartInterrupts.rxDone[uint8_t id]( uint8_t data ) {
     if ( m_rx_buf ) {
       m_rx_buf[ m_rx_pos++ ] = data;
       if ( m_rx_pos >= m_rx_len ) {
        uint8_t* buf = m_rx_buf;
        m_rx_buf = NULL;
-       signal UartStream.receiveDone( buf, m_rx_len, SUCCESS );
+       signal UartStream.receiveDone[id]( buf, m_rx_len, SUCCESS );
       }
-    }
-    else {
-      signal UartStream.receivedByte( data );
+    } else {
+      signal UartStream.receivedByte[id]( data );
     }
   }
   
-  async command error_t UartStream.send( uint8_t* buf, uint16_t len ) {
+  async command error_t UartStream.send[ uint8_t id ]( uint8_t* buf, uint16_t len ) {
+    if (call UsartResource.isOwner[id]() == FALSE)
+      return FAIL;
     if ( len == 0 )
       return FAIL;
     else if ( m_tx_buf )
@@ -159,36 +154,50 @@ implementation {
     m_tx_buf = buf;
     m_tx_len = len;
     m_tx_pos = 0;
+    current_owner = id;
     call Usart.tx( buf[ m_tx_pos++ ] );
     return SUCCESS;
   }
   
-  async event void UsartInterrupts.txDone() {
-    if ( m_tx_pos < m_tx_len ) {
+  async event void UsartInterrupts.txDone[uint8_t id]() {
+    if(current_owner != id) {
+      uint8_t* buf = m_tx_buf;
+      m_tx_buf = NULL;
+      signal UartStream.sendDone[id]( buf, m_tx_len, FAIL );
+    }
+    else if ( m_tx_pos < m_tx_len ) {
       call Usart.tx( m_tx_buf[ m_tx_pos++ ] );
     }
     else {
       uint8_t* buf = m_tx_buf;
       m_tx_buf = NULL;
-      signal UartStream.sendDone( buf, m_tx_len, SUCCESS );
+      signal UartStream.sendDone[id]( buf, m_tx_len, SUCCESS );
     }
   }
   
-  async command error_t UartByte.send( uint8_t data ) {
+  async command error_t UartByte.send[ uint8_t id ]( uint8_t data ) {
+    if (call UsartResource.isOwner[id]() == FALSE)
+      return FAIL;
+    call Usart.clrTxIntr();
+    call Usart.disableTxIntr ();
     call Usart.tx( data );
     while( !call Usart.isTxIntrPending() );
+    call Usart.clrTxIntr();
+    call Usart.enableTxIntr();
     return SUCCESS;
   }
   
-  async command error_t UartByte.receive( uint8_t* byte, uint8_t timeout ) {
+  async command error_t UartByte.receive[ uint8_t id ]( uint8_t* byte, uint8_t timeout ) {
     
     uint16_t timeout_micro = m_byte_time * timeout + 1;
     uint16_t start;
     
+    if (call UsartResource.isOwner[id]() == FALSE)
+      return FAIL;
     start = call Counter.get();
     while( !call Usart.isRxIntrPending() ) {
       if ( ( call Counter.get() - start ) >= timeout_micro )
-       return FAIL;
+                               return FAIL;
     }
     *byte = call Usart.rx();
     
@@ -202,9 +211,13 @@ implementation {
   default async command error_t UsartResource.request[ uint8_t id ]() { return FAIL; }
   default async command error_t UsartResource.immediateRequest[ uint8_t id ]() { return FAIL; }
   default async command error_t UsartResource.release[ uint8_t id ]() { return FAIL; }
-  default async command msp430_uart_config_t* Msp430UartConfigure.getConfig[uint8_t id]() {
+  default async command msp430_uart_union_config_t* Msp430UartConfigure.getConfig[uint8_t id]() {
     return &msp430_uart_default_config;
   }
 
   default event void Resource.granted[ uint8_t id ]() {}
+
+  default async event void UartStream.sendDone[ uint8_t id ](uint8_t* buf, uint16_t len, error_t error) {}
+  default async event void UartStream.receivedByte[ uint8_t id ](uint8_t byte) {}
+  default async event void UartStream.receiveDone[ uint8_t id ]( uint8_t* buf, uint16_t len, error_t error ) {}
 }