78 #define GPIO_CNT_EXT (GPIO_CNT + 1)
79 #define CH347F_SRST_FAKE_GPIO GPIO_CNT
80 #define CH347T_TRST_GPIO 5
81 #define CH347F_TRST_GPIO 3
85 #define GPIO_SET_L (BIT(4) | BIT(5) | BIT(6) | BIT(7))
86 #define GPIO_SET_H (BIT(3) | BIT(4) | BIT(5) | BIT(6) | BIT(7))
88 #define GPIO_SET_INPUT (BIT(6) | BIT(7))
90 #define VENDOR_VERSION 0x5F
93 #define HW_TDO_BUF_SIZE 4096
95 #define LARGER_PACK_MAX_SIZE 51200
98 #define UCMDPKT_DATA_MAX_BYTES_USBHS 507
99 #define USBC_PACKET_USBHS 512
100 #define CH347_CMD_HEADER 3
101 #define CH347_CMD_INIT_READ_LEN 1
103 #define CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE 9
105 #define MAX_BITS_PER_BIT_OP 248
109 #define CH347_CMD_INFO_RD 0xCA
110 #define CH347_CMD_GPIO 0xCC
111 #define CH347_CMD_JTAG_INIT 0xD0
112 #define CH347_CMD_JTAG_BIT_OP 0xD1
113 #define CH347_CMD_JTAG_BIT_OP_RD 0xD2
114 #define CH347_CMD_JTAG_DATA_SHIFT 0xD3
115 #define CH347_CMD_JTAG_DATA_SHIFT_RD 0xD4
116 #define CH347_CMD_PIN_SET 0xE2
119 #define CH347_CMD_SWD_INIT 0xE5
120 #define CH347_CMD_SWD 0xE8
121 #define CH347_CMD_SWD_REG_W 0xA0
122 #define CH347_CMD_SWD_SEQ_W 0xA1
123 #define CH347_CMD_SWD_REG_R 0xA2
124 #define CH347_MAX_PROCESSING_US 7000
126 #define CH347_MAX_SEND_BUF UCMDPKT_DATA_MAX_BYTES_USBHS
127 #define CH347_MAX_RECV_BUF (USBC_PACKET_USBHS - CH347_CMD_HEADER)
128 #define CH347_MAX_CMD_BUF 128
129 #define CH347_SWD_CLOCK_MAX 5000
130 #define CH347_SWD_CLOCK_BASE 1000
132 #define CH347_SWD_CLOCK_MAX_DIVISOR (CH347_MAX_PROCESSING_US / swd_seq_dormant_to_swd_len)
134 #define CH347_EPOUT 0x06u
135 #define CH347_EPIN 0x86u
136 #define CH347T_MPHSI_INTERFACE 2
137 #define CH347F_MPHSI_INTERFACE 4
138 #define USB_WRITE_TIMEOUT 500
139 #define USB_READ_TIMEOUT 500
141 #define BYTEWISE_MODE_VERSION 0x241
144 #define DEFAULT_VENDOR_ID 0x1a86
145 #define DEFAULT_CH347T_PRODUCT_ID 0x55dd
146 #define DEFAULT_CH347F_PRODUCT_ID 0x55de
147 #define DEFAULT_OTHER_PRODUCT_ID 0x55e7
286 [1] = { .jtag_use =
true },
287 [2] = { .bit =
TMS_H, .jtag_use =
true, .swd_use =
true, .ptr = &
ch347.
tms_pin },
293 [4] = { .bit =
TCK_H, .jtag_use =
true, .swd_use =
true, .ptr = &
ch347.
tck_pin },
294 [5] = { .jtag_use =
true },
296 [7] = { .bit =
TMS_H, .jtag_use =
true, .swd_use =
true, .ptr = &
ch347.
tms_pin },
318 unsigned int i = 0, j = 0;
319 unsigned int n =
size * 3 + 1;
320 char *str = malloc(n);
324 while (i <
size && j < n) {
325 uint8_t
cmd = data[i++];
330 unsigned int cmd_payload_size =
le_to_h_u16(data + i);
331 if (i + 2 <=
size && j + 4 < n) {
332 hexify(str + j, data + i, 2, n - j);
341 if (i + cmd_payload_size >
size) {
343 cmd_payload_size =
size - i;
348 unsigned int swd_base_i = i;
350 while (i < swd_base_i + cmd_payload_size) {
356 unsigned int swd_bits = 0;
357 unsigned int swd_payload_size = 0;
361 hexify(str + j, data + i, 2, n - j);
371 swd_payload_size = 1;
381 swd_payload_size = 1 + 4 + 1;
383 swd_payload_size = 1;
387 hexify(str + j, data + i, MIN(swd_payload_size,
size - i), n - j);
388 i += swd_payload_size;
389 j += 2 * swd_payload_size;
398 hexify(str + j, data + i, MIN(cmd_payload_size,
size - i), n - j);
399 i += cmd_payload_size;
400 j += 2 * cmd_payload_size;
429 LOG_ERROR(
"CH347 write fail (size %d)", write_len);
465 int read_len = buf_length - done;
495 cmd->tdo_bit_count = 0;
500 cmd->read_len =
cmd->write_data_len;
513 for (
int i = 0; i <
cmd->write_data_len; i++) {
516 cmd->tdo_bit_count++;
522 cmd->read_len =
cmd->write_data_len;
524 cmd->tdo_bit_count =
cmd->read_len * 8;
546 if (!
cmd || !
cmd->write_data) {
574 static int ch347_read_scan(uint8_t *decoded_buf,
int decoded_buf_len,
int raw_read_len)
577 uint8_t *read_buf = malloc(read_bf_len);
591 int decoded_buf_idx = 0;
594 unsigned int type = read_buf[rd_idx++];
595 uint16_t data_len =
le_to_h_u16(&read_buf[rd_idx]);
603 LOG_ERROR(
"CH347 unexpected returned data length from init CMD");
613 LOG_ERROR(
"CH347 unexpected returned data length from CMD GPIO");
622 if (!decoded_buf || decoded_buf_idx + data_len > decoded_buf_len) {
623 LOG_ERROR(
"CH347 no decoded_buf or too small");
627 memcpy(&decoded_buf[decoded_buf_idx], &read_buf[rd_idx], data_len);
628 decoded_buf_idx += data_len;
633 if (!decoded_buf || decoded_buf_idx +
DIV_ROUND_UP(data_len, 8) > decoded_buf_len) {
634 LOG_ERROR(
"CH347 no decoded_buf or too small");
638 for (
int i = 0; i < data_len; i++) {
639 if (read_buf[rd_idx + i] &
BIT(0))
640 decoded_buf[decoded_buf_idx + i / 8] |=
BIT(i % 8);
642 decoded_buf[decoded_buf_idx + i / 8] &= ~(
BIT(i % 8));
649 LOG_ERROR(
"CH347 SWD recv buf too small");
682 for (
int i = 0; i <
scan->fields_len; i++) {
683 int num_bits =
scan->fields[i].num_bits;
686 if (
scan->fields[i].in_value) {
687 uint8_t *capture_buf = malloc(
DIV_ROUND_UP(num_bits, 8));
700 buf_cpy(captured,
scan->fields[i].in_value, num_bits);
716 if (byte_offset < decoded_buf_len)
717 LOG_DEBUG(
"decoded %d of %d bytes", byte_offset, decoded_buf_len);
734 free(
cmd->write_data);
767 int decoded_buf_len = 0;
773 uint8_t *decoded_buf =
NULL;
774 int decoded_buf_idx = 0;
775 if (decoded_buf_len > 0) {
776 decoded_buf = malloc(decoded_buf_len);
786 int total_tdo_count = 0;
787 int bytes_to_write = 0;
792 total_tdo_count +=
cmd->tdo_bit_count;
800 bytes_to_write = total_len;
804 if (!last_cmd || bytes_to_write == 0) {
811 uint8_t *write_buf = malloc(bytes_to_write);
819 int bytes_to_read = 0;
820 int current_decoded_buf_len = 0;
825 write_buf[idx++] =
cmd->type;
828 memcpy(&write_buf[idx],
cmd->write_data,
cmd->write_data_len);
829 idx +=
cmd->write_data_len;
831 if (
cmd->read_len > 0) {
839 free(
cmd->write_data);
858 if (!current_decoded_buf_len) {
862 retval =
ch347_read_scan(&decoded_buf[decoded_buf_idx], current_decoded_buf_len, bytes_to_read);
863 decoded_buf_idx += current_decoded_buf_len;
954 scan->fields = scan_fields;
955 scan->fields_len = scan_fields_len;
1008 LOG_ERROR(
"call ch347_next_cmd first!");
1047 LOG_ERROR(
"call ch347_next_cmd first!");
1053 LOG_ERROR(
"BUG: CH347 command 0x%02" PRIx8
1054 " is not variable data size type and payload is too long",
1059 int remaining =
count;
1061 while (remaining > 0) {
1071 bytes_idx += bytes_to_store;
1072 remaining -= bytes_to_store;
1105 uint8_t cmd_data[] = {0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1110 uint8_t init_result;
1115 LOG_DEBUG(
"CH347F PIN_SET result %02" PRIx8, init_result);
1153 for (
int i = 0; i <
count; i++) {
1191 LOG_DEBUG_IO(
"TMS Value: %02x..., step = %d, skip = %d", tms_value[0],
step, skip);
1197 for (
int i = skip; i <
step; i++) {
1220 for (
unsigned int i = 0; i <
cmd->num_states; i++) {
1275 int byte_count = bits_len / 8;
1276 int bit_count = bits_len % 8;
1279 if (byte_count > 0 && bit_count == 0) {
1288 bit_count = bits_len;
1294 if (byte_count > 0) {
1322 for (
int i = 0; i < bit_count; i++) {
1327 if (i + 1 == bit_count)
1392 static const char *
const type2str[] = {
"",
"SCAN_IN",
"SCAN_OUT",
"SCAN_IO"};
1395 uint8_t *buf =
NULL;
1412 LOG_DEBUG_IO(
"scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d",
1413 cmd->ir_scan ?
"IRSCAN" :
"DRSCAN",
1415 scan_bits, log_buf,
cmd->end_state);
1464 unsigned int *gpios_used)
1498 uint8_t *gpios_data, uint8_t *bit_op)
1504 bool value = active ^ gpio_config->
active_low;
1531 gpios_data[gpio_num] = gpio_val;
1534 if (gpio_to_bit_op[gpio_num].
ptr) {
1539 *gpio_to_bit_op[gpio_num].
ptr = value ? gpio_to_bit_op[gpio_num].
bit : 0;
1546 if (gpio_to_bit_op[gpio_num].
ptr) {
1547 *gpio_to_bit_op[gpio_num].
ptr = value ? gpio_to_bit_op[gpio_num].
bit : 0;
1548 *bit_op |= gpio_to_bit_op[gpio_num].
bit;
1551 LOG_WARNING(
"%s drive mode ignored, GPIO %u is push-pull only",
1568 }
else if (!active) {
1574 LOG_ERROR(
"Too old CH347 version, %s GPIO configuration not supported",
1583 uint8_t *gpios_data, uint8_t *bit_op)
1592 LOG_WARNING(
"Cannot set %s GPIO to initial input state",
1600 uint8_t *gpios_data, uint8_t *bit_op)
1612 LOG_WARNING(
"Cannot set %s GPIO to exit input state",
1649 if (flush_now && (bit_op || any_gpio_set))
1652 return retval1 ==
ERROR_OK ? retval2 : retval1;
1695 return retval1 !=
ERROR_OK ? retval1 : retval2;
1727 switch (
cmd->type) {
1750 LOG_ERROR(
"BUG: unknown JTAG command type 0x%X",
cmd->type);
1792 char error_message[256];
1793 snprintf(error_message,
sizeof(error_message),
"CH347 not found. Tried VID/PID pairs: ");
1794 for (
int i = 0; ch347_vids[i] != 0; i++)
1795 snprintf(error_message + strlen(error_message),
sizeof(error_message) - strlen(error_message),
1796 "%04x:%04x ", ch347_vids[i], ch347_pids[i]);
1802 struct libusb_device_descriptor ch347_device_descriptor;
1805 LOG_ERROR(
"CH347 error calling libusb_get_device");
1810 retval = libusb_get_device_descriptor(
device, &ch347_device_descriptor);
1812 LOG_ERROR(
"CH347 error getting device descriptor: %s", libusb_error_name(retval));
1820 if (retval != LIBUSB_SUCCESS && retval != LIBUSB_ERROR_BUSY)
1821 LOG_WARNING(
"CH347 set_configuration failed: %s", libusb_error_name(retval));
1829 LOG_ERROR(
"CH347 unable to claim interface: %s", libusb_error_name(retval));
1838 char firmware_version;
1840 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1841 VENDOR_VERSION, 0, 0, &firmware_version,
sizeof(firmware_version),
1844 LOG_ERROR(
"CH347 unable to get firmware version");
1849 char manufacturer[256 + 1];
1850 if (libusb_get_string_descriptor_ascii(
ch347_handle, ch347_device_descriptor.iManufacturer,
1851 (
unsigned char *)manufacturer,
sizeof(manufacturer) - 1) < 0) {
1852 strcpy(manufacturer,
"(unknown)");
1854 char product[256 + 1];
1855 if (libusb_get_string_descriptor_ascii(
ch347_handle, ch347_device_descriptor.iProduct,
1856 (
unsigned char *)product,
sizeof(product) - 1) < 0) {
1857 strcpy(product,
"(unknown)");
1859 char serial_number[256 + 1];
1860 if (libusb_get_string_descriptor_ascii(
ch347_handle, ch347_device_descriptor.iSerialNumber,
1861 (
unsigned char *)serial_number,
sizeof(serial_number) - 1) < 0) {
1862 strcpy(serial_number,
"(unknown)");
1865 LOG_INFO(
"CH347%c %s from vendor %s with serial number %s found. (Chip version=%X.%02X, Firmware=0x%02X)",
1870 (ch347_device_descriptor.bcdDevice >> 8) & 0xFF,
1871 ch347_device_descriptor.bcdDevice & 0xFF,
1880 LOG_INFO(
"CH347T old version of the chip, JTAG only working in bitwise mode. For bytewise mode at least version %X.%X is needed.",
1885 if (ch347_device_descriptor.bcdDevice < 0x441)
1891 if (ch347_device_descriptor.bcdDevice < 0x441)
1892 LOG_WARNING(
"CH347T version older than 4.41 probably does not support SWD transport");
1894 if (ch347_device_descriptor.bcdDevice == 0x441)
1895 LOG_WARNING(
"If CH347T version 4.41 cannot connect or SWD fails often, insert a resistor to SWDIO circuit");
1897 }
else if (ch347_device_descriptor.bcdDevice == 0x241) {
1898 LOG_WARNING(
"CH347T version 2.41 has very weird clock timing, may not work with a slower JTAG device");
1901 if (ch347_device_descriptor.bcdDevice < 0x544)
1902 LOG_INFO(
"Please upgrade CH347T firmware to a production version >= 5.44");
1907 if (ch347_device_descriptor.bcdDevice < 0x101)
1908 LOG_INFO(
"Please upgrade CH347F firmware to a production version >= 1.1");
1960 for (
int i = 0; i < 4; i++) {
1971 *supports_larger_pack_mode =
mode != 0;
2013 uint8_t cmd_data[] = {0x40, 0x42, 0x0f, 0x00, clock_divisor, 0x00, 0x00, 0x00 };
2021 uint8_t init_result = 0;
2023 LOG_DEBUG(
"SWD init clk div %" PRIu8
", result %02" PRIx8,
2024 clock_divisor, init_result);
2062 bool supports_larger_pack_mode;
2095 *khz = speeds[speed_idx];
2111 LOG_ERROR(
"Adaptive clocking not supported");
2138 while (idx > 0 && speeds[idx] > khz)
2141 if (speeds[idx] > khz) {
2142 LOG_WARNING(
"Requested speed of %d kHz is not possible. Using %d kHz",
2153 .help =
"perform ch347 management",
2191 unsigned int gpios_used = 0;
2197 gpios_used |=
BIT(i);
2233 LOG_WARNING(
"CH347F dedicated SRST enable failed");
2258 LOG_INFO(
"CH347 SWD mode enabled");
2380 len = 1 + 1 + 1 + 1;
2384 len = 1 + 1 + 4 + 1;
2391 len = 1 + 1 + 1 + 1 + 4 + 1;
2428 && ap_delay_clk > max_processing_clk;
2439 : expected_total_clk / 5;
2451 unsigned int this_delay_clk = MIN(ap_delay_clk, 255);
2452 if ((
unsigned int)send_room * 8 < this_delay_clk)
2453 this_delay_clk = send_room * 8;
2459 ap_delay_clk -= this_delay_clk;
2477 LOG_ERROR(
"ch347 SWD queue not empty after ch347_swd_run_queue");
2483 unsigned int this_delay_clk = MIN(ap_delay_clk, 255);
2484 if (send_room * 8 < this_delay_clk)
2485 this_delay_clk = send_room * 8;
2486 if (max_processing_clk < this_delay_clk)
2487 this_delay_clk = max_processing_clk;
2491 ap_delay_clk -= this_delay_clk;
2494 }
while (ap_delay_clk);
2509 LOG_ERROR(
"CH347 usb write/read failed");
2523 LOG_ERROR(
"CH347 usb write/read failed - not CH347_CMD_SWD_SEQ_W");
2538 uint32_t data =
buf_get_u32(&recv_buf[recv_len], 0, 32);
2542 "%s%s %s read reg %X = %08" PRIx32,
2543 check_ack ?
"" :
"ack ignored ",
2558 LOG_ERROR(
"SWD Read data parity mismatch");
2564 *pswd_io->
dst = data;
2567 LOG_ERROR(
"CH347 usb write/read failed - not SWD_CMD_RNW");
2580 "%s%s %s write reg %X = %08" PRIx32,
2581 check_ack ?
"" :
"ack ignored ",
2595 LOG_ERROR(
"CH347 usb write/read failed recv_len = %d", recv_len);
2651 : expected_total_clk / 5;
2653 LOG_DEBUG_IO(
"Expected queue run %u cycles, with this cmd %u",
2672 LOG_ERROR(
"ch347 SWD queue not empty after ch347_swd_run_queue");
2685 pswd_io->
value = data;
2724 LOG_ERROR(
"Sequence %d not supported", seq);
const struct adapter_gpio_config * adapter_gpio_get_config(void)
Retrieves gpio configuration set with command "adapter gpio <signal_name>".
const uint16_t * adapter_usb_get_pids(void)
const char * adapter_usb_get_product_name(void)
const uint16_t * adapter_usb_get_vids(void)
struct adapter_gpio_config gpios[ADAPTER_GPIO_IDX_NUM]
const char * adapter_gpio_get_name(enum adapter_gpio_config_index idx)
Retrieves gpio name.
@ ADAPTER_GPIO_INIT_STATE_ACTIVE
@ ADAPTER_GPIO_INIT_STATE_INPUT
@ ADAPTER_GPIO_EXIT_STATE_NO_CHANGE
@ ADAPTER_GPIO_EXIT_STATE_INPUT
@ ADAPTER_GPIO_EXIT_STATE_ACTIVE
adapter_gpio_config_index
Adapter GPIO.
adapter_gpio_drive_mode
Supported output drive modes for adaptor GPIO.
@ ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
@ ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
@ ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
#define ADAPTER_GPIO_NOT_SET
static const struct device_t * device
char * buf_to_hex_str(const void *_buf, unsigned int buf_len)
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
void * buf_cpy(const void *from, void *_to, unsigned int size)
Copies size bits out of from and into to.
size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t length)
Convert binary data into a string of hexadecimal pairs.
Support functions to access arbitrary bits in a byte array.
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 int ch347_srst_enable(void)
Enable the dedicated SRST output on CH347F devices.
static int ch347_scratchpad_add_move_state(enum tap_state state, int skip)
Toggle the tap state to the target state.
static int ch347_cmd_from_scratchpad(void)
copy the scratchpad content into a new command in the command queue
struct adapter_driver ch347_adapter_driver
static int ch347_scratchpad_add_bytes(uint8_t *bytes, int count)
adds bytes from a buffer to the scratchpad if scratchpad is full after this byte the command will be ...
#define CH347_MAX_CMD_BUF
static int ch347_init_pack_size(void)
inits ch347.pack_size and ch347.max_len
static int ch347_check_gpio_unique(enum adapter_gpio_config_index gpio_idx, unsigned int *gpios_used)
static int ch347_swd_switch_out(enum swd_special_seq seq, const uint8_t *out, unsigned int out_len)
static int ch347_scan_data_to_fields(uint8_t *decoded_buf, int decoded_buf_len)
Used to put the data from the decoded buffer into the scan command fields.
static const int ch347_larger_pack_clock_speeds[]
static int ch347_quit(void)
CH347 Device Release Function.
static int ch347_single_read_get_byte(uint8_t *byte)
Function executes the single command and deliver one byte from the buffer that's read back from USB.
#define CH347F_MPHSI_INTERFACE
#define DEFAULT_CH347F_PRODUCT_ID
static struct ch347_swd_io * ch347_get_one_swd_io(void)
#define CH347_MAX_PROCESSING_US
static int ch347_read_data(uint8_t *data, int buf_length, int *length)
reads data from the CH347 via libusb driver See https://libusb.sourceforge.io/api-1....
static int ch347_scratchpad_add_move_path(struct pathmove_command *cmd)
Obtain the current Tap status and switch to the status TMS value passed down by cmd.
#define CH347_SWD_CLOCK_MAX
#define DEFAULT_OTHER_PRODUCT_ID
#define CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE
static void ch347_cmd_calc_reads(struct ch347_cmd *cmd)
calculates the amount of bits and bytes that should be read for this command
static const struct command_registration ch347_command_handlers[]
#define DEFAULT_VENDOR_ID
static void ch347_write_swd_reg(uint8_t cmd, const uint32_t out)
static bool ch347_dedicated_srst_enabled
#define CH347_CMD_JTAG_INIT
static int ch347_cmd_start_next(uint8_t type)
starts the next command in the scratchpad.
#define CH347_SWD_CLOCK_BASE
#define CH347_MAX_SEND_BUF
static bool ch347_is_single_cmd_type(uint8_t type)
#define CH347_CMD_INIT_READ_LEN
static int ch347_swd_run_queue(void)
static const struct swd_driver ch347_swd
static int ch347_speed_get_index(int khz, int *speed_idx)
converts the requested speed to a CH347 speed index
static int ch347_adapter_set_speed(uint8_t clock_index)
Sends the CH347_CMD_JTAG_INIT (D0) command to get the JTAG interface initialized with the speed index...
static int ch347_open_device(void)
opens the CH347 device via libusb driver
static int ch347_scratchpad_add_stableclocks(int count)
Function adds a certain amount of TCK pulses without changing the TMS pin.
static const uint16_t default_ch347_vids[]
static int ch347_scratchpad_add_byte(uint8_t byte)
adds one byte to the scratchpad if scratchpad is full after this byte the command will be created fro...
static int ch347_set_gpio(enum adapter_gpio_config_index gpio_idx, bool active, uint8_t *gpios_data, uint8_t *bit_op)
static const struct ch347_bit_op ch347t_gpio_to_bit_op[GPIO_CNT_EXT]
static int ch347_execute_queue(struct jtag_command *cmd_queue)
Executes the command queue.
static int ch347_swd_send_idle(uint32_t ap_delay_clk)
static int ch347_cmd_transmit_queue(void)
Sends all queued commands and puts the data read back into the scan fields.
static void log_buf_dump(const uint8_t *data, unsigned int size, bool recv)
#define CH347_CMD_SWD_INIT
static bool ch347_cmd_has_variable_data_size(uint8_t type)
static int ch347_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
static int ch347_gpios_update(uint8_t *gpios_data, bool bit_op, bool flush_now)
Update GPIOS or JTAG_BIT_OP controlled outputs Avoid failing on the first error - the second part may...
static int ch347_swd_queue_flush(void)
static int ch347_scratchpad_add_pin_byte(void)
adds the output pin byte to the scratchpad if scratchpad is full after this byte the command will be ...
static void ch347_write_spec_seq(const uint8_t *out, uint8_t out_len)
static struct libusb_device_handle * ch347_handle
#define UCMDPKT_DATA_MAX_BYTES_USBHS
static int ch347_scan_queue_fields(struct scan_field *scan_fields, int scan_fields_len)
queue the scan fields into the scan queue
static int ch347_scratchpad_add_run_test(int cycles, enum tap_state state)
Toggle the Tap state to run test/idle.
#define CH347_MAX_RECV_BUF
static int ch347_init_gpio(enum adapter_gpio_config_index gpio_idx, uint8_t *gpios_data, uint8_t *bit_op)
static unsigned int ch347_trst_gpio(void)
GPIO number of the TRST pin.
static int ch347_speed_get(int speed_idx, int *khz)
returns the speed in kHz for the given speed index
#define CH347_CMD_PIN_SET
static int ch347_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
static int ch347_swd_switch_seq(enum swd_special_seq seq)
static unsigned int ch347_get_gpio_num_or_default(enum adapter_gpio_config_index gpio_idx)
static struct jtag_interface ch347_interface
static bool ch347_chk_buf_size(uint8_t cmd, uint32_t ap_delay_clk)
static int ch347_scratchpad_add_write_read(struct scan_command *cmd, uint8_t *bits, int bits_len, enum scan_type scan)
CH347 Batch read/write function.
#define USB_WRITE_TIMEOUT
static int ch347_write_data(uint8_t *data, int *length)
writes data to the CH347 via libusb driver
static struct ch347_info ch347
#define CH347_SWD_CLOCK_MAX_DIVISOR
#define DEFAULT_CH347T_PRODUCT_ID
#define CH347_CMD_JTAG_DATA_SHIFT
static int ch347_read_scan(uint8_t *decoded_buf, int decoded_buf_len, int raw_read_len)
Reads data back from CH347 and decode it byte- and bitwise into the buffer.
#define CH347_CMD_SWD_REG_R
static uint8_t ch347_gpio_drive_value(bool level, enum adapter_gpio_drive_mode drive)
Translates an output level into a GPIO command byte for a drive mode.
#define CH347F_SRST_FAKE_GPIO
static int ch347_scratchpad_add_tms_change(const uint8_t *tms_value, int step, int skip)
Function that performs state switching by changing the value of TMS.
static int ch347_swd_init(void)
Initialization for the swd mode.
static const struct ch347_bit_op ch347f_gpio_to_bit_op[GPIO_CNT_EXT]
#define CH347T_MPHSI_INTERFACE
static int ch347_exit_gpio(enum adapter_gpio_config_index gpio_idx, uint8_t *gpios_data, uint8_t *bit_op)
static int ch347_sleep(int us)
Flushes the command buffer and sleeps for a specific timespan.
static void ch347_read_swd_reg(uint8_t cmd)
#define CH347_CMD_JTAG_BIT_OP
static int ch347_adapter_supports_larger_pack_mode(bool *supports_larger_pack_mode)
Sends the CH347_CMD_JTAG_INIT (D0) command to ask the JTAG interface with special clock index 9 for t...
static int ch347_init(void)
CH347 Initialization function.
static int ch347_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
static int ch347_adapter_init(uint8_t clock_index, bool *supports_larger_pack_mode)
Sends the CH347_CMD_JTAG_INIT (D0) command to get the JTAG interface initialized with the speed index...
#define LARGER_PACK_MAX_SIZE
static const int ch347_standard_pack_clock_speeds[]
static const uint16_t default_ch347_pids[]
static int ch347_scratchpad_add_clock_tms(bool tms)
Function used to change the TMS value at the rising edge of TCK to switch its TAP state.
static int ch347_cmd_transmit_queue_inner(void)
Sends the write buffer via libusb and if LARGER_PACK mode is active read also data back.
static int ch347_swd_run_queue_inner(void)
static void ch347_activity_led_set(bool led_state, bool flush_now)
Turn the activity LED on or off Errors are intentionally ignored.
#define CH347_CMD_SWD_SEQ_W
#define CH347_CMD_SWD_REG_W
static void ch347_scratchpad_check_full(void)
checks if the scratchpad is full.
static void ch347_queues_discard(void)
Drops everything queued but not transmitted yet: the scratchpad, the command queue and the scan field...
static int ch347_swd_init_cmd(uint8_t clock_divisor)
swd init function
#define CH347_CMD_JTAG_BIT_OP_RD
static int ch347_speed_set(int speed_index)
Initializes the JTAG interface and set CH347 TCK frequency.
#define BYTEWISE_MODE_VERSION
#define USBC_PACKET_USBHS
static int ch347_reset(int trst, int srst)
Control (assert/deassert) the signals SRST and TRST on the interface.
static int ch347_scratchpad_add_scan(struct scan_command *cmd)
Switch to SHIFT-DR or SHIFT-IR status for scanning.
#define MAX_BITS_PER_BIT_OP
#define CH347_CMD_JTAG_DATA_SHIFT_RD
static int ch347_scratchpad_add_tck_idle(void)
Return TCK to its low idle level, if it isn't there already.
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
enum scan_type jtag_scan_type(const struct scan_command *cmd)
scan_type
The inferred type of a scan_command structure, indicating whether the command has the host scan in fr...
@ SCAN_IN
From device to host,.
@ SCAN_IO
Full-duplex scan.
uint32_t size
Size of dw_spi_transaction::buffer.
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...
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)
tap_state
Defines JTAG Test Access Port states.
#define ERROR_JTAG_TRANSITION_INVALID
static struct scan_blk scan
int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], const char *product, struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
int jtag_libusb_bulk_write(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_control_transfer(struct libusb_device_handle *dev, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, char *bytes, uint16_t size, unsigned int timeout, int *transferred)
void jtag_libusb_close(struct libusb_device_handle *dev)
int jtag_libusb_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
#define list_first_entry(ptr, type, member)
static void list_add_tail(struct list_head *new, struct list_head *head)
static int list_empty(const struct list_head *head)
#define list_for_each_entry_safe(p, n, h, field)
#define list_for_each_entry(p, h, field)
static void list_del(struct list_head *entry)
#define list_entry(ptr, type, field)
#define list_for_each_safe(p, n, head)
static void list_del_init(struct list_head *entry)
static void INIT_LIST_HEAD(struct list_head *list)
#define LOG_CUSTOM_LEVEL(level, expr ...)
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_LEVEL_IS(FOO)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
const unsigned char bit_offset
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
static int step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
enum stldr_func_type type
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
Configuration options for a single GPIO.
enum adapter_gpio_exit_state exit_state
enum adapter_gpio_init_state init_state
enum adapter_gpio_drive_mode drive
unsigned int srst_default_gpio
struct list_head scan_queue
struct list_head cmd_queue
bool swclk_5mhz_supported
unsigned int trst_default_gpio
enum ch347_variant chip_variant
uint8_t scratchpad[UCMDPKT_DATA_MAX_BYTES_USBHS]
uint8_t gpio_status_buf[GPIO_CNT]
uint8_t scratchpad_cmd_type
uint8_t init_result_buf[CH347_CMD_INIT_READ_LEN]
struct scan_field * fields
struct list_head free_cmd_head
struct ch347_swd_io ch347_cmd_buf[CH347_MAX_CMD_BUF]
uint8_t send_buf[CH347_MAX_SEND_BUF]
uint8_t recv_buf[CH347_MAX_RECV_BUF]
struct list_head send_cmd_head
unsigned int total_swd_clk
struct list_head list_entry
Represents a driver for a debugging interface.
unsigned int supported
Bit vector listing capabilities exposed by this driver.
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
This structure defines a single scan field in the scan.
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 uint8_t swd_cmd(bool is_read, bool is_ap, uint8_t regnum)
Construct a "cmd" byte, in lSB bit order, which swd_driver.read_reg() and swd_driver....
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.
static uint16_t le_to_h_u16(const uint8_t *buf)
static void h_u32_to_le(uint8_t *buf, uint32_t val)
#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 void h_u16_to_le(uint8_t *buf, uint16_t val)
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
static unsigned int parity(unsigned int v)