9254f9325039c5665be46db7289530253fbfc422
[goodfet] / firmware / apps / radios / nrf.c
1 /*! \file nrf.c
2   \author Travis Goodspeed
3   \brief NordicRF Register Interface
4 */
5
6 //Higher level left to client application.
7
8 #include "platform.h"
9 #include "command.h"
10
11 #include <signal.h>
12 #include <io.h>
13 #include <iomacros.h>
14
15 #include "nrf.h"
16 #include "spi.h"
17
18 //Weird HOPE badge wiring.  This was a fuckup.
19 //BIT0 should be SS, but in point of fact it is IRQ.
20 //BIT4 is actually SS, BIT5 is CE.
21 #define SS BIT4
22 #define CE BIT5;
23
24 #define RADIOACTIVE  P5OUT|=CE
25 #define RADIOPASSIVE P5OUT&=~CE
26
27
28 //! Set up the pins for NRF mode.
29 void nrfsetup(){
30   P5OUT|=SS;
31   P5DIR&=~MISO;
32   P5DIR|=MOSI+SCK+SS+CE;
33   
34   
35   //Begin a new transaction.
36   P5OUT&=~SS; 
37   P5OUT|=SS;
38 }
39
40 //! Read and write an NRF byte.
41 u8 nrftrans8(u8 byte){
42   register unsigned int bit;
43   //This function came from the NRF Wikipedia article.
44   //Minor alterations.
45   
46   for (bit = 0; bit < 8; bit++) {
47     /* write MOSI on trailing edge of previous clock */
48     if (byte & 0x80)
49       SETMOSI;
50     else
51       CLRMOSI;
52     byte <<= 1;
53  
54     SETCLK;
55   
56     /* read MISO on trailing edge */
57     byte |= READMISO;
58     CLRCLK;
59   }
60   
61   return byte;
62 }
63
64
65 //! Writes a register
66 u8 nrf_regwrite(u8 reg, const u8 *buf, int len){
67   P5OUT&=~SS;
68   
69   reg=nrftrans8(reg);
70   while(len--)
71     nrftrans8(*buf++);
72   
73   P5OUT|=SS;
74   return reg;//status
75 }
76 //! Reads a register
77 u8 nrf_regread(u8 reg, u8 *buf, int len){
78   P5OUT&=~SS;
79   
80   reg=nrftrans8(reg);
81   while(len--)
82     *buf++=nrftrans8(0);
83   
84   P5OUT|=SS;
85   return reg;//status
86 }
87
88 //! Handles a Nordic RF command.
89 void nrfhandle(unsigned char app,
90                unsigned char verb,
91                unsigned long len){
92   unsigned long i;
93   
94   //Drop CE to passify radio.
95   RADIOPASSIVE;
96   //Raise !SS to end transaction, just in case we forgot.
97   P5OUT|=SS;
98   nrfsetup();
99   
100   switch(verb){
101     //PEEK and POKE might come later.
102   case READ:  
103   case WRITE:
104     P5OUT&=~SS; //Drop !SS to begin transaction.
105     for(i=0;i<len;i++)
106       cmddata[i]=nrftrans8(cmddata[i]);
107     P5OUT|=SS;  //Raise !SS to end transaction.
108     txdata(app,verb,len);
109     break;
110
111   case PEEK://Grab NRF Register
112     P5OUT&=~SS; //Drop !SS to begin transaction.
113     nrftrans8(NRF_R_REGISTER | cmddata[0]); //000A AAAA
114     for(i=1;i<len;i++)
115       cmddata[i]=nrftrans8(cmddata[i]);
116     P5OUT|=SS;  //Raise !SS to end transaction.
117     txdata(app,verb,len);
118     break;
119     
120   case POKE://Poke NRF Register
121     P5OUT&=~SS; //Drop !SS to begin transaction.
122     nrftrans8(NRF_W_REGISTER | cmddata[0]); //001A AAAA
123     for(i=1;i<len;i++)
124       cmddata[i]=nrftrans8(cmddata[i]);
125     P5OUT|=SS;  //Raise !SS to end transaction.
126     txdata(app,verb,len);
127     break;
128   case SETUP:
129     nrfsetup();
130     txdata(app,verb,0);
131     break;
132   case NRF_RX:
133     RADIOPASSIVE;
134     //Get the packet.
135     P5OUT&=~SS;
136     nrftrans8(NRF_R_RX_PAYLOAD);
137     for(i=0;i<32;i++)
138       cmddata[i]=nrftrans8(0xde);
139     P5OUT|=SS;
140     //no break
141     txdata(app,verb,32);
142     break;
143   case NRF_RX_FLUSH:
144     //Flush the buffer.
145     P5OUT&=~SS;
146     nrftrans8(NRF_FLUSH_RX);
147     P5OUT|=SS;
148     
149     //Return the packet.
150     txdata(app,verb,32);
151     break;
152   case NRF_TX:
153   case NRF_TX_FLUSH:
154   default:
155     debugstr("Not yet supported.");
156     txdata(app,verb,0);
157     break;
158   }
159   
160
161   P5OUT|=SS;//End session
162   RADIOACTIVE;
163 }