2 \author Travis Goodspeed <travis at radiantmachines.com>
13 //! Handles a monitor command.
14 void jtag_handle_fn(uint8_t const app,
19 unsigned char jtagid=0;
21 // define the jtag app's app_t
22 app_t const jtag_app = {
34 "\tThe JTAG app handles basic JTAG operations such as\n"
35 "\tresetting the TAP, resetting the target, detecting\n"
36 "\tthe instruction register width, shifting bits into\n"
37 "\tboth the instruction and data registers.\n"
41 //! Remembers what the current JTAG state is
42 enum eTAPState jtag_state = UNKNOWN;
44 //! Returns true if we're in any of the data register states
47 return (int)(jtag_state & (SELECT_DR_SCAN | CAPTURE_DR |
48 SHIFT_DR | EXIT1_DR | PAUSE_DR |
49 EXIT2_DR | UPDATE_DR));
52 //! Returns true if we're in any of the instruction register states
55 return (int)(jtag_state & (SELECT_IR_SCAN | CAPTURE_IR |
56 SHIFT_IR | EXIT1_IR | PAUSE_IR |
57 EXIT2_IR | UPDATE_IR));
60 //! Returns true if we're in run-test-idle state
61 int in_run_test_idle()
63 return (int)(jtag_state & RUN_TEST_IDLE);
67 int in_state(enum eTAPState state)
69 return (int)(jtag_state & state);
73 //! Reset the target device
74 void jtag_reset_target()
82 //! Clock the JTAG clock line
91 //! Goes through test-logic-reset and ends in run-test-idle
100 jtag_tcktock(); // now in Reset state
102 jtag_tcktock(); // now in Run-Test/Idle state
103 jtag_state = RUN_TEST_IDLE;
106 //! Set up the pins for JTAG mode.
114 jtag_state = UNKNOWN;
117 //! Stop JTAG, release pins
124 //! Get into Shift-IR or Shift-DR state
125 void jtag_shift_register()
127 // assumes we're in any state that can transition to Shift-IR or Shift-DR
128 if (!in_state(CAPTURE_DR | CAPTURE_IR | SHIFT_DR |
129 SHIFT_IR | EXIT2_DR | EXIT2_IR ))
131 debugstr("Invalid JTAG state");
140 jtag_state = SHIFT_DR;
142 jtag_state = SHIFT_IR;
145 //! Get into Capture-IR state
146 void jtag_capture_ir()
148 // assumes you're in Run-Test/Idle, Update-DR or Update-IR
149 if (!in_state(RUN_TEST_IDLE | UPDATE_DR | UPDATE_IR))
151 debugstr("Invalid JTAG state");
157 jtag_tcktock(); // Select-DR-Scan
158 jtag_tcktock(); // Select-IR-Scan
160 jtag_tcktock(); // Capture-IR
162 jtag_state = CAPTURE_IR;
165 //! Get into Capture-DR state
166 void jtag_capture_dr()
168 // assumes you're in Run-Test/Idle, Update-DR or Update-IR
169 if (!in_state(RUN_TEST_IDLE | UPDATE_DR | UPDATE_IR))
171 debugstr("Invalid JTAG state");
177 jtag_tcktock(); // Select-DR-Scan
179 jtag_tcktock(); // Capture-IR
181 jtag_state = CAPTURE_DR;
184 //! Gets back to run-test-idle without going through the test-logic-reset
185 void jtag_run_test_idle()
189 if (in_state(SELECT_DR_SCAN | SELECT_IR_SCAN))
193 jtag_state <<= 1; //CAPTURE_DR or CAPTURE_IR
196 if (in_state(CAPTURE_DR | CAPTURE_IR))
200 jtag_state <<= 2; //EXIT1_DR or EXIT1_IR
203 if (in_state(SHIFT_DR | SHIFT_IR))
207 jtag_state <<= 1; //EXIT1_DR or EXIT1_IR
210 if (in_state(EXIT1_DR | EXIT1_IR))
214 jtag_state <<=3; //UPDATE_DR or UPDATE_IR
217 if (in_state(PAUSE_DR | PAUSE_IR))
221 jtag_state <<= 1; // EXIT2_DR or EXIT2_IR
224 if (in_state(EXIT2_DR | EXIT2_IR))
228 jtag_state <<= 1; // UPDATE_DR or UPDATE_IR
231 if (in_state(UPDATE_DR | UPDATE_IR | TEST_LOGIC_RESET))
235 jtag_state = RUN_TEST_IDLE;
240 // NOTE: important: THIS MODULE REVOLVES AROUND RETURNING TO RUNTEST/IDLE, OR
241 // THE FUNCTIONAL EQUIVALENT
242 //! Shift N bits over TDI/TDO. May choose LSB or MSB, and select whether to
243 // terminate (TMS-high on last bit) and whether to return to RUNTEST/IDLE
244 // flags should be 0 for most uses.
245 // for the extreme case, flags should be (NOEND|NORETDLE|LSB)
246 // other edge cases can involve a combination of those three flags
248 // the max bit-size that can be be shifted is 32-bits.
249 // for longer shifts, use the NOEND flag (which infers NORETIDLE so the
250 // additional flag is unnecessary)
252 // NORETIDLE is used for special cases where (as with arm) the debug
253 // subsystem does not want to return to the RUN-TEST/IDLE state between
255 uint32_t jtag_trans_n(uint32_t word,
257 enum eTransFlags flags)
260 uint32_t high = (1L << (bitcount - 1));
261 uint32_t mask = high - 1;
263 if (!in_state(SHIFT_IR | SHIFT_DR))
265 debugstr("jtag_trans_n from invalid TAP state");
273 for (bit = bitcount; bit > 0; bit--)
275 /* write MOSI on trailing edge of previous clock */
286 if ((bit == 1) && !(flags & NOEND))
287 SETTMS; //TMS high on last bit to exit.
291 if ((bit == 1) && !(flags & NOEND))
292 jtag_state <<= 1; // Exit1-DR or Exit1-IR
294 /* read MISO on trailing edge */
303 for (bit = bitcount; bit > 0; bit--)
305 /* write MOSI on trailing edge of previous clock */
314 word = (word & mask) << 1;
316 if ((bit==1) && !(flags & NOEND))
317 SETTMS; //TMS high on last bit to exit.
321 if ((bit == 1) && !(flags & NOEND))
322 jtag_state <<= 1; // Exit1-DR or Exit1-IR
324 /* read MISO on trailing edge */
329 //This is needed for 20-bit MSP430 chips.
330 //Might break another 20-bit chip, if one exists.
332 word = ((word << 16) | (word >> 4)) & 0x000FFFFF;
337 if (!(flags & NOEND))
342 jtag_state <<= 3; // Update-DR or Update-IR
345 if (!(flags & NORETIDLE))
350 jtag_state = RUN_TEST_IDLE;
357 //! Detects the width of the IR register
358 uint16_t jtag_detect_ir_width()
363 if (!in_run_test_idle())
365 debugstr("Not in run-test-idle state");
369 // get to shift-ir state
371 jtag_shift_register();
373 // first we shift in 1024 zeros
375 for(i = 0; i < 1024; i++)
380 // now we'll clock in ones until we see one
382 for(i = 0; i < 1024; i++)
390 // now get back to run-test-idle
391 jtag_run_test_idle();
396 //! Detects how many TAPs are in the JTAG chain
397 uint16_t jtag_detect_chain_length()
403 if (!in_run_test_idle())
405 debugstr("detect chain length must start from run-test-idle");
409 // The standard JTAG instruction for "bypass" mode is to set all 1's in the
410 // instruction register. When in "bypass" mode, the DR acts like a 1-bit
411 // shift regiser. So we can use that to detect how many TAPs are in the
414 // first get into shift IR mode
416 jtag_shift_register();
418 // then we flood the IR chain with all 1's to put all device's DRs
422 for (i = 0; i < 1024; i++)
425 SETTMS; // exit on last bit
428 jtag_state = EXIT1_IR;
433 jtag_state = UPDATE_IR;
435 // go to Shift-DR state
437 jtag_shift_register();
439 // flush the DR's with zeros
442 for (i = 0; i < 1024; i++)
447 // send 1's into the DRs until we see one come out the other side
449 for (i = 0; i < 1024; i++)
458 // now get back to run-test-idle
459 jtag_run_test_idle();
464 //! Gets device ID for specified chip in the chain
465 uint32_t jtag_get_device_id(int chip)
468 uint16_t chain_length;
474 // figure out how many devices are in the chain
475 chain_length = jtag_detect_chain_length();
477 if (chip >= chain_length)
479 debugstr("invalid part index");
483 // reset everything again because going through test-logic-reset forces
484 // all IR's to have their manufacturer specific instruction for IDCODE
485 // and loads the DR's with the chip's ID code.
488 // get into shift DR state
490 jtag_shift_register();
492 // read out the 32-bit ID codes for each device
495 for (i = 0; i < (chip + 1); i++)
497 id = jtag_trans_n(0xFFFFFFFF, 32, LSB | NOEND | NORETIDLE);
500 jtag_run_test_idle();
505 //! Shift 8 bits in/out of selected register
506 uint8_t jtag_trans_8(uint8_t in)
508 uint32_t out = jtag_trans_n((uint32_t)in, 8, MSB);
509 return (uint8_t)(0x000000FF & out);
512 //! Shift 16 bits in/out of selected register
513 uint16_t jtag_trans_16(uint16_t in)
515 uint32_t out = jtag_trans_n((uint32_t)in, 16, MSB);
516 return (uint16_t)(0x0000FFFF & out);
519 //! Shift 8 bits of the IR.
520 uint8_t jtag_ir_shift_8(uint8_t in)
522 /* Huseby's code, which breaks MSP430 support.
523 The code is broken because either the invalid jtag state error
524 causes the client to give up, or because it adds an extra clock edge.
526 if (!in_run_test_idle())
528 debugstr("Not in run-test-idle state");
532 // get intot the right state
534 jtag_shift_register();
549 //jtag_state = CAPTURE_IR;
550 jtag_state = SHIFT_IR;
552 return jtag_trans_8(in);
555 //! Shift 16 bits of the DR.
556 uint16_t jtag_dr_shift_16(uint16_t in)
559 if (!in_run_test_idle())
561 debugstr("Not in run-test-idle state");
565 // get intot the right state
567 jtag_shift_register();
569 // shift DR, then idle
570 return jtag_trans_16(in);
573 //! Handles a monitor command.
574 void jtag_handle_fn(uint8_t const app,
580 // START handled by specific JTAG
592 cmddata[0] = jtag_ir_shift_8(cmddata[0]);
597 cmddataword[0] = htons(jtag_dr_shift_16(ntohs(cmddataword[0])));
606 case JTAG_RESET_TARGET:
612 case JTAG_DETECT_IR_WIDTH:
614 cmddataword[0] = htons(jtag_detect_ir_width());
618 case JTAG_DETECT_CHAIN_LENGTH:
620 cmddataword[0] = htons(jtag_detect_chain_length());
624 case JTAG_GET_DEVICE_ID:
626 cmddatalong[0] = htonl(jtag_get_device_id(ntohs(cmddataword[0])));