A major refactor of the GoodFET firmware build system and apps to give better
[goodfet] / firmware / apps / radios / nrf.c
index f65d2e5..0b1b6bd 100644 (file)
 #include "nrf.h"
 #include "spi.h"
 
-//Weird HOPE badge wiring.  This was a fuckup.
-//BIT0 should be SS, but in point of fact it is IRQ.
-//BIT4 is actually SS, BIT5 is CE.
-#define SS BIT4
+//! Handles a Nordic RF command.
+void nrf_handle_fn( uint8_t const app,
+                                       uint8_t const verb,
+                                       uint32_t const len);
+
+// define the nrf app's app_t
+app_t const nrf_app = {
+
+       /* app number */
+       NRF,
+
+       /* handle fn */
+       nrf_handle_fn,
 
-//This could be more accurate.
-//Does it ever need to be?
-#define NRFSPEED 0
-#define NRFDELAY(x)
-//delay(x)
+       /* name */
+       "NRF",
 
+       /* desc */
+       "\tThe NRF app adds support for the NordicRF register\n"
+       "\tinterface.\n"
+};
+
+#define RADIOACTIVE SETCE
+#define RADIOPASSIVE CLRCE
 
 //! Set up the pins for NRF mode.
 void nrfsetup(){
-  P5OUT|=SS;
+  SETSS;
   P5DIR&=~MISO;
-  P5DIR|=MOSI+SCK+SS;
-  
+  P5DIR|=MOSI+SCK;
+  DIRSS;
+  DIRCE;
   
   //Begin a new transaction.
-  P5OUT&=~SS; 
-  P5OUT|=SS;
+  CLRSS; 
+  SETSS;
 }
 
 //! Read and write an NRF byte.
@@ -66,68 +80,101 @@ u8 nrftrans8(u8 byte){
 
 //! Writes a register
 u8 nrf_regwrite(u8 reg, const u8 *buf, int len){
-  P5OUT&=~SS;
+  CLRSS;
   
   reg=nrftrans8(reg);
   while(len--)
     nrftrans8(*buf++);
   
-  P5OUT|=SS;
+  SETSS;
+  return reg;//status
 }
 //! Reads a register
 u8 nrf_regread(u8 reg, u8 *buf, int len){
-  P5OUT&=~SS;
+  CLRSS;
   
   reg=nrftrans8(reg);
   while(len--)
     *buf++=nrftrans8(0);
   
-  P5OUT|=SS;
+  SETSS;
+  return reg;//status
 }
 
 //! Handles a Nordic RF command.
-void nrfhandle(unsigned char app,
-              unsigned char verb,
-              unsigned long len){
+void nrf_handle_fn( uint8_t const app,
+                                       uint8_t const verb,
+                                       uint32_t const len)
+{
   unsigned long i;
   
+  //Drop CE to passify radio.
+  RADIOPASSIVE;
   //Raise !SS to end transaction, just in case we forgot.
-  P5OUT|=SS;
+  SETSS;
   nrfsetup();
-    
+  
   switch(verb){
     //PEEK and POKE might come later.
   case READ:  
   case WRITE:
-    P5OUT&=~SS; //Drop !SS to begin transaction.
+    CLRSS; //Drop !SS to begin transaction.
     for(i=0;i<len;i++)
       cmddata[i]=nrftrans8(cmddata[i]);
-    P5OUT|=SS;  //Raise !SS to end transaction.
+    SETSS;  //Raise !SS to end transaction.
     txdata(app,verb,len);
     break;
 
   case PEEK://Grab NRF Register
-    P5OUT&=~SS; //Drop !SS to begin transaction.
+    CLRSS; //Drop !SS to begin transaction.
     nrftrans8(NRF_R_REGISTER | cmddata[0]); //000A AAAA
     for(i=1;i<len;i++)
       cmddata[i]=nrftrans8(cmddata[i]);
-    P5OUT|=SS;  //Raise !SS to end transaction.
+    SETSS;  //Raise !SS to end transaction.
     txdata(app,verb,len);
     break;
     
   case POKE://Poke NRF Register
-    P5OUT&=~SS; //Drop !SS to begin transaction.
+    CLRSS; //Drop !SS to begin transaction.
     nrftrans8(NRF_W_REGISTER | cmddata[0]); //001A AAAA
     for(i=1;i<len;i++)
       cmddata[i]=nrftrans8(cmddata[i]);
-    P5OUT|=SS;  //Raise !SS to end transaction.
+    SETSS;  //Raise !SS to end transaction.
     txdata(app,verb,len);
     break;
-    
   case SETUP:
     nrfsetup();
     txdata(app,verb,0);
     break;
+  case NRF_RX:
+    RADIOPASSIVE;
+    //Get the packet.
+    CLRSS;
+    nrftrans8(NRF_R_RX_PAYLOAD);
+    for(i=0;i<32;i++)
+      cmddata[i]=nrftrans8(0xde);
+    SETSS;
+    //no break
+    txdata(app,verb,32);
+    break;
+  case NRF_RX_FLUSH:
+    //Flush the buffer.
+    CLRSS;
+    nrftrans8(NRF_FLUSH_RX);
+    SETSS;
+    
+    //Return the packet.
+    txdata(app,verb,32);
+    break;
+  case NRF_TX:
+  case NRF_TX_FLUSH:
+  default:
+    debugstr("Not yet supported.");
+    txdata(app,verb,0);
+    break;
   }
   
+
+  SETSS;//End session
+  RADIOACTIVE;
 }