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
60 //Begin a new transaction.
65 //! Read and write an CCSPI byte.
66 u8 ccspitrans8(u8 byte){
67 register unsigned int bit;
68 //This function came from the CCSPI Wikipedia article.
71 for (bit = 0; bit < 8; bit++) {
72 /* write MOSI on trailing edge of previous clock */
81 /* read MISO on trailing edge */
91 u8 ccspi_regwrite(u8 reg, const u8 *buf, int len){
102 u8 ccspi_regread(u8 reg, u8 *buf, int len){
105 reg=ccspitrans8(reg);
107 *buf++=ccspitrans8(0);
113 //! Handles a Chipcon SPI command.
114 void ccspi_handle_fn( uint8_t const app,
119 //debugstr("Chipcon SPI handler.");
123 cmddata[0]|=0x40; //Set the read bit.
128 CLRSS; //Drop !SS to begin transaction.
130 cmddata[i]=ccspitrans8(cmddata[i]);
131 SETSS; //Raise !SS to end transaction.
132 txdata(app,verb,len);
140 //Has there been an overflow?
142 //debugstr("Clearing overflow");
144 ccspitrans8(0x08); //SFLUSHRX
150 //Wait for completion.
155 ccspitrans8(CCSPI_RXFIFO | 0x40);
156 //ccspitrans8(0x3F|0x40);
157 cmddata[0]=0xff; //to be replaced with length
158 for(i=0;i<cmddata[0]+2;i++)
159 cmddata[i]=ccspitrans8(0xde);
164 //ccspitrans8(0x08); //SFLUSHRX
168 //Only should transmit length of one more than the reported
169 // length of the frame, which holds the length byte:
170 txdata(app,verb,cmddata[0]+1);
176 debugstr("Can't RX a packet with SFD and FIFOP definitions.");
183 ccspitrans8(CCSPI_SFLUSHRX);
190 #if defined(FIFOP) && defined(SFD) && defined(FIFO) && defined(PLED2DIR) && defined(PLED2PIN) && defined(PLED2OUT)
191 txdata(app,verb,1); //Just sending some response back to client
193 //Wait until a packet is received
195 //Turn on LED 2 (green) as signal
196 PLED2DIR |= PLED2PIN;
197 PLED2OUT &= ~PLED2PIN;
199 //Put radio in TX mode
204 //Load the jamming packet.
205 //Note: attempts to preload this actually slowed the jam time down from 7 to 9 bytes.
207 ccspitrans8(CCSPI_TXFIFO);
208 char pkt[15] = {0x0f, 0x01, 0x08, 0x82, 0xff, 0xff, 0xff, 0xff, 0xde, 0xad, 0xbe, 0xef, 0xba, 0xbe, 0xc0};
209 //char pkt[12] = {0x0c, 0x01, 0x08, 0x82, 0xff, 0xff, 0xff, 0xff, 0xde, 0xad, 0xbe, 0xef};
210 for(i=0;i<pkt[0];i++)
214 //Transmit the packet.
216 ccspitrans8(0x04); //STXON
218 msdelay(100); //Instead of waiting for pulse on SFD
221 ccspitrans8(0x09); //SFLUSHTX
224 //Turn off LED 2 (green) as signal
225 PLED2DIR |= PLED2PIN;
226 PLED2OUT |= PLED2PIN;
228 //TODO the firmware stops staying in this mode after a while, and stops jamming... need to find a fix.
231 debugstr("Can't reflexively jam without SFD, FIFO, FIFOP, and P2LEDx definitions - try using telosb platform.");
235 case CCSPI_REFLEX_AUTOACK:
236 #if defined(FIFOP) && defined(SFD) && defined(FIFO) && defined(PLED2DIR) && defined(PLED2PIN) && defined(PLED2OUT)
237 //txdata(app, verb, 1);
241 //Has there been an overflow in the RX buffer?
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 //Put radio in TX mode
256 //Note: Not doing this slows down jamming, so can't jam short packets.
257 // However, if we do this, it seems to mess up our RXFIFO ability.
261 //Load the jamming packet
263 ccspitrans8(CCSPI_TXFIFO);
264 char pkt[7] = {0x07, 0x01, 0x08, 0xff, 0xff, 0xff, 0xff};
265 for(i=0;i<pkt[0];i++)
268 //Transmit the jamming packet
270 ccspitrans8(0x04); //STXON
272 msdelay(200); //Instead of examining SFD line status
275 ccspitrans8(0x09); //SFLUSHTX
278 //Get the orignally received packet, up to the seqnum field.
280 ccspitrans8(CCSPI_RXFIFO | 0x40);
282 cmddata[i]=ccspitrans8(0xde);
286 ccspitrans8(0x08); //SFLUSHRX
288 //Send the sequence number of the jammed packet back to the client
289 //itoa(cmddata[3], byte, 16);
291 //txdata(app,verb,cmddata[3]);
293 //TODO turn on AUTOCRC for it to apply to the TX???
294 // this may overcome issues of bad crc / length issues?
295 //mdmctrl0 (0x11) register set bit 5 to true.
297 //Create the forged ACK packet
298 cmddata[0] = 6; //length of ack frame plus length
299 cmddata[1] = 0x02; //first byte of FCF
300 cmddata[2] = 0x00; //second byte of FCF
301 //[3] is already filled with the sequence number
305 int q = (crc ^ c) & 15; //Do low-order 4 bits
306 crc = (crc / 16) ^ (q * 4225);
307 q = (crc ^ (c / 16)) & 15; //And high 4 bits
308 crc = (crc / 16) ^ (q * 4225);
310 cmddata[4] = crc & 0xFF;
311 cmddata[5] = (crc >> 8) & 0xFF;
313 for(i=0;i<cmddata[0];i++) {
314 itoa(cmddata[i], byte, 16);
317 //Load the forged ACK packet
319 ccspitrans8(CCSPI_TXFIFO);
320 for(i=0;i<cmddata[0];i++)
321 ccspitrans8(cmddata[i]);
323 //Transmit the forged ACK packet
326 ccspitrans8(0x04); //STXON
328 msdelay(200); //TODO try doing this based on SFD line status instead
331 ccspitrans8(0x09); //SFLUSHTX
334 //TODO disable AUTOCRC here again to go back to promiscous mode
336 //Turn off LED 2 (green) as signal
337 PLED2DIR |= PLED2PIN;
338 PLED2OUT |= PLED2PIN;
340 //TODO the firmware stops staying in this mode after a while, and stops jamming... need to find a fix.
342 debugstr("Can't reflexively jam without SFD, FIFO, FIFOP, and P2LEDx definitions - try using telosb platform.");
350 ccspitrans8(CCSPI_SFLUSHTX);
358 //Wait for last packet to TX.
359 //while(ccspi_status()&BIT3);
363 ccspitrans8(CCSPI_TXFIFO);
364 for(i=0;i<cmddata[0];i++)
365 ccspitrans8(cmddata[i]);
368 //Transmit the packet.
370 ccspitrans8(0x04); //STXON
373 //Wait for the pulse on SFD, after which the packet has been sent.
379 ccspitrans8(0x09); //SFLUSHTX
384 debugstr("Can't TX a packet with SFD and FIFOP definitions.");
389 debugstr("Not yet supported in CCSPI");