67 #define PACKET_SIZE (64 + 1) 68 #define USB_TIMEOUT 1000 71 #define CMD_DAP_INFO 0x00 72 #define CMD_DAP_LED 0x01 73 #define CMD_DAP_CONNECT 0x02 74 #define CMD_DAP_DISCONNECT 0x03 75 #define CMD_DAP_WRITE_ABORT 0x08 76 #define CMD_DAP_DELAY 0x09 77 #define CMD_DAP_RESET_TARGET 0x0A 80 #define INFO_ID_VID 0x00 81 #define INFO_ID_PID 0x02 82 #define INFO_ID_SERNUM 0x03 83 #define INFO_ID_FW_VER 0x04 84 #define INFO_ID_TD_VEND 0x05 85 #define INFO_ID_TD_NAME 0x06 86 #define INFO_ID_CAPS 0xf0 87 #define INFO_ID_PKT_CNT 0xfe 88 #define INFO_ID_PKT_SZ 0xff 90 #define INFO_CAPS_SWD 0x01 91 #define INFO_CAPS_JTAG 0x02 94 #define LED_ID_CONNECT 0x00 95 #define LED_ID_RUN 0x01 101 #define CONNECT_DEFAULT 0x00 102 #define CONNECT_SWD 0x01 103 #define CONNECT_JTAG 0x02 106 #define CMD_DAP_DELAY 0x09 107 #define CMD_DAP_SWJ_PINS 0x10 108 #define CMD_DAP_SWJ_CLOCK 0x11 109 #define CMD_DAP_SWJ_SEQ 0x12 121 #define SWJ_PIN_TCK (1<<0) 122 #define SWJ_PIN_TMS (1<<1) 123 #define SWJ_PIN_TDI (1<<2) 124 #define SWJ_PIN_TDO (1<<3) 125 #define SWJ_PIN_TRST (1<<5) 126 #define SWJ_PIN_SRST (1<<7) 129 #define CMD_DAP_SWD_CONFIGURE 0x13 132 #define CMD_DAP_JTAG_SEQ 0x14 133 #define CMD_DAP_JTAG_CONFIGURE 0x15 134 #define CMD_DAP_JTAG_IDCODE 0x16 138 #define DAP_JTAG_SEQ_TCK 0x3F 140 #define DAP_JTAG_SEQ_TMS 0x40 142 #define DAP_JTAG_SEQ_TDO 0x80 146 #define CMD_DAP_TFER_CONFIGURE 0x04 147 #define CMD_DAP_TFER 0x05 148 #define CMD_DAP_TFER_BLOCK 0x06 149 #define CMD_DAP_TFER_ABORT 0x07 153 #define DAP_ERROR 0xFF 164 #define DAP_MAX_CLOCK 5000 196 #define MAX_PENDING_SCAN_RESULTS 256 201 #define QUEUED_SEQ_BUF_LEN (cmsis_dap_handle->packet_size - 3) 213 hid_device *dev =
NULL;
215 struct hid_device_info *
devs, *cur_dev;
216 unsigned short target_vid, target_pid;
217 wchar_t *target_serial =
NULL;
220 bool serial_found =
false;
230 devs = hid_enumerate(0x0, 0x0);
232 while (
NULL != cur_dev) {
234 if (
NULL == cur_dev->product_string) {
235 LOG_DEBUG(
"Cannot read product string of device 0x%x:0x%x",
236 cur_dev->vendor_id, cur_dev->product_id);
238 if (wcsstr(cur_dev->product_string, L
"CMSIS-DAP")) {
260 if ((cur_dev->serial_number !=
NULL) && wcscmp(
cmsis_dap_serial, cur_dev->serial_number) == 0) {
270 cur_dev = cur_dev->next;
273 if (
NULL != cur_dev) {
274 target_vid = cur_dev->vendor_id;
275 target_pid = cur_dev->product_id;
280 hid_free_enumeration(devs);
282 if (target_vid == 0 && target_pid == 0) {
283 LOG_ERROR(
"unable to find CMSIS-DAP device");
287 if (hid_init() != 0) {
292 dev = hid_open(target_vid, target_pid, target_serial);
295 LOG_ERROR(
"unable to open CMSIS-DAP device 0x%x:0x%x", target_vid, target_pid);
309 cmsis_dap_handle = dap;
323 if (target_vid == 0x03eb && target_pid != 0x2145)
324 packet_size = 512 + 1;
343 free(cmsis_dap_handle);
344 cmsis_dap_handle =
NULL;
347 free(pending_transfers);
348 pending_transfers =
NULL;
356 #ifdef CMSIS_DAP_JTAG_DEBUG 371 if (retval == -1 || retval == 0) {
388 buffer[4] = delay & 0xff;
389 buffer[5] = (delay >> 8) & 0xff;
390 buffer[6] = (delay >> 16) & 0xff;
391 buffer[7] = (delay >> 24) & 0xff;
395 LOG_ERROR(
"CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
414 buffer[2] = swj_clock & 0xff;
415 buffer[3] = (swj_clock >> 8) & 0xff;
416 buffer[4] = (swj_clock >> 16) & 0xff;
417 buffer[5] = (swj_clock >> 24) & 0xff;
421 LOG_ERROR(
"CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
434 #ifdef CMSIS_DAP_JTAG_DEBUG 435 LOG_DEBUG(
"cmsis-dap TMS sequence: len=%d", s_len);
437 printf(
"%02X ", sequence[i]);
445 bit_copy(&buffer[3], 0, sequence, 0, s_len);
466 LOG_ERROR(
"CMSIS-DAP command CMD_INFO failed.");
470 *data = &(buffer[1]);
486 if (retval !=
ERROR_OK || buffer[1] != 0x00) {
487 LOG_ERROR(
"CMSIS-DAP command CMD_LED failed.");
505 LOG_ERROR(
"CMSIS-DAP command CMD_CONNECT failed.");
509 if (buffer[1] != mode) {
510 LOG_ERROR(
"CMSIS-DAP failed to connect in mode (%d)", mode);
527 LOG_ERROR(
"CMSIS-DAP command CMD_DISCONNECT failed.");
542 buffer[3] = retry_count & 0xff;
543 buffer[4] = (retry_count >> 8) & 0xff;
544 buffer[5] = match_retry & 0xff;
545 buffer[6] = (match_retry >> 8) & 0xff;
549 LOG_ERROR(
"CMSIS-DAP command CMD_TFER_Configure failed.");
567 LOG_ERROR(
"CMSIS-DAP command CMD_SWD_Configure failed.");
575 static int cmsis_dap_cmd_DAP_Delay(uint16_t
delay_us)
582 buffer[2] = delay_us & 0xff;
583 buffer[3] = (delay_us >> 8) & 0xff;
587 LOG_ERROR(
"CMSIS-DAP command CMD_Delay failed.");
612 buffer[idx++] = 0x00;
616 uint8_t cmd = pending_transfers[i].
cmd;
617 uint32_t data = pending_transfers[i].
data;
635 if (!(cmd & SWD_CMD_RnW) &&
636 !(cmd & SWD_CMD_APnDP) &&
639 LOG_DEBUG(
"refusing to enable sticky overrun detection");
640 data &= ~CORUNDETECT;
643 buffer[idx++] = (cmd >> 1) & 0x0f;
644 if (!(cmd & SWD_CMD_RnW)) {
645 buffer[idx++] = (data) & 0xff;
646 buffer[idx++] = (data >> 8) & 0xff;
647 buffer[idx++] = (data >> 16) & 0xff;
648 buffer[idx++] = (data >> 24) & 0xff;
657 uint8_t ack = buffer[idx] & 0x07;
658 if (ack !=
SWD_ACK_OK || (buffer[idx] & 0x08)) {
659 LOG_DEBUG(
"SWD ack not OK: %d %s", buffer[idx-1],
666 if (pending_transfer_count != buffer[1])
667 LOG_ERROR(
"CMSIS-DAP transfer count mismatch: expected %d, got %d",
668 pending_transfer_count, buffer[1]);
670 for (
int i = 0; i < buffer[1]; i++) {
672 static uint32_t last_read;
686 if (pending_transfers[i].buffer)
687 *(uint32_t *)pending_transfers[i].buffer = tmp;
692 pending_transfer_count = 0;
740 LOG_INFO(
"CMSIS-DAP: FW Version = %s", &data[1]);
755 uint8_t
caps = data[1];
775 LOG_INFO(
"SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
820 LOG_ERROR(
"Sequence %d not supported", seq);
831 if (cmsis_dap_handle ==
NULL) {
843 LOG_ERROR(
"CMSIS-DAP: SWD not supported");
853 LOG_INFO(
"CMSIS-DAP: Interface Initialised (SWD)");
868 if (cmsis_dap_handle ==
NULL) {
881 LOG_ERROR(
"CMSIS-DAP: JTAG not supported");
889 LOG_INFO(
"CMSIS-DAP: Interface Initialised (JTAG)");
902 uint16_t pkt_sz = data[1] + (data[2] << 8);
908 pending_transfers = malloc(pending_queue_len *
sizeof(*pending_transfers));
909 if (!pending_transfers) {
910 LOG_ERROR(
"Unable to allocate memory for CMSIS-DAP queue");
920 LOG_ERROR(
"unable to reallocate memory");
925 LOG_DEBUG(
"CMSIS-DAP: Packet Size = %" PRId16, pkt_sz);
934 uint16_t pkt_cnt = data[1];
936 LOG_DEBUG(
"CMSIS-DAP: Packet Count = %" PRId16, pkt_cnt);
979 LOG_INFO(
"CMSIS-DAP: Interface ready");
1014 LOG_ERROR(
"CMSIS-DAP: Interface reset failed");
1020 int retval = cmsis_dap_cmd_DAP_Delay(cmd->
cmd.
sleep->
us);
1029 LOG_INFO(
"cmsis-dap JTAG TLR_RESET");
1043 LOG_ERROR(
"BUG: %i is not a valid end state", state);
1048 #ifdef SPRINT_BINARY 1049 static void sprint_binary(
char *s,
const uint8_t *buf,
int offset,
int len)
1060 for (
int i = offset; i < offset + len; ++i) {
1061 uint8_t c = buf[i / 8],
mask = 1 << (i % 8);
1062 if ((i != offset) && !(i % 8))
1064 *s++ = (c &
mask) ?
'1' :
'0';
1070 #ifdef CMSIS_DAP_JTAG_DEBUG 1071 static void debug_parse_cmsis_buf(
const uint8_t *cmd,
int cmdlen)
1074 printf(
"cmsis-dap buffer (%d b): ", cmdlen);
1075 for (
int i = 0; i < cmdlen; ++i)
1076 printf(
" %02x", cmd[i]);
1080 printf(
"cmsis-dap jtag sequence command %02x (n=%d)\n", cmd[1], cmd[2]);
1089 for (
int seq = 0; seq < cmd[2]; ++seq) {
1090 uint8_t
info = cmd[pos++];
1094 printf(
" sequence %d starting %d: info %02x (len=%d tms=%d read_tdo=%d): ",
1097 printf(
" %02x", cmd[pos+i]);
1101 if (pos != cmdlen) {
1102 printf(
"BUFFER LENGTH MISMATCH looks like %d but %d specified", pos, cmdlen);
1109 LOG_DEBUG(
"unknown cmsis-dap command %02x", cmd[1]);
1120 DEBUG_JTAG_IO(
"Flushing %d queued sequences (%d bytes) with %d pending scan results to capture",
1130 #ifdef CMSIS_DAP_JTAG_DEBUG 1137 LOG_ERROR(
"CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
1141 #ifdef CMSIS_DAP_JTAG_DEBUG 1144 printf(
"%02X ", buffer[c]);
1151 DEBUG_JTAG_IO(
"Copying pending_scan_result %d/%d: %d bits from byte %d -> buffer + %d bits",
1153 #ifdef CMSIS_DAP_JTAG_DEBUG 1155 printf(
"%02X ", buffer[2+scan->
first+b]);
1163 queued_seq_buf_end = 0;
1165 pending_scan_result_count = 0;
1174 bool tms, uint8_t *
tdo_buffer,
int tdo_buffer_offset)
1176 DEBUG_JTAG_IO(
"[at %d] %d bits, tms %s, seq offset %d, tdo buf %p, tdo offset %d",
1178 s_len, tms ?
"HIGH" :
"LOW", s_offset, tdo_buffer, tdo_buffer_offset);
1185 for (
int offset = 0; offset < s_len; offset += 64) {
1186 int len = s_len - offset;
1189 DEBUG_JTAG_IO(
"Splitting long jtag sequence: %d-bit chunk starting at offset %d", len, offset);
1196 tdo_buffer ==
NULL ? 0 : (tdo_buffer_offset + offset)
1214 (s_len == 64 ? 0 : s_len);
1216 if (sequence !=
NULL)
1223 if (tdo_buffer !=
NULL) {
1242 for (
int i = 0; i < s_len; ++i) {
1243 bool bit = (sequence[i / 8] & (1 << (i % 8))) != 0;
1252 uint8_t tms_scan_bits;
1257 DEBUG_JTAG_IO(
"state move from %s to %s: %d clocks, %02X on tms",
1259 tms_scan_bits, tms_scan);
1276 LOG_DEBUG(
"discarding trailing empty field");
1299 unsigned scan_size = 0;
1311 DEBUG_JTAG_IO(
"Last field and have to move out of SHIFT state");
1323 uint8_t last_bit = 0;
1345 DEBUG_JTAG_IO(
"Internal field, staying in SHIFT state afterwards");
1371 uint8_t tms0 = 0x00;
1372 uint8_t tms1 = 0xff;
1374 for (i = 0; i < num_states; i++) {
1380 LOG_ERROR(
"BUG: %s -> %s isn't a valid TAP transition.",
1407 for (i = 0; i < num_cycles; i++)
1448 switch (cmd->
type) {
1475 LOG_ERROR(
"BUG: unknown JTAG command type 0x%X encountered", cmd->
type);
1484 while (cmd !=
NULL) {
1541 LOG_WARNING(
"ignoring extra IDs in cmsis_dap_vid_pid " 1546 LOG_WARNING(
"incomplete cmsis_dap_vid_pid configuration directive");
1554 for (i = 0; i <
CMD_ARGC; i += 2) {
1583 LOG_ERROR(
"expected exactly one argument to cmsis_dap_serial <serial-number>");
1592 .handler = &cmsis_dap_handle_info_command,
1595 .help =
"show cmsis-dap info",
1602 .
name =
"cmsis-dap",
1604 .help =
"perform CMSIS-DAP management",
1609 .
name =
"cmsis_dap_vid_pid",
1610 .handler = &cmsis_dap_handle_vid_pid_command,
1612 .help =
"the vendor ID and product ID of the CMSIS-DAP device",
1613 .usage =
"(vid pid)* ",
1616 .name =
"cmsis_dap_serial",
1617 .handler = &cmsis_dap_handle_serial_command,
1619 .help =
"set the serial number of the adapter",
1620 .usage =
"serial_string",
1637 .
name =
"cmsis-dap",
static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
int num_fields
number of fields in *fields array
static void cmsis_dap_end_state(tap_state_t state)
unsigned buffer_offset
Offset in the destination buffer.
COMMAND_HANDLER(cmsis_dap_handle_info_command)
struct pathmove_command * pathmove
#define QUEUED_SEQ_BUF_LEN
static void tap_set_state(tap_state_t new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
static const unsigned swd_seq_swd_to_jtag_len
#define LOG_DEBUG(expr...)
static int queued_seq_tdo_ptr
Represents a driver for a debugging interface.
int srst
Set SRST output: 0 = deassert, 1 = assert, -1 = no change.
uint32_t us
number of microseconds to sleep
static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
bool ir_scan
instruction/not data scan
struct jtag_interface cmsis_dap_interface
static void cmsis_dap_execute_command(struct jtag_command *cmd)
#define MAX_PENDING_SCAN_RESULTS
#define CMD_DAP_DISCONNECT
int tap_get_tms_path_len(tap_state_t from, tap_state_t to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
static int cmsis_dap_swd_run_queue(void)
static void cmsis_dap_stableclocks(int num_cycles)
static void cmsis_dap_add_tms_sequence(const uint8_t *sequence, int s_len)
#define LOG_INFO(expr...)
static int pending_scan_result_count
static const struct command_registration cmsis_dap_command_handlers[]
const uint8_t * out_value
A pointer to value to be scanned into the device.
bool tap_is_state_stable(tap_state_t astate)
Function tap_is_state_stable returns true if the astate is stable.
#define ERROR_JTAG_NOT_IMPLEMENTED
unsigned first
Offset in bytes in the CMD_DAP_JTAG_SEQ response buffer.
static void cmsis_dap_usb_close(struct cmsis_dap *dap)
unsigned length
Number of bits to read.
#define DEBUG_JTAG_IO(expr...)
void delay_us(uint16_t delay)
static struct scan_blk scan
static const struct command_registration cmsis_dap_subcommand_handlers[]
static uint32_t le_to_h_u32(const uint8_t *buf)
static int cmsis_dap_swd_init(void)
static int cmsis_dap_init(void)
tap_state_t tap_get_state()
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
static int cmsis_dap_usb_xfer(struct cmsis_dap *dap, int txlen)
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
static int cmsis_dap_speed_div(int speed, int *khz)
This structure defines a single scan field in the scan.
static void cmsis_dap_pathmove(int num_states, tap_state_t *path)
static struct ublast_lowlevel_priv info
union jtag_command_container cmd
#define CMD_DAP_SWD_CONFIGURE
int length
Number of bits to read.
static void cmsis_dap_execute_stableclocks(struct jtag_command *cmd)
int num_states
number of states in *path
static wchar_t * cmsis_dap_serial
const char * tap_state_name(tap_state_t state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state...
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
static void cmsis_dap_execute_runtest(struct jtag_command *cmd)
int num_bits
The number of bits this field specifies.
#define CMD_DAP_SWJ_CLOCK
static void cmsis_dap_add_jtag_sequence(int s_len, const uint8_t *sequence, int s_offset, bool tms, uint8_t *tdo_buffer, int tdo_buffer_offset)
static int cmsis_dap_cmd_DAP_Disconnect(void)
static int cmsis_dap_quit(void)
static struct cmsis_dap * cmsis_dap_handle
#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...
static int cmsis_dap_execute_tlr_reset(struct jtag_command *cmd)
const char *const name
The name of the JTAG interface driver.
#define ERROR_JTAG_DEVICE_ERROR
static int queued_seq_count
static int cmsis_dap_execute_queue(void)
static uint16_t cmsis_dap_pid[MAX_USB_IDS+1]
static int cmsis_dap_get_version_info(void)
static struct pending_transfer_result * pending_transfers
static void cmsis_dap_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
static int pending_queue_len
int tap_get_tms_path(tap_state_t from, tap_state_t to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
static void cmsis_dap_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
static const unsigned swd_seq_line_reset_len
#define LOG_ERROR(expr...)
#define ERROR_COMMAND_SYNTAX_ERROR
enum scan_type jtag_scan_type(const struct scan_command *cmd)
static int cmsis_dap_cmd_DAP_SWJ_Sequence(uint8_t s_len, const uint8_t *sequence)
static int cmsis_dap_khz(int khz, int *jtag_speed)
static int pending_transfer_count
struct scan_field * fields
pointer to an array of data scan fields
tap_state_t tap_get_end_state()
For more information,.
enum reset_types jtag_get_reset_config(void)
#define CMD_DAP_TFER_CONFIGURE
static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq)
static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
static void cmsis_dap_flush(void)
static void cmsis_dap_execute_scan(struct jtag_command *cmd)
static uint8_t queued_seq_buf[1024]
int first
First bit position in tdo_buffer to read.
static int cmsis_dap_speed(int speed)
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
static int cmsis_dap_get_status(void)
static uint8_t tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE]
tap_state_t tap_state_transition(tap_state_t cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
tap_state_t end_state
state in which JTAG commands should finish
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
static int cmsis_dap_swd_open(void)
static struct pending_scan_result pending_scan_results[MAX_PENDING_SCAN_RESULTS]
static void cmsis_dap_runtest(int num_cycles)
enum jtag_command_type type
static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
void tap_set_end_state(tap_state_t new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
struct runtest_command * runtest
static const uint8_t swd_seq_line_reset[]
Line reset.
static void cmsis_dap_state_move(void)
uint8_t * buffer
Location to store the result.
struct scan_command * scan
tap_state_t end_state
state in which JTAG commands should finish
static int cmsis_dap_usb_open(void)
static void cmsis_dap_execute_reset(struct jtag_command *cmd)
static const char *const info_caps_str[]
struct jtag_command * next
static const struct swd_driver cmsis_dap_swd_driver
static int cmsis_dap_cmd_DAP_LED(uint8_t leds)
static int_least32_t cmsis_dap_swd_frequency(int_least32_t hz)
static const char *const cmsis_dap_transport[]
static libusb_device ** devs
The usb device list.
static void cmsis_dap_execute_pathmove(struct jtag_command *cmd)
void jtag_sleep(uint32_t us)
static void bit_copy(uint8_t *dst, unsigned dst_offset, const uint8_t *src, unsigned src_offset, unsigned bit_count)
static int queued_seq_buf_end
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
static int cmsis_dap_get_caps_info(void)
tap_state_t * path
states that have to be passed
struct reset_command * reset
int num_cycles
number of cycles to spend in Run-Test/Idle state
static uint16_t cmsis_dap_vid[MAX_USB_IDS+1]
static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
struct sleep_command * sleep
int trst
Set TRST output: 0 = deassert, 1 = assert, -1 = no change.
static enum reset_types jtag_reset_config
unsigned jtag_get_speed_khz(void)
Retreives the clock speed of the JTAG interface in KHz.
#define LOG_WARNING(expr...)
struct jtag_command * jtag_command_queue
The current queue of jtag_command_s structures.
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
static const unsigned swd_seq_jtag_to_swd_len