Nordic RF register peeks, pokes now work in the firmware.
[goodfet] / firmware / apps / radios / nrf.c
index f81afba..f65d2e5 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
+
 //This could be more accurate.
 //Does it ever need to be?
 #define NRFSPEED 0
 //! Set up the pins for NRF mode.
 void nrfsetup(){
   P5OUT|=SS;
-  P5DIR|=MOSI+SCK+SS;
   P5DIR&=~MISO;
+  P5DIR|=MOSI+SCK+SS;
+  
   
   //Begin a new transaction.
   P5OUT&=~SS; 
   P5OUT|=SS;
 }
 
-
 //! Read and write an NRF byte.
-unsigned char nrftrans8(unsigned char byte){
+u8 nrftrans8(u8 byte){
   register unsigned int bit;
   //This function came from the NRF Wikipedia article.
   //Minor alterations.
@@ -59,7 +64,26 @@ unsigned char nrftrans8(unsigned char byte){
 }
 
 
-
+//! Writes a register
+u8 nrf_regwrite(u8 reg, const u8 *buf, int len){
+  P5OUT&=~SS;
+  
+  reg=nrftrans8(reg);
+  while(len--)
+    nrftrans8(*buf++);
+  
+  P5OUT|=SS;
+}
+//! Reads a register
+u8 nrf_regread(u8 reg, u8 *buf, int len){
+  P5OUT&=~SS;
+  
+  reg=nrftrans8(reg);
+  while(len--)
+    *buf++=nrftrans8(0);
+  
+  P5OUT|=SS;
+}
 
 //! Handles a Nordic RF command.
 void nrfhandle(unsigned char app,
@@ -70,10 +94,10 @@ void nrfhandle(unsigned char app,
   //Raise !SS to end transaction, just in case we forgot.
   P5OUT|=SS;
   nrfsetup();
-  
+    
   switch(verb){
     //PEEK and POKE might come later.
-  case READ:
+  case READ:  
   case WRITE:
     P5OUT&=~SS; //Drop !SS to begin transaction.
     for(i=0;i<len;i++)
@@ -82,15 +106,22 @@ void nrfhandle(unsigned char app,
     txdata(app,verb,len);
     break;
 
-
-
   case PEEK://Grab NRF Register
-    txdata(app,verb,0);
+    P5OUT&=~SS; //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.
+    txdata(app,verb,len);
     break;
     
   case POKE://Poke NRF Register
-    
-    txdata(app,verb,0);
+    P5OUT&=~SS; //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.
+    txdata(app,verb,len);
     break;
     
   case SETUP: