OpenOCD
ch347.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Driver for CH347-JTAG interface V1.1 *
5  * *
6  * Copyright (C) 2022 by oidcat. *
7  * Author: oidcatiot@163.com *
8  * *
9  * Enhancements by EasyDevKits - info@easydevkits.com *
10  * *
11  * CH347 is a high-speed USB bus converter chip that provides UART, I2C *
12  * and SPI synchronous serial ports and JTAG interface through USB bus. *
13  * *
14  * The JTAG interface by CH347 can supports transmission frequency *
15  * configuration up to 60MHz. *
16  * *
17  * The USB2.0 to JTAG scheme based on CH347 can be used to build *
18  * customized USB high-speed JTAG debugger and other products. *
19  * *
20  * _____________ *
21  * | |____JTAG/SWD (TDO,TDI,TMS,TCK,TRST) *
22  * USB__| CH347T/F | *
23  * |_____________|____UART(TXD1,RXD1,RTS1,CTS1,DTR1) *
24  * ______|______ *
25  * | | *
26  * | 8 MHz XTAL | *
27  * |_____________| *
28  * *
29  * This CH347 driver is only tested for the CH347T chip in mode 3. *
30  * The CH347 datasheet mention another chip the CH347F which was not *
31  * available for testing. *
32  * *
33  * The datasheet for the wch-ic.com's CH347 part is here: *
34  * https://www.wch-ic.com/downloads/CH347DS1_PDF.html *
35  * *
36  ***************************************************************************/
37 
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 
42 #if IS_CYGWIN == 1
43 #include "windows.h"
44 #undef LOG_ERROR
45 #endif
46 
47 // project specific includes
48 #include <jtag/interface.h>
49 #include <jtag/commands.h>
50 #include <jtag/swd.h>
51 #include <jtag/adapter.h>
52 #include <helper/time_support.h>
53 #include <helper/replacements.h>
54 #include <helper/list.h>
55 #include <helper/binarybuffer.h>
56 #include "libusb_helper.h"
57 
58 // system includes
59 #include <stdlib.h>
60 #include <string.h>
61 #include <sys/time.h>
62 #include <time.h>
63 #include <unistd.h>
64 
65 #define TDI_H BIT(4)
66 #define TDI_L 0
67 #define TMS_H BIT(1)
68 #define TMS_L 0
69 #define TCK_H BIT(0)
70 #define TCK_L 0
71 #define TRST_H BIT(5)
72 #define TRST_L 0
73 #define LED_ON 1
74 #define LED_OFF 0
75 #define GPIO_CNT 8 // the CH347 has 8 GPIO's
76 /* mask which GPIO's are available in mode 3 of CH347T only GPIO3 (Pin11 / SCL), GPIO4 (Pin15 / ACT),
77  GPIO5 (Pin9 / TRST) and GPIO6 (Pin2 / CTS1) are possible. Tested only with CH347T not CH347F chip.
78  pin numbers are for CH347T */
79 #define USEABLE_GPIOS 0x78
80 /* For GPIO command: always set bits 7 and 6 for GPIO enable
81  bits 5 and 4 for pin direction output bit 3 is the data bit */
82 #define GPIO_SET_L (BIT(4) | BIT(5) | BIT(6) | BIT(7)) // value for setting a GPIO to low
83 #define GPIO_SET_H (BIT(3) | BIT(4) | BIT(5) | BIT(6) | BIT(7)) // value for setting a GPIO to high
84 
85 #define VENDOR_VERSION 0x5F // for getting the chip version
86 
87 // maybe the hardware of the CH347 chip can capture only this amount of TDO bits
88 #define HW_TDO_BUF_SIZE 4096
89 // Don't send more than this amount of bytes via libusb in one packet. Also that is the limit for LARGER_PACK mode.
90 #define LARGER_PACK_MAX_SIZE 51200
91 // The data length contained in each command packet during USB high-speed operation
92 #define UCMDPKT_DATA_MAX_BYTES_USBHS 507
93 #define USBC_PACKET_USBHS 512 // Maximum data length per packet at USB high speed
94 #define CH347_CMD_HEADER 3 // Protocol header length (1 byte command type + 2 bytes data length)
95 #define CH347_CMD_INIT_READ_LEN 1 // for JTAG_INIT/SWD_INIT we have only one data byte
96 // if we send 9 in the init command we get the STANDARD mode or LARGER_PACK mode flag
97 #define CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE 9
98 // No more bits are allowed per CH347_CMD_JTAG_BIT_OP command; this should be dividable by 8
99 #define MAX_BITS_PER_BIT_OP 248
100 
101 /* Protocol transmission format: CMD (1 byte) + Length (2 bytes) + Data
102  Parameter acquisition, used to obtain firmware version, JTAG interface related parameters, etc */
103 #define CH347_CMD_INFO_RD 0xCA
104 #define CH347_CMD_GPIO 0xCC // GPIO Command
105 #define CH347_CMD_JTAG_INIT 0xD0 // JTAG Interface initialization command
106 #define CH347_CMD_JTAG_BIT_OP 0xD1 // JTAG interface pin bit control command
107 #define CH347_CMD_JTAG_BIT_OP_RD 0xD2 // JTAG interface pin bit control and read commands
108 #define CH347_CMD_JTAG_DATA_SHIFT 0xD3 // JTAG interface data shift command
109 #define CH347_CMD_JTAG_DATA_SHIFT_RD 0xD4 // JTAG interface data shift and read command
110 // for a single command these amount of data can be read at max
111 #define CH347_SINGLE_CMD_MAX_READ MAX(GPIO_CNT, CH347_CMD_INIT_READ_LEN)
112 
113 // for SWD
114 #define CH347_CMD_SWD_INIT 0xE5 // SWD Interface Initialization Command
115 #define CH347_CMD_SWD 0xE8 // SWD Command group header
116 #define CH347_CMD_SWD_REG_W 0xA0 // SWD Interface write reg
117 #define CH347_CMD_SWD_SEQ_W 0xA1 // SWD Interface write spec seq
118 #define CH347_CMD_SWD_REG_R 0xA2 // SWD Interface read reg
119 #define CH347_MAX_PROCESSING_US 7000 // max time in us for packet processing
120  // USB hosts disconnect the adapter if SWD processing takes more!
121 #define CH347_MAX_SEND_BUF USBC_PACKET_USBHS
122 #define CH347_MAX_RECV_BUF USBC_PACKET_USBHS
123 #define CH347_MAX_CMD_BUF 128
124 #define CH347_SWD_CLOCK_MAX 5000
125 #define CH347_SWD_CLOCK_BASE 1000
126 // limited by the longest sequence processing time
127 #define CH347_SWD_CLOCK_MAX_DIVISOR (CH347_MAX_PROCESSING_US / swd_seq_dormant_to_swd_len)
128 
129 #define CH347_EPOUT 0x06u // the usb endpoint number for writing
130 #define CH347_EPIN 0x86u // the usb endpoint number for reading
131 #define CH347T_MPHSI_INTERFACE 2 // the CH347T JTAG interface is number 2
132 #define CH347F_MPHSI_INTERFACE 4 // the CH347F JTAG interface is number 4
133 #define USB_WRITE_TIMEOUT 500 // write timeout in milliseconds
134 #define USB_READ_TIMEOUT 500 // read timeout in milliseconds
135 // BCD version for devices that can use bytewise mode below this version only bitwise mode can be used
136 #define BYTEWISE_MODE_VERSION 0x241
137 /* if the configuration is not setting the vendor id / product id we search for vendor id 1a86
138  and product id's 0x55dd (CH347T chip in mode 3), 0x55de (CH347F chip) and 0x55e7 (other chip) */
139 #define DEFAULT_VENDOR_ID 0x1a86
140 #define DEFAULT_CH347T_PRODUCT_ID 0x55dd
141 #define DEFAULT_CH347F_PRODUCT_ID 0x55de
142 #define DEFAULT_OTHER_PRODUCT_ID 0x55e7
143 
144 /* There are 3 different CH347 variants. The datasheet mentions the CH347T chip which has the product id 0x55dd
145  as default when it's configured to mode 3. The CH347F chip is also mentioned in the datasheet and has the
146  default product id 0x55de. The 3rd variant is not mentioned in a datasheet but was found here
147  https://github.com/WCHSoftGroup/ch347
148  The code from WCHSoftGroup is also searching for this product id */
150  CH347T = 0, // The CH347T chip
151  CH347F = 1, // The CH347F chip
152  OTHER_PRODUCT_ID = 2, // other product id not mentioned in the datasheet
153 };
154 
155 /* STANDARD_PACK means that we can send only one USBC_PACKET_USBHS-sized USB packet
156  and then read data back. LARGER_PACK means, we can send packets as large as
157  LARGER_PACK_MAX_SIZE. libusb splits there large packets into smaller USB packets and
158  transmit the data. Then we read back the data in a bigger packet. */
159 enum pack_size {
160  UNSET = -1,
163 };
164 
165 // for STANDARD_PACK mode: these are the 6 possible speeds; values in kHz
166 static const int ch347_standard_pack_clock_speeds[] = {
167  1875, // 1.875 MHz (60000 : 32)
168  3750, // 3.75 MHz (60000 : 16)
169  7500, // 7.5 MHz (60000 : 8)
170  15000, // 15 MHz (60000 : 4)
171  30000, // 30 MHz (60000 : 2)
172  60000 // 60 MHz
173 };
174 
175 // for LARGER_PACK mode: these are the 8 possible speeds; values in kHz
176 static const int ch347_larger_pack_clock_speeds[] = {
177  469, // 468.75 kHz (60000 : 128)
178  938, // 937.5 kHz (60000 : 64)
179  1875, // 1.875 MHz (60000 : 32)
180  3750, // 3.75 MHz (60000 : 16)
181  7500, // 7.5 MHz (60000 : 8)
182  15000, // 15 MHz (60000 : 4)
183  30000, // 30 MHz (60000 : 2)
184  60000 // 60 MHz
185 };
186 
187 struct ch347_cmd {
188  uint8_t type; // the command type
189  uint8_t *write_data; // data bytes for write
190  uint16_t write_data_len; // count of data bytes in the write_data buffer
191  uint16_t read_len; // if >0 a read is needed after this command
192  uint16_t tdo_bit_count; // how many TDO bits are needed to shift in by this read
193  struct list_head queue; // for handling a queue (list)
194 };
195 
196 struct ch347_scan {
197  struct scan_field *fields; // array of scan_field's for data from the device
198  int fields_len; // scan_fields array length
199  struct list_head queue; // for handling a queue (list)
200 };
201 
202 struct ch347_info {
203  // Record the CH347 pin status
204  int tms_pin;
205  int tdi_pin;
206  int tck_pin;
207  int trst_pin;
208 
209  enum ch347_variant chip_variant; // ch347_variant for explanation
210  // if true then we can't us the bytewise commands due to a bug of the chip; depends on BYTEWISE_MODE_VERSION
212  enum pack_size pack_size; // see: pack_size for explanation
213  int max_len; // in STANDARD_PACK or bitwise mode we can send only one USBC_PACKET_USBHS sized package
215 
216  // a "scratchpad" where we record all bytes for one command
217  uint8_t scratchpad_cmd_type; // command type
218  uint8_t scratchpad[UCMDPKT_DATA_MAX_BYTES_USBHS]; // scratchpad buffer
219  int scratchpad_idx; // current index in scratchpad
220 
221  // after a command is complete it will be stored for later processing
222  struct list_head cmd_queue;
223  // all data input scan fields are queued here
224  struct list_head scan_queue;
225  // read buffer for the single commands like CH347_CMD_GPIO and CH347_CMD_JTAG_INIT
227  int singe_read_len; // data length in single_read
228 };
229 
230 struct ch347_swd_io {
231  uint8_t usb_cmd; // 0xA0, 0xA1, 0xA2
232  uint8_t cmd;
233  uint32_t *dst;
234  uint32_t value;
235  struct list_head list_entry;
236 };
237 
241  int send_len;
242  int recv_len;
246  unsigned int clk_divisor;
247  unsigned int total_swd_clk;
248  struct list_head send_cmd_head;
249  struct list_head free_cmd_head;
251 };
252 
254 static bool swd_mode;
258 static uint16_t custom_ch347_vids[] = {0, 0, 0, 0};
259 static uint16_t custom_ch347_pids[] = {0, 0, 0, 0};
260 static char *ch347_device_desc;
261 static uint8_t ch347_activity_led_gpio_pin = 0xFF;
263 static struct ch347_info ch347;
264 static struct libusb_device_handle *ch347_handle;
265 
266 /* there are "single" commands. These commands can't be chained together and
267  need to be send as single command and need a read after write */
268 static inline bool ch347_is_single_cmd_type(uint8_t type)
269 {
271 }
272 
273 static void log_buf_dump(const uint8_t *data, unsigned int size, bool recv)
274 {
275  unsigned int i = 0, j = 0;
276  unsigned int n = size * 3 + 1;
277  char *str = malloc(n);
278  if (!str)
279  return;
280 
281  while (i < size && j < n) {
282  uint8_t cmd = data[i++];
283  hexify(str + j, &cmd, 1, n - j);
284  j += 2;
285  str[j++] = ' ';
286 
287  unsigned int cmd_payload_size = le_to_h_u16(data + i);
288  if (i + 2 <= size && j + 4 < n) {
289  hexify(str + j, data + i, 2, n - j);
290  i += 2;
291  j += 4;
292  str[j++] = ' ';
293  }
294 
295  if (cmd == CH347_CMD_SWD) {
296  // nested SWD commands
297  str[j] = '\0';
298  if (i + cmd_payload_size > size) {
299  LOG_DEBUG_IO("%s - bad size", str);
300  cmd_payload_size = size - i;
301  } else {
302  LOG_DEBUG_IO("%s", str);
303  }
304  j = 0;
305  unsigned int swd_base_i = i;
306 
307  while (i < swd_base_i + cmd_payload_size) {
308  uint8_t swd_cmd = data[i++];
309  hexify(str + j, &swd_cmd, 1, n - j);
310  j += 2;
311  str[j++] = ' ';
312 
313  unsigned int swd_bits = 0;
314  unsigned int swd_payload_size = 0;
315  if (!recv) {
316  if (i + 2 <= size) {
317  swd_bits = le_to_h_u16(data + i);
318  hexify(str + j, data + i, 2, n - j);
319  i += 2;
320  j += 4;
321  str[j++] = ' ';
322  }
323  }
324 
325  switch (swd_cmd) {
326  case CH347_CMD_SWD_REG_W:
327  if (recv)
328  swd_payload_size = 1;
329  else
330  swd_payload_size = DIV_ROUND_UP(swd_bits, 8);
331  break;
332  case CH347_CMD_SWD_SEQ_W:
333  if (!recv)
334  swd_payload_size = DIV_ROUND_UP(swd_bits, 8);
335  break;
336  case CH347_CMD_SWD_REG_R:
337  if (recv)
338  swd_payload_size = 1 + 4 + 1;
339  else
340  swd_payload_size = 1;
341  break;
342  }
343 
344  hexify(str + j, data + i, MIN(swd_payload_size, size - i), n - j);
345  i += swd_payload_size;
346  j += 2 * swd_payload_size;
347  str[j] = '\0';
348  if (i > size)
349  LOG_DEBUG_IO(" %s - bad size", str);
350  else
351  LOG_DEBUG_IO(" %s", str);
352  j = 0;
353  }
354  } else {
355  hexify(str + j, data + i, MIN(cmd_payload_size, size - i), n - j);
356  i += cmd_payload_size;
357  j += 2 * cmd_payload_size;
358  str[j] = '\0';
359  if (i > size)
360  LOG_DEBUG_IO("%s - bad size", str);
361  else
362  LOG_DEBUG_IO("%s", str);
363  j = 0;
364  }
365  }
366  free(str);
367 }
368 
376 static int ch347_write_data(uint8_t *data, int *length)
377 {
378  int write_len = *length;
379  int i = 0;
380  int transferred = 0;
381 
382  while (true) {
383  int retval = jtag_libusb_bulk_write(ch347_handle, CH347_EPOUT, (char *)&data[i],
384  write_len, USB_WRITE_TIMEOUT, &transferred);
385  if (retval != ERROR_OK) {
386  LOG_ERROR("CH347 write fail");
387  *length = 0;
388  return retval;
389  }
390  i += transferred;
391  if (i >= *length)
392  break;
393  write_len = *length - i;
394  }
395 
397  LOG_DEBUG_IO("size=%d, buf=", i);
398  log_buf_dump(data, i, false);
399  }
400 
401  *length = i;
402  return ERROR_OK;
403 }
404 
412 static int ch347_read_data(uint8_t *data, int *length)
413 {
414  int read_len = *length;
415  int i = 0;
416  int transferred = 0;
417 
418  while (true) {
419  int retval = jtag_libusb_bulk_read(ch347_handle, CH347_EPIN, (char *)&data[i],
420  read_len, USB_READ_TIMEOUT, &transferred);
421  if (retval != ERROR_OK) {
422  LOG_ERROR("CH347 read fail");
423  *length = 0;
424  return retval;
425  }
426 
427  i += transferred;
428  if (i >= *length)
429  break;
430  read_len = *length - i;
431  }
432 
434  LOG_DEBUG_IO("size=%d, buf=", i);
435  log_buf_dump(data, i, true);
436  }
437 
438  *length = i;
439  return ERROR_OK;
440 }
441 
448 static void ch347_cmd_calc_reads(struct ch347_cmd *cmd)
449 {
450  cmd->read_len = 0;
451  cmd->tdo_bit_count = 0;
452 
453  switch (cmd->type) {
454  case CH347_CMD_GPIO:
455  // for GPIO we need to read back the same amount of data that we had send
456  cmd->read_len = cmd->write_data_len;
457  break;
458  case CH347_CMD_JTAG_INIT:
459  case CH347_CMD_SWD_INIT:
460  // for JTAG_INIT/SWD_INIT the amount is fixed
461  cmd->read_len = CH347_CMD_INIT_READ_LEN;
462  break;
464  // for bit operations we need to count the TCK high edges
465  for (int i = 0; i < cmd->write_data_len; i++) {
466  if ((cmd->write_data[i] & TCK_H) == TCK_H) {
467  cmd->read_len++;
468  cmd->tdo_bit_count++;
469  }
470  }
471  break;
473  // for byte operations: need to read one byte back for each data byte
474  cmd->read_len = cmd->write_data_len;
475  // we occupy 8 bits per byte in the TDO hardware buffer
476  cmd->tdo_bit_count = cmd->read_len * 8;
477  break;
478  }
479 }
480 
489 {
490  // nothing to do if no bytes are recorded
491  if (!ch347.scratchpad_idx)
492  return ERROR_OK;
493 
494  // malloc for the command and data bytes
495  struct ch347_cmd *cmd = malloc(sizeof(struct ch347_cmd));
496  if (cmd)
497  cmd->write_data = malloc(ch347.scratchpad_idx);
498  if (!cmd || !cmd->write_data) {
499  LOG_ERROR("malloc failed");
500  free(cmd);
501  return ERROR_FAIL;
502  }
503 
504  // copy data, calculate the reads and add to the command queue
505  cmd->type = ch347.scratchpad_cmd_type;
506  cmd->write_data_len = ch347.scratchpad_idx;
507  memcpy(cmd->write_data, ch347.scratchpad, ch347.scratchpad_idx);
509  list_add_tail(&cmd->queue, &ch347.cmd_queue);
510 
511  // cleanup the scratchpad for the next command
513  ch347.scratchpad_idx = 0;
514  return ERROR_OK;
515 }
516 
526 static int ch347_read_scan(uint8_t *decoded_buf, int decoded_buf_len, int raw_read_len)
527 {
528  int read_len = raw_read_len;
529  uint8_t *read_buf = malloc(read_len);
530  if (!read_buf) {
531  LOG_ERROR("malloc failed");
532  return ERROR_FAIL;
533  }
534 
535  int retval = ch347_read_data(read_buf, &read_len);
536  if (retval != ERROR_OK) {
537  free(read_buf);
538  return retval;
539  }
540 
541  int rd_idx = 0;
542  int decoded_buf_idx = 0;
543 
544  while (rd_idx < read_len) {
545  unsigned int type = read_buf[rd_idx++];
546  uint16_t data_len = le_to_h_u16(&read_buf[rd_idx]);
547  rd_idx += 2;
548  if (decoded_buf_idx > decoded_buf_len) {
549  LOG_ERROR("CH347 decoded_buf too small");
550  free(read_buf);
551  return ERROR_FAIL;
552  }
553 
554  // nothing to decode? Only read to make the CH347 happy!
555  if (!decoded_buf) {
556  rd_idx += data_len;
557  continue;
558  }
559 
560  switch (type) {
561  case CH347_CMD_GPIO:
562  case CH347_CMD_JTAG_INIT:
563  case CH347_CMD_SWD_INIT:
565  // for all bytewise commands: copy the data bytes
566  memcpy(&decoded_buf[decoded_buf_idx], &read_buf[rd_idx], data_len);
567  decoded_buf_idx += data_len;
568  rd_idx += data_len;
569  break;
571  // for CH347_CMD_JTAG_BIT_OP_RD we need to copy bit by bit
572  for (int i = 0; i < data_len; i++) {
573  if (read_buf[rd_idx + i] & BIT(0))
574  decoded_buf[decoded_buf_idx + i / 8] |= BIT(i % 8);
575  else
576  decoded_buf[decoded_buf_idx + i / 8] &= ~(BIT(i % 8));
577  }
578  rd_idx += data_len;
579  decoded_buf_idx += DIV_ROUND_UP(data_len, 8);
580  break;
581  default:
582  LOG_ERROR("CH347 read command fail");
583  free(read_buf);
584  return ERROR_FAIL;
585  }
586  }
587 
588  free(read_buf);
589  return ERROR_OK;
590 }
591 
599 static int ch347_scan_data_to_fields(uint8_t *decoded_buf, int decoded_buf_len)
600 {
601  int byte_offset = 0;
602  struct ch347_scan *scan;
603  struct ch347_scan *tmp;
604  int bit_offset = 0;
606  for (int i = 0; i < scan->fields_len; i++) {
607  int num_bits = scan->fields[i].num_bits;
608  LOG_DEBUG("fields[%d].in_value[%d], read from bit offset: %d", i, num_bits, bit_offset);
609  // only if we need the value
610  if (scan->fields[i].in_value) {
611  uint8_t *capture_buf = malloc(DIV_ROUND_UP(num_bits, 8));
612  if (!capture_buf) {
613  LOG_ERROR("malloc failed");
614  return ERROR_FAIL;
615  }
616  uint8_t *captured = buf_set_buf(decoded_buf, bit_offset, capture_buf, 0, num_bits);
617 
619  char *str = buf_to_hex_str(captured, num_bits);
620  LOG_DEBUG_IO("size=%d, buf=[%s]", num_bits, str);
621  free(str);
622  }
623 
624  buf_cpy(captured, scan->fields[i].in_value, num_bits);
625  free(capture_buf);
626  } else {
628  LOG_DEBUG_IO("field skipped");
629  }
630  bit_offset += num_bits;
631  }
632  list_del(&scan->queue);
633  free(scan);
634  /* after one round of scan field processing the
635  next data bits are read from the next data byte
636  => round up and calculate the next start bit */
637  byte_offset = DIV_ROUND_UP(bit_offset, 8);
638  bit_offset = byte_offset * 8;
639  }
640 
641  // if not all bytes are transferred: put the rest into single_read buffer
642  if (byte_offset < decoded_buf_len) {
643  ch347.singe_read_len = decoded_buf_len - byte_offset;
644  LOG_DEBUG("single read of %d bytes", ch347.singe_read_len);
646  LOG_ERROR("Can't read more than %d bytes for a single command!", CH347_SINGLE_CMD_MAX_READ);
648  }
649  memcpy(ch347.single_read, &decoded_buf[byte_offset], ch347.singe_read_len);
650  }
651 
652  return ERROR_OK;
653 }
654 
661 static int ch347_cmd_transmit_queue(void)
662 {
663  // queue last command
664  int retval = ch347_cmd_from_scratchpad();
665  if (retval != ERROR_OK)
666  return retval;
667 
668  // nothing to do! => done here
669  if (list_empty(&ch347.cmd_queue))
670  return ERROR_OK;
671 
672  // calculate the needed buffer length for all decoded bytes
673  struct ch347_cmd *cmd;
674  int decoded_buf_len = 0;
676  if (cmd->read_len > 0)
677  decoded_buf_len += ch347_is_single_cmd_type(cmd->type) ?
678  cmd->read_len : DIV_ROUND_UP(cmd->tdo_bit_count, 8);
679 
680  // create the buffer for all decoded bytes
681  uint8_t *decoded_buf = NULL;
682  int decoded_buf_idx = 0;
683  if (decoded_buf_len > 0) {
684  decoded_buf = malloc(decoded_buf_len);
685  if (!decoded_buf) {
686  LOG_ERROR("malloc failed");
687  return ERROR_FAIL;
688  }
689  }
690 
691  while (!list_empty(&ch347.cmd_queue)) {
692  struct ch347_cmd *last_cmd = NULL;
693  int total_len = 0;
694  int total_tdo_count = 0;
695  int bytes_to_write = 0;
696 
698  total_len += CH347_CMD_HEADER + cmd->write_data_len;
699  total_tdo_count += cmd->tdo_bit_count;
700  // don't exceed max length or max TDO bit count
701  if (total_len >= ch347.max_len || total_tdo_count >= HW_TDO_BUF_SIZE)
702  break;
703  // remember the last cmd to send and bytes to send
704  last_cmd = cmd;
705  bytes_to_write = total_len;
706  }
707 
708  // sanity checks
709  if (!last_cmd || bytes_to_write == 0) {
710  LOG_ERROR("Nothing to send!");
711  free(decoded_buf);
712  return ERROR_FAIL;
713  }
714 
715  // create the write buffer
716  uint8_t *write_buf = malloc(bytes_to_write);
717  if (!write_buf) {
718  LOG_ERROR("malloc failed");
719  free(decoded_buf);
720  return ERROR_FAIL;
721  }
722 
723  int idx = 0;
724  int bytes_to_read = 0;
725  int current_decoded_buf_len = 0;
726  struct ch347_cmd *tmp;
727 
729  // copy command to buffer
730  write_buf[idx++] = cmd->type;
731  h_u16_to_le(&write_buf[idx], cmd->write_data_len);
732  idx += 2;
733  memcpy(&write_buf[idx], cmd->write_data, cmd->write_data_len);
734  idx += cmd->write_data_len;
735  // need to read something back?
736  if (cmd->read_len > 0) {
737  bytes_to_read += CH347_CMD_HEADER + cmd->read_len;
738  current_decoded_buf_len += ch347_is_single_cmd_type(cmd->type) ?
739  cmd->read_len : DIV_ROUND_UP(cmd->tdo_bit_count, 8);
740  }
741 
742  // cmd data no longer needed
743  list_del(&cmd->queue);
744  free(cmd->write_data);
745  free(cmd);
746 
747  if (cmd == last_cmd)
748  break;
749  }
750 
751  // write data to device
752  retval = ch347_write_data(write_buf, &idx);
753  free(write_buf);
754  if (retval != ERROR_OK) {
755  free(decoded_buf);
756  return retval;
757  }
758 
759  if (!bytes_to_read)
760  continue;
761 
762  // Need only to execute a read without decoding the data to make the CH347 happy?
763  if (!current_decoded_buf_len) {
764  // read but don't decode anything
765  retval = ch347_read_scan(NULL, 0, bytes_to_read);
766  } else {
767  retval = ch347_read_scan(&decoded_buf[decoded_buf_idx], current_decoded_buf_len, bytes_to_read);
768  decoded_buf_idx += current_decoded_buf_len;
769  }
770 
771  if (retval != ERROR_OK) {
772  free(decoded_buf);
773  return retval;
774  }
775  }
776 
777  // something decoded from the data read back from CH347?
778  if (decoded_buf) {
779  // put the decoded data into the scan fields or single_read buffer
780  retval = ch347_scan_data_to_fields(decoded_buf, decoded_buf_len);
781  free(decoded_buf);
782  }
783 
784  return retval;
785 }
786 
793 static int ch347_cmd_start_next(uint8_t type)
794 {
795  // different command type or non chainable command? (GPIO commands can't be concat)
796  uint8_t prev_type = ch347.scratchpad_cmd_type;
797  if (prev_type != type || ch347_is_single_cmd_type(type)) {
798  // something written in the scratchpad? => store it as command
799  if (prev_type != 0 && ch347.scratchpad_idx > 0) {
800  int retval = ch347_cmd_from_scratchpad();
801  if (retval != ERROR_OK)
802  return retval;
803 
804  /* if the last queued command is not chainable we should send it immediately
805  because e.g. the GPIO command can't be combined with any other command */
806  if (ch347_is_single_cmd_type(prev_type)) {
807  retval = ch347_cmd_transmit_queue();
808  if (retval != ERROR_OK)
809  return retval;
810  }
811  }
812 
813  /* before we can send non chainable command ("single" like GPIO command) we should send all
814  other commands because we can't send it together with other commands */
816  int retval = ch347_cmd_transmit_queue();
817  if (retval != ERROR_OK)
818  return retval;
819  }
820 
821  // store the next command type
823  }
824 
825  return ERROR_OK;
826 }
827 
835 static int ch347_scan_queue_fields(struct scan_field *scan_fields, int scan_fields_len)
836 {
837  // malloc for the scan struct
838  struct ch347_scan *scan = malloc(sizeof(struct ch347_scan));
839  if (!scan) {
840  LOG_ERROR("malloc failed");
841  return ERROR_FAIL;
842  }
843 
844  scan->fields = scan_fields;
845  scan->fields_len = scan_fields_len;
846  list_add_tail(&scan->queue, &ch347.scan_queue);
847  return ERROR_OK;
848 }
849 
858 static int ch347_single_read_get_byte(int read_buf_idx, uint8_t *byte)
859 {
860  int retval = ch347_cmd_transmit_queue();
861  if (retval != ERROR_OK)
862  return retval;
863 
864  if (read_buf_idx > CH347_SINGLE_CMD_MAX_READ || read_buf_idx < 0) {
865  LOG_ERROR("read_buf_idx out of range");
866  return ERROR_FAIL;
867  }
868 
869  *byte = ch347.single_read[read_buf_idx];
870  return ERROR_OK;
871 }
872 
878 {
879  // if full create a new command in the queue
881  uint8_t type = ch347.scratchpad_cmd_type;
884  }
885 }
886 
895 static int ch347_scratchpad_add_byte(uint8_t byte)
896 {
897  if (ch347.scratchpad_cmd_type == 0) {
898  LOG_ERROR("call ch347_next_cmd first!");
899  return ERROR_FAIL;
900  }
901 
904  return ERROR_OK;
905 }
906 
915 {
917 }
918 
929 static int ch347_scratchpad_add_bytes(uint8_t *bytes, int count)
930 {
931  if (ch347.scratchpad_cmd_type == 0) {
932  LOG_ERROR("call ch347_next_cmd first!");
933  return ERROR_FAIL;
934  }
935 
936  int remaining = count;
937  int bytes_idx = 0;
938  while (remaining > 0) {
939  int bytes_to_store = ch347.scratchpad_idx + remaining <= UCMDPKT_DATA_MAX_BYTES_USBHS ?
941 
942  if (bytes)
943  memcpy(&ch347.scratchpad[ch347.scratchpad_idx], &bytes[bytes_idx], bytes_to_store);
944  else
945  memset(&ch347.scratchpad[ch347.scratchpad_idx], 0, bytes_to_store);
946 
947  ch347.scratchpad_idx += bytes_to_store;
948  bytes_idx += bytes_to_store;
949  remaining -= bytes_to_store;
951  }
952 
953  return ERROR_OK;
954 }
955 
963 static int ch347_scratchpad_add_clock_tms(bool tms)
964 {
965  ch347.tms_pin = tms ? TMS_H : TMS_L;
966  ch347.tck_pin = TCK_L;
967  int retval = ch347_scratchpad_add_pin_byte();
968  if (retval != ERROR_OK)
969  return retval;
970 
971  ch347.tck_pin = TCK_H;
973 }
974 
982 {
983  if (ch347.scratchpad_cmd_type == 0) {
985  if (retval != ERROR_OK)
986  return retval;
987  }
988 
989  bool tms = ch347.tms_pin == TMS_H;
990  for (int i = 0; i < count; i++) {
991  int retval = ch347_scratchpad_add_clock_tms(tms);
992  if (retval != ERROR_OK)
993  return retval;
994  }
995 
996  return ERROR_OK;
997 }
998 
1005 {
1006  ch347.tck_pin = TCK_L;
1008 }
1009 
1018 static int ch347_scratchpad_add_tms_change(const uint8_t *tms_value, int step, int skip)
1019 {
1020  LOG_DEBUG_IO("TMS Value: %02x..., step = %d, skip = %d", tms_value[0], step, skip);
1021 
1023  if (retval != ERROR_OK)
1024  return retval;
1025 
1026  for (int i = skip; i < step; i++) {
1027  retval = ch347_scratchpad_add_clock_tms((tms_value[i / 8] >> (i % 8)) & BIT(0));
1028  if (retval != ERROR_OK)
1029  return retval;
1030  }
1031 
1033 }
1034 
1042 {
1043  LOG_DEBUG_IO("num_states=%d, last_state=%d", cmd->num_states, cmd->path[cmd->num_states - 1]);
1044 
1046  if (retval != ERROR_OK)
1047  return retval;
1048 
1049  for (unsigned int i = 0; i < cmd->num_states; i++) {
1050  if (tap_state_transition(tap_get_state(), false) == cmd->path[i]) {
1051  retval = ch347_scratchpad_add_clock_tms(0);
1052  if (retval != ERROR_OK)
1053  return retval;
1054  } else if (tap_state_transition(tap_get_state(), true) == cmd->path[i]) {
1055  retval = ch347_scratchpad_add_clock_tms(1);
1056  if (retval != ERROR_OK)
1057  return retval;
1058  } else {
1059  LOG_ERROR("No transition possible!");
1061  }
1062  tap_set_state(cmd->path[i]);
1063  }
1064 
1066 }
1067 
1076 {
1077  uint8_t tms_scan;
1078  int tms_len;
1079 
1081  // don't do anything if we are already in the right state; but do execute always the TAP_RESET
1082  if (tap_get_state() == state && state != TAP_RESET)
1083  return ERROR_OK;
1084 
1085  tms_scan = tap_get_tms_path(tap_get_state(), state);
1087  int retval = ch347_scratchpad_add_tms_change(&tms_scan, tms_len, skip);
1089  return retval;
1090 }
1091 
1101 static int ch347_scratchpad_add_write_read(struct scan_command *cmd, uint8_t *bits, int bits_len, enum scan_type scan)
1102 {
1103  // the bits and bytes to transfer
1104  int byte_count = bits_len / 8;
1105  int bit_count = bits_len % 8;
1106 
1107  // only bytes are not possible because we need to set TMS high for the last bit
1108  if (byte_count > 0 && bit_count == 0) {
1109  // make one byte to eight bits
1110  byte_count--;
1111  bit_count = 8;
1112  }
1113 
1114  // in bitwise mode only bits are allowed
1115  if (ch347.use_bitwise_mode) {
1116  byte_count = 0;
1117  bit_count = bits_len;
1118  }
1119 
1120  bool is_read = (scan == SCAN_IN || scan == SCAN_IO);
1121 
1122  // if we need to send bytes
1123  if (byte_count > 0) {
1124  // start the next cmd and copy the data out bytes to it
1126  if (retval != ERROR_OK)
1127  return retval;
1128 
1129  if (bits)
1130  retval = ch347_scratchpad_add_bytes(bits, byte_count);
1131  else
1132  retval = ch347_scratchpad_add_bytes(NULL, byte_count);
1133 
1134  if (retval != ERROR_OK)
1135  return retval;
1136  }
1137 
1138  // bits are always need to send; no possibility to not send bits
1140  if (retval != ERROR_OK)
1141  return retval;
1142 
1143  ch347.tms_pin = TMS_L;
1144  ch347.tdi_pin = TDI_L;
1145 
1146  for (int i = 0; i < bit_count; i++) {
1147  if (bits)
1148  ch347.tdi_pin = ((bits[byte_count + i / 8] >> i % 8) & BIT(0)) ? TDI_H : TDI_L;
1149 
1150  // for the last bit set TMS high to exit the shift state
1151  if (i + 1 == bit_count)
1152  ch347.tms_pin = TMS_H;
1153 
1154  ch347.tck_pin = TCK_L;
1155  retval = ch347_scratchpad_add_pin_byte();
1156  if (retval != ERROR_OK)
1157  return retval;
1158 
1159  ch347.tck_pin = TCK_H;
1160  retval = ch347_scratchpad_add_pin_byte();
1161  if (retval != ERROR_OK)
1162  return retval;
1163 
1164  /* cut the package after each MAX_BITS_PER_BIT_OP bits because it
1165  needs a dividable by 8 bits package */
1166  if (i > 0 && (i + 1) % MAX_BITS_PER_BIT_OP == 0) {
1167  retval = ch347_cmd_from_scratchpad();
1168  if (retval != ERROR_OK)
1169  return retval;
1170 
1172  if (retval != ERROR_OK)
1173  return retval;
1174  }
1175  }
1176 
1177  // one TCK_L after the last bit
1179  if (retval != ERROR_OK)
1180  return retval;
1181 
1182  // if read is involved we need to queue the scan fields
1183  if (is_read)
1184  return ch347_scan_queue_fields(cmd->fields, cmd->num_fields);
1185 
1186  return ERROR_OK;
1187 }
1188 
1196 static int ch347_scratchpad_add_run_test(int cycles, enum tap_state state)
1197 {
1198  LOG_DEBUG_IO("cycles=%d, end_state=%d", cycles, state);
1199  int retval;
1200  if (tap_get_state() != TAP_IDLE) {
1202  if (retval != ERROR_OK)
1203  return retval;
1204  }
1205 
1206  retval = ch347_scratchpad_add_stableclocks(cycles);
1207  if (retval != ERROR_OK)
1208  return retval;
1209 
1211 }
1212 
1220 {
1221  static const char *const type2str[] = {"", "SCAN_IN", "SCAN_OUT", "SCAN_IO"};
1222 
1223  enum scan_type type = jtag_scan_type(cmd);
1224  uint8_t *buf = NULL;
1225  int scan_bits = jtag_build_buffer(cmd, &buf);
1226 
1227  // add a move to IRSHIFT or DRSHIFT state
1228  int retval;
1229  if (cmd->ir_scan)
1231  else
1233 
1234  if (retval != ERROR_OK) {
1235  free(buf);
1236  return retval;
1237  }
1238 
1240  char *log_buf = buf_to_hex_str(buf, scan_bits);
1241  LOG_DEBUG_IO("scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d",
1242  cmd->ir_scan ? "IRSCAN" : "DRSCAN",
1243  type2str[type],
1244  scan_bits, log_buf, cmd->end_state);
1245  free(log_buf);
1246  }
1247 
1248  retval = ch347_scratchpad_add_write_read(cmd, buf, scan_bits, type);
1249  free(buf);
1250  if (retval != ERROR_OK)
1251  return retval;
1252 
1253  // add a move to the final state
1254  return ch347_scratchpad_add_move_state(cmd->end_state, 1);
1255 }
1256 
1264 static int ch347_gpio_set(int gpio, bool data)
1265 {
1266  int retval = ch347_cmd_start_next(CH347_CMD_GPIO);
1267  if (retval != ERROR_OK)
1268  return retval;
1269 
1270  uint8_t gpios[GPIO_CNT];
1271  memset(gpios, 0, GPIO_CNT);
1272  /* always set bits 7 and 6 for GPIO enable
1273  bits 5 and 4 for pin direction output
1274  bit 3 is the data bit */
1275  gpios[gpio] = data == 0 ? GPIO_SET_L : GPIO_SET_H;
1277  if (retval != ERROR_OK)
1278  return retval;
1279 
1280  // check in the read if the bit is set/cleared correctly
1281  uint8_t byte;
1282  retval = ch347_single_read_get_byte(gpio, &byte);
1283  if (retval != ERROR_OK)
1284  return retval;
1285 
1286  if ((byte & BIT(6)) >> 6 != data) {
1287  LOG_ERROR("Output not set.");
1288  return ERROR_FAIL;
1289  }
1290 
1291  return ERROR_OK;
1292 }
1293 
1300 static int ch347_activity_led_set(int led_state)
1301 {
1302  if (ch347_activity_led_gpio_pin != 0xFF)
1303  return ch347_gpio_set(ch347_activity_led_gpio_pin, ch347_activity_led_active_high ? led_state : 1 - led_state);
1304 
1305  // not configured => also OK
1306  return ERROR_OK;
1307 }
1308 
1317 static int ch347_reset(int trst, int srst)
1318 {
1319  LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst);
1320  if (srst) {
1321  LOG_ERROR("Asserting SRST not supported!");
1322  return ERROR_FAIL;
1323  }
1324 
1325  if (swd_mode) {
1326  if (trst)
1327  LOG_WARNING("Asserting TRST not supported in SWD mode!");
1328  return ERROR_OK;
1329  }
1330 
1332  if (retval != ERROR_OK)
1333  return retval;
1334 
1335  ch347.trst_pin = trst ? TRST_L : TRST_H;
1336  retval = ch347_scratchpad_add_pin_byte();
1337  if (retval != ERROR_OK)
1338  return retval;
1339 
1341  if (retval != ERROR_OK)
1342  return retval;
1343 
1344  return ch347_cmd_transmit_queue();
1345 }
1346 
1353 static int ch347_sleep(int us)
1354 {
1355  LOG_DEBUG_IO("us=%d", us);
1356  int retval = ch347_cmd_transmit_queue();
1357  jtag_sleep(us);
1358  return retval;
1359 }
1360 
1366 static int ch347_execute_queue(struct jtag_command *cmd_queue)
1367 {
1368  struct jtag_command *cmd = cmd_queue;
1369 
1370  int retval = ch347_activity_led_set(LED_ON);
1371  if (retval != ERROR_OK)
1372  return retval;
1373 
1374  while (retval == ERROR_OK && cmd) {
1375  switch (cmd->type) {
1376  case JTAG_RUNTEST:
1377  retval = ch347_scratchpad_add_run_test(cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
1378  break;
1379  case JTAG_STABLECLOCKS:
1380  retval = ch347_scratchpad_add_stableclocks(cmd->cmd.stableclocks->num_cycles);
1381  break;
1382  case JTAG_TLR_RESET:
1383  retval = ch347_scratchpad_add_move_state(cmd->cmd.statemove->end_state, 0);
1384  break;
1385  case JTAG_PATHMOVE:
1386  retval = ch347_scratchpad_add_move_path(cmd->cmd.pathmove);
1387  break;
1388  case JTAG_TMS:
1389  retval = ch347_scratchpad_add_tms_change(cmd->cmd.tms->bits, cmd->cmd.tms->num_bits, 0);
1390  break;
1391  case JTAG_SLEEP:
1392  retval = ch347_sleep(cmd->cmd.sleep->us);
1393  break;
1394  case JTAG_SCAN:
1395  retval = ch347_scratchpad_add_scan(cmd->cmd.scan);
1396  break;
1397  default:
1398  LOG_ERROR("BUG: unknown JTAG command type 0x%X", cmd->type);
1399  retval = ERROR_FAIL;
1400  break;
1401  }
1402 
1403  cmd = cmd->next;
1404  }
1405 
1406  if (retval != ERROR_OK)
1407  return retval;
1408 
1409  retval = ch347_cmd_transmit_queue();
1410  if (retval != ERROR_OK)
1411  return retval;
1412 
1414 }
1415 
1421 static int ch347_open_device(void)
1422 {
1423  const uint16_t *ch347_vids = custom_ch347_vids[0] != 0 ? custom_ch347_vids : default_ch347_vids;
1424  const uint16_t *ch347_pids = custom_ch347_pids[0] != 0 ? custom_ch347_pids : default_ch347_pids;
1425 
1426  int retval = jtag_libusb_open(ch347_vids, ch347_pids, ch347_device_desc, &ch347_handle, NULL);
1427  if (retval != ERROR_OK) {
1428  char error_message[256];
1429  snprintf(error_message, sizeof(error_message), "CH347 not found. Tried VID/PID pairs: ");
1430  for (int i = 0; ch347_vids[i] != 0; i++)
1431  snprintf(error_message + strlen(error_message), sizeof(error_message) - strlen(error_message),
1432  "%04x:%04x ", ch347_vids[i], ch347_pids[i]);
1433 
1434  LOG_ERROR("%s", error_message);
1435  return retval;
1436  }
1437 
1438  struct libusb_device_descriptor ch347_device_descriptor;
1439  libusb_device *device = libusb_get_device(ch347_handle);
1440  if (!device) {
1441  LOG_ERROR("CH347 error calling libusb_get_device");
1443  return ERROR_FAIL;
1444  }
1445 
1446  retval = libusb_get_device_descriptor(device, &ch347_device_descriptor);
1447  if (retval != ERROR_OK) {
1448  LOG_ERROR("CH347 error getting device descriptor: %s", libusb_error_name(retval));
1450  return retval;
1451  }
1452 
1453  // CH347T / CH347F detection
1454  // if we can claim interface 4 we found a CH347F chip; if we can claim interface 2 we found CH347T chip
1455  retval = libusb_claim_interface(ch347_handle, CH347F_MPHSI_INTERFACE);
1456  if (retval != ERROR_OK) {
1457  retval = libusb_claim_interface(ch347_handle, CH347T_MPHSI_INTERFACE);
1458  if (retval != ERROR_OK) {
1459  LOG_ERROR("CH347 unable to claim interface: %s", libusb_error_name(retval));
1461  return retval;
1462  }
1464  } else {
1466  }
1467 
1468  char firmware_version;
1470  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1471  VENDOR_VERSION, 0, 0, &firmware_version, sizeof(firmware_version),
1473  if (retval != ERROR_OK) {
1474  LOG_ERROR("CH347 unable to get firmware version");
1476  return retval;
1477  }
1478 
1479  char manufacturer[256 + 1];
1480  if (libusb_get_string_descriptor_ascii(ch347_handle, ch347_device_descriptor.iManufacturer,
1481  (unsigned char *)manufacturer, sizeof(manufacturer) - 1) < 0) {
1482  strcpy(manufacturer, "(unknown)");
1483  }
1484  char product[256 + 1];
1485  if (libusb_get_string_descriptor_ascii(ch347_handle, ch347_device_descriptor.iProduct,
1486  (unsigned char *)product, sizeof(product) - 1) < 0) {
1487  strcpy(product, "(unknown)");
1488  }
1489  char serial_number[256 + 1];
1490  if (libusb_get_string_descriptor_ascii(ch347_handle, ch347_device_descriptor.iSerialNumber,
1491  (unsigned char *)serial_number, sizeof(serial_number) - 1) < 0) {
1492  strcpy(serial_number, "(unknown)");
1493  }
1494 
1495  LOG_INFO("CH347 %s from vendor %s with serial number %s found. (Chip version=%X.%2X, Firmware=0x%02X)",
1496  product,
1497  manufacturer,
1498  serial_number,
1499  (ch347_device_descriptor.bcdDevice >> 8) & 0xFF,
1500  ch347_device_descriptor.bcdDevice & 0xFF,
1501  firmware_version);
1502 
1503  if (ch347.chip_variant == CH347T && ch347_device_descriptor.bcdDevice < BYTEWISE_MODE_VERSION) {
1504  LOG_INFO("CH347T old version of the chip, JTAG only working in bitwise mode. For bytewise mode at least version %X.%X is needed.",
1505  (BYTEWISE_MODE_VERSION >> 8) & 0xFF,
1506  BYTEWISE_MODE_VERSION & 0xFF);
1507  ch347.use_bitwise_mode = true;
1508  } else {
1509  ch347.use_bitwise_mode = false;
1510  }
1511 
1512  if (ch347.chip_variant == CH347T) {
1513  if (swd_mode) {
1514  ch347.swclk_5mhz_supported = ch347_device_descriptor.bcdDevice >= 0x544;
1515 
1516  if (ch347_device_descriptor.bcdDevice < 0x441)
1517  LOG_WARNING("CH347T version older than 4.41 probably does not support SWD transport");
1518 
1519  if (ch347_device_descriptor.bcdDevice == 0x441)
1520  LOG_WARNING("If CH347T version 4.41 cannot connect or SWD fails often, insert a resistor to SWDIO circuit");
1521 
1522  } else if (ch347_device_descriptor.bcdDevice == 0x241) {
1523  LOG_WARNING("CH347T version 2.41 has very weird clock timing, may not work with a slower JTAG device");
1524  }
1525 
1526  if (ch347_device_descriptor.bcdDevice < 0x544)
1527  LOG_INFO("Please upgrade CH347T firmware to a production version >= 5.44");
1528 
1529  } else if (ch347.chip_variant == CH347F) {
1530  ch347.swclk_5mhz_supported = ch347_device_descriptor.bcdDevice >= 0x101;
1531 
1532  if (ch347_device_descriptor.bcdDevice < 0x101)
1533  LOG_INFO("Please upgrade CH347F firmware to a production version >= 1.1");
1534  }
1535 
1536  return ERROR_OK;
1537 }
1538 
1544 static int ch347_quit(void)
1545 {
1546  // on close set the LED on, because the state without JTAG is on
1548  int retval = ch347_cmd_transmit_queue();
1550  LOG_DEBUG_IO("CH347 close");
1551  return retval;
1552 }
1553 
1565 static int ch347_adapter_init(uint8_t clock_index, bool *supports_larger_pack_mode)
1566 {
1568  if (retval != ERROR_OK)
1569  return retval;
1570 
1571  retval = ch347_scratchpad_add_byte(0);
1572  if (retval != ERROR_OK)
1573  return retval;
1574 
1575  retval = ch347_scratchpad_add_byte(clock_index);
1576  if (retval != ERROR_OK)
1577  return retval;
1578 
1579  for (int i = 0; i < 4; i++) {
1580  retval = ch347_scratchpad_add_pin_byte();
1581  if (retval != ERROR_OK)
1582  return retval;
1583  }
1584 
1585  uint8_t mode;
1586  retval = ch347_single_read_get_byte(0, &mode);
1587  if (retval != ERROR_OK)
1588  return retval;
1589 
1590  *supports_larger_pack_mode = mode != 0;
1591  return ERROR_OK;
1592 }
1593 
1602 static int ch347_adapter_supports_larger_pack_mode(bool *supports_larger_pack_mode)
1603 {
1604  return ch347_adapter_init(CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE, supports_larger_pack_mode);
1605 }
1606 
1614 static int ch347_adapter_set_speed(uint8_t clock_index)
1615 {
1616  bool unused;
1617  return ch347_adapter_init(clock_index, &unused);
1618 }
1619 
1626 static int ch347_swd_init_cmd(uint8_t clock_divisor)
1627 {
1629  if (retval != ERROR_OK)
1630  return retval;
1631 
1632  uint8_t cmd_data[] = {0x40, 0x42, 0x0f, 0x00, clock_divisor, 0x00, 0x00, 0x00 };
1633  retval = ch347_scratchpad_add_bytes(cmd_data, ARRAY_SIZE(cmd_data));
1634  if (retval != ERROR_OK)
1635  return retval;
1636 
1637  /* TODO: CH347_CMD_SWD_INIT reads one data byte.
1638  But how can we decide if SWD init was successfully executed?
1639  Return an error code if init was failed */
1640  uint8_t init_result = 0;
1641  retval = ch347_single_read_get_byte(0, &init_result);
1642  LOG_DEBUG("SWD init clk div %" PRIu8 ", result %02" PRIx8,
1643  clock_divisor, init_result);
1644  ch347_swd_context.clk_divisor = clock_divisor;
1645  return retval;
1646 }
1647 
1654 static int ch347_speed_set(int speed_index)
1655 {
1656  if (swd_mode)
1657  return ch347_swd_init_cmd(speed_index);
1658 
1659  int retval = ch347_adapter_set_speed(speed_index);
1660  if (retval != ERROR_OK) {
1661  LOG_ERROR("Couldn't set CH347 speed");
1662  return retval;
1663  }
1664 
1665  return ERROR_OK;
1666 }
1667 
1673 static int ch347_init_pack_size(void)
1674 {
1675  // already set?
1676  if (ch347.pack_size != UNSET)
1677  return ERROR_OK;
1678 
1679  // set the lower limit for starting
1681  bool supports_larger_pack_mode;
1682  int retval = ch347_adapter_supports_larger_pack_mode(&supports_larger_pack_mode);
1683  if (retval != ERROR_OK)
1684  return retval;
1685 
1686  ch347.pack_size = supports_larger_pack_mode ? LARGER_PACK : STANDARD_PACK;
1689  return ERROR_OK;
1690 }
1691 
1699 static int ch347_speed_get(int speed_idx, int *khz)
1700 {
1701  if (swd_mode) {
1702  if (speed_idx)
1703  *khz = DIV_ROUND_UP(CH347_SWD_CLOCK_BASE, speed_idx);
1704  else
1705  *khz = CH347_SWD_CLOCK_MAX;
1706  return ERROR_OK;
1707  }
1708 
1709  int retval = ch347_init_pack_size();
1710  if (retval != ERROR_OK)
1711  return retval;
1712  const int *speeds = ch347.pack_size == STANDARD_PACK ?
1714  *khz = speeds[speed_idx];
1715  return ERROR_OK;
1716 }
1717 
1725 static int ch347_speed_get_index(int khz, int *speed_idx)
1726 {
1727  if (khz == 0) {
1728  LOG_ERROR("Adaptive clocking not supported");
1729  return ERROR_FAIL;
1730  }
1731 
1732  if (swd_mode) {
1734  *speed_idx = 0;
1735  } else {
1736  // Don't allow too low clk speeds: packet processing is limited to ~8 msec
1737  // or triggers host USB disconnect
1738  *speed_idx = MIN(DIV_ROUND_UP(CH347_SWD_CLOCK_BASE, khz),
1740  }
1741  return ERROR_OK;
1742  }
1743 
1744  // when checking with speed index 9 we can see if the device supports STANDARD_PACK or LARGER_PACK mode
1745  int retval = ch347_init_pack_size();
1746  if (retval != ERROR_OK)
1747  return retval;
1748  // depending on pack size there are different fixed clock speeds possible
1749  const int *speeds = ch347.pack_size == STANDARD_PACK ?
1751  int length = ch347.pack_size == STANDARD_PACK ?
1753  int idx = -1;
1754  int lower_bound = 0;
1755  // find the suitable speed index
1756  for (int i = 0; i < length; i++) {
1757  if (khz >= lower_bound && khz <= speeds[i]) {
1758  idx = i;
1759  break;
1760  }
1761  lower_bound = speeds[i];
1762  }
1763  // too high! => use max possible speed
1764  if (idx == -1) {
1765  LOG_INFO("Speed %d kHz is higher than highest speed of %d kHz. Using %d khz!",
1766  khz, speeds[length - 1], speeds[length - 1]);
1767  idx = length - 1;
1768  } else if (speeds[idx] != khz) {
1769  LOG_INFO("Requested speed of %d kHz is not possible. Using the next higher speed of %d kHz!",
1770  khz, speeds[idx]);
1771  }
1772  *speed_idx = idx;
1773  return ERROR_OK;
1774 }
1775 
1781 COMMAND_HANDLER(ch347_handle_vid_pid_command)
1782 {
1783  if (CMD_ARGC < 2)
1785 
1786  for (int i = 0; i < (int)(MIN(CMD_ARGC, ARRAY_SIZE(custom_ch347_pids))); i += 2) {
1788  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], custom_ch347_pids[i / 2]);
1789  }
1790 
1791  return ERROR_OK;
1792 }
1793 
1799 COMMAND_HANDLER(ch347_handle_device_desc_command)
1800 {
1801  if (CMD_ARGC != 1)
1803 
1804  free(ch347_device_desc);
1805  ch347_device_desc = strdup(CMD_ARGV[0]);
1806  return ERROR_OK;
1807 }
1808 
1809 static const struct command_registration ch347_subcommand_handlers[] = {
1810  {
1811  .name = "vid_pid",
1812  .handler = &ch347_handle_vid_pid_command,
1813  .mode = COMMAND_CONFIG,
1814  .help = "the vendor ID and product ID of the CH347 device",
1815  .usage = "(vid pid)*",
1816  },
1817  {
1818  .name = "device_desc",
1819  .handler = &ch347_handle_device_desc_command,
1820  .mode = COMMAND_CONFIG,
1821  .help = "set the USB device description of the CH347 device",
1822  .usage = "description_string",
1823  },
1825 };
1826 
1827 static const struct command_registration ch347_command_handlers[] = {
1828  {
1829  .name = "ch347",
1830  .mode = COMMAND_ANY,
1831  .help = "perform ch347 management",
1832  .chain = ch347_subcommand_handlers,
1833  .usage = "",
1834  },
1836 };
1837 
1847 static void ch347_configure_activity_led(const struct adapter_gpio_config *led_config)
1848 {
1849  uint8_t gpio = led_config->gpio_num;
1850  if (gpio >= GPIO_CNT || (BIT(gpio) & USEABLE_GPIOS) == 0)
1851  return;
1852 
1855 }
1856 
1862 static int ch347_init(void)
1863 {
1864  int retval = ch347_open_device();
1865 
1866  if (retval != ERROR_OK) {
1867  LOG_ERROR("CH347 open error");
1868  return retval;
1869  }
1870 
1871  LOG_DEBUG_IO("CH347 open success");
1872 
1873  // CH347 JTAG init
1874  ch347.tck_pin = TCK_L;
1875  ch347.tms_pin = TMS_H;
1876  ch347.tdi_pin = TDI_L;
1877  ch347.trst_pin = TRST_H;
1880 
1881  ch347.pack_size = UNSET;
1882 
1884 
1885  if (!swd_mode) {
1887  } else {
1888  retval = ch347_init_pack_size();
1889  if (retval != ERROR_OK)
1890  return retval;
1891 
1892  retval = ch347_swd_init_cmd(1);
1893  }
1894  return retval;
1895 }
1896 
1902 static int ch347_swd_init(void)
1903 {
1904  LOG_INFO("CH347 SWD mode enabled");
1905  swd_mode = true;
1906  memset(&ch347_swd_context, 0, sizeof(ch347_swd_context));
1907 
1910 
1912  // 0XE8 + 2byte len + N byte cmds
1914  // 0XE8 + 2byte len + N byte ack + data
1916  struct ch347_swd_io *pswd_io = ch347_swd_context.ch347_cmd_buf;
1917  for (int i = 0; i < CH347_MAX_CMD_BUF; i++, pswd_io++) {
1918  INIT_LIST_HEAD(&pswd_io->list_entry);
1920  }
1921  return ERROR_OK;
1922 }
1923 
1925 {
1926  struct ch347_swd_io *pswd_io;
1928  return NULL;
1929 
1931  struct ch347_swd_io, list_entry);
1932  list_del_init(&pswd_io->list_entry);
1933  pswd_io->cmd = 0;
1934  pswd_io->usb_cmd = CH347_CMD_SWD_SEQ_W;
1935  pswd_io->dst = NULL;
1936  return pswd_io;
1937 }
1938 
1939 static int ch347_swd_queue_flush(void)
1940 {
1945  if (retval != ERROR_OK) {
1947  LOG_DEBUG("CH347WriteData error");
1948  return retval;
1949  }
1950 
1954  if (retval != ERROR_OK) {
1956  LOG_DEBUG("CH347ReadData error");
1957  return retval;
1958  }
1959 
1962  LOG_ERROR("write/read failed %d %d",
1965  retval = ERROR_FAIL;
1966  }
1967 
1968  return retval;
1969 }
1970 
1971 static void ch347_write_swd_reg(uint8_t cmd, const uint32_t out)
1972 {
1974  // 8bit + 32bit +1bit
1981  // 0xA0 + 1 byte(3bit ACK)
1982  ch347_swd_context.need_recv_len += (1 + 1);
1984 }
1985 
1986 static void ch347_write_spec_seq(const uint8_t *out, uint8_t out_len)
1987 {
1992  for (uint8_t i = 0; i < DIV_ROUND_UP(out_len, 8); i++)
1993  ch347_swd_context.send_buf[ch347_swd_context.send_len++] = out ? out[i] : 0x00;
1994  ch347_swd_context.need_recv_len += 1; // 0xA1
1995  ch347_swd_context.total_swd_clk += out_len;
1996 }
1997 
1998 static void ch347_read_swd_reg(uint8_t cmd)
1999 {
2004  // 0xA2 + 1 byte(3bit ACK) + 4 byte(data) + 1 byte(1bit parity+1bit trn)
2005  ch347_swd_context.need_recv_len += 1 + 1 + 4 + 1;
2007 }
2008 
2009 static int ch347_swd_switch_out(enum swd_special_seq seq, const uint8_t *out, unsigned int out_len)
2010 {
2011  if ((ch347_swd_context.send_len + (1 + 2 + DIV_ROUND_UP(out_len, 8))) > CH347_MAX_SEND_BUF)
2012  return ERROR_FAIL;
2014  return ERROR_FAIL;
2015 
2016  struct ch347_swd_io *pswd_io = ch347_get_one_swd_io();
2017  if (pswd_io) {
2018  ch347_write_spec_seq(out, out_len);
2020  return ERROR_OK;
2021  } else {
2022  return ERROR_FAIL;
2023  }
2024 }
2025 
2026 // check read/write REG can fill in remaining buff
2027 static bool ch347_chk_buf_size(uint8_t cmd, uint32_t ap_delay_clk)
2028 {
2029  bool flush = false;
2030  int send_len = ch347_swd_context.send_len;
2031  int recv_len = ch347_swd_context.need_recv_len;
2032  int len;
2033  do {
2034  if (cmd & SWD_CMD_RNW) {
2035  len = 1 + 1 + 1 + 1; // 0xA2 + len + rev + cmd
2036  if (send_len + len > CH347_MAX_SEND_BUF)
2037  break;
2038  send_len += len;
2039  len = 1 + 1 + 4 + 1;
2040  /* 0xA2 + 1byte(3bit ack) + 4byte(data) +
2041  1byte(1bit parity+1bit trn) */
2042  if (recv_len + len > CH347_MAX_RECV_BUF)
2043  break;
2044  recv_len += len;
2045  } else { // write reg
2046  len = 1 + 1 + 1 + 1 + 4 + 1;
2047  // 0xA0 + len + rev + cmd +data + parity
2048  if (send_len + len > CH347_MAX_SEND_BUF)
2049  break;
2050  send_len += len;
2051  len = 1 + 1; // 0xA0 + 1byte(3bit ack)
2052  if (recv_len + len > CH347_MAX_RECV_BUF)
2053  break;
2054  recv_len += len;
2055  }
2056  if (cmd & SWD_CMD_APNDP) {
2057  len = 1 + 1 + 1 + DIV_ROUND_UP(ap_delay_clk, 8);
2058  // 0xA1 + Len + rev + n byte(delay)
2059  if (send_len + len > CH347_MAX_SEND_BUF)
2060  break;
2061  len = 1; // 0xA1
2062  if ((recv_len + len) > CH347_MAX_RECV_BUF)
2063  break;
2064  }
2065  // swd packet requests
2066  flush = true;
2067  } while (false);
2068 
2069  return flush;
2070 }
2071 
2072 static int ch347_swd_run_queue_inner(void);
2073 
2074 static int ch347_swd_send_idle(uint32_t ap_delay_clk)
2075 {
2076  bool run_q = false;
2077  struct ch347_swd_io *pswd_io = NULL;
2078  unsigned int max_processing_clk = ch347_swd_context.clk_divisor
2081  bool more_q_runs =
2082  1 + 1 + 1 + DIV_ROUND_UP(ap_delay_clk, 8) > CH347_MAX_SEND_BUF
2083  && ap_delay_clk > max_processing_clk;
2084 
2086  unsigned int expected_total_clk = ch347_swd_context.total_swd_clk + ap_delay_clk;
2087  unsigned int expected_send_len = ch347_swd_context.send_len
2088  + 1 + 1 + 1 + DIV_ROUND_UP(ap_delay_clk, 8);
2089  // 0xA1 + Len + rev + n byte(delay)
2090  unsigned int expected_recv_len = ch347_swd_context.need_recv_len + 1;
2091  // 0xA1
2092  unsigned int expected_time = ch347_swd_context.clk_divisor
2093  ? expected_total_clk * ch347_swd_context.clk_divisor
2094  : expected_total_clk / 5;
2095  if (expected_time > CH347_MAX_PROCESSING_US
2096  || expected_send_len > CH347_MAX_SEND_BUF
2097  || expected_recv_len > CH347_MAX_RECV_BUF) {
2098  int send_room = CH347_MAX_SEND_BUF - ch347_swd_context.send_len - 1 - 1 - 1;
2099  if (more_q_runs
2100  && send_room > 0
2101  && expected_recv_len <= CH347_MAX_RECV_BUF
2102  && ch347_swd_context.total_swd_clk < max_processing_clk) {
2103  pswd_io = ch347_get_one_swd_io();
2104  if (pswd_io) {
2105  // fill the rest of queue/time by part of delay
2106  unsigned int this_delay_clk = MIN(ap_delay_clk, 255);
2107  if ((unsigned int)send_room * 8 < this_delay_clk)
2108  this_delay_clk = send_room * 8;
2109  if (max_processing_clk - ch347_swd_context.total_swd_clk < this_delay_clk)
2110  this_delay_clk = max_processing_clk - ch347_swd_context.total_swd_clk;
2111  LOG_DEBUG_IO("partial delay %u clk", this_delay_clk);
2112  ch347_write_spec_seq(NULL, this_delay_clk);
2114  ap_delay_clk -= this_delay_clk;
2115  }
2116  }
2117  run_q = true;
2118  }
2119  }
2120 
2121  do {
2122  if (!run_q)
2123  pswd_io = ch347_get_one_swd_io();
2124 
2125  if (!pswd_io) {
2126  int retval = ch347_swd_run_queue_inner();
2127  if (retval != ERROR_OK)
2128  return retval;
2129 
2130  pswd_io = ch347_get_one_swd_io();
2131  if (!pswd_io) {
2132  LOG_ERROR("ch347 SWD queue not empty after ch347_swd_run_queue");
2134  return ERROR_FAIL;
2135  }
2136  }
2137 
2138  unsigned int send_room = CH347_MAX_SEND_BUF - 1 - 1 - 1;
2139  unsigned int this_delay_clk = MIN(ap_delay_clk, 255);
2140  if (send_room * 8 < this_delay_clk)
2141  this_delay_clk = send_room * 8;
2142  if (max_processing_clk < this_delay_clk)
2143  this_delay_clk = max_processing_clk;
2144  LOG_DEBUG_IO("delay %u clk", this_delay_clk);
2145  ch347_write_spec_seq(NULL, this_delay_clk);
2147  ap_delay_clk -= this_delay_clk;
2148  run_q = true;
2149  pswd_io = NULL;
2150  } while (ap_delay_clk);
2151  return ERROR_OK;
2152 }
2153 
2155 {
2156  LOG_DEBUG_IO("Executing %u queued transactions", ch347_swd_context.sent_cmd_count);
2158  LOG_DEBUG_IO("Skipping due to previous errors: %d", ch347_swd_context.queued_retval);
2159  goto skip;
2160  }
2161 
2162  int retval = ch347_swd_queue_flush();
2163  if (retval != ERROR_OK)
2164  return retval;
2165 
2167  LOG_ERROR("CH347 usb write/read failed - queued_retval");
2168  goto skip;
2169  }
2170  uint8_t *recv_buf = ch347_swd_context.recv_buf;
2171  int recv_len = 0;
2172  if (recv_buf[recv_len++] != CH347_CMD_SWD) { // 0XE8
2174  LOG_ERROR("CH347 usb write/read failed - not CH347_CMD_SWD");
2175  goto skip;
2176  }
2177 
2178  int cmds_len = le_to_h_u16(&recv_buf[recv_len]);
2179  recv_len += 2; // cmds_len
2180  if ((cmds_len + CH347_CMD_HEADER) > ch347_swd_context.recv_len) {
2182  LOG_ERROR("CH347 usb write/read failed - too long");
2183  goto skip;
2184  }
2185 
2186  struct list_head *tmp;
2187  struct list_head *pos;
2188  struct ch347_swd_io *pswd_io;
2189 
2191  pswd_io = list_entry(pos, struct ch347_swd_io, list_entry);
2192  if (pswd_io->usb_cmd == CH347_CMD_SWD_SEQ_W) {
2193  if (recv_buf[recv_len++] != CH347_CMD_SWD_SEQ_W) {
2195  LOG_ERROR("CH347 usb write/read failed - not CH347_CMD_SWD_SEQ_W");
2196  goto skip;
2197  }
2198  } else { // read/write Reg
2199  uint32_t ack;
2200  bool check_ack;
2201  // read Reg
2202  if (recv_buf[recv_len] == CH347_CMD_SWD_REG_R) {
2203  recv_len++;
2204  ack = buf_get_u32(&recv_buf[recv_len++], 0, 3);
2205  /* Devices do not reply to DP_TARGETSEL write
2206  cmd, ignore received ack */
2207  check_ack = swd_cmd_returns_ack(pswd_io->cmd);
2208  if (pswd_io->cmd & SWD_CMD_RNW) {
2209  uint32_t data = buf_get_u32(&recv_buf[recv_len], 0, 32);
2210 
2211  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK)
2213  "%s%s %s read reg %X = %08" PRIx32,
2214  check_ack ? "" : "ack ignored ",
2215  ack == SWD_ACK_OK ? "OK" :
2216  ack == SWD_ACK_WAIT ? "WAIT" :
2217  ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
2218  pswd_io->cmd & SWD_CMD_APNDP ? "AP" : "DP",
2219  (pswd_io->cmd & SWD_CMD_A32) >> 1,
2220  data);
2221 
2222  if (ack != SWD_ACK_OK && check_ack) {
2224  goto skip;
2225  }
2226 
2227  uint32_t parity = buf_get_u32(&recv_buf[recv_len], 32, 1);
2228  if (parity != (uint32_t)parity_u32(data)) {
2229  LOG_ERROR("SWD Read data parity mismatch");
2231  goto skip;
2232  }
2233 
2234  if (pswd_io->dst)
2235  *pswd_io->dst = data;
2236  } else {
2238  LOG_ERROR("CH347 usb write/read failed - not SWD_CMD_RNW");
2239  goto skip;
2240  }
2241  recv_len += 5;
2242  } else if (recv_buf[recv_len] == CH347_CMD_SWD_REG_W) {
2243  recv_len++;
2244  ack = buf_get_u32(&recv_buf[recv_len++], 0, 3);
2245  /* Devices do not reply to DP_TARGETSEL write
2246  cmd, ignore received ack */
2247  check_ack = swd_cmd_returns_ack(pswd_io->cmd);
2248 
2249  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK)
2251  "%s%s %s write reg %X = %08" PRIx32,
2252  check_ack ? "" : "ack ignored ",
2253  ack == SWD_ACK_OK ? "OK" :
2254  ack == SWD_ACK_WAIT ? "WAIT" :
2255  ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
2256  pswd_io->cmd & SWD_CMD_APNDP ? "AP" : "DP",
2257  (pswd_io->cmd & SWD_CMD_A32) >> 1,
2258  pswd_io->value);
2259 
2260  if (ack != SWD_ACK_OK && check_ack) {
2262  goto skip;
2263  }
2264  } else {
2266  LOG_ERROR("CH347 usb write/read failed recv_len = %d", recv_len);
2267  goto skip;
2268  }
2269  }
2270  list_del_init(&pswd_io->list_entry);
2271  list_add_tail(&pswd_io->list_entry,
2273  }
2274 
2275 skip:
2278  pswd_io = list_entry(pos, struct ch347_swd_io, list_entry);
2279  list_del_init(&pswd_io->list_entry);
2280  list_add_tail(&pswd_io->list_entry,
2282  }
2283  }
2284 
2285  // 0xE8 + 2byte len
2287  // 0xE8 + 2byte len
2294  return retval;
2295 }
2296 
2297 static int ch347_swd_run_queue(void)
2298 {
2299  /* A transaction must be followed by another transaction or at least 8
2300  idle cycles to ensure that data is clocked through the AP. */
2301  int retval = ch347_swd_send_idle(8);
2302  if (retval != ERROR_OK)
2303  return retval;
2304 
2305  return ch347_swd_run_queue_inner();
2306 }
2307 
2308 static int ch347_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2309 {
2310  int retval = ERROR_OK;
2311  bool run_q = false;
2313  unsigned int expected_total_clk = ch347_swd_context.total_swd_clk
2314  + 46 // SWD transaction
2315  + ap_delay_clk
2316  + 8; // 8 idle cycles at the end of queue
2317  unsigned int expected_time = ch347_swd_context.clk_divisor
2318  ? expected_total_clk * ch347_swd_context.clk_divisor
2319  : expected_total_clk / 5;
2320  if (expected_time > CH347_MAX_PROCESSING_US) {
2321  LOG_DEBUG_IO("Expected queue run %u cycles, with this cmd %u",
2322  ch347_swd_context.total_swd_clk, expected_total_clk);
2323  run_q = true;
2324  } else if (!ch347_chk_buf_size(cmd, ap_delay_clk)) {
2325  run_q = true;
2326  }
2327  }
2328 
2329  struct ch347_swd_io *pswd_io = NULL;
2330  if (!run_q)
2331  pswd_io = ch347_get_one_swd_io();
2332 
2333  if (!pswd_io) {
2334  retval = ch347_swd_run_queue_inner();
2335  if (retval != ERROR_OK)
2336  return retval;
2337 
2338  pswd_io = ch347_get_one_swd_io();
2339  if (!pswd_io) {
2340  LOG_ERROR("ch347 SWD queue not empty after ch347_swd_run_queue");
2342  return ERROR_FAIL;
2343  }
2344  }
2345 
2346  pswd_io->cmd = cmd | SWD_CMD_START | SWD_CMD_PARK;
2347 
2348  if (pswd_io->cmd & SWD_CMD_RNW) {
2349  pswd_io->usb_cmd = CH347_CMD_SWD_REG_R;
2350  pswd_io->dst = dst;
2351  ch347_read_swd_reg(pswd_io->cmd);
2352  } else {
2353  pswd_io->usb_cmd = CH347_CMD_SWD_REG_W;
2354  pswd_io->value = data;
2355  ch347_write_swd_reg(pswd_io->cmd, data);
2356  }
2357 
2360 
2361  // Insert idle cycles after AP accesses to avoid WAIT
2362  if (ap_delay_clk)
2363  retval = ch347_swd_send_idle(ap_delay_clk);
2364 
2365  return retval;
2366 }
2367 
2369 {
2370  switch (seq) {
2371  case LINE_RESET:
2372  LOG_DEBUG("SWD line reset");
2374  case JTAG_TO_SWD:
2375  LOG_DEBUG("JTAG-to-SWD");
2377  case JTAG_TO_DORMANT:
2378  LOG_DEBUG("JTAG-to-DORMANT");
2380  case SWD_TO_JTAG:
2381  LOG_DEBUG("SWD-to-JTAG");
2383  case SWD_TO_DORMANT:
2384  LOG_DEBUG("SWD-to-DORMANT");
2386  case DORMANT_TO_SWD:
2387  LOG_DEBUG("DORMANT-to-SWD");
2389  case DORMANT_TO_JTAG:
2390  LOG_DEBUG("DORMANT-to-JTAG");
2392  default:
2393  LOG_ERROR("Sequence %d not supported", seq);
2394  return ERROR_FAIL;
2395  }
2396 }
2397 
2398 static void ch347_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
2399 {
2400  assert(cmd & SWD_CMD_RNW);
2401  int retval = ch347_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
2402  if (retval != ERROR_OK)
2404 }
2405 
2406 static void ch347_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
2407 {
2408  assert(!(cmd & SWD_CMD_RNW));
2409  int retval = ch347_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
2410  if (retval != ERROR_OK)
2412 }
2413 
2414 static const struct swd_driver ch347_swd = {
2415  .init = ch347_swd_init,
2416  .switch_seq = ch347_swd_switch_seq,
2417  .read_reg = ch347_swd_read_reg,
2418  .write_reg = ch347_swd_write_reg,
2419  .run = ch347_swd_run_queue,
2420 };
2421 
2422 static struct jtag_interface ch347_interface = {
2424  .execute_queue = ch347_execute_queue,
2425 };
2426 
2428  .name = "ch347",
2429  .transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
2430  .transport_preferred_id = TRANSPORT_JTAG,
2431  .commands = ch347_command_handlers,
2432 
2433  .init = ch347_init,
2434  .quit = ch347_quit,
2435  .reset = ch347_reset,
2436  .speed = ch347_speed_set,
2437  .khz = ch347_speed_get_index,
2438  .speed_div = ch347_speed_get,
2439 
2440  .jtag_ops = &ch347_interface,
2441  .swd_ops = &ch347_swd,
2442 };
const struct adapter_gpio_config * adapter_gpio_get_config(void)
Retrieves gpio configuration set with command "adapter gpio <signal_name>".
Definition: adapter.c:1263
struct adapter_gpio_config gpios[ADAPTER_GPIO_IDX_NUM]
Definition: adapter.c:47
@ ADAPTER_GPIO_IDX_LED
Definition: adapter.h:62
#define SWD_ACK_FAULT
Definition: arm_adi_v5.h:33
swd_special_seq
Definition: arm_adi_v5.h:236
@ DORMANT_TO_JTAG
Definition: arm_adi_v5.h:243
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ DORMANT_TO_SWD
Definition: arm_adi_v5.h:242
@ LINE_RESET
Definition: arm_adi_v5.h:237
@ JTAG_TO_DORMANT
Definition: arm_adi_v5.h:239
@ SWD_TO_DORMANT
Definition: arm_adi_v5.h:241
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:240
#define SWD_ACK_WAIT
Definition: arm_adi_v5.h:32
#define SWD_ACK_OK
Definition: arm_adi_v5.h:31
enum arm_mode mode
Definition: armv4_5.c:281
static const struct device_t * device
Definition: at91rm9200.c:94
char * buf_to_hex_str(const void *_buf, unsigned int buf_len)
Definition: binarybuffer.c:178
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
Definition: binarybuffer.c:120
void * buf_cpy(const void *from, void *_to, unsigned int size)
Copies size bits out of from and into to.
Definition: binarybuffer.c:43
size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t length)
Convert binary data into a string of hexadecimal pairs.
Definition: binarybuffer.c:380
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.
Definition: binarybuffer.h:104
static int ch347_scratchpad_add_idle_clock(void)
Function to ensure that the clock is in a low state.
Definition: ch347.c:1004
static int ch347_scratchpad_add_move_state(enum tap_state state, int skip)
Toggle the tap state to the target state.
Definition: ch347.c:1075
static int ch347_cmd_from_scratchpad(void)
copy the scratchpad content into a new command in the command queue
Definition: ch347.c:488
struct adapter_driver ch347_adapter_driver
Definition: ch347.c:2427
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 ...
Definition: ch347.c:929
#define CH347_MAX_CMD_BUF
Definition: ch347.c:123
static int ch347_init_pack_size(void)
inits ch347.pack_size and ch347.max_len
Definition: ch347.c:1673
#define TDI_L
Definition: ch347.c:66
static int ch347_swd_switch_out(enum swd_special_seq seq, const uint8_t *out, unsigned int out_len)
Definition: ch347.c:2009
#define HW_TDO_BUF_SIZE
Definition: ch347.c:88
static int ch347_read_data(uint8_t *data, int *length)
reads data from the CH347 via libusb driver
Definition: ch347.c:412
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.
Definition: ch347.c:599
#define CH347_CMD_SWD
Definition: ch347.c:115
static const int ch347_larger_pack_clock_speeds[]
Definition: ch347.c:176
#define CH347_EPOUT
Definition: ch347.c:129
static void ch347_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: ch347.c:2398
static int ch347_quit(void)
CH347 Device Release Function.
Definition: ch347.c:1544
#define CH347F_MPHSI_INTERFACE
Definition: ch347.c:132
#define DEFAULT_CH347F_PRODUCT_ID
Definition: ch347.c:141
static struct ch347_swd_io * ch347_get_one_swd_io(void)
Definition: ch347.c:1924
#define CH347_MAX_PROCESSING_US
Definition: ch347.c:119
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.
Definition: ch347.c:1041
#define CH347_SWD_CLOCK_MAX
Definition: ch347.c:124
#define DEFAULT_OTHER_PRODUCT_ID
Definition: ch347.c:142
#define CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE
Definition: ch347.c:97
#define CH347_EPIN
Definition: ch347.c:130
static void ch347_cmd_calc_reads(struct ch347_cmd *cmd)
calculates the amount of bits and bytes that should be read for this command
Definition: ch347.c:448
static uint16_t custom_ch347_vids[]
Definition: ch347.c:258
#define VENDOR_VERSION
Definition: ch347.c:85
#define TMS_L
Definition: ch347.c:68
static void ch347_configure_activity_led(const struct adapter_gpio_config *led_config)
Configure which GPIO pin is used as the activity LED.
Definition: ch347.c:1847
#define TMS_H
Definition: ch347.c:67
#define TDI_H
Definition: ch347.c:65
static const struct command_registration ch347_command_handlers[]
Definition: ch347.c:1827
#define DEFAULT_VENDOR_ID
Definition: ch347.c:139
static void ch347_write_swd_reg(uint8_t cmd, const uint32_t out)
Definition: ch347.c:1971
#define CH347_CMD_JTAG_INIT
Definition: ch347.c:105
static int ch347_cmd_start_next(uint8_t type)
starts the next command in the scratchpad.
Definition: ch347.c:793
#define CH347_SWD_CLOCK_BASE
Definition: ch347.c:125
#define USB_READ_TIMEOUT
Definition: ch347.c:134
#define CH347_MAX_SEND_BUF
Definition: ch347.c:121
static bool ch347_is_single_cmd_type(uint8_t type)
Definition: ch347.c:268
static bool swd_mode
Definition: ch347.c:254
#define CH347_CMD_INIT_READ_LEN
Definition: ch347.c:95
static int ch347_swd_run_queue(void)
Definition: ch347.c:2297
#define USEABLE_GPIOS
Definition: ch347.c:79
static const struct swd_driver ch347_swd
Definition: ch347.c:2414
#define CH347_SINGLE_CMD_MAX_READ
Definition: ch347.c:111
static int ch347_speed_get_index(int khz, int *speed_idx)
multiplies the input speed by 1000
Definition: ch347.c:1725
#define TCK_H
Definition: ch347.c:69
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...
Definition: ch347.c:1614
static int ch347_open_device(void)
opens the CH347 device via libusb driver
Definition: ch347.c:1421
static int ch347_scratchpad_add_stableclocks(int count)
Function adds a certain amount of TCK pulses without changing the TMS pin.
Definition: ch347.c:981
#define GPIO_SET_H
Definition: ch347.c:83
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...
Definition: ch347.c:895
static int ch347_execute_queue(struct jtag_command *cmd_queue)
Executes the command queue.
Definition: ch347.c:1366
static int ch347_swd_send_idle(uint32_t ap_delay_clk)
Definition: ch347.c:2074
#define TRST_L
Definition: ch347.c:72
#define LED_OFF
Definition: ch347.c:74
static int ch347_cmd_transmit_queue(void)
Sends the write buffer via libusb and if LARGER_PACK mode is active read also data back.
Definition: ch347.c:661
static void log_buf_dump(const uint8_t *data, unsigned int size, bool recv)
Definition: ch347.c:273
static int ch347_activity_led_set(int led_state)
Turn the activity LED on or off.
Definition: ch347.c:1300
#define CH347_CMD_SWD_INIT
Definition: ch347.c:114
static int ch347_gpio_set(int gpio, bool data)
Sets a GPIO bit.
Definition: ch347.c:1264
static int ch347_swd_queue_flush(void)
Definition: ch347.c:1939
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 ...
Definition: ch347.c:914
static void ch347_write_spec_seq(const uint8_t *out, uint8_t out_len)
Definition: ch347.c:1986
static struct libusb_device_handle * ch347_handle
Definition: ch347.c:264
#define UCMDPKT_DATA_MAX_BYTES_USBHS
Definition: ch347.c:92
static uint16_t default_ch347_pids[]
Definition: ch347.c:256
static uint16_t custom_ch347_pids[]
Definition: ch347.c:259
static int ch347_scan_queue_fields(struct scan_field *scan_fields, int scan_fields_len)
queue the scan fields into the scan queue
Definition: ch347.c:835
static int ch347_scratchpad_add_run_test(int cycles, enum tap_state state)
Toggle the Tap state to run test/idle.
Definition: ch347.c:1196
#define CH347_MAX_RECV_BUF
Definition: ch347.c:122
static int ch347_speed_get(int speed_idx, int *khz)
returns the speed in kHz by the give speed index
Definition: ch347.c:1699
#define CH347_CMD_GPIO
Definition: ch347.c:104
static int ch347_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
Definition: ch347.c:2308
static int ch347_swd_switch_seq(enum swd_special_seq seq)
Definition: ch347.c:2368
static struct jtag_interface ch347_interface
Definition: ch347.c:2422
ch347_variant
Definition: ch347.c:149
@ CH347F
Definition: ch347.c:151
@ OTHER_PRODUCT_ID
Definition: ch347.c:152
@ CH347T
Definition: ch347.c:150
static bool ch347_chk_buf_size(uint8_t cmd, uint32_t ap_delay_clk)
Definition: ch347.c:2027
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.
Definition: ch347.c:1101
#define USB_WRITE_TIMEOUT
Definition: ch347.c:133
static int ch347_write_data(uint8_t *data, int *length)
writes data to the CH347 via libusb driver
Definition: ch347.c:376
static struct ch347_info ch347
Definition: ch347.c:263
#define CH347_SWD_CLOCK_MAX_DIVISOR
Definition: ch347.c:127
#define DEFAULT_CH347T_PRODUCT_ID
Definition: ch347.c:140
#define CH347_CMD_JTAG_DATA_SHIFT
Definition: ch347.c:108
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.
Definition: ch347.c:526
#define CH347_CMD_SWD_REG_R
Definition: ch347.c:118
static uint8_t ch347_activity_led_gpio_pin
Definition: ch347.c:261
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.
Definition: ch347.c:1018
static int ch347_swd_init(void)
Initialization for the swd mode.
Definition: ch347.c:1902
static int ch347_single_read_get_byte(int read_buf_idx, uint8_t *byte)
Function executes the single command and deliver one byte from the buffer that's read back from USB.
Definition: ch347.c:858
#define TCK_L
Definition: ch347.c:70
#define CH347T_MPHSI_INTERFACE
Definition: ch347.c:131
#define GPIO_CNT
Definition: ch347.c:75
#define TRST_H
Definition: ch347.c:71
#define GPIO_SET_L
Definition: ch347.c:82
static int ch347_sleep(int us)
Flushes the command buffer and sleeps for a specific timespan.
Definition: ch347.c:1353
static void ch347_read_swd_reg(uint8_t cmd)
Definition: ch347.c:1998
#define CH347_CMD_JTAG_BIT_OP
Definition: ch347.c:106
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...
Definition: ch347.c:1602
static int ch347_init(void)
CH347 Initialization function.
Definition: ch347.c:1862
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...
Definition: ch347.c:1565
#define LARGER_PACK_MAX_SIZE
Definition: ch347.c:90
#define CH347_CMD_HEADER
Definition: ch347.c:94
static const int ch347_standard_pack_clock_speeds[]
Definition: ch347.c:166
static uint16_t default_ch347_vids[]
Definition: ch347.c:255
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.
Definition: ch347.c:963
static int ch347_swd_run_queue_inner(void)
Definition: ch347.c:2154
#define CH347_CMD_SWD_SEQ_W
Definition: ch347.c:117
#define CH347_CMD_SWD_REG_W
Definition: ch347.c:116
static const struct command_registration ch347_subcommand_handlers[]
Definition: ch347.c:1809
static void ch347_scratchpad_check_full(void)
checks if the scratchpad is full.
Definition: ch347.c:877
static void ch347_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: ch347.c:2406
pack_size
Definition: ch347.c:159
@ STANDARD_PACK
Definition: ch347.c:161
@ LARGER_PACK
Definition: ch347.c:162
@ UNSET
Definition: ch347.c:160
static int ch347_swd_init_cmd(uint8_t clock_divisor)
swd init function
Definition: ch347.c:1626
#define CH347_CMD_JTAG_BIT_OP_RD
Definition: ch347.c:107
static int ch347_speed_set(int speed_index)
Initializes the JTAG interface and set CH347 TCK frequency.
Definition: ch347.c:1654
#define BYTEWISE_MODE_VERSION
Definition: ch347.c:136
#define USBC_PACKET_USBHS
Definition: ch347.c:93
static int ch347_reset(int trst, int srst)
Control (assert/deassert) the signals SRST and TRST on the interface.
Definition: ch347.c:1317
static int ch347_scratchpad_add_scan(struct scan_command *cmd)
Switch to SHIFT-DR or SHIFT-IR status for scanning.
Definition: ch347.c:1219
#define MAX_BITS_PER_BIT_OP
Definition: ch347.c:99
#define LED_ON
Definition: ch347.c:73
COMMAND_HANDLER(ch347_handle_vid_pid_command)
The command handler for setting the device usb vid/pid.
Definition: ch347.c:1781
static bool ch347_activity_led_active_high
Definition: ch347.c:262
static char * ch347_device_desc
Definition: ch347.c:260
#define CH347_CMD_JTAG_DATA_SHIFT_RD
Definition: ch347.c:109
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#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...
Definition: command.h:440
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
Definition: commands.c:192
enum scan_type jtag_scan_type(const struct scan_command *cmd)
Definition: commands.c:167
scan_type
The inferred type of a scan_command structure, indicating whether the command has the host scan in fr...
Definition: commands.h:22
@ SCAN_IN
From device to host,.
Definition: commands.h:24
@ SCAN_IO
Full-duplex scan.
Definition: commands.h:28
@ JTAG_TLR_RESET
Definition: commands.h:137
@ JTAG_SCAN
Definition: commands.h:129
@ JTAG_PATHMOVE
Definition: commands.h:140
@ JTAG_STABLECLOCKS
Definition: commands.h:142
@ JTAG_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
@ JTAG_TMS
Definition: commands.h:143
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint8_t type
Definition: esp_usb_jtag.c:0
uint8_t length
Definition: esp_usb_jtag.c:1
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...
Definition: interface.c:223
const char * tap_state_name(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
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...
Definition: interface.c:195
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 ...
Definition: interface.c:37
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 ...
Definition: interface.c:190
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:188
#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...
Definition: interface.h:50
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1075
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_RESET
Definition: jtag.h:56
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
#define ERROR_JTAG_TRANSITION_INVALID
Definition: jtag.h:560
static struct scan_blk scan
Definition: lakemont.c:60
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)
Definition: list.h:131
static void list_add_tail(struct list_head *new, struct list_head *head)
Definition: list.h:203
static int list_empty(const struct list_head *head)
Definition: list.h:61
#define list_for_each_entry_safe(p, n, h, field)
Definition: list.h:159
#define list_for_each_entry(p, h, field)
Definition: list.h:155
static void list_del(struct list_head *entry)
Definition: list.h:88
#define list_entry(ptr, type, field)
Definition: list.h:129
#define list_for_each_safe(p, n, head)
Definition: list.h:152
static void list_del_init(struct list_head *entry)
Definition: list.h:123
static void INIT_LIST_HEAD(struct list_head *list)
Definition: list.h:54
#define LOG_CUSTOM_LEVEL(level, expr ...)
Definition: log.h:119
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:103
#define LOG_WARNING(expr ...)
Definition: log.h:131
#define ERROR_FAIL
Definition: log.h:175
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define LOG_LEVEL_IS(FOO)
Definition: log.h:101
#define LOG_INFO(expr ...)
Definition: log.h:128
#define LOG_DEBUG(expr ...)
Definition: log.h:111
#define ERROR_OK
Definition: log.h:169
@ LOG_LVL_DEBUG
Definition: log.h:48
@ LOG_LVL_DEBUG_IO
Definition: log.h:49
const unsigned char bit_offset
Definition: mspm0.c:533
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
#define MIN(a, b)
Definition: replacements.h:22
static int step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv-011.c:1442
#define BIT(nr)
Definition: stm32l4x.h:18
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
Configuration options for a single GPIO.
Definition: adapter.h:68
unsigned int gpio_num
Definition: adapter.h:69
uint16_t read_len
Definition: ch347.c:191
uint8_t type
Definition: ch347.c:188
uint16_t tdo_bit_count
Definition: ch347.c:192
uint16_t write_data_len
Definition: ch347.c:190
uint8_t * write_data
Definition: ch347.c:189
int singe_read_len
Definition: ch347.c:227
struct list_head scan_queue
Definition: ch347.c:224
int max_len
Definition: ch347.c:213
bool use_bitwise_mode
Definition: ch347.c:211
struct list_head cmd_queue
Definition: ch347.c:222
int tms_pin
Definition: ch347.c:204
bool swclk_5mhz_supported
Definition: ch347.c:214
int tdi_pin
Definition: ch347.c:205
uint8_t single_read[CH347_SINGLE_CMD_MAX_READ]
Definition: ch347.c:226
int trst_pin
Definition: ch347.c:207
enum ch347_variant chip_variant
Definition: ch347.c:209
uint8_t scratchpad[UCMDPKT_DATA_MAX_BYTES_USBHS]
Definition: ch347.c:218
int scratchpad_idx
Definition: ch347.c:219
int tck_pin
Definition: ch347.c:206
enum pack_size pack_size
Definition: ch347.c:212
uint8_t scratchpad_cmd_type
Definition: ch347.c:217
int fields_len
Definition: ch347.c:198
struct scan_field * fields
Definition: ch347.c:197
int queued_retval
Definition: ch347.c:244
struct list_head free_cmd_head
Definition: ch347.c:249
struct ch347_swd_io ch347_cmd_buf[CH347_MAX_CMD_BUF]
Definition: ch347.c:250
int sent_cmd_count
Definition: ch347.c:245
unsigned int clk_divisor
Definition: ch347.c:246
int need_recv_len
Definition: ch347.c:243
uint8_t send_buf[CH347_MAX_SEND_BUF]
Definition: ch347.c:239
uint8_t recv_buf[CH347_MAX_RECV_BUF]
Definition: ch347.c:240
struct list_head send_cmd_head
Definition: ch347.c:248
unsigned int total_swd_clk
Definition: ch347.c:247
uint32_t * dst
Definition: ch347.c:233
struct list_head list_entry
Definition: ch347.c:235
uint8_t usb_cmd
Definition: ch347.c:231
uint8_t cmd
Definition: ch347.c:232
uint32_t value
Definition: ch347.c:234
const char * name
Definition: command.h:234
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:239
Represents a driver for a debugging interface.
Definition: interface.h:183
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:187
Definition: list.h:41
Definition: osbdm.c:25
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
Definition: commands.h:35
This structure defines a single scan field in the scan.
Definition: jtag.h:87
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
static const unsigned int swd_seq_dormant_to_swd_len
Definition: swd.h:190
static const uint8_t swd_seq_dormant_to_jtag[]
Dormant-to-JTAG sequence.
Definition: swd.h:230
#define SWD_CMD_A32
Definition: swd.h:19
static const uint8_t swd_seq_dormant_to_swd[]
Dormant-to-SWD sequence.
Definition: swd.h:171
static const uint8_t swd_seq_jtag_to_dormant[]
JTAG-to-dormant sequence.
Definition: swd.h:199
static bool swd_cmd_returns_ack(uint8_t cmd)
Test if we can rely on ACK returned by SWD command.
Definition: swd.h:58
#define SWD_CMD_PARK
Definition: swd.h:22
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
Definition: swd.h:72
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....
Definition: swd.h:35
static const unsigned int swd_seq_jtag_to_swd_len
Definition: swd.h:125
static const unsigned int swd_seq_line_reset_len
Definition: swd.h:104
static const unsigned int swd_seq_dormant_to_jtag_len
Definition: swd.h:244
#define SWD_CMD_APNDP
Definition: swd.h:17
static const unsigned int swd_seq_swd_to_dormant_len
Definition: swd.h:159
#define SWD_CMD_START
Definition: swd.h:16
#define SWD_CMD_RNW
Definition: swd.h:18
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
Definition: swd.h:98
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
Definition: swd.h:115
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
Definition: swd.h:136
static const unsigned int swd_seq_swd_to_jtag_len
Definition: swd.h:144
static const unsigned int swd_seq_jtag_to_dormant_len
Definition: swd.h:211
static const uint8_t swd_seq_swd_to_dormant[]
SWD-to-dormant sequence.
Definition: swd.h:153
#define TRANSPORT_SWD
Definition: transport.h:20
#define TRANSPORT_JTAG
Definition: transport.h:19
static uint16_t le_to_h_u16(const uint8_t *buf)
Definition: types.h:122
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static void h_u16_to_le(uint8_t *buf, uint16_t val)
Definition: types.h:208
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
Definition: types.h:265
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22
static unsigned int parity(unsigned int v)
Definition: xscale.c:624