2 \author Travis Goodspeed
3 \brief Chipcon SPI Register Interface
5 Unfortunately, there is very little similarity between the CC2420
6 and the CC2500, to name just two of the myriad of Chipcon SPI
7 radios. Auto-detection will be a bit difficult, but more to the
8 point, all high level functionality must be moved into the client.
11 //Higher level left to client application.
15 #include <stdlib.h> //added for itoa
23 //! Handles a Chipcon SPI command.
24 void ccspi_handle_fn( uint8_t const app,
28 // define the ccspi app's app_t
29 app_t const ccspi_app = {
41 "\tThe CCSPI app adds support for the Chipcon SPI register\n"
42 "\tinterface. Unfortunately, there is very little similarity\n"
43 "\tbetween the CC2420 and the CC2500, to name just two of the\n"
44 "\tmyriad of Chipcon SPI radios. Auto-detection will be a bit\n"
45 "\tdifficult, but more to the point, all high level functionality\n"
46 "\tmust be moved into the client.\n"
49 //! Set up the pins for CCSPI mode.
56 P4OUT|=BIT5; //activate CC2420 voltage regulator
63 //Begin a new transaction.
68 //! Read and write an CCSPI byte.
69 u8 ccspitrans8(u8 byte){
70 register unsigned int bit;
71 //This function came from the CCSPI Wikipedia article.
74 for (bit = 0; bit < 8; bit++) {
75 /* write MOSI on trailing edge of previous clock */
84 /* read MISO on trailing edge */
94 u8 ccspi_regwrite(u8 reg, const u8 *buf, int len){
105 u8 ccspi_regread(u8 reg, u8 *buf, int len){
108 reg=ccspitrans8(reg);
110 *buf++=ccspitrans8(0);
116 //! Handles a Chipcon SPI command.
117 void ccspi_handle_fn( uint8_t const app,
122 //debugstr("Chipcon SPI handler.");
126 cmddata[0]|=0x40; //Set the read bit.
131 CLRSS; //Drop !SS to begin transaction.
133 cmddata[i]=ccspitrans8(cmddata[i]);
134 SETSS; //Raise !SS to end transaction.
135 txdata(app,verb,len);
143 //Has there been an overflow?
145 debugstr("Clearing overflow");
147 ccspitrans8(0x08); //SFLUSHRX
153 //Wait for completion.
158 ccspitrans8(CCSPI_RXFIFO | 0x40);
159 //ccspitrans8(0x3F|0x40);
160 cmddata[0]=0xff; //to be replaced with length
161 for(i=0;i<cmddata[0]+2;i++)
162 cmddata[i]=ccspitrans8(0xde);
167 ccspitrans8(0x08); //SFLUSHRX
169 //Only should transmit length of one more than the reported
170 // length of the frame, which holds the length byte:
171 txdata(app,verb,cmddata[0]+1);
177 debugstr("Can't RX a packet with SFD and FIFOP definitions.");
184 ccspitrans8(CCSPI_SFLUSHRX);
191 #if defined(FIFOP) && defined(SFD) && defined(FIFO) && defined(PLED2DIR) && defined(PLED2PIN) && defined(PLED2OUT)
192 txdata(app,verb,1); //Just sending some response back to client
194 //Wait until a packet is received
196 //Turn on LED 2 (green) as signal
197 PLED2DIR |= PLED2PIN;
198 PLED2OUT &= ~PLED2PIN;
200 //Put radio in TX mode
205 //Load the jamming packet.
206 //TODO try to preload this to get faster effects
208 ccspitrans8(CCSPI_TXFIFO);
209 char pkt[15] = {0x0f, 0x01, 0x08, 0x82, 0xff, 0xff, 0xff, 0xff, 0xde, 0xad, 0xbe, 0xef, 0xba, 0xbe, 0xc0};
210 //char pkt[12] = {0x0c, 0x01, 0x08, 0x82, 0xff, 0xff, 0xff, 0xff, 0xde, 0xad, 0xbe, 0xef};
211 for(i=0;i<pkt[0];i++)
215 //Transmit the packet.
217 ccspitrans8(0x04); //STXON
219 msdelay(100); //Instead of waiting for pulse on SFD
222 ccspitrans8(0x09); //SFLUSHTX
225 //Turn off LED 2 (green) as signal
226 PLED2DIR |= PLED2PIN;
227 PLED2OUT |= PLED2PIN;
229 //TODO the firmware stops staying in this mode after a while, and stops jamming... need to find a fix.
232 debugstr("Can't reflexively jam without SFD, FIFO, FIFOP, and P2LEDx definitions - try using telosb platform.");
236 case CCSPI_REFLEX_SEQNUM:
237 #if defined(FIFOP) && defined(SFD) && defined(FIFO) && defined(PLED2DIR) && defined(PLED2PIN) && defined(PLED2OUT)
240 //Has there been an overflow in the RX buffer?
241 //TODO do we really need to check this??
243 //debugstr("Clearing overflow");
245 ccspitrans8(0x08); //SFLUSHRX
249 //Wait until a packet is received
251 //Turn on LED 2 (green) as signal
252 PLED2DIR |= PLED2PIN;
253 PLED2OUT &= ~PLED2PIN;
255 //Get the orignally received packet, up to the seqnum field.
257 ccspitrans8(CCSPI_RXFIFO | 0x40);
259 cmddata[i]=ccspitrans8(0xde);
263 ccspitrans8(0x08); //SFLUSHRX
265 //Send the sequence number of the jammed packet back to the client
266 //itoa(cmddata[3], byte, 16);
268 txdata(app,verb,cmddata[3]);
270 //Put radio in TX mode
277 ccspitrans8(CCSPI_TXFIFO);
278 char pkt[12] = {0x0c, 0x01, 0x08, 0x82, 0xff, 0xff, 0xff, 0xff, 0xde, 0xad, 0xbe, 0xef};
279 for(i=0;i<pkt[0];i++)
283 //Transmit the packet.
285 ccspitrans8(0x04); //STXON
287 msdelay(200); //Instead of examining SFD line status
290 ccspitrans8(0x09); //SFLUSHTX
293 //Turn off LED 2 (green) as signal
294 PLED2DIR |= PLED2PIN;
295 PLED2OUT |= PLED2PIN;
297 //TODO the firmware stops staying in this mode after a while, and stops jamming... need to find a fix.
299 debugstr("Can't reflexively jam without SFD, FIFO, FIFOP, and P2LEDx definitions - try using telosb platform.");
307 ccspitrans8(CCSPI_SFLUSHTX);
315 //Wait for last packet to TX.
316 //while(ccspi_status()&BIT3);
320 ccspitrans8(CCSPI_TXFIFO);
321 for(i=0;i<cmddata[0];i++)
322 ccspitrans8(cmddata[i]);
325 //Transmit the packet.
327 ccspitrans8(0x04); //STXON
330 //Wait for the pulse on SFD, after which the packet has been sent.
336 ccspitrans8(0x09); //SFLUSHTX
341 debugstr("Can't TX a packet with SFD and FIFOP definitions.");
346 debugstr("Not yet supported in CCSPI");