]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/cc2420_tkn154/CC2420ControlP.nc
Merge TinyOS 2.1.1 into master.
[tinyos-2.x.git] / tos / chips / cc2420_tkn154 / CC2420ControlP.nc
index 64e4adf8f33f8b726fb867bf8266f4d32e99402b..030f8afcf21ca8f1e364544f1e636a957e0610ce 100644 (file)
@@ -33,7 +33,7 @@
  * @author Jonathan Hui <jhui@archrock.com>
  * @author David Moss
  * @author Urs Hunkeler (ReadRssi implementation)
- * @author Jan Hauer <hauer@tkn.tu-berlin.de> (support for promiscuous mode)
+ * @author Jan Hauer <hauer@tkn.tu-berlin.de>
  * @version $Revision$ $Date$
  */
 
@@ -53,6 +53,7 @@ module CC2420ControlP {
   uses interface GeneralIO as RSTN;
   uses interface GeneralIO as VREN;
   uses interface GpioInterrupt as InterruptCCA;
+  uses interface GeneralIO as FIFO;
 
   uses interface CC2420Ram as IEEEADR;
   uses interface CC2420Register as FSCTRL;
@@ -62,17 +63,17 @@ module CC2420ControlP {
   uses interface CC2420Register as MDMCTRL1;
   uses interface CC2420Register as RXCTRL1;
   uses interface CC2420Register as RSSI;
+  uses interface CC2420Register as RXFIFO_REGISTER;
+  uses interface CC2420Strobe as SNOP;
   uses interface CC2420Strobe as SRXON;
   uses interface CC2420Strobe as SRFOFF;
   uses interface CC2420Strobe as SXOSCOFF;
   uses interface CC2420Strobe as SXOSCON;
-  uses interface CC2420Strobe as SACKPEND; // JH: ACKs must have pending flag set
+  uses interface CC2420Strobe as SACKPEND;
+  uses interface CC2420Strobe as SFLUSHRX;
   uses interface CC2420Register as TXCTRL;
   uses interface AMPacket;
-  
   uses interface Resource as SpiResource;
-
-  uses interface Leds;
   uses interface FrameUtility;
 }
 
@@ -122,6 +123,12 @@ implementation {
   }
 
   /***************** Resource Commands ****************/
+  /* This module never actively requests the SPI resource,
+   * instead the caller MUST request the SPI through this module
+   * before it calls any of the provided commands and it must
+   * release it afterwards (the caller can call multiple  
+   * commands in this module before it releases the SPI, though).
+   */ 
   async command error_t Resource.immediateRequest() {
     error_t error = call SpiResource.immediateRequest();
     if ( error == SUCCESS ) {
@@ -145,29 +152,9 @@ implementation {
     }
   }
 
-  void switchToUnbufferedMode()
-  {
-    uint16_t mdmctrol1;
-    call CSN.set();
-    call CSN.clr();
-    call MDMCTRL1.read(&mdmctrol1);
-    call CSN.set();
-    mdmctrol1 &= ~0x0003;
-    mdmctrol1 |= 0x0000;
-    call CSN.clr();
-    call MDMCTRL1.write(mdmctrol1);
-    call CSN.set();
-  }
-
-  void switchToBufferedMode()
-  {
-    uint16_t mdmctrol1;
-    call CSN.set();
-    call CSN.clr();
-    call MDMCTRL1.read(&mdmctrol1);
-    mdmctrol1 &= ~0x03;
-    call MDMCTRL1.write(mdmctrol1);
-    call CSN.set();
+  event void SpiResource.granted() {
+/*    call CSN.clr();*/
+    signal Resource.granted();
   }
 
   /***************** CC2420Power Commands ****************/
@@ -179,7 +166,7 @@ implementation {
       m_state = S_VREG_STARTING;
     }
     call VREN.set();
-    call StartupAlarm.start( CC2420_TIME_VREN * 2 ); // JH: a 15.4 symbol is about two 32khz ticks
+    call StartupAlarm.start( CC2420_TIME_VREN * 2 ); // JH: changed from 32khz jiffies
     return SUCCESS;
   }
 
@@ -241,32 +228,52 @@ implementation {
 
   async command error_t CC2420Power.rxOn() {
     atomic {
-      if ( m_state != S_XOSC_STARTED ) {
+      if ( !call SpiResource.isOwner() )
         return FAIL;
-      }
       call CSN.set();
       call CSN.clr();
       call SRXON.strobe();
-      call SACKPEND.strobe();  // JH: ACKs have the pending bit set
+      call SACKPEND.strobe();  // JH: ACKs need the pending bit set
       call CSN.set();
     }
     return SUCCESS;
   }
 
   async command error_t CC2420Power.rfOff() {
-    atomic {  
-      if ( m_state != S_XOSC_STARTED ) {
+    atomic {
+      if ( !call SpiResource.isOwner() )
         return FAIL;
-      }
       call CSN.set();
       call CSN.clr();
-      call SACKPEND.strobe();  // JH: ACKs have the pending bit set
       call SRFOFF.strobe();
       call CSN.set();
     }
     return SUCCESS;
   }
 
+  async command error_t CC2420Power.flushRxFifo()
+  {
+    uint16_t dummy;
+    atomic {
+      if ( !call SpiResource.isOwner() )
+        return FAIL;
+      if ( call FIFO.get() ){ // check if there is something in the RXFIFO
+        // SFLUSHRX: "Flush the RX FIFO buffer and reset the demodulator. 
+        // Always read at least one byte from the RXFIFO before 
+        // issuing the SFLUSHRX command strobe" (CC2420 Datasheet)
+        call CSN.clr();
+        call RXFIFO_REGISTER.read(&dummy); // reading the byte
+        call CSN.set();
+        call CSN.clr();
+        // "SFLUSHRX command strobe should be issued twice to ensure 
+        // that the SFD pin goes back to its idle state." (CC2420 Datasheet)
+        call SFLUSHRX.strobe();
+        call SFLUSHRX.strobe();
+        call CSN.set();
+      }
+    }
+    return SUCCESS;
+  }
   
   /***************** CC2420Config Commands ****************/
   command uint8_t CC2420Config.getChannel() {
@@ -363,17 +370,16 @@ implementation {
   async command bool CC2420Config.needsSync(){
     atomic return m_needsSync;
   }
+
   /**
    * Sync must be called to commit software parameters configured on
    * the microcontroller (through the CC2420Config interface) to the
    * CC2420 radio chip.
-   * ASSUMPTION: caller owns the SPI, radio will be switched off !
    */
   async command error_t CC2420Config.sync() {
     atomic {
-      if ( m_state != S_XOSC_STARTED ) {
+      if ( !call SpiResource.isOwner() )
         return FAIL;
-      }
       if (m_needsSync){
         call CSN.set();
         call CSN.clr();
@@ -396,25 +402,22 @@ implementation {
   /***************** ReadRssi Commands ****************/
   
   async command error_t CC2420Power.rssi(int8_t *rssi) {
-    // we are owner of the Spi !
     uint16_t data;
     cc2420_status_t status;
-    call CSN.clr();
-    status = call RSSI.read(&data);
-    call CSN.set();
-    if ((status & 0x02)){
-      *rssi = (data & 0x00FF);
-      return SUCCESS;
-    } else
-      return FAIL;
-  }
-
-  event void SpiResource.granted() {
-/*    call CSN.clr();*/
-    signal Resource.granted();
+    atomic {
+      if ( !call SpiResource.isOwner() )
+        return FAIL;
+      call CSN.set();
+      call CSN.clr();
+      status = call RSSI.read(&data);
+      call CSN.set();
+      if ((status & 0x02)){
+        *rssi = (data & 0x00FF);
+        return SUCCESS;
+      } else
+        return FAIL;
+    }
   }
-
-
   
   /***************** StartupAlarm Events ****************/
   async event void StartupAlarm.fired() {