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