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