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