78 #if BUILD_FTDI_CJTAG == 1
79 #define DO_CLOCK_DATA clock_data
80 #define DO_CLOCK_TMS_CS clock_tms_cs
81 #define DO_CLOCK_TMS_CS_OUT clock_tms_cs_out
83 #define DO_CLOCK_DATA mpsse_clock_data
84 #define DO_CLOCK_TMS_CS mpsse_clock_tms_cs
85 #define DO_CLOCK_TMS_CS_OUT mpsse_clock_tms_cs_out
88 #define JTAG_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
89 #define JTAG_MODE_ALT (LSB_FIRST | NEG_EDGE_IN | NEG_EDGE_OUT)
90 #define SWD_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
97 #if BUILD_FTDI_CJTAG == 1
98 #define ESCAPE_SEQ_OAC_BIT2 28
100 static void cjtag_reset_online_activate(
void);
115 static void oscan1_mpsse_clock_data(
struct mpsse_ctx *ctx,
const uint8_t *out,
unsigned int out_offset, uint8_t *in,
116 unsigned int in_offset,
unsigned int length, uint8_t
mode);
117 static void oscan1_mpsse_clock_tms_cs(
struct mpsse_ctx *ctx,
const uint8_t *out,
unsigned int out_offset, uint8_t *in,
118 unsigned int in_offset,
unsigned int length,
bool tdi, uint8_t
mode);
119 static void oscan1_mpsse_clock_tms_cs_out(
struct mpsse_ctx *ctx,
const uint8_t *out,
unsigned int out_offset,
122 static bool oscan1_mode;
127 static bool jscan3_mode;
166 if (strcmp(
name, sig->name) == 0)
176 psig = &(*psig)->
next;
178 *psig = calloc(1,
sizeof(**psig));
182 (*psig)->name = strdup(
name);
183 if (!(*psig)->name) {
222 LOG_ERROR(
"invalid signal level specifier \'%c\'(0x%02x)", value, value);
226 uint16_t old_output =
output;
235 if ((
output & 0xff) != (old_output & 0xff) || (
direction & 0xff) != (old_direction & 0xff))
237 if ((
output >> 8 != old_output >> 8) || (
direction >> 8 != old_direction >> 8))
245 uint8_t data_low = 0;
246 uint8_t data_high = 0;
260 *value_out = (((uint16_t)data_high) << 8) | data_low;
263 *value_out = ~(*value_out);
270 #if BUILD_FTDI_CJTAG == 1
271 static void clock_data(
struct mpsse_ctx *ctx,
const uint8_t *out,
unsigned int out_offset, uint8_t *in,
272 unsigned int in_offset,
unsigned int length, uint8_t
mode)
275 oscan1_mpsse_clock_data(ctx, out, out_offset, in, in_offset,
length,
mode);
280 static void clock_tms_cs(
struct mpsse_ctx *ctx,
const uint8_t *out,
unsigned int out_offset, uint8_t *in,
281 unsigned int in_offset,
unsigned int length,
bool tdi, uint8_t
mode)
284 oscan1_mpsse_clock_tms_cs(ctx, out, out_offset, in, in_offset,
length, tdi,
mode);
289 static void clock_tms_cs_out(
struct mpsse_ctx *ctx,
const uint8_t *out,
unsigned int out_offset,
293 oscan1_mpsse_clock_tms_cs_out(ctx, out, out_offset,
length, tdi,
mode);
319 assert(tms_count <= 8);
324 for (
int i = 0; i < tms_count; i++)
341 LOG_ERROR(
"couldn't set FTDI TCK speed");
346 LOG_INFO(
"ftdi: if you experience problems at higher adapter clocks, try "
347 "the command \"ftdi tdo_sample_edge falling\"");
364 *jtag_speed = khz * 1000;
373 cmd->cmd.runtest->num_cycles,
380 unsigned int i =
cmd->cmd.runtest->num_cycles;
383 unsigned int this_len = i > 7 ? 7 : i;
396 cmd->cmd.runtest->num_cycles,
429 cmd->cmd.tms->num_bits,
438 unsigned int num_states =
cmd->cmd.pathmove->num_states;
440 LOG_DEBUG_IO(
"pathmove: %u states, current: %s end: %s", num_states,
445 unsigned int bit_count = 0;
446 uint8_t tms_byte = 0;
451 while (num_states--) {
455 == path[state_count])
458 == path[state_count]) {
464 "TAP state transition",
473 if (bit_count == 7 || num_states == 0) {
489 LOG_DEBUG_IO(
"%s type:%d",
cmd->cmd.scan->ir_scan ?
"IRSCAN" :
"DRSCAN",
493 while (
cmd->cmd.scan->num_fields > 0
494 &&
cmd->cmd.scan->fields[
cmd->cmd.scan->num_fields - 1].num_bits == 0) {
495 cmd->cmd.scan->num_fields--;
499 if (!
cmd->cmd.scan->num_fields) {
504 if (
cmd->cmd.scan->ir_scan) {
517 unsigned int scan_size = 0;
519 for (
unsigned int i = 0; i <
cmd->cmd.scan->num_fields; i++, field++) {
525 cmd->cmd.scan->num_fields,
538 uint8_t last_bit = 0;
545 uint8_t tms_bits = 0x03;
587 (
cmd->cmd.scan->ir_scan) ?
"IR" :
"DR", scan_size,
604 LOG_ERROR(
"Can't assert TRST: nTRST signal is not defined");
618 LOG_ERROR(
"Can't assert SRST: nSRST signal is not defined");
649 unsigned int num_cycles =
cmd->cmd.stableclocks->num_cycles;
656 while (num_cycles > 0) {
658 unsigned int this_len = num_cycles > 7 ? 7 : num_cycles;
660 num_cycles -= this_len;
664 cmd->cmd.stableclocks->num_cycles,
672 #if BUILD_FTDI_CJTAG == 1
674 if (
cmd->cmd.reset->trst)
675 cjtag_reset_online_activate();
681 #if BUILD_FTDI_CJTAG == 1
682 cjtag_reset_online_activate();
696 LOG_ERROR(
"BUG: unknown JTAG command type encountered: %d",
cmd->type);
721 LOG_ERROR(
"error while flushing MPSSE queue: %d", retval2);
731 LOG_DEBUG(
"ftdi interface using 7 step jtag state transitions");
733 LOG_DEBUG(
"ftdi interface using shortest path jtag state transitions");
736 LOG_ERROR(
"Please specify 'adapter usb vid_pid'");
752 LOG_ERROR(
"SWD mode is active but SWD_EN signal is not defined");
758 #if BUILD_FTDI_CJTAG == 1
759 }
else if (oscan1_mode || jscan3_mode) {
762 LOG_ERROR(
"A cJTAG mode is active but JTAG_SEL signal is not defined");
768 }
else if (jscan3_mode) {
769 LOG_ERROR(
"In JScan3 mode JTAG_SEL signal cannot be dummy, data mask needed");
792 free((
void *)sig->
name);
802 #if BUILD_FTDI_CJTAG == 1
803 static void oscan1_mpsse_clock_data(
struct mpsse_ctx *ctx,
const uint8_t *out,
unsigned int out_offset, uint8_t *in,
804 unsigned int in_offset,
unsigned int length, uint8_t
mode)
806 static const uint8_t
zero;
807 static const uint8_t one = 1;
813 for (
unsigned int i = 0; i <
length; i++) {
820 bitnum = out_offset + i;
821 bit = out ? ((out[bitnum / 8] >> (bitnum % 8)) & 0x1) : 0;
826 const uint8_t tmsbits = 0x3;
847 static void oscan1_mpsse_clock_tms_cs(
struct mpsse_ctx *ctx,
const uint8_t *out,
unsigned int out_offset, uint8_t *in,
848 unsigned int in_offset,
unsigned int length,
bool tdi, uint8_t
mode)
850 static const uint8_t
zero;
851 static const uint8_t one = 1;
857 for (
unsigned int i = 0; i <
length; i++) {
865 tdibit = tdi ? 0 : 1;
868 bitnum = out_offset + i;
869 tmsbit = ((out[bitnum / 8] >> (bitnum % 8)) & 0x1);
871 if (tdibit == tmsbit) {
873 const uint8_t tmsbits = 0x3;
892 static void oscan1_mpsse_clock_tms_cs_out(
struct mpsse_ctx *ctx,
const uint8_t *out,
unsigned int out_offset,
895 oscan1_mpsse_clock_tms_cs(ctx, out, out_offset, 0, 0,
length, tdi,
mode);
898 static void cjtag_set_tck_tms_tdi(
struct signal *tck,
char tckvalue,
struct signal *tms,
899 char tmsvalue,
struct signal *tdi,
char tdivalue)
906 static void cjtag_reset_online_activate(
void)
1032 if (!oscan1_mode && !jscan3_mode)
1035 if (oscan1_mode && jscan3_mode) {
1036 LOG_ERROR(
"Both oscan1_mode and jscan3_mode are \"on\". At most one of them can be enabled.");
1041 LOG_ERROR(
"Can't run cJTAG online/activate escape sequences: TCK signal is not defined");
1046 LOG_ERROR(
"Can't run cJTAG online/activate escape sequences: TDI signal is not defined");
1051 LOG_ERROR(
"Can't run cJTAG online/activate escape sequences: TMS signal is not defined");
1056 LOG_ERROR(
"Can't run cJTAG online/activate escape sequences: TDO signal is not defined");
1108 bool invert_data =
false;
1109 uint16_t data_mask = 0;
1110 bool invert_input =
false;
1111 uint16_t input_mask = 0;
1112 bool invert_oe =
false;
1113 uint16_t oe_mask = 0;
1114 for (
unsigned int i = 1; i <
CMD_ARGC; i += 2) {
1115 if (strcmp(
"-data",
CMD_ARGV[i]) == 0) {
1116 invert_data =
false;
1118 }
else if (strcmp(
"-ndata",
CMD_ARGV[i]) == 0) {
1121 }
else if (strcmp(
"-input",
CMD_ARGV[i]) == 0) {
1122 invert_input =
false;
1124 }
else if (strcmp(
"-ninput",
CMD_ARGV[i]) == 0) {
1125 invert_input =
true;
1127 }
else if (strcmp(
"-oe",
CMD_ARGV[i]) == 0) {
1130 }
else if (strcmp(
"-noe",
CMD_ARGV[i]) == 0) {
1133 }
else if (!strcmp(
"-alias",
CMD_ARGV[i]) ||
1135 if (!strcmp(
"-nalias",
CMD_ARGV[i])) {
1137 invert_input =
true;
1212 uint16_t sig_data = 0;
1230 const struct nvp *n;
1231 static const struct nvp nvp_ftdi_jtag_modes[] = {
1234 { .name =
NULL, .value = -1 },
1251 #if BUILD_FTDI_CJTAG == 1
1280 .handler = &ftdi_handle_channel_command,
1282 .help =
"set the channel of the FTDI device that is used as JTAG",
1286 .name =
"layout_init",
1287 .handler = &ftdi_handle_layout_init_command,
1289 .help =
"initialize the FTDI GPIO signals used "
1290 "to control output-enables and reset signals",
1291 .usage =
"data direction",
1294 .name =
"layout_signal",
1295 .handler = &ftdi_handle_layout_signal_command,
1297 .help =
"define a signal controlled by one or more FTDI GPIO as data "
1298 "and/or output enable",
1299 .usage =
"name [-data mask|-ndata mask] [-oe mask|-noe mask] [-alias|-nalias name]",
1302 .name =
"set_signal",
1303 .handler = &ftdi_handle_set_signal_command,
1305 .help =
"control a layout-specific signal",
1306 .usage =
"name (1|0|z)",
1309 .name =
"get_signal",
1310 .handler = &ftdi_handle_get_signal_command,
1312 .help =
"read the value of a layout-specific signal",
1316 .name =
"tdo_sample_edge",
1317 .handler = &ftdi_handle_tdo_sample_edge_command,
1319 .help =
"set which TCK clock edge is used for sampling TDO "
1320 "- default is rising-edge (Setting to falling-edge may "
1321 "allow signalling speed increase)",
1322 .usage =
"(rising|falling)",
1324 #if BUILD_FTDI_CJTAG == 1
1326 .name =
"oscan1_mode",
1327 .handler = &ftdi_handle_oscan1_mode_command,
1329 .help =
"set to 'on' to use OScan1 mode for signaling, otherwise 'off' (default is 'off')",
1330 .usage =
"(on|off)",
1333 .name =
"jscan3_mode",
1334 .handler = &ftdi_handle_jscan3_mode_command,
1336 .help =
"set to 'on' to use JScan3 mode for signaling, otherwise 'off' (default is 'off')",
1337 .usage =
"(on|off)",
1347 .help =
"perform ftdi management",
1450 "%s%s %s %s reg %X = %08" PRIx32,
1451 check_ack ?
"" :
"ack ignored ",
1468 LOG_ERROR(
"SWD Read data parity mismatch");
1598 LOG_ERROR(
"Sequence %d not supported", seq);
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
const uint16_t * adapter_usb_get_pids(void)
unsigned int adapter_get_speed_khz(void)
Retrieves the clock speed of the adapter in kHz.
const char * adapter_usb_get_product_name(void)
const uint16_t * adapter_usb_get_vids(void)
const char * adapter_usb_get_location(void)
static void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_COMMAND_ARGUMENT_INVALID
enum scan_type jtag_scan_type(const struct scan_command *cmd)
static void move_to_state(enum tap_state goal_state)
Function move_to_state moves the TAP controller from the current state to a goal_state through a path...
static uint8_t ftdi_channel
static const struct command_registration ftdi_command_handlers[]
static void ftdi_swd_swdio_en(bool enable)
#define DO_CLOCK_TMS_CS_OUT
static struct signal * create_signal(const char *name)
static int ftdi_swd_init(void)
static uint16_t direction
static int ftdi_execute_pathmove(struct jtag_command *cmd)
static struct jtag_interface ftdi_interface
static int ftdi_execute_stableclocks(struct jtag_command *cmd)
static int ftdi_execute_sleep(struct jtag_command *cmd)
static uint16_t jtag_output_init
static int ftdi_speed(int speed)
static int ftdi_get_signal(const struct signal *s, uint16_t *value_out)
static size_t swd_cmd_queue_length
struct adapter_driver ftdi_adapter_driver
static const struct swd_driver ftdi_swd
static struct mpsse_ctx * mpsse_ctx
static size_t swd_cmd_queue_alloced
static int ftdi_reset(int trst, int srst)
static int ftdi_khz(int khz, int *jtag_speed)
static int ftdi_speed_div(int speed, int *khz)
static int ftdi_execute_queue(struct jtag_command *cmd_queue)
static int ftdi_execute_scan(struct jtag_command *cmd)
static int ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
static int ftdi_execute_tms(struct jtag_command *cmd)
Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG (or SWD) state machine.
static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
static int ftdi_swd_run_queue(void)
Flush the MPSSE queue and process the SWD transaction queue.
static int ftdi_execute_statemove(struct jtag_command *cmd)
static uint16_t jtag_direction_init
static int ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
static struct signal * find_signal_by_name(const char *name)
static int ftdi_initialize(void)
static int ftdi_quit(void)
COMMAND_HANDLER(ftdi_handle_channel_command)
static int create_default_signal(const char *name, uint16_t data_mask)
static uint8_t ftdi_jtag_mode
static int ftdi_execute_runtest(struct jtag_command *cmd)
static int ftdi_execute_command(struct jtag_command *cmd)
static int ftdi_swd_switch_seq(enum swd_special_seq seq)
static int create_signals(void)
static const struct command_registration ftdi_subcommand_handlers[]
static int ftdi_set_signal(const struct signal *s, char value)
static struct signal * signals
static struct swd_cmd_queue_entry * swd_cmd_queue
enum tap_state tap_get_end_state(void)
For more information,.
enum tap_state tap_state_transition(enum tap_state cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
const char * tap_state_name(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
int tap_get_tms_path_len(enum tap_state from, enum tap_state to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
int tap_set_end_state_stable(enum tap_state new_end_state)
Similar as tap_set_end_state(), checks new_end_state is stable.
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
int tap_get_tms_path(enum tap_state from, enum tap_state to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
#define DEBUG_CAP_TMS_SEQ
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
void jtag_sleep(uint32_t us)
enum reset_types jtag_get_reset_config(void)
tap_state
Defines JTAG Test Access Port states.
#define ERROR_JTAG_QUEUE_FAILED
#define ERROR_JTAG_INIT_FAILED
#define ERROR_JTAG_TRANSITION_INVALID
#define LOG_CUSTOM_LEVEL(level, expr ...)
#define LOG_DEBUG_IO(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
int mpsse_flush(struct mpsse_ctx *ctx)
void mpsse_read_data_bits_high_byte(struct mpsse_ctx *ctx, uint8_t *data)
void mpsse_set_data_bits_high_byte(struct mpsse_ctx *ctx, uint8_t data, uint8_t dir)
int mpsse_set_frequency(struct mpsse_ctx *ctx, int frequency)
void mpsse_read_data_bits_low_byte(struct mpsse_ctx *ctx, uint8_t *data)
void mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, unsigned int length, bool tdi, uint8_t mode)
void mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in, unsigned int in_offset, unsigned int length, uint8_t mode)
void mpsse_clock_data_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, unsigned int length, uint8_t mode)
void mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in, unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode)
struct mpsse_ctx * mpsse_open(const uint16_t vids[], const uint16_t pids[], const char *description, const char *serial, const char *location, int channel)
bool mpsse_is_high_speed(struct mpsse_ctx *ctx)
void mpsse_close(struct mpsse_ctx *ctx)
void mpsse_clock_data_in(struct mpsse_ctx *ctx, uint8_t *in, unsigned int in_offset, unsigned int length, uint8_t mode)
void mpsse_loopback_config(struct mpsse_ctx *ctx, bool enable)
void mpsse_set_data_bits_low_byte(struct mpsse_ctx *ctx, uint8_t data, uint8_t dir)
const struct nvp * nvp_name2value(const struct nvp *p, const char *name)
const struct nvp * nvp_value2name(const struct nvp *p, int value)
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
const char * usage
a string listing the options and arguments, required or optional
Represents a driver for a debugging interface.
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Name Value Pairs, aka: NVP.
This structure defines a single scan field in the scan.
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
const uint8_t * out_value
A pointer to value to be scanned into the device.
unsigned int num_bits
The number of bits this field specifies.
uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4+3+32+1+4, 8)]
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
static const unsigned int swd_seq_dormant_to_swd_len
static const uint8_t swd_seq_dormant_to_jtag[]
Dormant-to-JTAG sequence.
static const uint8_t swd_seq_dormant_to_swd[]
Dormant-to-SWD sequence.
static const uint8_t swd_seq_jtag_to_dormant[]
JTAG-to-dormant sequence.
static bool swd_cmd_returns_ack(uint8_t cmd)
Test if we can rely on ACK returned by SWD command.
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
static const unsigned int swd_seq_jtag_to_swd_len
static const unsigned int swd_seq_line_reset_len
static const unsigned int swd_seq_dormant_to_jtag_len
static const unsigned int swd_seq_swd_to_dormant_len
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
static const unsigned int swd_seq_swd_to_jtag_len
static const unsigned int swd_seq_jtag_to_dormant_len
static const uint8_t swd_seq_swd_to_dormant[]
SWD-to-dormant sequence.
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
static unsigned int parity(unsigned int v)