2 \author Travis Goodspeed
3 \brief Chipcon 8051 debugging.
7 //This is like SPI, except that you read or write, not both.
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
23 /* Concerning clock rates,
24 the maximimum clock rates are defined on page 4 of the spec.
25 They vary, but are roughly 30MHz. Raising this clock rate might
26 allow for clock glitching, but the GoodFET isn't sufficient fast for that.
27 Perhaps a 200MHz ARM or an FPGA in the BadassFET?
31 //MISO and MOSI are the same pin, direction changes.
37 //This could be more accurate.
38 //Does it ever need to be?
40 #define CCDELAY(x) delay(x)
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)
48 #define CCWRITE P5DIR|=MOSI
49 #define CCREAD P5DIR&=~MISO
51 //! Set up the pins for CC mode. Does not init debugger.
55 //P5DIR&=~MISO; //MOSI is MISO
58 //! Initialize the debugger
60 //Two positive debug clock pulses while !RST is low.
61 //Take RST low, pulse twice, then high.
79 //! Read and write a CC bit.
80 unsigned char cctrans8(unsigned char byte){
82 //This function came from the SPI Wikipedia article.
85 for (bit = 0; bit < 8; bit++) {
86 /* write MOSI on trailing edge of previous clock */
93 /* half a clock cycle before leading/rising edge */
97 /* half a clock cycle before trailing/falling edge */
100 /* read MISO on trailing edge */
108 //! Send a command from txbytes.
109 void cccmd(unsigned char len){
113 cctrans8(cmddata[i]);
116 //! Fetch a reply, usually 1 byte.
117 void ccread(unsigned char len){
121 cmddata[i]=cctrans8(0);
124 //! Handles a monitor command.
125 void cchandle(unsigned char app,
129 //CC_PEEK and CC_POKE will come later.
130 case READ: //Write a command and return 1-byte reply.
135 case WRITE: //Write a command with no reply.
139 case START://enter debugger
143 case STOP://exit debugger
144 //Take RST low, then high.
161 cc_wr_config(cmddata[0]);
176 case CC_SET_HW_BRKPNT:
177 cc_set_hw_brkpnt(cmddataword[0]);
196 case CC_STEP_REPLACE:
197 txdata(app,NOK,0);//TODO add me
206 case CC_READ_CODE_MEMORY:
207 cmddata[0]=cc_peekcodebyte(cmddataword[0]);
210 case CC_READ_XDATA_MEMORY:
211 cmddata[0]=cc_peekdatabyte(cmddataword[0]);
214 case CC_WRITE_XDATA_MEMORY:
215 cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
220 case CC_WRITE_FLASH_PAGE:
221 case CC_MASS_ERASE_FLASH:
222 case CC_PROGRAM_FLASH:
223 txdata(app,NOK,0);//TODO implement me.
228 //! Erase all of a Chipcon's memory.
229 void cc_chip_erase(){
234 //! Write the configuration byte.
235 void cc_wr_config(unsigned char config){
241 //! Read the configuration byte.
242 unsigned char cc_rd_config(){
251 //! Read the status register
252 unsigned char cc_read_status(){
259 //! Read the CHIP ID bytes.
260 unsigned short cc_get_chip_id(){
261 unsigned short toret;
268 toret=(toret<<8)+cmddata[1];
274 unsigned short cc_get_pc(){
280 return cmddataword[0];
283 //! Set a hardware breakpoint.
284 void cc_set_hw_brkpnt(unsigned short adr){
308 //! Step an instruction
309 void cc_step_instr(){
316 //! Debug an instruction.
317 void cc_debug_instr(unsigned char len){
318 //Bottom two bits of command indicate length.
319 unsigned char cmd=0x54+(len&0x3);
321 cctrans8(cmd); //Second command code
322 cccmd(len&0x3); //Command itself.
327 //! Debug an instruction, for local use.
328 unsigned char cc_debug(unsigned char len,
332 unsigned char cmd=0x54+(len&0x3);//(len&0x3);
342 return cctrans8(0x00);
345 //! Fetch a byte of code memory.
346 unsigned char cc_peekcodebyte(unsigned long adr){
347 /** See page 9 of SWRA124 */
348 unsigned char bank=adr>>15,
354 //MOV MEMCTR, (bank*16)+1
355 cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
357 cc_debug(3, 0x90, hb, lb);
361 cc_debug(2, 0xE4, 0, 0);
363 toret=cc_debug(3, 0x93, 0, 0);
365 //cc_debug(1, 0xA3, 0, 0);
371 //! Set a byte of data memory.
372 unsigned char cc_pokedatabyte(unsigned int adr,
379 cc_debug(3, 0x90, hb, lb);
381 cc_debug(2, 0x74, val, 0);
383 cc_debug(1, 0xF0, 0, 0);
387 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
388 for (n = 0; n < count; n++) {
389 DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
390 DEBUG_INSTR(IN: 0xF0, OUT: Discard);
391 DEBUG_INSTR(IN: 0xA3, OUT: Discard);
396 //! Fetch a byte of data memory.
397 unsigned char cc_peekdatabyte(unsigned int adr){
404 cc_debug(3, 0x90, hb, lb);
406 //Must be 2, perhaps for clocking?
407 toret=cc_debug(3, 0xE0, 0, 0);
411 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
412 for (n = 0; n < count; n++) {
413 DEBUG_INSTR(IN: 0xE0, OUT: outputArray[n]);
414 DEBUG_INSTR(IN: 0xA3, OUT: Discard);