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