Some bits of Spy-Bi-Wire support, thanks to Mark Rages. (Not yet complete.)
[goodfet] / firmware / apps / jtag / jtag430.c
index e4ee945..7bfcba6 100644 (file)
@@ -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,6 +250,7 @@ void jtag430_start(){
   SETRST;
   P5DIR&=~RST;
   delay(0xFFFF);
+  #endif
   
   //Perform a reset and disable watchdog.
   jtag430_por();
@@ -257,6 +259,27 @@ void jtag430_start(){
   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(){
   
@@ -275,13 +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.
@@ -289,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:
@@ -306,31 +347,24 @@ 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];
     
     //Fetch large blocks for bulk fetches,
     //small blocks for individual peeks.
-    if(blocks>1)
-      len=0x80;
+    if(len>5)
+      len=(cmddataword[2]);//always even.
+    else
+      len=2;
+    len&=~1;//clear lsbit
     
     txhead(app,verb,len);
-    
-    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);
-      }
+    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:
@@ -340,17 +374,35 @@ void jtag430handle(unsigned char app,
     cmddataword[0]=jtag430_readmem(cmddataword[0]);
     txdata(app,verb,2);
     break;
+    /*
   case JTAG430_WRITEFLASH:
+
     //debugstr("Poking flash memory.");
     jtag430_writeflash(cmddataword[0],cmddataword[2]);
     
     //Try again if failure.
-    if(cmddataword[0]!=jtag430_readmem(cmddataword[0]))
-      jtag430_writeflash(cmddataword[0],cmddataword[2]);
+    //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: