7 //! Set the program counter.
8 void jtag430_setpc(unsigned int adr){
9 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
10 jtag_dr_shift16(0x3401);// release low byte
11 jtag_ir_shift8(IR_DATA_16BIT);
12 jtag_dr_shift16(0x4030);//Instruction to load PC
15 jtag_dr_shift16(adr);// Value for PC
17 jtag_ir_shift8(IR_ADDR_CAPTURE);
19 CLRTCLK ;// Now PC is set to "PC_Value"
20 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
21 jtag_dr_shift16(0x2401);// low byte controlled by JTAG
25 void jtag430_haltcpu(){
26 //jtag430_setinstrfetch();
28 jtag_ir_shift8(IR_DATA_16BIT);
29 jtag_dr_shift16(0x3FFF);//JMP $+0
32 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
33 jtag_dr_shift16(0x2409);//set JTAG_HALT bit
38 void jtag430_releasecpu(){
40 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
41 jtag_dr_shift16(0x2401);
42 jtag_ir_shift8(IR_ADDR_CAPTURE);
46 //! Read data from address
47 unsigned int jtag430_readmem(unsigned int adr){
51 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
53 jtag_dr_shift16(0x2409);//word read
55 jtag_dr_shift16(0x2419);//byte read
56 jtag_ir_shift8(IR_ADDR_16BIT);
57 jtag_dr_shift16(adr);//address
58 jtag_ir_shift8(IR_DATA_TO_ADDR);
62 toret=jtag_dr_shift16(0x0000);//16 bit return
67 //! Write data to address.
68 void jtag430_writemem(unsigned int adr, unsigned int data){
70 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
72 jtag_dr_shift16(0x2408);//word write
74 jtag_dr_shift16(0x2418);//byte write
75 jtag_ir_shift8(IR_ADDR_16BIT);
77 jtag_ir_shift8(IR_DATA_TO_ADDR);
78 jtag_dr_shift16(data);
82 //! Pulse TCLK at 350kHz +/- 100kHz
83 void jtag430_tclk_flashpulses(unsigned int i){
84 //TODO check this on a scope.
86 //At 2MHz, 350kHz is obtained with 5 clocks of delay
89 What happens if the frequency is too low or to high?
90 Is there any risk of damaging the chip, or only of a poor write?
103 //! Write data to flash memory. Must be preconfigured.
104 void jtag430_writeflashword(unsigned int adr, unsigned int data){
108 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
109 jtag_dr_shift16(0x2408);//word write
110 jtag_ir_shift8(IR_ADDR_16BIT);
111 jtag_dr_shift16(adr);
112 jtag_ir_shift8(IR_DATA_TO_ADDR);
113 jtag_dr_shift16(data);
116 //Return to read mode.
118 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
119 jtag_dr_shift16(0x2409);
122 jtag430_writemem(adr,data);
124 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
125 jtag_dr_shift16(0x2409);
129 jtag430_tclk_flashpulses(35);
130 //jtag430_releasecpu();
133 //! Configure flash, then write a word.
134 void jtag430_writeflash(unsigned int adr, unsigned int data){
137 //FCTL1=0xA540, enabling flash write
138 jtag430_writemem(0x0128, 0xA540);
139 //FCTL2=0xA540, selecting MCLK as source, DIV=1
140 jtag430_writemem(0x012A, 0xA540);
141 //FCTL3=0xA500, should be 0xA540 for Info Seg A on 2xx chips.
142 jtag430_writemem(0x012C, 0xA500);
144 //Write the word itself.
145 jtag430_writeflashword(adr,data);
147 //FCTL1=0xA500, disabling flash write
148 jtag430_writemem(0x0128, 0xA500);
150 jtag430_releasecpu();
155 #define ERASE_GLOB 0xA50E
156 #define ERASE_ALLMAIN 0xA50C
157 #define ERASE_MASS 0xA506
158 #define ERASE_MAIN 0xA504
159 #define ERASE_SGMT 0xA502
161 //! Configure flash, then write a word.
162 void jtag430_eraseflash(unsigned int mode, unsigned int adr, unsigned int count){
164 jtag430_writemem(0x0128, mode);
165 //FCTL2=0xA540, selecting MCLK as source, DIV=1
166 jtag430_writemem(0x012A, 0xA540);
167 //FCTL3=0xA500, should be 0xA540 for Info Seg A on 2xx chips.
168 jtag430_writemem(0x012C, 0xA500);
170 //Write the erase word.
171 jtag430_writemem(adr, 0x55AA);
172 //Return to read mode.
174 jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
175 jtag_dr_shift16(0x2409);
178 jtag430_tclk_flashpulses(count);
180 //FCTL1=0xA500, disabling flash write
181 jtag430_writemem(0x0128, 0xA500);
185 //! Reset the TAP state machine.
186 void jtag430_resettap(){
193 // Navigate to reset state.
194 // Should be at least six.
209 Sometimes this isn't necessary. */
221 //! Start JTAG, take pins
222 void jtag430_start(){
225 //Known-good starting position.
226 //Might be unnecessary.
231 //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
243 //! Set CPU to Instruction Fetch
244 void jtag430_setinstrfetch(){
245 jtag_ir_shift8(IR_CNTRL_SIG_CAPTURE);
247 // Wait until instruction fetch state.
249 if (jtag_dr_shift16(0x0000) & 0x0080)
256 //! Handles unique MSP430 JTAG commands. Forwards others to JTAG.
257 void jtag430handle(unsigned char app,
265 //TAP setup, fuse check
269 case JTAG430_HALTCPU:
273 case JTAG430_RELEASECPU:
274 jtag430_releasecpu();
277 case JTAG430_SETINSTRFETCH:
278 jtag430_setinstrfetch();
283 case JTAG430_READMEM:
285 cmddataword[0]=jtag430_readmem(cmddataword[0]);
288 case JTAG430_WRITEMEM:
290 jtag430_writemem(cmddataword[0],cmddataword[1]);
291 cmddataword[0]=jtag430_readmem(cmddataword[0]);
294 case JTAG430_WRITEFLASH:
295 jtag430_writeflash(cmddataword[0],cmddataword[1]);
296 cmddataword[0]=jtag430_readmem(cmddataword[0]);
299 case JTAG430_ERASEFLASH:
300 jtag430_eraseflash(ERASE_MASS,0xFFFE,0xFFFF);
304 jtag430_setpc(cmddataword[0]);
308 jtaghandle(app,verb,len);