Spy-Bi-Wire support, from examples by Mark rages.
[goodfet] / firmware / apps / jtag / jtag430.c
1 /*! \file jtag430.c
2   \author Travis Goodspeed <travis at radiantmachines.com>
3   \brief MSP430 JTAG (16-bit)
4 */
5
6 #include "platform.h"
7 #include "command.h"
8 #include "jtag430.h"
9
10 //! Handles classic MSP430 JTAG commands.  Forwards others to JTAG.
11 void jtag430_handle_fn(uint8_t const app,
12                        uint8_t const verb,
13                        uint32_t const len);
14
15 // define the jtag430 app's app_t
16 app_t const jtag430_app = {
17   /* app number */
18   JTAG430,
19   
20   /* handle fn */
21   jtag430_handle_fn,
22   
23   /* name */
24   "JTAG430",
25   
26   /* desc */
27   "\tThe JTAG430 app adds to the basic JTAG app\n"
28   "\tsupport for JTAG'ing MSP430 devices.\n"
29 };
30
31 unsigned int jtag430mode=MSP430X2MODE;
32
33 //! Set a register.
34 void jtag430_setr(u8 reg, u16 val){
35   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
36   jtag_dr_shift16(0x3401);// release low byte
37   jtag_ir_shift8(IR_DATA_16BIT);
38   
39   //0x4030 is "MOV #foo, r0"
40   //Right-most field is register, so 0x4035 loads r5
41   jtag_dr_shift16(0x4030+reg);
42   CLRTCLK;
43   SETTCLK;
44   jtag_dr_shift16(val);// Value for the register
45   CLRTCLK;
46   jtag_ir_shift8(IR_ADDR_CAPTURE);
47   SETTCLK;
48   CLRTCLK ;// Now reg is set to new value.
49   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
50   jtag_dr_shift16(0x2401);// low byte controlled by JTAG
51 }
52
53 //! Set the program counter.
54 void jtag430_setpc(unsigned int adr){
55   jtag430_setr(0,adr);
56 }
57
58 //! Halt the CPU
59 void jtag430_haltcpu(){
60   //jtag430_setinstrfetch();
61   
62   jtag_ir_shift8(IR_DATA_16BIT);
63   jtag_dr_shift16(0x3FFF);//JMP $+0
64   
65   CLRTCLK;
66   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
67   jtag_dr_shift16(0x2409);//set JTAG_HALT bit
68   SETTCLK;
69 }
70
71 //! Release the CPU
72 void jtag430_releasecpu(){
73   CLRTCLK;
74   //debugstr("Releasing target MSP430.");
75   
76   /*
77   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
78   jtag_dr_shift16(0x2C01); //Apply reset.
79   jtag_dr_shift16(0x2401); //Release reset.
80   */
81   jtag_ir_shift8(IR_CNTRL_SIG_RELEASE);
82   SETTCLK;
83 }
84
85 //! Read data from address
86 unsigned int jtag430_readmem(unsigned int adr){
87   unsigned int toret;
88   jtag430_haltcpu();
89   
90   CLRTCLK;
91   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
92   
93   if(adr>0xFF)
94     jtag_dr_shift16(0x2409);//word read
95   else
96     jtag_dr_shift16(0x2419);//byte read
97   jtag_ir_shift8(IR_ADDR_16BIT);
98   jtag_dr_shiftadr(adr);//address
99   jtag_ir_shift8(IR_DATA_TO_ADDR);
100   SETTCLK;
101
102   CLRTCLK;
103   toret=jtag_dr_shift16(0x0000);//16 bit return
104   
105   return toret;
106 }
107
108 //! Write data to address.
109 void jtag430_writemem(unsigned int adr, unsigned int data){
110   CLRTCLK;
111   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
112   if(adr>0xFF)
113     jtag_dr_shift16(0x2408);//word write
114   else
115     jtag_dr_shift16(0x2418);//byte write
116   jtag_ir_shift8(IR_ADDR_16BIT);
117   jtag_dr_shiftadr(adr);
118   jtag_ir_shift8(IR_DATA_TO_ADDR);
119   jtag_dr_shift16(data);
120   SETTCLK;
121 }
122
123 //! Write data to flash memory.  Must be preconfigured.
124 void jtag430_writeflashword(unsigned int adr, unsigned int data){
125   
126   CLRTCLK;
127   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
128   jtag_dr_shift16(0x2408);//word write
129   jtag_ir_shift8(IR_ADDR_16BIT);
130   jtag_dr_shiftadr(adr);
131   jtag_ir_shift8(IR_DATA_TO_ADDR);
132   jtag_dr_shift16(data);
133   SETTCLK;
134   
135   //Return to read mode.
136   CLRTCLK;
137   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
138   jtag_dr_shift16(0x2409);
139   
140   /*
141   jtag430_writemem(adr,data);
142   CLRTCLK;
143   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
144   jtag_dr_shift16(0x2409);
145   */
146   
147   //Pulse TCLK
148   jtag430_tclk_flashpulses(35); //35 standard
149 }
150
151 //! Configure flash, then write a word.
152 void jtag430_writeflash(unsigned int adr, unsigned int data){
153   jtag430_haltcpu();
154   
155   //FCTL1=0xA540, enabling flash write
156   jtag430_writemem(0x0128, 0xA540);
157   //FCTL2=0xA540, selecting MCLK as source, DIV=1
158   jtag430_writemem(0x012A, 0xA540);
159   //FCTL3=0xA500, should be 0xA540 for Info Seg A on 2xx chips.
160   jtag430_writemem(0x012C, 0xA500); //all but info flash.
161   //if(jtag430_readmem(0x012C));
162   
163   //Write the word itself.
164   jtag430_writeflashword(adr,data);
165   
166   //FCTL1=0xA500, disabling flash write
167   jtag430_writemem(0x0128, 0xA500);
168   
169   //jtag430_releasecpu();
170 }
171
172
173
174 //! Power-On Reset
175 void jtag430_por(){
176   // Perform Reset
177   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
178   jtag_dr_shift16(0x2C01); // apply
179   jtag_dr_shift16(0x2401); // remove
180   CLRTCLK;
181   SETTCLK;
182   CLRTCLK;
183   SETTCLK;
184   CLRTCLK;
185   jtagid = jtag_ir_shift8(IR_ADDR_CAPTURE); // get JTAG identifier
186   SETTCLK;
187   
188   jtag430_writemem(0x0120, 0x5A80);   // Diabled Watchdog
189 }
190
191
192
193 #define ERASE_GLOB 0xA50E
194 #define ERASE_ALLMAIN 0xA50C
195 #define ERASE_MASS 0xA506
196 #define ERASE_MAIN 0xA504
197 #define ERASE_SGMT 0xA502
198
199 //! Configure flash, then write a word.
200 void jtag430_eraseflash(unsigned int mode, unsigned int adr, unsigned int count,
201                         unsigned int info){
202   jtag430_haltcpu();
203   
204   //FCTL1= erase mode
205   jtag430_writemem(0x0128, mode);
206   //FCTL2=0xA540, selecting MCLK as source, DIV=1
207   jtag430_writemem(0x012A, 0xA540);
208   //FCTL3=0xA500, should be 0xA540 for Info Seg A on 2xx chips.
209   if(info)
210     jtag430_writemem(0x012C, 0xA540);
211   else
212     jtag430_writemem(0x012C, 0xA500);
213   
214   //Write the erase word.
215   jtag430_writemem(adr, 0x55AA);
216   //Return to read mode.
217   CLRTCLK;
218   jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
219   jtag_dr_shift16(0x2409);
220   
221   //Send the pulses.
222   jtag430_tclk_flashpulses(count);
223   
224   //FCTL1=0xA500, disabling flash write
225   jtag430_writemem(0x0128, 0xA500);
226   
227   //jtag430_releasecpu();
228 }
229
230
231 //! Reset the TAP state machine.
232 void jtag430_resettap(){
233   int i;
234   // Settle output
235   SETTDI; //430X2
236   SETTMS;
237   //SETTDI; //classic
238   TCKTOCK;
239
240   // Navigate to reset state.
241   // Should be at least six.
242   for(i=0;i<4;i++){
243     TCKTOCK;
244   }
245
246   // test-logic-reset
247   CLRTMS;
248   TCKTOCK;
249   SETTMS;
250   // idle
251
252     
253   /* sacred, by spec.
254      Sometimes this isn't necessary.  */
255   // fuse check
256   CLRTMS;
257   delay(50);
258   SETTMS;
259   CLRTMS;
260   delay(50);
261   SETTMS;
262   /**/
263   
264 }
265
266
267 //! Get the JTAG ID
268 unsigned char jtag430x2_jtagid(){
269   jtag430_resettap();
270   jtagid=jtag_ir_shift8(IR_BYPASS);
271   if(jtagid!=0x89 && jtagid!=0x91){
272     debugstr("Unknown JTAG ID");
273     debughex(jtagid);
274   }
275   return jtagid;
276 }
277 //! Start JTAG, take pins
278 unsigned char jtag430x2_start(){
279   jtagsetup();
280   
281   //Known-good starting position.
282   //Might be unnecessary.
283   SETTST;
284   SETRST;
285   
286   delay(0xFFFF);
287   
288   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
289   CLRRST;
290   delay(20);//10
291   CLRTST;
292
293   delay(10);//5
294   SETTST;
295   msdelay(10);//5
296   SETRST;
297   P5DIR&=~RST;
298   
299   delay(0xFFFF);
300   
301   //Perform a reset and disable watchdog.
302   return jtag430x2_jtagid();
303 }
304
305
306 //! Start JTAG, take pins
307 void jtag430_start(){
308   jtagsetup();
309   
310   //Known-good starting position.
311   //Might be unnecessary.
312   SETTST;
313   SETRST;
314   delay(0xFFFF);
315
316
317   #ifndef SBWREWRITE
318   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
319   CLRRST;
320   delay(100); //100
321   CLRTST;
322   delay(50);  //50
323   SETTST;
324   delay(50);  //50
325   SETRST;
326   P5DIR&=~RST;
327   delay(0xFFFF);
328   #endif
329   
330   //Perform a reset and disable watchdog.
331   jtag430_por();
332   jtag430_writemem(0x120,0x5a80);//disable watchdog
333   
334   jtag430_haltcpu();
335 }
336
337 //! Stop JTAG.
338 void jtag430_stop(){
339   debugstr("Exiting JTAG.");
340   jtagsetup();
341   
342   //Known-good starting position.
343   //Might be unnecessary.
344   //SETTST;
345   CLRTST;
346   SETRST;
347   delay(0xFFFF);
348   
349   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
350   CLRRST;
351   delay(0xFFFF);
352   SETRST;
353   //P5DIR&=~RST;
354   //delay(0xFFFF);
355   
356 }
357
358 //! Set CPU to Instruction Fetch
359 void jtag430_setinstrfetch(){
360   
361   jtag_ir_shift8(IR_CNTRL_SIG_CAPTURE);
362
363   // Wait until instruction fetch state.
364   while(1){
365     if (jtag_dr_shift16(0x0000) & 0x0080)
366       return;
367     CLRTCLK;
368     SETTCLK;
369   }
370 }
371
372
373
374
375
376 //! Handles classic MSP430 JTAG commands.  Forwards others to JTAG.
377 void jtag430_handle_fn(uint8_t const app,
378                        uint8_t const verb,
379                        uint32_t const len)
380 {
381   unsigned long at, l;
382   unsigned int i, val;
383   
384   
385   /* FIXME
386    * Sometimes JTAG doesn't init correctly.
387    * This restarts the connection if the masked-rom
388    * chip ID cannot be read.  Should print warning
389    * for testing server.
390    */
391   if (jtagid!=0)
392     while((i=jtag430_readmem(0xff0))==0xFFFF){
393       debugstr("Reconnecting to target MSP430.");
394       jtag430x2_start();
395       PLEDOUT^=PLEDPIN;
396     }
397   PLEDOUT&=~PLEDPIN;
398   
399   
400   switch(verb){
401   case START:
402     debugstr("Using JTAG430 (instead of JTAG430X2)!");
403     
404     jtag430x2_start();
405     cmddata[0]=jtagid;
406     
407     jtag430mode=MSP430MODE;
408     
409     /* So the way this works is that a width of 20 does some
410        backward-compatibility finagling, causing the correct value
411        to be exchanged for addresses on 16-bit chips as well as the
412        new MSP430X chips.  (This has only been verified on the
413        MSP430F2xx family.  TODO verify for others.)
414     */
415     
416     drwidth=20;
417     
418     //Perform a reset and disable watchdog.
419     jtag430_por();
420     jtag430_writemem(0x120,0x5a80);//disable watchdog
421     
422     jtag430_haltcpu();
423     
424     jtag430_resettap();
425     txdata(app,verb,1);
426     
427     break;
428   case STOP:
429     jtag430_stop();
430     txdata(app,verb,0);
431     break;
432   case JTAG430_HALTCPU:
433     jtag430_haltcpu();
434     txdata(app,verb,0);
435     break;
436   case JTAG430_RELEASECPU:
437     jtag430_releasecpu();
438     txdata(app,verb,0);
439     break;
440   case JTAG430_SETINSTRFETCH:
441     jtag430_setinstrfetch();
442     txdata(app,verb,0);
443     break;
444     
445   case JTAG430_READMEM:
446   case PEEK:
447     at=cmddatalong[0];
448     
449     //Fetch large blocks for bulk fetches,
450     //small blocks for individual peeks.
451     if(len>5)
452       l=(cmddataword[2]);//always even.
453     else
454       l=2;
455     l&=~1;//clear lsbit
456     
457     txhead(app,verb,l);
458     for(i = 0; i < l; i += 2) {
459       jtag430_resettap();
460       val=jtag430_readmem(at);
461       
462       at+=2;
463       serial_tx(val&0xFF);
464       serial_tx((val&0xFF00)>>8);
465     }
466     break;
467   case JTAG430_WRITEMEM:
468   case POKE:
469     jtag430_haltcpu();
470     jtag430_writemem(cmddataword[0],cmddataword[2]);
471     cmddataword[0]=jtag430_readmem(cmddataword[0]);
472     txdata(app,verb,2);
473     break;
474   case JTAG430_WRITEFLASH:
475     at=cmddataword[0];
476     
477     for(i=0;i<(len>>1)-2;i++){
478       //debugstr("Poking flash memory.");
479       jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
480       //Reflash if needed.  Try this twice to save grace?
481       if(cmddataword[i]!=jtag430_readmem(at))
482         jtag430_writeflash(at+(i<<1),cmddataword[i+2]);
483     }
484     
485     //Return result of first write as a word.
486     cmddataword[0]=jtag430_readmem(cmddataword[0]);
487     
488     txdata(app,verb,2);
489     break;
490   case JTAG430_ERASEFLASH:
491     jtag430_eraseflash(ERASE_MASS,0xFFFE,0x3000,0);
492     txdata(app,verb,0);
493     break;
494   case JTAG430_ERASEINFO:
495     jtag430_eraseflash(ERASE_SGMT,0x1000,0x3000,1);
496     txdata(app,verb,0);
497     break;
498   case JTAG430_SETPC:
499     jtag430_haltcpu();
500     //debughex("Setting PC.");
501     //debughex(cmddataword[0]);
502     jtag430_setpc(cmddataword[0]);
503     jtag430_releasecpu();
504     txdata(app,verb,0);
505     break;
506   case JTAG430_SETREG:
507     jtag430_setr(cmddata[0],cmddataword[1]);
508     txdata(app,verb,0);
509     break;
510   case JTAG430_GETREG:
511     //jtag430_getr(cmddata[0]);
512     debugstr("JTAG430_GETREG not yet implemented.");
513     cmddataword[0]=0xDEAD;
514     txdata(app,verb,2);
515     break;
516   case JTAG430_COREIP_ID:
517     //cmddataword[0]=jtag430_coreid();
518     cmddataword[0]=0xdead;
519     txdata(app,verb,2);
520     break;
521   case JTAG430_DEVICE_ID:
522     //cmddatalong[0]=jtag430_deviceid();
523     cmddataword[0]=0xdead;
524     cmddataword[1]=0xbeef;
525     txdata(app,verb,4);
526     break;
527   default:
528     (*(jtag_app.handle))(app,verb,len);
529   }
530   //jtag430_resettap();  //DO NOT UNCOMMENT
531 }