f65d2e537e2a97a76995a677b8e923083133ff5a
[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
23 //This could be more accurate.
24 //Does it ever need to be?
25 #define NRFSPEED 0
26 #define NRFDELAY(x)
27 //delay(x)
28
29
30 //! Set up the pins for NRF mode.
31 void nrfsetup(){
32   P5OUT|=SS;
33   P5DIR&=~MISO;
34   P5DIR|=MOSI+SCK+SS;
35   
36   
37   //Begin a new transaction.
38   P5OUT&=~SS; 
39   P5OUT|=SS;
40 }
41
42 //! Read and write an NRF byte.
43 u8 nrftrans8(u8 byte){
44   register unsigned int bit;
45   //This function came from the NRF Wikipedia article.
46   //Minor alterations.
47   
48   for (bit = 0; bit < 8; bit++) {
49     /* write MOSI on trailing edge of previous clock */
50     if (byte & 0x80)
51       SETMOSI;
52     else
53       CLRMOSI;
54     byte <<= 1;
55  
56     SETCLK;
57   
58     /* read MISO on trailing edge */
59     byte |= READMISO;
60     CLRCLK;
61   }
62   
63   return byte;
64 }
65
66
67 //! Writes a register
68 u8 nrf_regwrite(u8 reg, const u8 *buf, int len){
69   P5OUT&=~SS;
70   
71   reg=nrftrans8(reg);
72   while(len--)
73     nrftrans8(*buf++);
74   
75   P5OUT|=SS;
76 }
77 //! Reads a register
78 u8 nrf_regread(u8 reg, u8 *buf, int len){
79   P5OUT&=~SS;
80   
81   reg=nrftrans8(reg);
82   while(len--)
83     *buf++=nrftrans8(0);
84   
85   P5OUT|=SS;
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   //Raise !SS to end transaction, just in case we forgot.
95   P5OUT|=SS;
96   nrfsetup();
97     
98   switch(verb){
99     //PEEK and POKE might come later.
100   case READ:  
101   case WRITE:
102     P5OUT&=~SS; //Drop !SS to begin transaction.
103     for(i=0;i<len;i++)
104       cmddata[i]=nrftrans8(cmddata[i]);
105     P5OUT|=SS;  //Raise !SS to end transaction.
106     txdata(app,verb,len);
107     break;
108
109   case PEEK://Grab NRF Register
110     P5OUT&=~SS; //Drop !SS to begin transaction.
111     nrftrans8(NRF_R_REGISTER | cmddata[0]); //000A AAAA
112     for(i=1;i<len;i++)
113       cmddata[i]=nrftrans8(cmddata[i]);
114     P5OUT|=SS;  //Raise !SS to end transaction.
115     txdata(app,verb,len);
116     break;
117     
118   case POKE://Poke NRF Register
119     P5OUT&=~SS; //Drop !SS to begin transaction.
120     nrftrans8(NRF_W_REGISTER | cmddata[0]); //001A AAAA
121     for(i=1;i<len;i++)
122       cmddata[i]=nrftrans8(cmddata[i]);
123     P5OUT|=SS;  //Raise !SS to end transaction.
124     txdata(app,verb,len);
125     break;
126     
127   case SETUP:
128     nrfsetup();
129     txdata(app,verb,0);
130     break;
131   }
132   
133 }