This should allow for register inspection, but it doesn't. Is my board mis-soldered?
[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
43 //! Read and write an NRF byte.
44 unsigned char nrftrans8(unsigned char byte){
45   register unsigned int bit;
46   //This function came from the NRF Wikipedia article.
47   //Minor alterations.
48   
49   for (bit = 0; bit < 8; bit++) {
50     /* write MOSI on trailing edge of previous clock */
51     if (byte & 0x80)
52       SETMOSI;
53     else
54       CLRMOSI;
55     byte <<= 1;
56  
57     SETCLK;
58   
59     /* read MISO on trailing edge */
60     byte |= READMISO;
61     CLRCLK;
62   }
63   
64   return byte;
65 }
66
67
68 //! Handles a Nordic RF command.
69 void nrfhandle(unsigned char app,
70                unsigned char verb,
71                unsigned long len){
72   unsigned long i;
73   
74   //Raise !SS to end transaction, just in case we forgot.
75   P5OUT|=SS;
76   nrfsetup();
77     
78   switch(verb){
79     //PEEK and POKE might come later.
80   case READ:
81   case WRITE:
82     P5OUT&=~SS; //Drop !SS to begin transaction.
83     for(i=0;i<len;i++)
84       cmddata[i]=nrftrans8(cmddata[i]);
85     P5OUT|=SS;  //Raise !SS to end transaction.
86     txdata(app,verb,len);
87     break;
88
89   case PEEK://Grab NRF Register
90     P5OUT&=~SS; //Drop !SS to begin transaction.
91     nrftrans8(0|(NRF_R_REGISTER & cmddata[0])); //000A AAAA
92     for(i=1;i<len;i++)
93       cmddata[i]=nrftrans8(cmddata[i]);
94     P5OUT|=SS;  //Raise !SS to end transaction.
95     txdata(app,verb,len);
96     break;
97     
98   case POKE://Poke NRF Register
99     P5OUT&=~SS; //Drop !SS to begin transaction.
100     nrftrans8(0|(NRF_W_REGISTER & cmddata[0])); //001A AAAA
101     for(i=1;i<len;i++)
102       cmddata[i]=nrftrans8(cmddata[i]);
103     P5OUT|=SS;  //Raise !SS to end transaction.
104     txdata(app,verb,len);
105     break;
106     
107   case SETUP:
108     nrfsetup();
109     txdata(app,verb,0);
110     break;
111   }
112   
113 }