1 //GoodFET ChipCon Debugging Application
3 //<travis at radiantmachines.com>
6 //This is like SPI, except that you read or write, not both.
8 /** N.B. The READ verb performs a write of all (any) supplied data,
9 then reads a single byte reply from the target. The WRITE verb
22 /** Concerning clock rates,
23 the maximimum clock rates are defined on page 4 of the spec.
24 They vary, but are roughly 30MHz. Raising this clock rate might
25 allow for clock glitching, but the GoodFET isn't sufficient fast for that.
26 Perhaps a 200MHz ARM or an FPGA in the BadassFET?
30 //MISO and MOSI are the same pin, direction changes.
36 //This could be more accurate.
37 //Does it ever need to be?
39 #define CCDELAY(x) delay(x)
41 #define SETMOSI P5OUT|=MOSI
42 #define CLRMOSI P5OUT&=~MOSI
43 #define SETCLK P5OUT|=SCK
44 #define CLRCLK P5OUT&=~SCK
45 #define READMISO (P5IN&MISO?1:0)
47 #define CCWRITE P5DIR|=MOSI
48 #define CCREAD P5DIR&=~MISO
50 //! Set up the pins for CC mode. Does not init debugger.
54 //P5DIR&=~MISO; //MOSI is MISO
57 //! Initialize the debugger
59 //Two positive debug clock pulses while !RST is low.
60 //Take RST low, pulse twice, then high.
78 //! Read and write a CC bit.
79 unsigned char cctrans8(unsigned char byte){
81 //This function came from the SPI Wikipedia article.
84 for (bit = 0; bit < 8; bit++) {
85 /* write MOSI on trailing edge of previous clock */
92 /* half a clock cycle before leading/rising edge */
96 /* half a clock cycle before trailing/falling edge */
99 /* read MISO on trailing edge */
107 //! Send a command from txbytes.
108 void cccmd(unsigned char len){
112 cctrans8(cmddata[i]);
115 //! Fetch a reply, usually 1 byte.
116 void ccread(unsigned char len){
120 cmddata[i]=cctrans8(0);
123 //! Handles a monitor command.
124 void cchandle(unsigned char app,
128 //CC_PEEK and CC_POKE will come later.
129 case READ: //Write a command and return 1-byte reply.
134 case WRITE: //Write a command with no reply.
138 case START://enter debugger
142 case STOP://exit debugger
143 //Take RST low, then high.
160 cc_wr_config(cmddata[0]);
175 case CC_SET_HW_BRKPNT:
176 cc_set_hw_brkpnt(cmddataword[0]);
195 case CC_STEP_REPLACE:
196 txdata(app,NOK,0);//TODO add me
205 case CC_READ_CODE_MEMORY:
206 cmddata[0]=cc_peekcodebyte(cmddataword[0]);
209 case CC_READ_XDATA_MEMORY:
210 cmddata[0]=cc_peekdatabyte(cmddataword[0]);
213 case CC_WRITE_XDATA_MEMORY:
214 cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
219 case CC_WRITE_FLASH_PAGE:
220 case CC_MASS_ERASE_FLASH:
221 case CC_PROGRAM_FLASH:
222 txdata(app,NOK,0);//TODO implement me.
227 //! Erase all of a Chipcon's memory.
228 void cc_chip_erase(){
233 //! Write the configuration byte.
234 void cc_wr_config(unsigned char config){
240 //! Read the configuration byte.
241 unsigned char cc_rd_config(){
250 //! Read the status register
251 unsigned char cc_read_status(){
258 //! Read the CHIP ID bytes.
259 unsigned short cc_get_chip_id(){
260 unsigned short toret;
267 toret=(toret<<8)+cmddata[1];
273 unsigned short cc_get_pc(){
279 return cmddataword[0];
282 //! Set a hardware breakpoint.
283 void cc_set_hw_brkpnt(unsigned short adr){
307 //! Step an instruction
308 void cc_step_instr(){
315 //! Debug an instruction.
316 void cc_debug_instr(unsigned char len){
317 //Bottom two bits of command indicate length.
318 unsigned char cmd=0x54+(len&0x3);
320 cctrans8(cmd); //Second command code
321 cccmd(len&0x3); //Command itself.
326 //! Debug an instruction, for local use.
327 unsigned char cc_debug(unsigned char len,
331 unsigned char cmd=0x54+(len&0x3);//(len&0x3);
341 return cctrans8(0x00);
344 //! Fetch a byte of code memory.
345 unsigned char cc_peekcodebyte(unsigned long adr){
346 /** See page 9 of SWRA124 */
347 unsigned char bank=adr>>15,
353 //MOV MEMCTR, (bank*16)+1
354 cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
356 cc_debug(3, 0x90, hb, lb);
360 cc_debug(2, 0xE4, 0, 0);
362 toret=cc_debug(3, 0x93, 0, 0);
364 //cc_debug(1, 0xA3, 0, 0);
370 //! Set a byte of data memory.
371 unsigned char cc_pokedatabyte(unsigned int adr,
378 cc_debug(3, 0x90, hb, lb);
380 cc_debug(2, 0x74, val, 0);
382 cc_debug(1, 0xF0, 0, 0);
386 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
387 for (n = 0; n < count; n++) {
388 DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
389 DEBUG_INSTR(IN: 0xF0, OUT: Discard);
390 DEBUG_INSTR(IN: 0xA3, OUT: Discard);
395 //! Fetch a byte of data memory.
396 unsigned char cc_peekdatabyte(unsigned int adr){
403 cc_debug(3, 0x90, hb, lb);
405 //Must be 2, perhaps for clocking?
406 toret=cc_debug(3, 0xE0, 0, 0);
410 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
411 for (n = 0; n < count; n++) {
412 DEBUG_INSTR(IN: 0xE0, OUT: outputArray[n]);
413 DEBUG_INSTR(IN: 0xA3, OUT: Discard);