Last of the JTAG refactoring before Spy-bi-wire support on the MSP430.
[goodfet] / firmware / apps / jtag / jtag430.c
index 18a7c38..e1c6dc6 100644 (file)
@@ -199,19 +199,17 @@ void jtag430_resettap(){
   SETTDI; //430X2
   SETTMS;
   //SETTDI; //classic
-  SETTCK;
+  TCKTOCK;
 
   // Navigate to reset state.
   // Should be at least six.
   for(i=0;i<4;i++){
-    CLRTCK;
-    SETTCK;
+    TCKTOCK;
   }
 
   // test-logic-reset
-  CLRTCK;
   CLRTMS;
-  SETTCK;
+  TCKTOCK;
   SETTMS;
   // idle
 
@@ -238,7 +236,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 +248,7 @@ void jtag430_start(){
   SETRST;
   P5DIR&=~RST;
   delay(0xFFFF);
+  #endif
   
   //Perform a reset and disable watchdog.
   jtag430_por();
@@ -257,6 +257,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(){
   
@@ -301,6 +322,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:
@@ -363,13 +390,14 @@ void jtag430handle(unsigned char app,
   case JTAG430_WRITEFLASH:
     at=cmddataword[0];
     
-    for(i=2;i<(len>>1);i++){
+    for(i=0;i<(len>>1)-2;i++){
       //debugstr("Poking flash memory.");
-      jtag430_writeflash(at,cmddataword[i]);
+      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,cmddataword[i]);
+       jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
     }
-
+    
     //Return result of first write as a word.
     cmddataword[0]=jtag430_readmem(cmddataword[0]);