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