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