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