d105af547e08ecd6054ce7cfa68a2d86bc8256cf
[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 //This could be more accurate.
19 //Does it ever need to be?
20 #define NRFSPEED 0
21 #define NRFDELAY(x)
22 //delay(x)
23
24
25 //! Set up the pins for NRF mode.
26 void nrfsetup(){
27   P5OUT|=SS;
28   P5DIR|=MOSI+SCK+SS;
29   P5DIR&=~MISO;
30   
31   //Begin a new transaction.
32   P5OUT&=~SS; 
33   P5OUT|=SS;
34 }
35
36
37 //! Read and write an NRF byte.
38 unsigned char nrftrans8(unsigned char byte){
39   register unsigned int bit;
40   //This function came from the NRF Wikipedia article.
41   //Minor alterations.
42   
43   for (bit = 0; bit < 8; bit++) {
44     /* write MOSI on trailing edge of previous clock */
45     if (byte & 0x80)
46       SETMOSI;
47     else
48       CLRMOSI;
49     byte <<= 1;
50  
51     SETCLK;
52   
53     /* read MISO on trailing edge */
54     byte |= READMISO;
55     CLRCLK;
56   }
57   
58   return byte;
59 }
60
61
62 //! Handles a Nordic RF command.
63 void nrfhandle(unsigned char app,
64                unsigned char verb,
65                unsigned long len){
66   unsigned long i;
67   
68   //Raise !SS to end transaction, just in case we forgot.
69   P5OUT|=SS;
70   nrfsetup();
71   
72   debugstr("NRF Handler");
73   
74   switch(verb){
75     //PEEK and POKE might come later.
76   case READ:
77   case WRITE:
78     P5OUT&=~SS; //Drop !SS to begin transaction.
79     for(i=0;i<len;i++)
80       cmddata[i]=nrftrans8(cmddata[i]);
81     P5OUT|=SS;  //Raise !SS to end transaction.
82     txdata(app,verb,len);
83     break;
84
85   case PEEK://Grab NRF Register
86     P5OUT&=~SS; //Drop !SS to begin transaction.
87     nrftrans8(0|(0x1F & cmddata[0])); //000A AAAA
88     for(i=1;i<len;i++)
89       cmddata[i]=nrftrans8(cmddata[i]);
90     P5OUT|=SS;  //Raise !SS to end transaction.
91     txdata(app,verb,0);
92     break;
93     
94   case POKE://Poke NRF Register
95     
96     txdata(app,verb,0);
97     break;
98     
99   case SETUP:
100     nrfsetup();
101     txdata(app,verb,0);
102     break;
103   }
104   
105 }