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, 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
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)
43 #define SETMOSI P5OUT|=MOSI
44 #define CLRMOSI P5OUT&=~MOSI
45 #define SETCLK P5OUT|=SCK
46 #define CLRCLK P5OUT&=~SCK
47 #define READMISO (P5IN&MISO?1:0)
49 #define CCWRITE P5DIR|=MOSI
50 #define CCREAD P5DIR&=~MISO
52 //! Set up the pins for CC mode. Does not init debugger.
60 /* 33 cycle critical region
61 0000000e <ccdebuginit>:
62 e: f2 d0 0d 00 bis.b #13, &0x0031 ;5 cycles
64 14: f2 c2 31 00 bic.b #8, &0x0031 ;4 cycles
65 18: d2 c3 31 00 bic.b #1, &0x0031 ;4
66 1c: f2 e2 31 00 xor.b #8, &0x0031 ;4
67 20: f2 e2 31 00 xor.b #8, &0x0031 ;4
68 24: f2 e2 31 00 xor.b #8, &0x0031 ;4
69 28: f2 e2 31 00 xor.b #8, &0x0031 ;4
70 2c: d2 d3 31 00 bis.b #1, &0x0031 ;4
75 //! Initialize the debugger
77 //Port output BUT NOT DIRECTION is set at start.
80 //delay(30); //So the beginning is ready for glitching.
82 //Two positive debug clock pulses while !RST is low.
83 //Take RST low, pulse twice, then high.
91 P5OUT^=SCK; //Unnecessary.
98 //! Read and write a CC bit.
99 unsigned char cctrans8(unsigned char byte){
101 //This function came from the SPI Wikipedia article.
104 for (bit = 0; bit < 8; bit++) {
105 /* write MOSI on trailing edge of previous clock */
112 /* half a clock cycle before leading/rising edge */
116 /* half a clock cycle before trailing/falling edge */
119 /* read MISO on trailing edge */
127 //! Send a command from txbytes.
128 void cccmd(unsigned char len){
132 cctrans8(cmddata[i]);
135 //! Fetch a reply, usually 1 byte.
136 void ccread(unsigned char len){
140 cmddata[i]=cctrans8(0);
143 //! Handles a monitor command.
144 void cchandle(unsigned char app,
147 //Always init. Might help with buggy lines.
151 int blocklen, blockadr;
154 //CC_PEEK and CC_POKE will come later.
156 cmddata[0]=cc_peekirambyte(cmddata[0]);
160 cmddata[0]=cc_pokeirambyte(cmddata[0],cmddata[1]);
163 case READ: //Write a command and return 1-byte reply.
169 case WRITE: //Write a command with no reply.
173 case START://enter debugger
177 case STOP://exit debugger
178 //Take RST low, then high.
191 case CC_MASS_ERASE_FLASH:
196 cc_wr_config(cmddata[0]);
209 //no break, return status
214 case CC_SET_HW_BRKPNT:
215 cc_set_hw_brkpnt(cmddataword[0]);
234 case CC_STEP_REPLACE:
235 txdata(app,NOK,0);//TODO add me
238 cmddataword[0]=cc_get_chip_id();
244 case CC_READ_CODE_MEMORY:
245 cmddata[0]=cc_peekcodebyte(cmddataword[0]);
248 case CC_READ_XDATA_MEMORY:
252 blocklen=cmddataword[1];
253 blockadr=cmddataword[0];
255 //Return that many bytes.
256 for(i=0;i<blocklen;i++)
257 cmddata[i]=cc_peekdatabyte(blockadr+i);
261 case CC_WRITE_XDATA_MEMORY:
262 cmddata[0]=cc_pokedatabyte(cmddataword[0], cmddata[2]);
266 cc_set_pc(cmddatalong[0]);
269 case CC_WRITE_FLASH_PAGE:
270 cc_write_flash_page(cmddatalong[0]);
273 case CC_WIPEFLASHBUFFER:
274 for(i=0xf000;i<0xf800;i++)
275 cc_pokedatabyte(i,0xFF);
280 case CC_PROGRAM_FLASH:
282 debugstr("This Chipcon command is not yet implemented.");
283 txdata(app,NOK,0);//TODO implement me.
288 //! Set the Chipcon's Program Counter
289 void cc_set_pc(u32 adr){
290 cmddata[0]=0x02; //SetPC
291 cmddata[1]=((adr>>8)&0xff); //HIBYTE
292 cmddata[2]=adr&0xff; //LOBYTE
297 //! Erase all of a Chipcon's memory.
298 void cc_chip_erase(){
299 cmddata[0]=CCCMD_CHIP_ERASE; //0x14
303 //! Write the configuration byte.
304 void cc_wr_config(unsigned char config){
305 cmddata[0]=CCCMD_WR_CONFIG; //0x1D
315 //debugstr("Locking chip.");
316 cc_wr_config(1);//Select Info Flash
317 if(!(cc_rd_config()&1))
318 debugstr("Config forgotten!");
322 cc_pokedatabyte(0xf000+i,0);
323 cc_write_flash_page(0);
324 if(cc_peekcodebyte(0))
325 debugstr("Failed to clear info flash byte.");
329 debugstr("Stuck in info flash mode!");
332 //! Read the configuration byte.
333 unsigned char cc_rd_config(){
334 cmddata[0]=CCCMD_RD_CONFIG; //0x24
341 //! Read the status register
342 unsigned char cc_read_status(){
343 cmddata[0]=CCCMD_READ_STATUS; //0x3f
349 //! Read the CHIP ID bytes.
350 unsigned short cc_get_chip_id(){
351 cmddata[0]=CCCMD_GET_CHIP_ID; //0x68
356 //Find the flash word size.
362 //debugstr("2 bytes/flash word");
363 flash_word_size=0x02;
366 //debugstr("Warning: Guessing flash word size.");
371 //debugstr("4 bytes/flash word");
372 flash_word_size=0x04;
377 return cmddataword[0];
380 //! Populates flash buffer in xdata.
381 void cc_write_flash_buffer(u8 *data, u16 len){
382 cc_write_xdata(0xf000, data, len);
384 //! Populates flash buffer in xdata.
385 void cc_write_xdata(u16 adr, u8 *data, u16 len){
387 for(i=0; i<len; i++){
388 cc_pokedatabyte(adr+i,
394 //32-bit words, 2KB pages
395 //0x20 0x00 for CC2430, CC1110
396 #define HIBYTE_WORDS_PER_FLASH_PAGE 0x02
397 #define LOBYTE_WORDS_PER_FLASH_PAGE 0x00
399 /** Ugh, this varies by chip.
403 //#define FLASHPAGE_SIZE 0x400
404 #define MAXFLASHPAGE_SIZE 0x800
405 #define MINFLASHPAGE_SIZE 0x400
408 //32 bit words on CC2430
409 //16 bit words on CC1110
410 //#define FLASH_WORD_SIZE 0x2
411 u8 flash_word_size = 0; //0x02;
414 /* Flash Write Timing
421 32 | 0x2A (Modula.si)
425 const u8 flash_routine[] = {
429 0x00,//#imm=((address >> 8) / FLASH_WORD_SIZE) & 0x7E,
431 //0x75, 0xAB, 0x23, //Set FWT per clock
432 0x75, 0xAC, 0x00, // MOV FADDRL, #00;
434 0x75, 0xAE, 0x01, // MOV FLC, #01H; // ERASE
435 // ; Wait for flash erase to complete
436 0xE5, 0xAE, // eraseWaitLoop: MOV A, FLC;
437 0x20, 0xE7, 0xFB, // JB ACC_BUSY, eraseWaitLoop;
439 /* End erase page. */
440 // ; Initialize the data pointer
441 0x90, 0xF0, 0x00, // MOV DPTR, #0F000H;
443 0x7F, HIBYTE_WORDS_PER_FLASH_PAGE, // MOV R7, #imm;
444 0x7E, LOBYTE_WORDS_PER_FLASH_PAGE, // MOV R6, #imm;
445 0x75, 0xAE, 0x02, // MOV FLC, #02H; // WRITE
448 0x7D, 0xde /*FLASH_WORD_SIZE*/, // writeLoop: MOV R5, #imm;
449 0xE0, // writeWordLoop: MOVX A, @DPTR;
451 0xF5, 0xAF, // MOV FWDATA, A;
452 0xDD, 0xFA, // DJNZ R5, writeWordLoop;
453 // ; Wait for completion
454 0xE5, 0xAE, // writeWaitLoop: MOV A, FLC;
455 0x20, 0xE6, 0xFB, // JB ACC_SWBSY, writeWaitLoop;
456 0xDE, 0xF1, // DJNZ R6, writeLoop;
457 0xDF, 0xEF, // DJNZ R7, writeLoop;
458 // ; Done, fake a breakpoint
463 //! Copies flash buffer to flash.
464 void cc_write_flash_page(u32 adr){
465 //Assumes that page has already been written to XDATA 0xF000
466 //debugstr("Flashing 2kb at 0xF000 to given adr.");
468 if(adr&(MINFLASHPAGE_SIZE-1)){
469 debugstr("Flash page address is not on a page boundary. Aborting.");
473 if(flash_word_size!=2 && flash_word_size!=4){
474 debugstr("Flash word size is wrong, aborting write to");
480 //WRITE_XDATA_MEMORY(IN: 0xF000 + FLASH_PAGE_SIZE, sizeof(routine), routine);
481 cc_write_xdata(0xF000+MAXFLASHPAGE_SIZE,
482 (u8*) flash_routine, sizeof(flash_routine));
483 //Patch routine's third byte with
484 //((address >> 8) / FLASH_WORD_SIZE) & 0x7E
485 cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+2,
486 ((adr>>8)/flash_word_size)&0x7E);
487 //Patch routine to define FLASH_WORD_SIZE
488 if(flash_routine[25]!=0xde)
489 debugstr("Ugly patching code failing in chipcon.c");
490 cc_pokedatabyte(0xF000+MAXFLASHPAGE_SIZE+25,
493 //debugstr("Wrote flash routine.");
495 //MOV MEMCTR, (bank * 16) + 1;
500 //debugstr("Loaded bank info.");
502 cc_set_pc(0xf000+MAXFLASHPAGE_SIZE);//execute code fragment
505 //debugstr("Executing.");
508 while(!(cc_read_status()&CC_STATUS_CPUHALTED)){
509 PLEDOUT^=PLEDPIN;//blink LED while flashing
513 //debugstr("Done flashing.");
515 PLEDOUT&=~PLEDPIN;//clear LED
519 unsigned short cc_get_pc(){
520 cmddata[0]=CCCMD_GET_PC; //0x28
525 return cmddataword[0];
528 //! Set a hardware breakpoint.
529 void cc_set_hw_brkpnt(unsigned short adr){
530 debugstr("FIXME: This certainly won't work.");
540 cmddata[0]=CCCMD_HALT; //0x44
547 cmddata[0]=CCCMD_RESUME; //0x4C
554 //! Step an instruction
555 void cc_step_instr(){
556 cmddata[0]=CCCMD_STEP_INSTR; //0x5C
562 //! Debug an instruction.
563 void cc_debug_instr(unsigned char len){
564 //Bottom two bits of command indicate length.
565 unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3); //0x54+len
567 cctrans8(cmd); //Second command code
568 cccmd(len&0x3); //Command itself.
573 //! Debug an instruction, for local use.
574 unsigned char cc_debug(unsigned char len,
578 unsigned char cmd=CCCMD_DEBUG_INSTR+(len&0x3);//0x54+len
588 return cctrans8(0x00);
591 //! Fetch a byte of code memory.
592 unsigned char cc_peekcodebyte(unsigned long adr){
593 /** See page 9 of SWRA124 */
594 unsigned char bank=adr>>15,
600 //MOV MEMCTR, (bank*16)+1
601 cc_debug(3, 0x75, 0xC7, (bank<<4) + 1);
603 cc_debug(3, 0x90, hb, lb);
607 cc_debug(2, 0xE4, 0, 0);
609 toret=cc_debug(3, 0x93, 0, 0);
611 //cc_debug(1, 0xA3, 0, 0);
617 //! Set a byte of data memory.
618 unsigned char cc_pokedatabyte(unsigned int adr,
625 cc_debug(3, 0x90, hb, lb);
627 cc_debug(2, 0x74, val, 0);
629 cc_debug(1, 0xF0, 0, 0);
633 DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
634 for (n = 0; n < count; n++) {
635 DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
636 DEBUG_INSTR(IN: 0xF0, OUT: Discard);
637 DEBUG_INSTR(IN: 0xA3, OUT: Discard);
642 //! Fetch a byte of data memory.
643 unsigned char cc_peekdatabyte(unsigned int adr){
649 cc_debug(3, 0x90, hb, lb);
651 //Must be 2, perhaps for clocking?
652 return cc_debug(3, 0xE0, 0, 0);
656 //! Fetch a byte of IRAM.
657 u8 cc_peekirambyte(u8 adr){
659 cc_debug(2, 0xE4, 0, 0);
661 return cc_debug(3, 0xE5, adr, 0);
664 //! Write a byte of IRAM.
665 u8 cc_pokeirambyte(u8 adr, u8 val){
667 cc_debug(2, 0xE4, 0, 0);
669 return cc_debug(3, 0x75, adr, val);
670 //return cc_debug(3, 0x75, val, adr);