Checkpoint. CC flash works, might break it soon.
[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 //! Read the configuration byte.
271 unsigned char cc_rd_config(){
272   cmddata[0]=CCCMD_RD_CONFIG; //0x24
273   cccmd(1);
274   ccread(1);
275   return cmddata[0];
276 }
277
278
279 //! Read the status register
280 unsigned char cc_read_status(){
281   cmddata[0]=CCCMD_READ_STATUS; //0x3f
282   cccmd(1);
283   ccread(1);
284   return cmddata[0];
285 }
286
287 //! Read the CHIP ID bytes.
288 unsigned short cc_get_chip_id(){
289   unsigned short toret;
290   cmddata[0]=CCCMD_GET_CHIP_ID; //0x68
291   cccmd(1);
292   ccread(2);
293   
294   //Return the word.
295   toret=cmddata[1];
296   toret=(toret<<8)+cmddata[1];
297   return toret;
298 }
299
300 //! Populates flash buffer in xdata.
301 void cc_write_flash_buffer(u8 *data, u16 len){
302   cc_write_xdata(0xf000, data, len);
303 }
304 //! Populates flash buffer in xdata.
305 void cc_write_xdata(u16 adr, u8 *data, u16 len){
306   u16 i;
307   for(i=0; i<len; i++){
308     cc_pokedatabyte(adr+i,
309                     data[i]);
310   }
311 }
312
313
314 //32-bit words, 2KB pages
315 #define HIBYTE_WORDS_PER_FLASH_PAGE 0x02
316 #define LOBYTE_WORDS_PER_FLASH_PAGE 0x00
317 #define FLASHPAGE_SIZE 0x800
318
319 //32 bit words
320 #define FLASH_WORD_SIZE 0x4
321
322 const u8 flash_routine[] = {
323   //MOV FADDRH, #imm; 
324   0x75, 0xAD,
325   0x00,//#imm=((address >> 8) / FLASH_WORD_SIZE) & 0x7E,
326   
327   0x75, 0xAC, 0x00,                                          //                 MOV FADDRL, #00; 
328   /* Erase page. */
329   0x75, 0xAE, 0x01,                                          //                 MOV FLC, #01H; // ERASE 
330                                                              //                 ; Wait for flash erase to complete 
331   0xE5, 0xAE,                                                // eraseWaitLoop:  MOV A, FLC; 
332   0x20, 0xE7, 0xFB,                                          //                 JB ACC_BUSY, eraseWaitLoop; 
333   /* End erase page. */
334                                                              //                 ; Initialize the data pointer 
335   0x90, 0xF0, 0x00,                                          //                 MOV DPTR, #0F000H; 
336                                                              //                 ; Outer loops 
337   0x7F, HIBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R7, #imm; 
338   0x7E, LOBYTE_WORDS_PER_FLASH_PAGE,                         //                 MOV R6, #imm; 
339   0x75, 0xAE, 0x02,                                          //                 MOV FLC, #02H; // WRITE 
340                                                              //                     ; Inner loops 
341   0x7D, FLASH_WORD_SIZE,                                     // writeLoop:          MOV R5, #imm; 
342   0xE0,                                                      // writeWordLoop:          MOVX A, @DPTR; 
343   0xA3,                                                      //                         INC DPTR; 
344   0xF5, 0xAF,                                                //                         MOV FWDATA, A;  
345   0xDD, 0xFA,                                                //                     DJNZ R5, writeWordLoop; 
346                                                              //                     ; Wait for completion 
347   0xE5, 0xAE,                                                // writeWaitLoop:      MOV A, FLC; 
348   0x20, 0xE6, 0xFB,                                          //                     JB ACC_SWBSY, writeWaitLoop; 
349   0xDE, 0xF1,                                                //                 DJNZ R6, writeLoop; 
350   0xDF, 0xEF,                                                //                 DJNZ R7, writeLoop; 
351                                                              //                 ; Done, fake a breakpoint 
352   0xA5                                                       //                 DB 0xA5; 
353 }; 
354
355
356 //! Locks the chip.
357 void cc_lockchip(){
358   debugstr("Locking chip.");
359   cc_wr_config(1);//Select Info Flash 
360   cc_debug(3, 0x75, 0xAF, 0x00);//MOV FWDATA, #00H
361   //cc_debug(2, 0xF5, 0xAF, 0); //MOV FWDATA, A
362   
363 }
364
365 //! Copies flash buffer to flash.
366 void cc_write_flash_page(u32 adr){
367   //Assumes that page has already been written to XDATA 0xF000
368   //debugstr("Flashing 2kb at 0xF000 to given adr.");
369   
370   if(adr&(FLASHPAGE_SIZE-1)){
371     debugstr("Flash page address is not on a multiple of 2kB.  Aborting.");
372     return;
373   }
374   
375   //Routine comes next
376   //WRITE_XDATA_MEMORY(IN: 0xF000 + FLASH_PAGE_SIZE, sizeof(routine), routine);
377   cc_write_xdata(0xF000+FLASHPAGE_SIZE,
378                  (u8*) flash_routine, sizeof(flash_routine));
379   //Patch routine's third byte with
380   //((address >> 8) / FLASH_WORD_SIZE) & 0x7E
381   cc_pokedatabyte(0xF000+FLASHPAGE_SIZE+2,
382                   ((adr>>8)/FLASH_WORD_SIZE)&0x7E);
383   //debugstr("Wrote flash routine.");
384   
385   
386   //MOV MEMCTR, (bank * 16) + 1;
387   cmddata[0]=0x75;
388   cmddata[1]=0xc7;
389   cmddata[2]=0x51;
390   cc_debug_instr(3);
391   //debugstr("Loaded bank info.");
392   
393   cc_set_pc(0xf000+FLASHPAGE_SIZE);//execute code fragment
394   cc_resume();
395   //debugstr("Executing.");
396   
397   
398   while(!(cc_read_status()&CC_STATUS_CPUHALTED)){
399     P1OUT^=1;//blink LED while flashing
400   }
401   
402   //debugstr("Done flashing.");
403   
404   P1OUT&=~1;//clear LED
405 }
406
407 //! Read the PC
408 unsigned short cc_get_pc(){
409   cmddata[0]=CCCMD_GET_PC; //0x28
410   cccmd(1);
411   ccread(2);
412   
413   //Return the word.
414   return cmddataword[0];
415 }
416
417 //! Set a hardware breakpoint.
418 void cc_set_hw_brkpnt(unsigned short adr){
419   debugstr("FIXME: This certainly won't work.");
420   cmddataword[0]=adr;
421   cccmd(2);
422   ccread(1);
423   return;
424 }
425
426
427 //! Halt the CPU.
428 void cc_halt(){
429   cmddata[0]=CCCMD_HALT; //0x44
430   cccmd(1);
431   ccread(1);
432   return;
433 }
434 //! Resume the CPU.
435 void cc_resume(){
436   cmddata[0]=CCCMD_RESUME; //0x4C
437   cccmd(1);
438   ccread(1);
439   return;
440 }
441
442
443 //! Step an instruction
444 void cc_step_instr(){
445   cmddata[0]=CCCMD_STEP_INSTR; //0x5C
446   cccmd(1);
447   ccread(1);
448   return;
449 }
450
451 //! Debug an instruction.
452 void cc_debug_instr(unsigned char len){
453   //Bottom two bits of command indicate length.
454   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3); //0x54+len
455   CCWRITE;
456   cctrans8(cmd);  //Second command code
457   cccmd(len&0x3); //Command itself.
458   ccread(1);
459   return;
460 }
461
462 //! Debug an instruction, for local use.
463 unsigned char cc_debug(unsigned char len,
464               unsigned char a,
465               unsigned char b,
466               unsigned char c){
467   unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3);//0x54+len
468   CCWRITE;
469   cctrans8(cmd);
470   if(len--)
471     cctrans8(a);
472   if(len--)
473     cctrans8(b);
474   if(len--)
475     cctrans8(c);
476   CCREAD;
477   return cctrans8(0x00);
478 }
479
480 //! Fetch a byte of code memory.
481 unsigned char cc_peekcodebyte(unsigned long adr){
482   /** See page 9 of SWRA124 */
483   unsigned char bank=adr>>15,
484     lb=adr&0xFF,
485     hb=(adr>>8)&0x7F,
486     toret=0;
487   adr&=0x7FFF;
488   
489   //MOV MEMCTR, (bank*16)+1
490   cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
491   //MOV DPTR, address
492   cc_debug(3, 0x90, hb, lb);
493   
494   //for each byte
495   //CLR A
496   cc_debug(2, 0xE4, 0, 0);
497   //MOVC A, @A+DPTR;
498   toret=cc_debug(3, 0x93, 0, 0);
499   //INC DPTR
500   //cc_debug(1, 0xA3, 0, 0);
501   
502   return toret;
503 }
504
505
506 //! Set a byte of data memory.
507 unsigned char cc_pokedatabyte(unsigned int adr,
508                            unsigned char val){
509   unsigned char
510     hb=(adr&0xFF00)>>8,
511     lb=adr&0xFF;
512   
513   //MOV DPTR, adr
514   cc_debug(3, 0x90, hb, lb);
515   //MOV A, val
516   cc_debug(2, 0x74, val, 0);
517   //MOVX @DPTR, A
518   cc_debug(1, 0xF0, 0, 0);
519   
520   return 0;
521   /*
522 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
523 for (n = 0; n < count; n++) {
524     DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
525     DEBUG_INSTR(IN: 0xF0, OUT: Discard);
526     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
527 }
528    */
529 }
530
531 //! Fetch a byte of data memory.
532 unsigned char cc_peekdatabyte(unsigned int adr){
533   unsigned char
534     hb=(adr&0xFF00)>>8,
535     lb=adr&0xFF,
536     toret;
537
538   //MOV DPTR, adr
539   cc_debug(3, 0x90, hb, lb);
540   //MOVX A, @DPTR
541   //Must be 2, perhaps for clocking?
542   toret=cc_debug(3, 0xE0, 0, 0);
543   return toret;
544   
545     /*
546 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
547 for (n = 0; n < count; n++) {
548     DEBUG_INSTR(IN: 0xE0, OUT: outputArray[n]);
549     DEBUG_INSTR(IN: 0xA3, OUT: Discard);
550 }
551   */
552 }