Firmware now works with older Chipcon radios.
[goodfet] / firmware / apps / chipcon / chipcon.c
1 /*! \file chipcon.c
2   \author Travis Goodspeed
3   \brief Chipcon 8051 debugging.
4 */
5
6
7 //This is like SPI, except that you read or write, not both.
8
9 /* N.B. The READ verb performs a write of all (any) supplied data,
10    then reads a single byte reply from the target.  The WRITE verb
11    only writes.
12 */
13
14 #include "platform.h"
15 #include "command.h"
16 #include "chipcon.h"
17
18 #include <signal.h>
19 #include <io.h>
20 #include <iomacros.h>
21
22
23 /* Concerning clock rates, the maximimum clock rates are defined on
24    page 4 of the spec.  They vary, but are roughly 30MHz.  Raising
25    this clock rate might allow for clock glitching, but the GoodFET
26    isn't sufficient fast for that.  Perhaps a 200MHz ARM or an FPGA in
27    the BadassFET?
28 */
29
30 //Pins and I/O
31 //MISO and MOSI are the same pin, direction changes.
32 #define RST  BIT0
33 #define MOSI BIT2
34 #define MISO BIT2
35 #define SCK  BIT3
36
37 //This could be more accurate.
38 //Does it ever need to be?
39 #define CCSPEED 3
40 #define CCDELAY(x) delay(x)
41
42 #define SETMOSI P5OUT|=MOSI
43 #define CLRMOSI P5OUT&=~MOSI
44 #define SETCLK P5OUT|=SCK
45 #define CLRCLK P5OUT&=~SCK
46 #define READMISO (P5IN&MISO?1:0)
47
48 #define CCWRITE P5DIR|=MOSI
49 #define CCREAD P5DIR&=~MISO
50
51 //! Set up the pins for CC mode.  Does not init debugger.
52 void ccsetup(){
53   P5OUT|=MOSI+SCK+RST;
54   P5DIR|=MOSI+SCK+RST;
55   //P5DIR&=~MISO;  //MOSI is MISO
56 }
57
58 //! Initialize the debugger
59 void ccdebuginit(){
60   //Two positive debug clock pulses while !RST is low.
61   //Take RST low, pulse twice, then high.
62   P5OUT&=~SCK;
63   P5OUT&=~RST;
64   
65   //pulse twice
66   CCDELAY(CCSPEED);
67   P5OUT|=SCK;  //up
68   CCDELAY(CCSPEED);
69   P5OUT&=~SCK; //down
70   CCDELAY(CCSPEED);
71   P5OUT|=SCK;  //up
72   CCDELAY(CCSPEED);
73   P5OUT&=~SCK; //down
74   
75   //Raise !RST.
76   P5OUT|=RST;
77 }
78
79 //! Read and write a CC bit.
80 unsigned char cctrans8(unsigned char byte){
81   unsigned int bit;
82   //This function came from the SPI Wikipedia article.
83   //Minor alterations.
84   
85   for (bit = 0; bit < 8; bit++) {
86     /* write MOSI on trailing edge of previous clock */
87     if (byte & 0x80)
88       SETMOSI;
89     else
90       CLRMOSI;
91     byte <<= 1;
92  
93     /* half a clock cycle before leading/rising edge */
94     CCDELAY(CCSPEED/2);
95     SETCLK;
96  
97     /* half a clock cycle before trailing/falling edge */
98     CCDELAY(CCSPEED/2);
99  
100     /* read MISO on trailing edge */
101     byte |= READMISO;
102     CLRCLK;
103   }
104   
105   return byte;
106 }
107
108 //! Send a command from txbytes.
109 void cccmd(unsigned char len){
110   unsigned char i;
111   CCWRITE;
112   for(i=0;i<len;i++)
113     cctrans8(cmddata[i]);
114 }
115
116 //! Fetch a reply, usually 1 byte.
117 void ccread(unsigned char len){
118   unsigned char i;
119   CCREAD;
120   for(i=0;i<len;i++)
121     cmddata[i]=cctrans8(0);
122 }
123
124 //! Handles a monitor command.
125 void cchandle(unsigned char app,
126                unsigned char verb,
127                unsigned long len){
128   //Always init.  Might help with buggy lines.
129   //Might hurt too.
130   //ccdebuginit();
131   long i;
132   
133   switch(verb){
134     //CC_PEEK and CC_POKE will come later.
135   case PEEK:
136     cmddata[0]=cc_peekirambyte(cmddata[0]);
137     txdata(app,verb,1);
138     break;
139   case POKE:
140     cmddata[0]=cc_pokeirambyte(cmddata[0],cmddata[1]);
141     txdata(app,verb,0);
142     break;
143   case READ:  //Write a command and return 1-byte reply.
144     cccmd(len);
145     if(cmddata[0]&0x4)
146       ccread(1);
147     txdata(app,verb,1);
148     
149     break;
150   case WRITE: //Write a command with no reply.
151     cccmd(len);
152     txdata(app,verb,0);
153     break;
154   case START://enter debugger
155     ccsetup();
156     ccdebuginit();
157     txdata(app,verb,0);
158     break;
159   case STOP://exit debugger
160     //Take RST low, then high.
161     P5OUT&=~RST;
162     CCDELAY(CCSPEED);
163     P5OUT|=RST;
164     txdata(app,verb,0);
165     break;
166   case SETUP:
167     ccsetup();
168     txdata(app,verb,0);
169     break;
170     
171   //Micro commands!
172   case CC_CHIP_ERASE:
173     cc_chip_erase();
174     txdata(app,verb,1);
175     break;
176   case CC_WR_CONFIG:
177     cc_wr_config(cmddata[0]);
178     txdata(app,verb,1);
179     break;
180   case CC_RD_CONFIG:
181     cc_rd_config();
182     txdata(app,verb,1);
183     break;
184   case CC_GET_PC:
185     cc_get_pc();
186     txdata(app,verb,2);
187     break;
188   case CC_LOCKCHIP:
189     cc_lockchip();
190     //no break, return status
191   case CC_READ_STATUS:
192     cc_read_status();
193     txdata(app,verb,1);
194     break;
195   case CC_SET_HW_BRKPNT:
196     cc_set_hw_brkpnt(cmddataword[0]);
197     txdata(app,verb,1);
198     break;
199   case CC_HALT:
200     cc_halt();
201     txdata(app,verb,1);
202     break;
203   case CC_RESUME:
204     cc_resume();
205     txdata(app,verb,1);
206     break;
207   case CC_DEBUG_INSTR:
208     cc_debug_instr(len);
209     txdata(app,verb,1);
210     break;
211   case CC_STEP_INSTR:
212     cc_step_instr();
213     txdata(app,verb,1);
214     break;
215   case CC_STEP_REPLACE:
216     txdata(app,NOK,0);//TODO add me
217     break;
218   case CC_GET_CHIP_ID:
219     cmddataword[0]=cc_get_chip_id();
220     txdata(app,verb,2);
221     break;
222
223
224   //Macro commands
225   case CC_READ_CODE_MEMORY:
226     cmddata[0]=cc_peekcodebyte(cmddataword[0]);
227     txdata(app,verb,1);
228     break;
229   case CC_READ_XDATA_MEMORY:
230     cmddata[0]=cc_peekdatabyte(cmddataword[0]);
231     txdata(app,verb,1);
232     break;
233   case CC_WRITE_XDATA_MEMORY:
234     cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
235     txdata(app,verb,1);
236     break;
237   case CC_SET_PC:
238     cc_set_pc(cmddatalong[0]);
239     txdata(app,verb,0);
240     break;
241   case CC_WRITE_FLASH_PAGE:
242     cc_write_flash_page(cmddatalong[0]);
243     txdata(app,verb,0);
244     break;
245   case CC_WIPEFLASHBUFFER:
246     for(i=0xf000;i<0xf800;i++)
247       cc_pokedatabyte(i,0xFF);
248     txdata(app,verb,0);
249     break;
250   case CC_MASS_ERASE_FLASH:
251   case CC_CLOCK_INIT:
252   case CC_PROGRAM_FLASH:
253     debugstr("This Chipcon command is not yet implemented.");
254     txdata(app,NOK,0);//TODO implement me.
255     break;
256   }
257 }
258
259 //! Set the Chipcon's Program Counter
260 void cc_set_pc(u32 adr){
261   cmddata[0]=0x02;             //SetPC
262   cmddata[1]=((adr>>8)&0xff);  //HIBYTE
263   cmddata[2]=adr&0xff;         //LOBYTE
264   cc_debug_instr(3);
265   return;
266 }
267
268 //! Erase all of a Chipcon's memory.
269 void cc_chip_erase(){
270   cmddata[0]=CCCMD_CHIP_ERASE; //0x14
271   cccmd(1);
272   ccread(1);
273 }
274 //! Write the configuration byte.
275 void cc_wr_config(unsigned char config){
276   cmddata[0]=CCCMD_WR_CONFIG; //0x1D
277   cmddata[1]=config;
278   cccmd(2);
279   ccread(1);
280 }
281
282 //! Locks the chip.
283 void cc_lockchip(){
284   register int i;
285   
286   debugstr("Locking chip.");
287   cc_wr_config(1);//Select Info Flash 
288   if(!(cc_rd_config()&1))
289     debugstr("Config forgotten!");
290   
291   //Clear config page.
292   for(i=0;i<2048;i++)
293     cc_pokedatabyte(0xf000+i,0);
294   cc_write_flash_page(0);
295   if(cc_peekcodebyte(0))
296     debugstr("Failed to clear info flash byte.");
297   
298   cc_wr_config(0);  
299   if(cc_rd_config()&1)
300     debugstr("Stuck in info flash mode!");
301 }
302
303 //! Read the configuration byte.
304 unsigned char cc_rd_config(){
305   cmddata[0]=CCCMD_RD_CONFIG; //0x24
306   cccmd(1);
307   ccread(1);
308   return cmddata[0];
309 }
310
311
312 //! Read the status register
313 unsigned char cc_read_status(){
314   cmddata[0]=CCCMD_READ_STATUS; //0x3f
315   cccmd(1);
316   ccread(1);
317   return cmddata[0];
318 }
319
320 //! Read the CHIP ID bytes.
321 unsigned short cc_get_chip_id(){
322   unsigned short toret;
323   cmddata[0]=CCCMD_GET_CHIP_ID; //0x68
324   cccmd(1);
325   ccread(2);
326
327   
328   //Find the flash word size.
329   switch(cmddata[0]){
330   case 0x01://CC1110
331   case 0x81://CC2510
332   case 0x91://CC2511
333     flash_word_size=0x02;
334     //debugstr("2 bytes/flash word");
335     break;
336   default:
337     flash_word_size=0x04;
338     break;
339     //debugstr("Warning: Guessing flash word size.");
340   case 0x85://CC2430
341   case 0x89://CC2431
342     debugstr("4 bytes/flash word");
343     break;
344   }
345   
346   //Return the word.
347   return cmddataword[0];
348 }
349
350 //! Populates flash buffer in xdata.
351 void cc_write_flash_buffer(u8 *data, u16 len){
352   cc_write_xdata(0xf000, data, len);
353 }
354 //! Populates flash buffer in xdata.
355 void cc_write_xdata(u16 adr, u8 *data, u16 len){
356   u16 i;
357   for(i=0; i<len; i++){
358     cc_pokedatabyte(adr+i,
359                     data[i]);
360   }
361 }
362
363
364 //32-bit words, 2KB pages
365 //0x20 0x00 for CC2430, CC1110
366 #define HIBYTE_WORDS_PER_FLASH_PAGE 0x02
367 #define LOBYTE_WORDS_PER_FLASH_PAGE 0x00
368
369 /** Ugh, this varies by chip.
370     0x800 for CC2430
371     0x400 for CC1110
372 */
373 //#define FLASHPAGE_SIZE 0x400
374 #define MAXFLASHPAGE_SIZE 0x800
375 #define MINFLASHPAGE_SIZE 0x400
376
377
378 //32 bit words on CC2430
379 //16 bit words on CC1110
380 //#define FLASH_WORD_SIZE 0x2
381 u8 flash_word_size = 0; //0x02;
382
383
384 /* Flash Write Timing
385    MHZ | FWT (0xAB)
386    12  | 0x10
387    13  | 0x11
388    16  | 0x15
389    24  | 0x20
390    26  | 0x23  (IM ME)
391    32  | 0x2A  (Modula.si)
392 */
393 //#define FWT 0x23
394
395 const u8 flash_routine[] = {
396   //0:
397   //MOV FADDRH, #imm; 
398   0x75, 0xAD,
399   0x00,//#imm=((address >> 8) / FLASH_WORD_SIZE) & 0x7E,
400   
401   //0x75, 0xAB, 0x23, //Set FWT per clock
402   0x75, 0xAC, 0x00,                                          //                 MOV FADDRL, #00; 
403   /* Erase page. */
404   0x75, 0xAE, 0x01,                                          //                 MOV FLC, #01H; // ERASE 
405                                                              //                 ; Wait for flash erase to complete 
406   0xE5, 0xAE,                                                // eraseWaitLoop:  MOV A, FLC; 
407   0x20, 0xE7, 0xFB,                                          //                 JB ACC_BUSY, eraseWaitLoop; 
408   
409   /* End erase page. */
410                                                              //                 ; Initialize the data pointer 
411   0x90, 0xF0, 0x00,                                          //                 MOV DPTR, #0F000H; 
412                                                              //                 ; Outer loops 
413   0x7F, HIBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R7, #imm; 
414   0x7E, LOBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R6, #imm; 
415   0x75, 0xAE, 0x02,                                          //                 MOV FLC, #02H; // WRITE 
416                                                              //                     ; Inner loops 
417   //24:
418   0x7D, 0xde /*FLASH_WORD_SIZE*/,                                     // writeLoop:          MOV R5, #imm; 
419   0xE0,                                                      // writeWordLoop:          MOVX A, @DPTR; 
420   0xA3,                                                      //                         INC DPTR; 
421   0xF5, 0xAF,                                                //                         MOV FWDATA, A;  
422   0xDD, 0xFA,                                                //                     DJNZ R5, writeWordLoop; 
423                                                              //                     ; Wait for completion 
424   0xE5, 0xAE,                                                // writeWaitLoop:      MOV A, FLC; 
425   0x20, 0xE6, 0xFB,                                          //                     JB ACC_SWBSY, writeWaitLoop; 
426   0xDE, 0xF1,                                                //                 DJNZ R6, writeLoop; 
427   0xDF, 0xEF,                                                //                 DJNZ R7, writeLoop; 
428                                                              //                 ; Done, fake a breakpoint 
429   0xA5                                                       //                 DB 0xA5; 
430 };
431
432
433 //! Copies flash buffer to flash.
434 void cc_write_flash_page(u32 adr){
435   //Assumes that page has already been written to XDATA 0xF000
436   //debugstr("Flashing 2kb at 0xF000 to given adr.");
437   
438   if(adr&(MINFLASHPAGE_SIZE-1)){
439     debugstr("Flash page address is not on a page boundary.  Aborting.");
440     return;
441   }
442   
443   //Routine comes next
444   //WRITE_XDATA_MEMORY(IN: 0xF000 + FLASH_PAGE_SIZE, sizeof(routine), routine);
445   cc_write_xdata(0xF000+MAXFLASHPAGE_SIZE,
446                  (u8*) flash_routine, sizeof(flash_routine));
447   //Patch routine's third byte with
448   //((address >> 8) / FLASH_WORD_SIZE) & 0x7E
449   cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+2,
450                   ((adr>>8)/flash_word_size)&0x7E);
451   //Patch routine to define FLASH_WORD_SIZE
452   if(flash_routine[25]!=0xde)
453     debugstr("Ugly patching code failing in chipcon.c");
454   cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+25,
455                   flash_word_size);
456   
457   //debugstr("Wrote flash routine.");
458   
459   
460   //MOV MEMCTR, (bank * 16) + 1;
461   cmddata[0]=0x75;
462   cmddata[1]=0xc7;
463   cmddata[2]=0x51;
464   cc_debug_instr(3);
465   debugstr("Loaded bank info.");
466   
467   cc_set_pc(0xf000+MAXFLASHPAGE_SIZE);//execute code fragment
468   cc_resume();
469   
470   debugstr("Executing.");
471   
472   
473   while(!(cc_read_status()&CC_STATUS_CPUHALTED)){
474     P1OUT^=1;//blink LED while flashing
475   }
476   
477   
478   debugstr("Done flashing.");
479   
480   P1OUT&=~1;//clear LED
481 }
482
483 //! Read the PC
484 unsigned short cc_get_pc(){
485   cmddata[0]=CCCMD_GET_PC; //0x28
486   cccmd(1);
487   ccread(2);
488   
489   //Return the word.
490   return cmddataword[0];
491 }
492
493 //! Set a hardware breakpoint.
494 void cc_set_hw_brkpnt(unsigned short adr){
495   debugstr("FIXME: This certainly won't work.");
496   cmddataword[0]=adr;
497   cccmd(2);
498   ccread(1);
499   return;
500 }
501
502
503 //! Halt the CPU.
504 void cc_halt(){
505   cmddata[0]=CCCMD_HALT; //0x44
506   cccmd(1);
507   ccread(1);
508   return;
509 }
510 //! Resume the CPU.
511 void cc_resume(){
512   cmddata[0]=CCCMD_RESUME; //0x4C
513   cccmd(1);
514   ccread(1);
515   return;
516 }
517
518
519 //! Step an instruction
520 void cc_step_instr(){
521   cmddata[0]=CCCMD_STEP_INSTR; //0x5C
522   cccmd(1);
523   ccread(1);
524   return;
525 }
526
527 //! Debug an instruction.
528 void cc_debug_instr(unsigned char len){
529   //Bottom two bits of command indicate length.
530   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3); //0x54+len
531   CCWRITE;
532   cctrans8(cmd);  //Second command code
533   cccmd(len&0x3); //Command itself.
534   ccread(1);
535   return;
536 }
537
538 //! Debug an instruction, for local use.
539 unsigned char cc_debug(unsigned char len,
540               unsigned char a,
541               unsigned char b,
542               unsigned char c){
543   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3);//0x54+len
544   CCWRITE;
545   cctrans8(cmd);
546   if(len>0)
547     cctrans8(a);
548   if(len>1)
549     cctrans8(b);
550   if(len>2)
551     cctrans8(c);
552   CCREAD;
553   return cctrans8(0x00);
554 }
555
556 //! Fetch a byte of code memory.
557 unsigned char cc_peekcodebyte(unsigned long adr){
558   /** See page 9 of SWRA124 */
559   unsigned char bank=adr>>15,
560     lb=adr&0xFF,
561     hb=(adr>>8)&0x7F,
562     toret=0;
563   adr&=0x7FFF;
564   
565   //MOV MEMCTR, (bank*16)+1
566   cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
567   //MOV DPTR, address
568   cc_debug(3, 0x90, hb, lb);
569   
570   //for each byte
571   //CLR A
572   cc_debug(2, 0xE4, 0, 0);
573   //MOVC A, @A+DPTR;
574   toret=cc_debug(3, 0x93, 0, 0);
575   //INC DPTR
576   //cc_debug(1, 0xA3, 0, 0);
577   
578   return toret;
579 }
580
581
582 //! Set a byte of data memory.
583 unsigned char cc_pokedatabyte(unsigned int adr,
584                            unsigned char val){
585   unsigned char
586     hb=(adr&0xFF00)>>8,
587     lb=adr&0xFF;
588   
589   //MOV DPTR, adr
590   cc_debug(3, 0x90, hb, lb);
591   //MOV A, val
592   cc_debug(2, 0x74, val, 0);
593   //MOVX @DPTR, A
594   cc_debug(1, 0xF0, 0, 0);
595   
596   return 0;
597   /*
598 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
599 for (n = 0; n < count; n++) {
600     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
601     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
602     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
603 }
604    */
605 }
606
607 //! Fetch a byte of data memory.
608 unsigned char cc_peekdatabyte(unsigned int adr){
609   unsigned char
610     hb=(adr&0xFF00)>>8,
611     lb=adr&0xFF;
612   
613   //MOV DPTR, adr
614   cc_debug(3, 0x90, hb, lb);
615   //MOVX A, @DPTR
616   //Must be 2, perhaps for clocking?
617   return cc_debug(3, 0xE0, 0, 0);
618 }
619
620
621 //! Fetch a byte of IRAM.
622 u8 cc_peekirambyte(u8 adr){
623   //CLR A
624   cc_debug(2, 0xE4, 0, 0);
625   //MOV A, #iram
626   return cc_debug(3, 0xE5, adr, 0);
627 }
628
629 //! Write a byte of IRAM.
630 u8 cc_pokeirambyte(u8 adr, u8 val){
631   //CLR A
632   cc_debug(2, 0xE4, 0, 0);
633   //MOV #iram, #val
634   return cc_debug(3, 0x75, adr, val);
635   //return cc_debug(3, 0x75, val, adr);
636 }
637
638