Some bits of Spy-Bi-Wire support, thanks to Mark Rages. (Not yet complete.)
[goodfet] / firmware / apps / jtag / jtag430.c
index 66adb11..7bfcba6 100644 (file)
@@ -52,6 +52,7 @@ void jtag430_releasecpu(){
 //! Read data from address
 unsigned int jtag430_readmem(unsigned int adr){
   unsigned int toret;
+  jtag430_haltcpu();
   
   CLRTCLK;
   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
@@ -112,7 +113,6 @@ void jtag430_writeflashword(unsigned int adr, unsigned int data){
   
   //Pulse TCLK
   jtag430_tclk_flashpulses(35); //35 standard
-  
 }
 
 //! Configure flash, then write a word.
@@ -132,7 +132,7 @@ void jtag430_writeflash(unsigned int adr, unsigned int data){
   //FCTL1=0xA500, disabling flash write
   jtag430_writemem(0x0128, 0xA500);
   
-  jtag430_releasecpu();
+  //jtag430_releasecpu();
 }
 
 
@@ -188,7 +188,7 @@ void jtag430_eraseflash(unsigned int mode, unsigned int adr, unsigned int count)
   //FCTL1=0xA500, disabling flash write
   jtag430_writemem(0x0128, 0xA500);
   
-  jtag430_releasecpu();
+  //jtag430_releasecpu();
 }
 
 
@@ -238,7 +238,8 @@ void jtag430_start(){
   SETTST;
   SETRST;
   delay(0xFFFF);
-  
+
+  #ifndef SBWREWRITE
   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
   CLRRST;
   delay(100); //100
@@ -249,13 +250,39 @@ void jtag430_start(){
   SETRST;
   P5DIR&=~RST;
   delay(0xFFFF);
+  #endif
   
   //Perform a reset and disable watchdog.
   jtag430_por();
+  jtag430_writemem(0x120,0x5a80);//disable watchdog
+  
+  jtag430_haltcpu();
+}
+
+//! Start normally, not JTAG.
+void jtag430_stop(){
+  debugstr("Exiting JTAG.");
+  jtagsetup();
+  
+  //Known-good starting position.
+  //Might be unnecessary.
+  //SETTST;
+  CLRTST;
+  SETRST;
+  delay(0xFFFF);
+  
+  //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
+  CLRRST;
+  delay(0xFFFF);
+  SETRST;
+  //P5DIR&=~RST;
+  //delay(0xFFFF);
+  
 }
 
 //! Set CPU to Instruction Fetch
 void jtag430_setinstrfetch(){
+  
   jtag_ir_shift8(IR_CNTRL_SIG_CAPTURE);
 
   // Wait until instruction fetch state.
@@ -271,11 +298,25 @@ void jtag430_setinstrfetch(){
 //! Handles classic MSP430 JTAG commands.  Forwards others to JTAG.
 void jtag430handle(unsigned char app,
                   unsigned char verb,
-                  unsigned char len){
-  register char blocks;
+                  unsigned long len){
   unsigned long at;
   unsigned int i, val;
   
+  //debugstr("Classic MSP430 handler.");
+  
+  
+  /* FIXME
+   * Sometimes JTAG doesn't init correctly.
+   * This restarts the connection if the masked-rom
+   * chip ID cannot be read.  Should print warning
+   * for testing server.
+   */
+  while((i=jtag430_readmem(0xff0))==0xFFFF){
+    jtag430_start();
+    P1OUT^=1;
+  }
+  P1OUT&=~1;
+    
   switch(verb){
   case START:
     //Enter JTAG mode.
@@ -283,6 +324,12 @@ void jtag430handle(unsigned char app,
     //TAP setup, fuse check
     jtag430_resettap();
     
+    cmddata[0]=jtag_ir_shift8(IR_BYPASS);    
+    txdata(app,verb,1);
+
+    break;
+  case STOP:
+    jtag430_stop();
     txdata(app,verb,0);
     break;
   case JTAG430_HALTCPU:
@@ -300,53 +347,70 @@ void jtag430handle(unsigned char app,
     
   case JTAG430_READMEM:
   case PEEK:
-    /*
-    cmddataword[0]=jtag430_readmem(cmddataword[0]);
-    txdata(app,verb,2);
-    */
-    blocks=(len>4?cmddata[4]:1);
     at=cmddatalong[0];
     
-    len=0x80;
-    serial_tx(app);
-    serial_tx(verb);
-    serial_tx(len);
+    //Fetch large blocks for bulk fetches,
+    //small blocks for individual peeks.
+    if(len>5)
+      len=(cmddataword[2]);//always even.
+    else
+      len=2;
+    len&=~1;//clear lsbit
     
-    while(blocks--){
-      for(i=0;i<len;i+=2){
-       jtag430_resettap();
-       delay(10);
-       
-       val=jtag430_readmem(at);
-               
-       at+=2;
-       serial_tx(val&0xFF);
-       serial_tx((val&0xFF00)>>8);
-      }
+    txhead(app,verb,len);
+    for(i=0;i<len;i+=2){
+      jtag430_resettap();
+      val=jtag430_readmem(at);
+      
+      at+=2;
+      serial_tx(val&0xFF);
+      serial_tx((val&0xFF00)>>8);
     }
-    
-    
-    
     break;
   case JTAG430_WRITEMEM:
   case POKE:
+    jtag430_haltcpu();
     jtag430_writemem(cmddataword[0],cmddataword[2]);
     cmddataword[0]=jtag430_readmem(cmddataword[0]);
     txdata(app,verb,2);
     break;
+    /*
   case JTAG430_WRITEFLASH:
-    debugstr("Poking flash memory.");
+
+    //debugstr("Poking flash memory.");
     jtag430_writeflash(cmddataword[0],cmddataword[2]);
+    
+    //Try again if failure.
+    //if(cmddataword[2]!=jtag430_readmem(cmddataword[0]))
+    //  jtag430_writeflash(cmddataword[0],cmddataword[2]);
+    
+    //Return result.
+    cmddataword[0]=jtag430_readmem(cmddataword[0]);
+    
+    txdata(app,verb,2);
+    break; */
+  case JTAG430_WRITEFLASH:
+    at=cmddataword[0];
+    
+    for(i=0;i<(len>>1)-2;i++){
+      //debugstr("Poking flash memory.");
+      jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
+      //Reflash if needed.  Try this twice to save grace?
+      if(cmddataword[i]!=jtag430_readmem(at))
+       jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
+    }
+    
+    //Return result of first write as a word.
     cmddataword[0]=jtag430_readmem(cmddataword[0]);
+    
     txdata(app,verb,2);
     break;
   case JTAG430_ERASEFLASH:
-    jtag430_eraseflash(ERASE_MASS,0xFFFE,0xFFFF);
-    jtag430_eraseflash(ERASE_MASS,0xFFFE,0xFFFF);
-    jtag430_eraseflash(ERASE_MASS,0xFFFE,0xFFFF);
+    jtag430_eraseflash(ERASE_MASS,0xFFFE,0x3000);
     txdata(app,verb,0);
     break;
   case JTAG430_SETPC:
+    jtag430_haltcpu();
     jtag430_setpc(cmddataword[0]);
     txdata(app,verb,0);
     break;
@@ -361,5 +425,5 @@ void jtag430handle(unsigned char app,
   default:
     jtaghandle(app,verb,len);
   }
-  jtag430_resettap();
+  //jtag430_resettap();  //DO NOT UNCOMMENT
 }