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