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