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