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