Moved TXFLUSH strobe from Python to C in CC2420 driver.
[goodfet] / firmware / apps / radios / ccspi.c
index d7fba81..377a5f0 100644 (file)
@@ -139,32 +139,42 @@ void ccspi_handle_fn( uint8_t const app,
     txdata(app,verb,0);
     break;
   case CCSPI_RX:
     txdata(app,verb,0);
     break;
   case CCSPI_RX:
-    #ifdef FIFOP
-    //Wait for any incoming packet to finish.
-    //while(!SFD);
-    while(SFD);
-    delay(1000);
-    
+#ifdef FIFOP
+     //Has there been an overflow?
+    if((!FIFO)&&FIFOP){
+      debugstr("Clearing overflow");
+      CLRSS;
+      ccspitrans8(0x08); //SFLUSHRX
+      SETSS;
+    }
     
     //Is there a packet?
     
     //Is there a packet?
-    if((!SFD)){
+    if(FIFOP&&FIFO){
+      //Wait for completion.
+      while(SFD);
+      
       //Get the packet.
       CLRSS;
       //Get the packet.
       CLRSS;
-      //ccspitrans8(CCSPI_RXFIFO);
-      ccspitrans8(0x3F|0x40);
-      cmddata[1]=0xff; //to be replaced with length
-      for(i=0;i<cmddata[1];i++)
+      ccspitrans8(CCSPI_RXFIFO | 0x40);
+      //ccspitrans8(0x3F|0x40);
+      cmddata[0]=0xff; //to be replaced with length
+      for(i=0;i<cmddata[0]+2;i++)
        cmddata[i]=ccspitrans8(0xde);
       SETSS;
        cmddata[i]=ccspitrans8(0xde);
       SETSS;
-      txdata(app,verb,cmddata[0]);
+      
+      //Flush buffer.
+      CLRSS;
+      ccspitrans8(0x08); //SFLUSHRX
+      SETSS;
+      txdata(app,verb,cmddata[0]+2);
     }else{
       //No packet.
       txdata(app,verb,0);
     }
     }else{
       //No packet.
       txdata(app,verb,0);
     }
-    #else
+#else
     debugstr("Can't RX a packet with SFD and FIFOP definitions.");
     txdata(app,NOK,0);
     debugstr("Can't RX a packet with SFD and FIFOP definitions.");
     txdata(app,NOK,0);
-    #endif
+#endif
     break;
   case CCSPI_RX_FLUSH:
     //Flush the buffer.
     break;
   case CCSPI_RX_FLUSH:
     //Flush the buffer.
@@ -187,8 +197,40 @@ void ccspi_handle_fn( uint8_t const app,
     txdata(app,verb,0);
     break;
   case CCSPI_TX:
     txdata(app,verb,0);
     break;
   case CCSPI_TX:
+#ifdef FIFOP
+    
+    //Wait for last packet to TX.
+    //while(ccspi_status()&BIT3);
+    
+    //Load the packet.
+    CLRSS;
+    ccspitrans8(CCSPI_TXFIFO);
+    for(i=0;i<cmddata[0];i++)
+      ccspitrans8(cmddata[i]);
+    SETSS;
+    
+    //Transmit the packet.
+    CLRSS;
+    ccspitrans8(0x04); //STXON
+    SETSS;
+    
+    //Wait for the pulse on SFD, after which the packet has been sent.
+    while(!SFD);
+    while(SFD);
+    
+    //Flush TX buffer.
+    CLRSS;
+    ccspitrans8(0x09); //SFLUSHTX
+    SETSS;
+    
+    txdata(app,verb,0);
+#else
+    debugstr("Can't TX a packet with SFD and FIFOP definitions.");
+    txdata(app,NOK,0);
+#endif
+    break;
   default:
   default:
-    debugstr("Not yet supported.");
+    debugstr("Not yet supported in CCSPI");
     txdata(app,verb,0);
     break;
   }
     txdata(app,verb,0);
     break;
   }