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