2 \author Travis Goodspeed
4 This is an implementation of the Chipcon 8051
5 debugging protocol for the GoodFET.
9 //This is like SPI, except that you read or write, not both.
11 /* N.B. The READ verb performs a write of all (any) supplied data,
12 then reads a single byte reply from the target. The WRITE verb
25 /* Concerning clock rates,
26 the maximimum clock rates are defined on page 4 of the spec.
27 They vary, but are roughly 30MHz. Raising this clock rate might
28 allow for clock glitching, but the GoodFET isn't sufficient fast for that.
29 Perhaps a 200MHz ARM or an FPGA in the BadassFET?
33 //MISO and MOSI are the same pin, direction changes.
39 //This could be more accurate.
40 //Does it ever need to be?
42 #define CCDELAY(x) delay(x)
44 #define SETMOSI P5OUT|=MOSI
45 #define CLRMOSI P5OUT&=~MOSI
46 #define SETCLK P5OUT|=SCK
47 #define CLRCLK P5OUT&=~SCK
48 #define READMISO (P5IN&MISO?1:0)
50 #define CCWRITE P5DIR|=MOSI
51 #define CCREAD P5DIR&=~MISO
53 //! Set up the pins for CC mode. Does not init debugger.
57 //P5DIR&=~MISO; //MOSI is MISO
60 //! Initialize the debugger
62 //Two positive debug clock pulses while !RST is low.
63 //Take RST low, pulse twice, then high.
81 //! Read and write a CC bit.
82 unsigned char cctrans8(unsigned char byte){
84 //This function came from the SPI Wikipedia article.
87 for (bit = 0; bit < 8; bit++) {
88 /* write MOSI on trailing edge of previous clock */
95 /* half a clock cycle before leading/rising edge */
99 /* half a clock cycle before trailing/falling edge */
102 /* read MISO on trailing edge */
110 //! Send a command from txbytes.
111 void cccmd(unsigned char len){
115 cctrans8(cmddata[i]);
118 //! Fetch a reply, usually 1 byte.
119 void ccread(unsigned char len){
123 cmddata[i]=cctrans8(0);
126 //! Handles a monitor command.
127 void cchandle(unsigned char app,
131 //CC_PEEK and CC_POKE will come later.
132 case READ: //Write a command and return 1-byte reply.
137 case WRITE: //Write a command with no reply.
141 case START://enter debugger
145 case STOP://exit debugger
146 //Take RST low, then high.
163 cc_wr_config(cmddata[0]);
178 case CC_SET_HW_BRKPNT:
179 cc_set_hw_brkpnt(cmddataword[0]);
198 case CC_STEP_REPLACE:
199 txdata(app,NOK,0);//TODO add me
208 case CC_READ_CODE_MEMORY:
209 cmddata[0]=cc_peekcodebyte(cmddataword[0]);
212 case CC_READ_XDATA_MEMORY:
213 cmddata[0]=cc_peekdatabyte(cmddataword[0]);
216 case CC_WRITE_XDATA_MEMORY:
217 cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
222 case CC_WRITE_FLASH_PAGE:
223 case CC_MASS_ERASE_FLASH:
224 case CC_PROGRAM_FLASH:
225 txdata(app,NOK,0);//TODO implement me.
230 //! Erase all of a Chipcon's memory.
231 void cc_chip_erase(){
236 //! Write the configuration byte.
237 void cc_wr_config(unsigned char config){
243 //! Read the configuration byte.
244 unsigned char cc_rd_config(){
253 //! Read the status register
254 unsigned char cc_read_status(){
261 //! Read the CHIP ID bytes.
262 unsigned short cc_get_chip_id(){
263 unsigned short toret;
270 toret=(toret<<8)+cmddata[1];
276 unsigned short cc_get_pc(){
282 return cmddataword[0];
285 //! Set a hardware breakpoint.
286 void cc_set_hw_brkpnt(unsigned short adr){
310 //! Step an instruction
311 void cc_step_instr(){
318 //! Debug an instruction.
319 void cc_debug_instr(unsigned char len){
320 //Bottom two bits of command indicate length.
321 unsigned char cmd=0x54+(len&0x3);
323 cctrans8(cmd); //Second command code
324 cccmd(len&0x3); //Command itself.
329 //! Debug an instruction, for local use.
330 unsigned char cc_debug(unsigned char len,
334 unsigned char cmd=0x54+(len&0x3);//(len&0x3);
344 return cctrans8(0x00);
347 //! Fetch a byte of code memory.
348 unsigned char cc_peekcodebyte(unsigned long adr){
349 /** See page 9 of SWRA124 */
350 unsigned char bank=adr>>15,
356 //MOV MEMCTR, (bank*16)+1
357 cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
359 cc_debug(3, 0x90, hb, lb);
363 cc_debug(2, 0xE4, 0, 0);
365 toret=cc_debug(3, 0x93, 0, 0);
367 //cc_debug(1, 0xA3, 0, 0);
373 //! Set a byte of data memory.
374 unsigned char cc_pokedatabyte(unsigned int adr,
381 cc_debug(3, 0x90, hb, lb);
383 cc_debug(2, 0x74, val, 0);
385 cc_debug(1, 0xF0, 0, 0);
389 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
390 for (n = 0; n < count; n++) {
391 DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
392 DEBUG_INSTR(IN: 0xF0, OUT: Discard);
393 DEBUG_INSTR(IN: 0xA3, OUT: Discard);
398 //! Fetch a byte of data memory.
399 unsigned char cc_peekdatabyte(unsigned int adr){
406 cc_debug(3, 0x90, hb, lb);
408 //Must be 2, perhaps for clocking?
409 toret=cc_debug(3, 0xE0, 0, 0);
413 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
414 for (n = 0; n < count; n++) {
415 DEBUG_INSTR(IN: 0xE0, OUT: outputArray[n]);
416 DEBUG_INSTR(IN: 0xA3, OUT: Discard);