Forked SPI to NRF.
[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
63
64 //! Handles a Nordic RF command.
65 void nrfhandle(unsigned char app,
66                unsigned char verb,
67                unsigned long len){
68   unsigned long i;
69   
70   //Raise !SS to end transaction, just in case we forgot.
71   P5OUT|=SS;
72   nrfsetup();
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
86
87   case PEEK://Grab NRF Register
88     txdata(app,verb,0);
89     break;
90     
91   case POKE://Poke NRF Register
92     
93     txdata(app,verb,0);
94     break;
95     
96   case SETUP:
97     nrfsetup();
98     txdata(app,verb,0);
99     break;
100   }
101   
102 }