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