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