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