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