Bit of rearranging of Chipcon support.
[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 #define CCDELAY(x) 
42
43 #define SETMOSI P5OUT|=MOSI
44 #define CLRMOSI P5OUT&=~MOSI
45 #define SETCLK P5OUT|=SCK
46 #define CLRCLK P5OUT&=~SCK
47 #define READMISO (P5IN&MISO?1:0)
48
49 #define CCWRITE P5DIR|=MOSI
50 #define CCREAD P5DIR&=~MISO
51
52 //! Set up the pins for CC mode.  Does not init debugger.
53 void ccsetup(){
54   P5OUT|=MOSI+SCK+RST;
55   P5DIR|=MOSI+SCK+RST;
56   //P5REN=0xFF;
57 }
58
59 //! Initialize the debugger
60 void ccdebuginit(){
61   //Port output BUT NOT DIRECTION is set at start.
62   P5OUT|=MOSI+SCK+RST;
63   
64   delay(30); //So the beginning is ready for glitching.
65   
66   //Two positive debug clock pulses while !RST is low.
67   //Take RST low, pulse twice, then high.
68   P5OUT&=~SCK;
69   P5OUT&=~RST;
70   
71   //Two rising edges.
72   P5OUT^=SCK; //up
73   P5OUT^=SCK; //down
74   P5OUT^=SCK; //up
75   P5OUT^=SCK; //Unnecessary.
76   
77   
78   //Raise !RST.
79   P5OUT|=RST;
80 }
81
82 //! Read and write a CC bit.
83 unsigned char cctrans8(unsigned char byte){
84   unsigned int bit;
85   //This function came from the SPI Wikipedia article.
86   //Minor alterations.
87   
88   for (bit = 0; bit < 8; bit++) {
89     /* write MOSI on trailing edge of previous clock */
90     if (byte & 0x80)
91       SETMOSI;
92     else
93       CLRMOSI;
94     byte <<= 1;
95  
96     /* half a clock cycle before leading/rising edge */
97     CCDELAY(CCSPEED/2);
98     SETCLK;
99  
100     /* half a clock cycle before trailing/falling edge */
101     CCDELAY(CCSPEED/2);
102  
103     /* read MISO on trailing edge */
104     byte |= READMISO;
105     CLRCLK;
106   }
107   
108   return byte;
109 }
110
111 //! Send a command from txbytes.
112 void cccmd(unsigned char len){
113   unsigned char i;
114   CCWRITE;
115   for(i=0;i<len;i++)
116     cctrans8(cmddata[i]);
117 }
118
119 //! Fetch a reply, usually 1 byte.
120 void ccread(unsigned char len){
121   unsigned char i;
122   CCREAD;
123   for(i=0;i<len;i++)
124     cmddata[i]=cctrans8(0);
125 }
126
127 //! Handles a monitor command.
128 void cchandle(unsigned char app,
129                unsigned char verb,
130                unsigned long len){
131   //Always init.  Might help with buggy lines.
132   //Might hurt too.
133   //ccdebuginit();
134   long i;
135   
136   switch(verb){
137     //CC_PEEK and CC_POKE will come later.
138   case PEEK:
139     cmddata[0]=cc_peekirambyte(cmddata[0]);
140     txdata(app,verb,1);
141     break;
142   case POKE:
143     cmddata[0]=cc_pokeirambyte(cmddata[0],cmddata[1]);
144     txdata(app,verb,0);
145     break;
146   case READ:  //Write a command and return 1-byte reply.
147     cccmd(len);
148     if(cmddata[0]&0x4)
149       ccread(1);
150     txdata(app,verb,1);
151     break;
152   case WRITE: //Write a command with no reply.
153     cccmd(len);
154     txdata(app,verb,0);
155     break;
156   case START://enter debugger
157     //ccsetup(); //interferes with glitching
158     ccdebuginit();
159     txdata(app,verb,0);
160     break;
161   case STOP://exit debugger
162     //Take RST low, then high.
163     P5OUT&=~RST;
164     CCDELAY(CCSPEED);
165     P5OUT|=RST;
166     txdata(app,verb,0);
167     break;
168   case SETUP:
169     ccsetup();
170     txdata(app,verb,0);
171     break;
172     
173   //Micro commands!
174   case CC_CHIP_ERASE:
175     cc_chip_erase();
176     txdata(app,verb,1);
177     break;
178   case CC_WR_CONFIG:
179     cc_wr_config(cmddata[0]);
180     txdata(app,verb,1);
181     break;
182   case CC_RD_CONFIG:
183     cc_rd_config();
184     txdata(app,verb,1);
185     break;
186   case CC_GET_PC:
187     cc_get_pc();
188     txdata(app,verb,2);
189     break;
190   case CC_LOCKCHIP:
191     cc_lockchip();
192     //no break, return status
193   case CC_READ_STATUS:
194     cc_read_status();
195     txdata(app,verb,1);
196     break;
197   case CC_SET_HW_BRKPNT:
198     cc_set_hw_brkpnt(cmddataword[0]);
199     txdata(app,verb,1);
200     break;
201   case CC_HALT:
202     cc_halt();
203     txdata(app,verb,1);
204     break;
205   case CC_RESUME:
206     cc_resume();
207     txdata(app,verb,1);
208     break;
209   case CC_DEBUG_INSTR:
210     cc_debug_instr(len);
211     txdata(app,verb,1);
212     break;
213   case CC_STEP_INSTR:
214     cc_step_instr();
215     txdata(app,verb,1);
216     break;
217   case CC_STEP_REPLACE:
218     txdata(app,NOK,0);//TODO add me
219     break;
220   case CC_GET_CHIP_ID:
221     cmddataword[0]=cc_get_chip_id();
222     txdata(app,verb,2);
223     break;
224
225
226   //Macro commands
227   case CC_READ_CODE_MEMORY:
228     cmddata[0]=cc_peekcodebyte(cmddataword[0]);
229     txdata(app,verb,1);
230     break;
231   case CC_READ_XDATA_MEMORY:
232     cmddata[0]=cc_peekdatabyte(cmddataword[0]);
233     txdata(app,verb,1);
234     break;
235   case CC_WRITE_XDATA_MEMORY:
236     cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
237     txdata(app,verb,1);
238     break;
239   case CC_SET_PC:
240     cc_set_pc(cmddatalong[0]);
241     txdata(app,verb,0);
242     break;
243   case CC_WRITE_FLASH_PAGE:
244     cc_write_flash_page(cmddatalong[0]);
245     txdata(app,verb,0);
246     break;
247   case CC_WIPEFLASHBUFFER:
248     for(i=0xf000;i<0xf800;i++)
249       cc_pokedatabyte(i,0xFF);
250     txdata(app,verb,0);
251     break;
252   case CC_MASS_ERASE_FLASH:
253   case CC_CLOCK_INIT:
254   case CC_PROGRAM_FLASH:
255     debugstr("This Chipcon command is not yet implemented.");
256     txdata(app,NOK,0);//TODO implement me.
257     break;
258   }
259 }
260
261 //! Set the Chipcon's Program Counter
262 void cc_set_pc(u32 adr){
263   cmddata[0]=0x02;             //SetPC
264   cmddata[1]=((adr>>8)&0xff);  //HIBYTE
265   cmddata[2]=adr&0xff;         //LOBYTE
266   cc_debug_instr(3);
267   return;
268 }
269
270 //! Erase all of a Chipcon's memory.
271 void cc_chip_erase(){
272   cmddata[0]=CCCMD_CHIP_ERASE; //0x14
273   cccmd(1);
274   ccread(1);
275 }
276 //! Write the configuration byte.
277 void cc_wr_config(unsigned char config){
278   cmddata[0]=CCCMD_WR_CONFIG; //0x1D
279   cmddata[1]=config;
280   cccmd(2);
281   ccread(1);
282 }
283
284 //! Locks the chip.
285 void cc_lockchip(){
286   register int i;
287   
288   //debugstr("Locking chip.");
289   cc_wr_config(1);//Select Info Flash 
290   if(!(cc_rd_config()&1))
291     debugstr("Config forgotten!");
292   
293   //Clear config page.
294   for(i=0;i<2048;i++)
295     cc_pokedatabyte(0xf000+i,0);
296   cc_write_flash_page(0);
297   if(cc_peekcodebyte(0))
298     debugstr("Failed to clear info flash byte.");
299   
300   cc_wr_config(0);  
301   if(cc_rd_config()&1)
302     debugstr("Stuck in info flash mode!");
303 }
304
305 //! Read the configuration byte.
306 unsigned char cc_rd_config(){
307   cmddata[0]=CCCMD_RD_CONFIG; //0x24
308   cccmd(1);
309   ccread(1);
310   return cmddata[0];
311 }
312
313
314 //! Read the status register
315 unsigned char cc_read_status(){
316   cmddata[0]=CCCMD_READ_STATUS; //0x3f
317   cccmd(1);
318   ccread(1);
319   return cmddata[0];
320 }
321
322 //! Read the CHIP ID bytes.
323 unsigned short cc_get_chip_id(){
324   cmddata[0]=CCCMD_GET_CHIP_ID; //0x68
325   cccmd(1);
326   ccread(2);
327
328   
329   //Find the flash word size.
330   switch(cmddata[0]){
331   case 0x01://CC1110
332   case 0x81://CC2510
333   case 0x91://CC2511
334     flash_word_size=0x02;
335     //debugstr("2 bytes/flash word");
336     break;
337   default:
338     debugstr("Warning: Guessing flash word size.");
339   case 0x85://CC2430
340   case 0x89://CC2431
341     //debugstr("4 bytes/flash word");
342     flash_word_size=0x04;
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   if(flash_word_size==0){
444     debugstr("Flash word size is wrong.");
445     while(1);
446   }
447   
448   //Routine comes next
449   //WRITE_XDATA_MEMORY(IN: 0xF000 + FLASH_PAGE_SIZE, sizeof(routine), routine);
450   cc_write_xdata(0xF000+MAXFLASHPAGE_SIZE,
451                  (u8*) flash_routine, sizeof(flash_routine));
452   //Patch routine's third byte with
453   //((address >> 8) / FLASH_WORD_SIZE) & 0x7E
454   cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+2,
455                   ((adr>>8)/flash_word_size)&0x7E);
456   //Patch routine to define FLASH_WORD_SIZE
457   if(flash_routine[25]!=0xde)
458     debugstr("Ugly patching code failing in chipcon.c");
459   cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+25,
460                   flash_word_size);
461   
462   //debugstr("Wrote flash routine.");
463   
464   
465   //MOV MEMCTR, (bank * 16) + 1;
466   cmddata[0]=0x75;
467   cmddata[1]=0xc7;
468   cmddata[2]=0x51;
469   cc_debug_instr(3);
470   //debugstr("Loaded bank info.");
471   
472   cc_set_pc(0xf000+MAXFLASHPAGE_SIZE);//execute code fragment
473   cc_resume();
474   
475   //debugstr("Executing.");
476   
477   
478   while(!(cc_read_status()&CC_STATUS_CPUHALTED)){
479     P1OUT^=1;//blink LED while flashing
480   }
481   
482   
483   //debugstr("Done flashing.");
484   
485   P1OUT&=~1;//clear LED
486 }
487
488 //! Read the PC
489 unsigned short cc_get_pc(){
490   cmddata[0]=CCCMD_GET_PC; //0x28
491   cccmd(1);
492   ccread(2);
493   
494   //Return the word.
495   return cmddataword[0];
496 }
497
498 //! Set a hardware breakpoint.
499 void cc_set_hw_brkpnt(unsigned short adr){
500   debugstr("FIXME: This certainly won't work.");
501   cmddataword[0]=adr;
502   cccmd(2);
503   ccread(1);
504   return;
505 }
506
507
508 //! Halt the CPU.
509 void cc_halt(){
510   cmddata[0]=CCCMD_HALT; //0x44
511   cccmd(1);
512   ccread(1);
513   return;
514 }
515 //! Resume the CPU.
516 void cc_resume(){
517   cmddata[0]=CCCMD_RESUME; //0x4C
518   cccmd(1);
519   ccread(1);
520   return;
521 }
522
523
524 //! Step an instruction
525 void cc_step_instr(){
526   cmddata[0]=CCCMD_STEP_INSTR; //0x5C
527   cccmd(1);
528   ccread(1);
529   return;
530 }
531
532 //! Debug an instruction.
533 void cc_debug_instr(unsigned char len){
534   //Bottom two bits of command indicate length.
535   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3); //0x54+len
536   CCWRITE;
537   cctrans8(cmd);  //Second command code
538   cccmd(len&0x3); //Command itself.
539   ccread(1);
540   return;
541 }
542
543 //! Debug an instruction, for local use.
544 unsigned char cc_debug(unsigned char len,
545               unsigned char a,
546               unsigned char b,
547               unsigned char c){
548   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3);//0x54+len
549   CCWRITE;
550   cctrans8(cmd);
551   if(len>0)
552     cctrans8(a);
553   if(len>1)
554     cctrans8(b);
555   if(len>2)
556     cctrans8(c);
557   CCREAD;
558   return cctrans8(0x00);
559 }
560
561 //! Fetch a byte of code memory.
562 unsigned char cc_peekcodebyte(unsigned long adr){
563   /** See page 9 of SWRA124 */
564   unsigned char bank=adr>>15,
565     lb=adr&0xFF,
566     hb=(adr>>8)&0x7F,
567     toret=0;
568   adr&=0x7FFF;
569   
570   //MOV MEMCTR, (bank*16)+1
571   cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
572   //MOV DPTR, address
573   cc_debug(3, 0x90, hb, lb);
574   
575   //for each byte
576   //CLR A
577   cc_debug(2, 0xE4, 0, 0);
578   //MOVC A, @A+DPTR;
579   toret=cc_debug(3, 0x93, 0, 0);
580   //INC DPTR
581   //cc_debug(1, 0xA3, 0, 0);
582   
583   return toret;
584 }
585
586
587 //! Set a byte of data memory.
588 unsigned char cc_pokedatabyte(unsigned int adr,
589                            unsigned char val){
590   unsigned char
591     hb=(adr&0xFF00)>>8,
592     lb=adr&0xFF;
593   
594   //MOV DPTR, adr
595   cc_debug(3, 0x90, hb, lb);
596   //MOV A, val
597   cc_debug(2, 0x74, val, 0);
598   //MOVX @DPTR, A
599   cc_debug(1, 0xF0, 0, 0);
600   
601   return 0;
602   /*
603 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
604 for (n = 0; n < count; n++) {
605     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
606     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
607     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
608 }
609    */
610 }
611
612 //! Fetch a byte of data memory.
613 unsigned char cc_peekdatabyte(unsigned int adr){
614   unsigned char
615     hb=(adr&0xFF00)>>8,
616     lb=adr&0xFF;
617   
618   //MOV DPTR, adr
619   cc_debug(3, 0x90, hb, lb);
620   //MOVX A, @DPTR
621   //Must be 2, perhaps for clocking?
622   return cc_debug(3, 0xE0, 0, 0);
623 }
624
625
626 //! Fetch a byte of IRAM.
627 u8 cc_peekirambyte(u8 adr){
628   //CLR A
629   cc_debug(2, 0xE4, 0, 0);
630   //MOV A, #iram
631   return cc_debug(3, 0xE5, adr, 0);
632 }
633
634 //! Write a byte of IRAM.
635 u8 cc_pokeirambyte(u8 adr, u8 val){
636   //CLR A
637   cc_debug(2, 0xE4, 0, 0);
638   //MOV #iram, #val
639   return cc_debug(3, 0x75, adr, val);
640   //return cc_debug(3, 0x75, val, adr);
641 }
642
643