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