X-Git-Url: http://git.rot13.org/?p=goodfet;a=blobdiff_plain;f=firmware%2Fapps%2Fjtag%2Fjtag430.c;h=a8a07bd49d09373dd4a387073af6b2c9183ced4d;hp=7aea264ed001f4ce7594e80f954cd685f21af013;hb=c1105ada99267eaceb19d45d422b7d652e619b2e;hpb=33c548994df07e43f25464b7d3f24642e9142a99 diff --git a/firmware/apps/jtag/jtag430.c b/firmware/apps/jtag/jtag430.c index 7aea264..a8a07bd 100644 --- a/firmware/apps/jtag/jtag430.c +++ b/firmware/apps/jtag/jtag430.c @@ -5,8 +5,29 @@ #include "platform.h" #include "command.h" -#include "jtag.h" +#include "jtag430.h" +//! Handles classic MSP430 JTAG commands. Forwards others to JTAG. +void jtag430_handle_fn(uint8_t const app, + uint8_t const verb, + uint32_t const len); + +// define the jtag430 app's app_t +app_t const jtag430_app = { + + /* app number */ + JTAG430, + + /* handle fn */ + jtag430_handle_fn, + + /* name */ + "JTAG430", + + /* desc */ + "\tThe JTAG430 app adds to the basic JTAG app\n" + "\tsupport for JTAG'ing MSP430 devices.\n" +}; unsigned int jtag430mode=MSP430X2MODE; @@ -51,9 +72,14 @@ void jtag430_haltcpu(){ //! Release the CPU void jtag430_releasecpu(){ CLRTCLK; + //debugstr("Releasing target MSP430."); + + /* jtag_ir_shift8(IR_CNTRL_SIG_16BIT); - jtag_dr_shift16(0x2401); - jtag_ir_shift8(IR_ADDR_CAPTURE); + jtag_dr_shift16(0x2C01); //Apply reset. + jtag_dr_shift16(0x2401); //Release reset. + */ + jtag_ir_shift8(IR_CNTRL_SIG_RELEASE); SETTCLK; } @@ -70,7 +96,7 @@ unsigned int jtag430_readmem(unsigned int adr){ else jtag_dr_shift16(0x2419);//byte read jtag_ir_shift8(IR_ADDR_16BIT); - jtag_dr_shift16(adr);//address + jtag_dr_shiftadr(adr);//address jtag_ir_shift8(IR_DATA_TO_ADDR); SETTCLK; @@ -89,7 +115,7 @@ void jtag430_writemem(unsigned int adr, unsigned int data){ else jtag_dr_shift16(0x2418);//byte write jtag_ir_shift8(IR_ADDR_16BIT); - jtag_dr_shift16(adr); + jtag_dr_shiftadr(adr); jtag_ir_shift8(IR_DATA_TO_ADDR); jtag_dr_shift16(data); SETTCLK; @@ -102,7 +128,7 @@ void jtag430_writeflashword(unsigned int adr, unsigned int data){ jtag_ir_shift8(IR_CNTRL_SIG_16BIT); jtag_dr_shift16(0x2408);//word write jtag_ir_shift8(IR_ADDR_16BIT); - jtag_dr_shift16(adr); + jtag_dr_shiftadr(adr); jtag_ir_shift8(IR_DATA_TO_ADDR); jtag_dr_shift16(data); SETTCLK; @@ -133,6 +159,7 @@ void jtag430_writeflash(unsigned int adr, unsigned int data){ jtag430_writemem(0x012A, 0xA540); //FCTL3=0xA500, should be 0xA540 for Info Seg A on 2xx chips. jtag430_writemem(0x012C, 0xA500); //all but info flash. + //if(jtag430_readmem(0x012C)); //Write the word itself. jtag430_writeflashword(adr,data); @@ -173,7 +200,8 @@ void jtag430_por(){ #define ERASE_SGMT 0xA502 //! Configure flash, then write a word. -void jtag430_eraseflash(unsigned int mode, unsigned int adr, unsigned int count){ +void jtag430_eraseflash(unsigned int mode, unsigned int adr, unsigned int count, + unsigned int info){ jtag430_haltcpu(); //FCTL1= erase mode @@ -181,7 +209,10 @@ void jtag430_eraseflash(unsigned int mode, unsigned int adr, unsigned int count) //FCTL2=0xA540, selecting MCLK as source, DIV=1 jtag430_writemem(0x012A, 0xA540); //FCTL3=0xA500, should be 0xA540 for Info Seg A on 2xx chips. - jtag430_writemem(0x012C, 0xA500); + if(info) + jtag430_writemem(0x012C, 0xA540); + else + jtag430_writemem(0x012C, 0xA500); //Write the erase word. jtag430_writemem(adr, 0x55AA); @@ -235,6 +266,47 @@ void jtag430_resettap(){ } +unsigned char jtagid; + +//! Get the JTAG ID +unsigned char jtag430x2_jtagid(){ + jtag430_resettap(); + jtagid=jtag_ir_shift8(IR_BYPASS); + if(jtagid!=0x89 && jtagid!=0x91){ + debugstr("Unknown JTAG ID"); + debughex(jtagid); + } + return jtagid; +} +//! Start JTAG, take pins +unsigned char jtag430x2_start(){ + jtagsetup(); + + //Known-good starting position. + //Might be unnecessary. + SETTST; + SETRST; + + delay(0xFFFF); + + //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG + CLRRST; + delay(20);//10 + CLRTST; + + delay(10);//5 + SETTST; + msdelay(10);//5 + SETRST; + P5DIR&=~RST; + + delay(0xFFFF); + + //Perform a reset and disable watchdog. + return jtag430x2_jtagid(); +} + + //! Start JTAG, take pins void jtag430_start(){ jtagsetup(); @@ -301,16 +373,29 @@ void jtag430_setinstrfetch(){ } } +//! Grab the core ID. +unsigned int jtag430_coreid(){ + jtag_ir_shift8(IR_COREIP_ID); + return jtag_dr_shift16(0); +} + +//! Grab the device ID. +unsigned long jtag430_deviceid(){ + jtag_ir_shift8(IR_DEVICE_ID); + return jtag_dr_shift20(0); +} + + + //! Handles classic MSP430 JTAG commands. Forwards others to JTAG. -void jtag430handle(unsigned char app, - unsigned char verb, - unsigned long len){ - unsigned long at; +void jtag430_handle_fn(uint8_t const app, + uint8_t const verb, + uint32_t const len) +{ + unsigned long at, l; unsigned int i, val; - //debugstr("Classic MSP430 handler."); - /* FIXME * Sometimes JTAG doesn't init correctly. @@ -319,22 +404,47 @@ void jtag430handle(unsigned char app, * for testing server. */ while((i=jtag430_readmem(0xff0))==0xFFFF){ - jtag430_start(); - P1OUT^=1; + debugstr("Reconnecting to target MSP430."); + jtag430x2_start(); + PLEDOUT^=PLEDPIN; } - P1OUT&=~1; + PLEDOUT&=~PLEDPIN; switch(verb){ case START: + /* old method, classic MSP430. //Enter JTAG mode. - jtag430_start(); + jtag430x2_start(); //TAP setup, fuse check jtag430_resettap(); cmddata[0]=jtag_ir_shift8(IR_BYPASS); txdata(app,verb,1); - + */ + jtag430x2_start(); + cmddata[0]=jtagid; + + jtag430mode=MSP430MODE; + + /* So the way this works is that a width of 20 does some + backward-compatibility finagling, causing the correct value + to be exchanged for addresses on 16-bit chips as well as the + new MSP430X chips. (This has only been verified on the + MSP430F2xx family. TODO verify for others.) + */ + + drwidth=20; + + //Perform a reset and disable watchdog. + jtag430_por(); + jtag430_writemem(0x120,0x5a80);//disable watchdog + + jtag430_haltcpu(); + + jtag430_resettap(); + txdata(app,verb,1); + break; case STOP: jtag430_stop(); @@ -360,13 +470,13 @@ void jtag430handle(unsigned char app, //Fetch large blocks for bulk fetches, //small blocks for individual peeks. if(len>5) - len=(cmddataword[2]);//always even. + l=(cmddataword[2]);//always even. else - len=2; - len&=~1;//clear lsbit + l=2; + l&=~1;//clear lsbit - txhead(app,verb,len); - for(i=0;i