0b1b6bd3f0d379a1ac7dcebc10557304a745cff6
[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 //! Handles a Nordic RF command.
19 void nrf_handle_fn( uint8_t const app,
20                                         uint8_t const verb,
21                                         uint32_t const len);
22
23 // define the nrf app's app_t
24 app_t const nrf_app = {
25
26         /* app number */
27         NRF,
28
29         /* handle fn */
30         nrf_handle_fn,
31
32         /* name */
33         "NRF",
34
35         /* desc */
36         "\tThe NRF app adds support for the NordicRF register\n"
37         "\tinterface.\n"
38 };
39
40 #define RADIOACTIVE SETCE
41 #define RADIOPASSIVE CLRCE
42
43 //! Set up the pins for NRF mode.
44 void nrfsetup(){
45   SETSS;
46   P5DIR&=~MISO;
47   P5DIR|=MOSI+SCK;
48   DIRSS;
49   DIRCE;
50   
51   //Begin a new transaction.
52   CLRSS; 
53   SETSS;
54 }
55
56 //! Read and write an NRF byte.
57 u8 nrftrans8(u8 byte){
58   register unsigned int bit;
59   //This function came from the NRF Wikipedia article.
60   //Minor alterations.
61   
62   for (bit = 0; bit < 8; bit++) {
63     /* write MOSI on trailing edge of previous clock */
64     if (byte & 0x80)
65       SETMOSI;
66     else
67       CLRMOSI;
68     byte <<= 1;
69  
70     SETCLK;
71   
72     /* read MISO on trailing edge */
73     byte |= READMISO;
74     CLRCLK;
75   }
76   
77   return byte;
78 }
79
80
81 //! Writes a register
82 u8 nrf_regwrite(u8 reg, const u8 *buf, int len){
83   CLRSS;
84   
85   reg=nrftrans8(reg);
86   while(len--)
87     nrftrans8(*buf++);
88   
89   SETSS;
90   return reg;//status
91 }
92 //! Reads a register
93 u8 nrf_regread(u8 reg, u8 *buf, int len){
94   CLRSS;
95   
96   reg=nrftrans8(reg);
97   while(len--)
98     *buf++=nrftrans8(0);
99   
100   SETSS;
101   return reg;//status
102 }
103
104 //! Handles a Nordic RF command.
105 void nrf_handle_fn( uint8_t const app,
106                                         uint8_t const verb,
107                                         uint32_t const len)
108 {
109   unsigned long i;
110   
111   //Drop CE to passify radio.
112   RADIOPASSIVE;
113   //Raise !SS to end transaction, just in case we forgot.
114   SETSS;
115   nrfsetup();
116   
117   switch(verb){
118     //PEEK and POKE might come later.
119   case READ:  
120   case WRITE:
121     CLRSS; //Drop !SS to begin transaction.
122     for(i=0;i<len;i++)
123       cmddata[i]=nrftrans8(cmddata[i]);
124     SETSS;  //Raise !SS to end transaction.
125     txdata(app,verb,len);
126     break;
127
128   case PEEK://Grab NRF Register
129     CLRSS; //Drop !SS to begin transaction.
130     nrftrans8(NRF_R_REGISTER | cmddata[0]); //000A AAAA
131     for(i=1;i<len;i++)
132       cmddata[i]=nrftrans8(cmddata[i]);
133     SETSS;  //Raise !SS to end transaction.
134     txdata(app,verb,len);
135     break;
136     
137   case POKE://Poke NRF Register
138     CLRSS; //Drop !SS to begin transaction.
139     nrftrans8(NRF_W_REGISTER | cmddata[0]); //001A AAAA
140     for(i=1;i<len;i++)
141       cmddata[i]=nrftrans8(cmddata[i]);
142     SETSS;  //Raise !SS to end transaction.
143     txdata(app,verb,len);
144     break;
145   case SETUP:
146     nrfsetup();
147     txdata(app,verb,0);
148     break;
149   case NRF_RX:
150     RADIOPASSIVE;
151     //Get the packet.
152     CLRSS;
153     nrftrans8(NRF_R_RX_PAYLOAD);
154     for(i=0;i<32;i++)
155       cmddata[i]=nrftrans8(0xde);
156     SETSS;
157     //no break
158     txdata(app,verb,32);
159     break;
160   case NRF_RX_FLUSH:
161     //Flush the buffer.
162     CLRSS;
163     nrftrans8(NRF_FLUSH_RX);
164     SETSS;
165     
166     //Return the packet.
167     txdata(app,verb,32);
168     break;
169   case NRF_TX:
170   case NRF_TX_FLUSH:
171   default:
172     debugstr("Not yet supported.");
173     txdata(app,verb,0);
174     break;
175   }
176   
177
178   SETSS;//End session
179   RADIOACTIVE;
180 }