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