#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;
//! 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;
}
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);
//! Power-On Reset
void jtag430_por(){
- unsigned int jtagid;
-
// Perform Reset
jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
jtag_dr_shift16(0x2C01); // apply
}
+
+//! 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();
}
+
+
+
//! 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.
* chip ID cannot be read. Should print warning
* for testing server.
*/
- while((i=jtag430_readmem(0xff0))==0xFFFF){
- jtag430_start();
- P1OUT^=1;
- }
- P1OUT&=~1;
+ if (jtagid!=0)
+ while((i=jtag430_readmem(0xff0))==0xFFFF){
+ debugstr("Reconnecting to target MSP430.");
+ jtag430x2_start();
+ PLEDOUT^=PLEDPIN;
+ }
+ 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;
+
+ if(jtagid!=MSP430JTAGID){
+ debugstr("Using JTAG430 (instead of JTAG430X2)!");
+ }
+ 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();
//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<len;i+=2){
+ txhead(app,verb,l);
+ for(i = 0; i < l; i += 2) {
jtag430_resettap();
val=jtag430_readmem(at);
txdata(app,verb,0);
break;
case JTAG430_ERASEINFO:
- jtag430_eraseflash(ERASE_MASS,0xFFFE,0x3000,1);
+ jtag430_eraseflash(ERASE_SGMT,0x1000,0x3000,1);
txdata(app,verb,0);
break;
case JTAG430_SETPC:
jtag430_haltcpu();
+ //debughex("Setting PC.");
+ //debughex(cmddataword[0]);
jtag430_setpc(cmddataword[0]);
+ jtag430_releasecpu();
txdata(app,verb,0);
break;
case JTAG430_SETREG:
txdata(app,verb,2);
break;
case JTAG430_COREIP_ID:
+ //cmddataword[0]=jtag430_coreid();
+ cmddataword[0]=0xdead;
+ txdata(app,verb,2);
+ break;
case JTAG430_DEVICE_ID:
- cmddataword[0]=0;
- cmddataword[1]=0;
+ //cmddatalong[0]=jtag430_deviceid();
+ cmddataword[0]=0xdead;
+ cmddataword[0]=0xbeef;
txdata(app,verb,4);
break;
-
default:
- jtaghandle(app,verb,len);
+ (*(jtag_app.handle))(app,verb,len);
}
//jtag430_resettap(); //DO NOT UNCOMMENT
}