Reverted Chipcon client to C implementation.
[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     flash_word_size=0x02;
350     //debugstr("2 bytes/flash word");
351     break;
352   default:
353     debugstr("Warning: Guessing flash word size.");
354   case 0x85://CC2430
355   case 0x89://CC2431
356     //debugstr("4 bytes/flash word");
357     flash_word_size=0x04;
358     break;
359   }
360   
361   //Return the word.
362   return cmddataword[0];
363 }
364
365 //! Populates flash buffer in xdata.
366 void cc_write_flash_buffer(u8 *data, u16 len){
367   cc_write_xdata(0xf000, data, len);
368 }
369 //! Populates flash buffer in xdata.
370 void cc_write_xdata(u16 adr, u8 *data, u16 len){
371   u16 i;
372   for(i=0; i<len; i++){
373     cc_pokedatabyte(adr+i,
374                     data[i]);
375   }
376 }
377
378
379 //32-bit words, 2KB pages
380 //0x20 0x00 for CC2430, CC1110
381 #define HIBYTE_WORDS_PER_FLASH_PAGE 0x02
382 #define LOBYTE_WORDS_PER_FLASH_PAGE 0x00
383
384 /** Ugh, this varies by chip.
385     0x800 for CC2430
386     0x400 for CC1110
387 */
388 //#define FLASHPAGE_SIZE 0x400
389 #define MAXFLASHPAGE_SIZE 0x800
390 #define MINFLASHPAGE_SIZE 0x400
391
392
393 //32 bit words on CC2430
394 //16 bit words on CC1110
395 //#define FLASH_WORD_SIZE 0x2
396 u8 flash_word_size = 0; //0x02;
397
398
399 /* Flash Write Timing
400    MHZ | FWT (0xAB)
401    12  | 0x10
402    13  | 0x11
403    16  | 0x15
404    24  | 0x20
405    26  | 0x23  (IM ME)
406    32  | 0x2A  (Modula.si)
407 */
408 //#define FWT 0x23
409
410 const u8 flash_routine[] = {
411   //0:
412   //MOV FADDRH, #imm; 
413   0x75, 0xAD,
414   0x00,//#imm=((address >> 8) / FLASH_WORD_SIZE) & 0x7E,
415   
416   //0x75, 0xAB, 0x23, //Set FWT per clock
417   0x75, 0xAC, 0x00,                                          //                 MOV FADDRL, #00; 
418   /* Erase page. */
419   0x75, 0xAE, 0x01,                                          //                 MOV FLC, #01H; // ERASE 
420                                                              //                 ; Wait for flash erase to complete 
421   0xE5, 0xAE,                                                // eraseWaitLoop:  MOV A, FLC; 
422   0x20, 0xE7, 0xFB,                                          //                 JB ACC_BUSY, eraseWaitLoop; 
423   
424   /* End erase page. */
425                                                              //                 ; Initialize the data pointer 
426   0x90, 0xF0, 0x00,                                          //                 MOV DPTR, #0F000H; 
427                                                              //                 ; Outer loops 
428   0x7F, HIBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R7, #imm; 
429   0x7E, LOBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R6, #imm; 
430   0x75, 0xAE, 0x02,                                          //                 MOV FLC, #02H; // WRITE 
431                                                              //                     ; Inner loops 
432   //24:
433   0x7D, 0xde /*FLASH_WORD_SIZE*/,                                     // writeLoop:          MOV R5, #imm; 
434   0xE0,                                                      // writeWordLoop:          MOVX A, @DPTR; 
435   0xA3,                                                      //                         INC DPTR; 
436   0xF5, 0xAF,                                                //                         MOV FWDATA, A;  
437   0xDD, 0xFA,                                                //                     DJNZ R5, writeWordLoop; 
438                                                              //                     ; Wait for completion 
439   0xE5, 0xAE,                                                // writeWaitLoop:      MOV A, FLC; 
440   0x20, 0xE6, 0xFB,                                          //                     JB ACC_SWBSY, writeWaitLoop; 
441   0xDE, 0xF1,                                                //                 DJNZ R6, writeLoop; 
442   0xDF, 0xEF,                                                //                 DJNZ R7, writeLoop; 
443                                                              //                 ; Done, fake a breakpoint 
444   0xA5                                                       //                 DB 0xA5; 
445 };
446
447
448 //! Copies flash buffer to flash.
449 void cc_write_flash_page(u32 adr){
450   //Assumes that page has already been written to XDATA 0xF000
451   //debugstr("Flashing 2kb at 0xF000 to given adr.");
452   
453   if(adr&(MINFLASHPAGE_SIZE-1)){
454     debugstr("Flash page address is not on a page boundary.  Aborting.");
455     return;
456   }
457   
458   if(flash_word_size==0){
459     debugstr("Flash word size is wrong.");
460     while(1);
461   }
462   
463   //Routine comes next
464   //WRITE_XDATA_MEMORY(IN: 0xF000 + FLASH_PAGE_SIZE, sizeof(routine), routine);
465   cc_write_xdata(0xF000+MAXFLASHPAGE_SIZE,
466                  (u8*) flash_routine, sizeof(flash_routine));
467   //Patch routine's third byte with
468   //((address >> 8) / FLASH_WORD_SIZE) & 0x7E
469   cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+2,
470                   ((adr>>8)/flash_word_size)&0x7E);
471   //Patch routine to define FLASH_WORD_SIZE
472   if(flash_routine[25]!=0xde)
473     debugstr("Ugly patching code failing in chipcon.c");
474   cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+25,
475                   flash_word_size);
476   
477   //debugstr("Wrote flash routine.");
478   
479   
480   //MOV MEMCTR, (bank * 16) + 1;
481   cmddata[0]=0x75;
482   cmddata[1]=0xc7;
483   cmddata[2]=0x51;
484   cc_debug_instr(3);
485   //debugstr("Loaded bank info.");
486   
487   cc_set_pc(0xf000+MAXFLASHPAGE_SIZE);//execute code fragment
488   cc_resume();
489   
490   //debugstr("Executing.");
491   
492   
493   while(!(cc_read_status()&CC_STATUS_CPUHALTED)){
494     P1OUT^=1;//blink LED while flashing
495   }
496   
497   
498   //debugstr("Done flashing.");
499   
500   P1OUT&=~1;//clear LED
501 }
502
503 //! Read the PC
504 unsigned short cc_get_pc(){
505   cmddata[0]=CCCMD_GET_PC; //0x28
506   cccmd(1);
507   ccread(2);
508   
509   //Return the word.
510   return cmddataword[0];
511 }
512
513 //! Set a hardware breakpoint.
514 void cc_set_hw_brkpnt(unsigned short adr){
515   debugstr("FIXME: This certainly won't work.");
516   cmddataword[0]=adr;
517   cccmd(2);
518   ccread(1);
519   return;
520 }
521
522
523 //! Halt the CPU.
524 void cc_halt(){
525   cmddata[0]=CCCMD_HALT; //0x44
526   cccmd(1);
527   ccread(1);
528   return;
529 }
530 //! Resume the CPU.
531 void cc_resume(){
532   cmddata[0]=CCCMD_RESUME; //0x4C
533   cccmd(1);
534   ccread(1);
535   return;
536 }
537
538
539 //! Step an instruction
540 void cc_step_instr(){
541   cmddata[0]=CCCMD_STEP_INSTR; //0x5C
542   cccmd(1);
543   ccread(1);
544   return;
545 }
546
547 //! Debug an instruction.
548 void cc_debug_instr(unsigned char len){
549   //Bottom two bits of command indicate length.
550   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3); //0x54+len
551   CCWRITE;
552   cctrans8(cmd);  //Second command code
553   cccmd(len&0x3); //Command itself.
554   ccread(1);
555   return;
556 }
557
558 //! Debug an instruction, for local use.
559 unsigned char cc_debug(unsigned char len,
560               unsigned char a,
561               unsigned char b,
562               unsigned char c){
563   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3);//0x54+len
564   CCWRITE;
565   cctrans8(cmd);
566   if(len>0)
567     cctrans8(a);
568   if(len>1)
569     cctrans8(b);
570   if(len>2)
571     cctrans8(c);
572   CCREAD;
573   return cctrans8(0x00);
574 }
575
576 //! Fetch a byte of code memory.
577 unsigned char cc_peekcodebyte(unsigned long adr){
578   /** See page 9 of SWRA124 */
579   unsigned char bank=adr>>15,
580     lb=adr&0xFF,
581     hb=(adr>>8)&0x7F,
582     toret=0;
583   adr&=0x7FFF;
584   
585   //MOV MEMCTR, (bank*16)+1
586   cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
587   //MOV DPTR, address
588   cc_debug(3, 0x90, hb, lb);
589   
590   //for each byte
591   //CLR A
592   cc_debug(2, 0xE4, 0, 0);
593   //MOVC A, @A+DPTR;
594   toret=cc_debug(3, 0x93, 0, 0);
595   //INC DPTR
596   //cc_debug(1, 0xA3, 0, 0);
597   
598   return toret;
599 }
600
601
602 //! Set a byte of data memory.
603 unsigned char cc_pokedatabyte(unsigned int adr,
604                            unsigned char val){
605   unsigned char
606     hb=(adr&0xFF00)>>8,
607     lb=adr&0xFF;
608   
609   //MOV DPTR, adr
610   cc_debug(3, 0x90, hb, lb);
611   //MOV A, val
612   cc_debug(2, 0x74, val, 0);
613   //MOVX @DPTR, A
614   cc_debug(1, 0xF0, 0, 0);
615   
616   return 0;
617   /*
618 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
619 for (n = 0; n < count; n++) {
620     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
621     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
622     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
623 }
624    */
625 }
626
627 //! Fetch a byte of data memory.
628 unsigned char cc_peekdatabyte(unsigned int adr){
629   unsigned char
630     hb=(adr&0xFF00)>>8,
631     lb=adr&0xFF;
632   
633   //MOV DPTR, adr
634   cc_debug(3, 0x90, hb, lb);
635   //MOVX A, @DPTR
636   //Must be 2, perhaps for clocking?
637   return cc_debug(3, 0xE0, 0, 0);
638 }
639
640
641 //! Fetch a byte of IRAM.
642 u8 cc_peekirambyte(u8 adr){
643   //CLR A
644   cc_debug(2, 0xE4, 0, 0);
645   //MOV A, #iram
646   return cc_debug(3, 0xE5, adr, 0);
647 }
648
649 //! Write a byte of IRAM.
650 u8 cc_pokeirambyte(u8 adr, u8 val){
651   //CLR A
652   cc_debug(2, 0xE4, 0, 0);
653   //MOV #iram, #val
654   return cc_debug(3, 0x75, adr, val);
655   //return cc_debug(3, 0x75, val, adr);
656 }
657
658