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
20 //! Handles a Chipcon SPI command.
21 void ccspi_handle_fn( uint8_t const app,
25 // define the ccspi app's app_t
26 app_t const ccspi_app = {
38 "\tThe CCSPI app adds support for the Chipcon SPI register\n"
39 "\tinterface. Unfortunately, there is very little similarity\n"
40 "\tbetween the CC2420 and the CC2500, to name just two of the\n"
41 "\tmyriad of Chipcon SPI radios. Auto-detection will be a bit\n"
42 "\tdifficult, but more to the point, all high level functionality\n"
43 "\tmust be moved into the client.\n"
46 //! Set up the pins for CCSPI mode.
53 //P4OUT|=BIT5; //activate CC2420 voltage regulator
57 /*P4OUT&=~BIT6; FIXME Does the new code work on Z1 and Telosb?
62 //Begin a new transaction.
67 //! Read and write an CCSPI byte.
68 u8 ccspitrans8(u8 byte){
69 register unsigned int bit;
70 //This function came from the CCSPI Wikipedia article.
73 for (bit = 0; bit < 8; bit++) {
74 /* write MOSI on trailing edge of previous clock */
83 /* read MISO on trailing edge */
92 //! Reflexively jam on the present channel.
93 void ccspireflexjam(u16 delay){
95 #if defined(FIFOP) && defined(SFD) && defined(FIFO) && defined(PLED2DIR) && defined(PLED2PIN) && defined(PLED2OUT)
98 debugstr("Reflex jamming until reset.");
100 txdata(CCSPI,CCSPI_REFLEX,1); //Let the client continue its business.
102 //Wait until a packet is received
104 //Has there been an overflow in the RX buffer?
106 //debugstr("Clearing RX overflow");
108 ccspitrans8(0x08); //SFLUSHRX
112 //Turn on LED 2 (green) as signal
113 PLED2DIR |= PLED2PIN;
114 PLED2OUT &= ~PLED2PIN;
118 //Wait a few us to send it.
121 //Transmit the packet.
127 //Load the next jamming packet.
128 //Note: attempts to preload this actually slowed the jam time down from 7 to 9 bytes.
130 ccspitrans8(CCSPI_TXFIFO);
131 char pkt[5] = {0x05, 0, 0, 0, 0};
132 //char pkt[15] = {0x0f, 0x01, 0x08, 0x82, 0xff, 0xff, 0xff, 0xff, 0xde, 0xad, 0xbe, 0xef, 0xba, 0xbe, 0xc0};
133 //char pkt[12] = {0x0c, 0x01, 0x08, 0x82, 0xff, 0xff, 0xff, 0xff, 0xde, 0xad, 0xbe, 0xef};
134 for(i=0;i<pkt[0];i++)
138 //* I think this might be unnecessary.
139 //msdelay(100+delay); //Instead of waiting for pulse on SFD
143 ccspitrans8(0x09); //SFLUSHTX
147 //Turn off LED 2 (green) as signal
148 PLED2DIR |= PLED2PIN;
149 PLED2OUT |= PLED2PIN;
152 debugstr("Can't reflexively jam without SFD, FIFO, FIFOP, and P2LEDx definitions - try using telosb platform.");
159 //! Writes a register
160 u8 ccspi_regwrite(u8 reg, const u8 *buf, int len){
163 reg=ccspitrans8(reg);
171 u8 ccspi_regread(u8 reg, u8 *buf, int len){
174 reg=ccspitrans8(reg);
176 *buf++=ccspitrans8(0);
182 //! Handles a Chipcon SPI command.
183 void ccspi_handle_fn( uint8_t const app,
189 //debugstr("Chipcon SPI handler.");
193 cmddata[0]|=0x40; //Set the read bit.
198 CLRSS; //Drop !SS to begin transaction.
199 j=cmddata[0];//Backup address.
201 cmddata[i]=ccspitrans8(cmddata[i]);
202 SETSS; //Raise !SS to end transaction.
203 cmddata[0]=j&~0x40;//Restore address.
204 txdata(app,verb,len);
212 //Has there been an overflow?
214 debugstr("Clearing overflow");
216 ccspitrans8(0x08); //SFLUSHRX
218 txdata(app,verb,0); //no packet
224 //Wait for completion.
229 ccspitrans8(CCSPI_RXFIFO | 0x40);
230 //ccspitrans8(0x3F|0x40);
231 cmddata[0]=0xff; //to be replaced with length
234 /* This reads too far on some CC2420 revisions, but on others it
235 works fine. It probably has to do with whether FIFO drops
236 before or after the SPI clocking.
238 A software fix is to reset the CC2420 between packets. This
239 works, but a better solution is desired.
241 //for(i=0;i<cmddata[0]+1;i++)
242 for(i=0;FIFO && i<0x80;i++)
243 cmddata[i]=ccspitrans8(0x00);
246 /* We used to flush the RX buffer after receive. No longer.
248 ccspitrans8(0x08); //SFLUSHRX
252 //Only should transmit length of one more than the reported
253 // length of the frame, which holds the length byte:
254 txdata(app,verb,i&0x7F);
260 debugstr("Can't RX a packet with SFD and FIFOP definitions.");
267 ccspitrans8(CCSPI_SFLUSHRX);
274 ccspireflexjam(len?cmddataword[0]:0);
277 case CCSPI_REFLEX_AUTOACK:
278 #if defined(FIFOP) && defined(SFD) && defined(FIFO) && defined(PLED2DIR) && defined(PLED2PIN) && defined(PLED2OUT)
279 //txdata(app, verb, 1);
283 //Has there been an overflow in the RX buffer?
285 //debugstr("Clearing overflow");
287 ccspitrans8(0x08); //SFLUSHRX
291 //Wait until a packet is received
293 //Turn on LED 2 (green) as signal
294 PLED2DIR |= PLED2PIN;
295 PLED2OUT &= ~PLED2PIN;
297 //Put radio in TX mode
298 //Note: Not doing this slows down jamming, so can't jam short packets.
299 // However, if we do this, it seems to mess up our RXFIFO ability.
303 //Load the jamming packet
305 ccspitrans8(CCSPI_TXFIFO);
306 char pkt[7] = {0x07, 0x01, 0x08, 0xff, 0xff, 0xff, 0xff};
307 for(i=0;i<pkt[0];i++)
310 //Transmit the jamming packet
312 ccspitrans8(0x04); //STXON
314 msdelay(200); //Instead of examining SFD line status
317 ccspitrans8(0x09); //SFLUSHTX
320 //Get the orignally received packet, up to the seqnum field.
322 ccspitrans8(CCSPI_RXFIFO | 0x40);
324 cmddata[i]=ccspitrans8(0xde);
328 ccspitrans8(0x08); //SFLUSHRX
330 //Send the sequence number of the jammed packet back to the client
331 //itoa(cmddata[3], byte, 16);
333 //txdata(app,verb,cmddata[3]);
335 //TODO turn on AUTOCRC for it to apply to the TX???
336 // this may overcome issues of bad crc / length issues?
337 //mdmctrl0 (0x11) register set bit 5 to true.
339 //Create the forged ACK packet
340 cmddata[0] = 6; //length of ack frame plus length
341 cmddata[1] = 0x02; //first byte of FCF
342 cmddata[2] = 0x00; //second byte of FCF
343 //[3] is already filled with the sequence number
347 int q = (crc ^ c) & 15; //Do low-order 4 bits
348 crc = (crc / 16) ^ (q * 4225);
349 q = (crc ^ (c / 16)) & 15; //And high 4 bits
350 crc = (crc / 16) ^ (q * 4225);
352 cmddata[4] = crc & 0xFF;
353 cmddata[5] = (crc >> 8) & 0xFF;
355 for(i=0;i<cmddata[0];i++) {
356 itoa(cmddata[i], byte, 16);
359 //Load the forged ACK packet
361 ccspitrans8(CCSPI_TXFIFO);
362 for(i=0;i<cmddata[0];i++)
363 ccspitrans8(cmddata[i]);
365 //Transmit the forged ACK packet
368 ccspitrans8(0x04); //STXON
370 msdelay(200); //TODO try doing this based on SFD line status instead
373 ccspitrans8(0x09); //SFLUSHTX
376 //TODO disable AUTOCRC here again to go back to promiscous mode
378 //Turn off LED 2 (green) as signal
379 PLED2DIR |= PLED2PIN;
380 PLED2OUT |= PLED2PIN;
382 //TODO the firmware stops staying in this mode after a while, and stops jamming... need to find a fix.
384 debugstr("Can't reflexively jam without SFD, FIFO, FIFOP, and P2LEDx definitions - try using telosb platform.");
392 ccspitrans8(CCSPI_SFLUSHTX);
400 //Wait for last packet to TX.
401 //while(ccspi_status()&BIT3);
405 ccspitrans8(0x09); //SFLUSHTX
411 ccspitrans8(CCSPI_TXFIFO);
412 for(i=0;i<cmddata[0];i++)
413 ccspitrans8(cmddata[i]);
416 //Transmit the packet.
418 ccspitrans8(0x04); //STXON
421 //Wait for the pulse on SFD, after which the packet has been sent.
427 debugstr("Can't TX a packet with SFD and FIFOP definitions.");
432 debugstr("Not yet supported in CCSPI");