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 uint8_t ch347_activity_led_gpio_pin = 0xFF;
260 static struct ch347_info ch347;
261 static struct libusb_device_handle *ch347_handle;
262 
263 /* there are "single" commands. These commands can't be chained together and
264  need to be send as single command and need a read after write */
265 static inline bool ch347_is_single_cmd_type(uint8_t type)
266 {
268 }
269 
270 static void log_buf_dump(const uint8_t *data, unsigned int size, bool recv)
271 {
272  unsigned int i = 0, j = 0;
273  unsigned int n = size * 3 + 1;
274  char *str = malloc(n);
275  if (!str)
276  return;
277 
278  while (i < size && j < n) {
279  uint8_t cmd = data[i++];
280  hexify(str + j, &cmd, 1, n - j);
281  j += 2;
282  str[j++] = ' ';
283 
284  unsigned int cmd_payload_size = le_to_h_u16(data + i);
285  if (i + 2 <= size && j + 4 < n) {
286  hexify(str + j, data + i, 2, n - j);
287  i += 2;
288  j += 4;
289  str[j++] = ' ';
290  }
291 
292  if (cmd == CH347_CMD_SWD) {
293  // nested SWD commands
294  str[j] = '\0';
295  if (i + cmd_payload_size > size) {
296  LOG_DEBUG_IO("%s - bad size", str);
297  cmd_payload_size = size - i;
298  } else {
299  LOG_DEBUG_IO("%s", str);
300  }
301  j = 0;
302  unsigned int swd_base_i = i;
303 
304  while (i < swd_base_i + cmd_payload_size) {
305  uint8_t swd_cmd = data[i++];
306  hexify(str + j, &swd_cmd, 1, n - j);
307  j += 2;
308  str[j++] = ' ';
309 
310  unsigned int swd_bits = 0;
311  unsigned int swd_payload_size = 0;
312  if (!recv) {
313  if (i + 2 <= size) {
314  swd_bits = le_to_h_u16(data + i);
315  hexify(str + j, data + i, 2, n - j);
316  i += 2;
317  j += 4;
318  str[j++] = ' ';
319  }
320  }
321 
322  switch (swd_cmd) {
323  case CH347_CMD_SWD_REG_W:
324  if (recv)
325  swd_payload_size = 1;
326  else
327  swd_payload_size = DIV_ROUND_UP(swd_bits, 8);
328  break;
329  case CH347_CMD_SWD_SEQ_W:
330  if (!recv)
331  swd_payload_size = DIV_ROUND_UP(swd_bits, 8);
332  break;
333  case CH347_CMD_SWD_REG_R:
334  if (recv)
335  swd_payload_size = 1 + 4 + 1;
336  else
337  swd_payload_size = 1;
338  break;
339  }
340 
341  hexify(str + j, data + i, MIN(swd_payload_size, size - i), n - j);
342  i += swd_payload_size;
343  j += 2 * swd_payload_size;
344  str[j] = '\0';
345  if (i > size)
346  LOG_DEBUG_IO(" %s - bad size", str);
347  else
348  LOG_DEBUG_IO(" %s", str);
349  j = 0;
350  }
351  } else {
352  hexify(str + j, data + i, MIN(cmd_payload_size, size - i), n - j);
353  i += cmd_payload_size;
354  j += 2 * cmd_payload_size;
355  str[j] = '\0';
356  if (i > size)
357  LOG_DEBUG_IO("%s - bad size", str);
358  else
359  LOG_DEBUG_IO("%s", str);
360  j = 0;
361  }
362  }
363  free(str);
364 }
365 
373 static int ch347_write_data(uint8_t *data, int *length)
374 {
375  int write_len = *length;
376  int i = 0;
377  int transferred = 0;
378 
379  while (true) {
380  int retval = jtag_libusb_bulk_write(ch347_handle, CH347_EPOUT, (char *)&data[i],
381  write_len, USB_WRITE_TIMEOUT, &transferred);
382  if (retval != ERROR_OK) {
383  LOG_ERROR("CH347 write fail");
384  *length = 0;
385  return retval;
386  }
387  i += transferred;
388  if (i >= *length)
389  break;
390  write_len = *length - i;
391  }
392 
394  LOG_DEBUG_IO("size=%d, buf=", i);
395  log_buf_dump(data, i, false);
396  }
397 
398  *length = i;
399  return ERROR_OK;
400 }
401 
409 static int ch347_read_data(uint8_t *data, int *length)
410 {
411  int read_len = *length;
412  int i = 0;
413  int transferred = 0;
414 
415  while (true) {
416  int retval = jtag_libusb_bulk_read(ch347_handle, CH347_EPIN, (char *)&data[i],
417  read_len, USB_READ_TIMEOUT, &transferred);
418  if (retval != ERROR_OK) {
419  LOG_ERROR("CH347 read fail");
420  *length = 0;
421  return retval;
422  }
423 
424  i += transferred;
425  if (i >= *length)
426  break;
427  read_len = *length - i;
428  }
429 
431  LOG_DEBUG_IO("size=%d, buf=", i);
432  log_buf_dump(data, i, true);
433  }
434 
435  *length = i;
436  return ERROR_OK;
437 }
438 
445 static void ch347_cmd_calc_reads(struct ch347_cmd *cmd)
446 {
447  cmd->read_len = 0;
448  cmd->tdo_bit_count = 0;
449 
450  switch (cmd->type) {
451  case CH347_CMD_GPIO:
452  // for GPIO we need to read back the same amount of data that we had send
453  cmd->read_len = cmd->write_data_len;
454  break;
455  case CH347_CMD_JTAG_INIT:
456  case CH347_CMD_SWD_INIT:
457  // for JTAG_INIT/SWD_INIT the amount is fixed
458  cmd->read_len = CH347_CMD_INIT_READ_LEN;
459  break;
461  // for bit operations we need to count the TCK high edges
462  for (int i = 0; i < cmd->write_data_len; i++) {
463  if ((cmd->write_data[i] & TCK_H) == TCK_H) {
464  cmd->read_len++;
465  cmd->tdo_bit_count++;
466  }
467  }
468  break;
470  // for byte operations: need to read one byte back for each data byte
471  cmd->read_len = cmd->write_data_len;
472  // we occupy 8 bits per byte in the TDO hardware buffer
473  cmd->tdo_bit_count = cmd->read_len * 8;
474  break;
475  }
476 }
477 
486 {
487  // nothing to do if no bytes are recorded
488  if (!ch347.scratchpad_idx)
489  return ERROR_OK;
490 
491  // malloc for the command and data bytes
492  struct ch347_cmd *cmd = malloc(sizeof(struct ch347_cmd));
493  if (cmd)
494  cmd->write_data = malloc(ch347.scratchpad_idx);
495  if (!cmd || !cmd->write_data) {
496  LOG_ERROR("malloc failed");
497  free(cmd);
498  return ERROR_FAIL;
499  }
500 
501  // copy data, calculate the reads and add to the command queue
502  cmd->type = ch347.scratchpad_cmd_type;
503  cmd->write_data_len = ch347.scratchpad_idx;
504  memcpy(cmd->write_data, ch347.scratchpad, ch347.scratchpad_idx);
506  list_add_tail(&cmd->queue, &ch347.cmd_queue);
507 
508  // cleanup the scratchpad for the next command
510  ch347.scratchpad_idx = 0;
511  return ERROR_OK;
512 }
513 
523 static int ch347_read_scan(uint8_t *decoded_buf, int decoded_buf_len, int raw_read_len)
524 {
525  int read_len = raw_read_len;
526  uint8_t *read_buf = malloc(read_len);
527  if (!read_buf) {
528  LOG_ERROR("malloc failed");
529  return ERROR_FAIL;
530  }
531 
532  int retval = ch347_read_data(read_buf, &read_len);
533  if (retval != ERROR_OK) {
534  free(read_buf);
535  return retval;
536  }
537 
538  int rd_idx = 0;
539  int decoded_buf_idx = 0;
540 
541  while (rd_idx < read_len) {
542  unsigned int type = read_buf[rd_idx++];
543  uint16_t data_len = le_to_h_u16(&read_buf[rd_idx]);
544  rd_idx += 2;
545  if (decoded_buf_idx > decoded_buf_len) {
546  LOG_ERROR("CH347 decoded_buf too small");
547  free(read_buf);
548  return ERROR_FAIL;
549  }
550 
551  // nothing to decode? Only read to make the CH347 happy!
552  if (!decoded_buf) {
553  rd_idx += data_len;
554  continue;
555  }
556 
557  switch (type) {
558  case CH347_CMD_GPIO:
559  case CH347_CMD_JTAG_INIT:
560  case CH347_CMD_SWD_INIT:
562  // for all bytewise commands: copy the data bytes
563  memcpy(&decoded_buf[decoded_buf_idx], &read_buf[rd_idx], data_len);
564  decoded_buf_idx += data_len;
565  rd_idx += data_len;
566  break;
568  // for CH347_CMD_JTAG_BIT_OP_RD we need to copy bit by bit
569  for (int i = 0; i < data_len; i++) {
570  if (read_buf[rd_idx + i] & BIT(0))
571  decoded_buf[decoded_buf_idx + i / 8] |= BIT(i % 8);
572  else
573  decoded_buf[decoded_buf_idx + i / 8] &= ~(BIT(i % 8));
574  }
575  rd_idx += data_len;
576  decoded_buf_idx += DIV_ROUND_UP(data_len, 8);
577  break;
578  default:
579  LOG_ERROR("CH347 read command fail");
580  free(read_buf);
581  return ERROR_FAIL;
582  }
583  }
584 
585  free(read_buf);
586  return ERROR_OK;
587 }
588 
596 static int ch347_scan_data_to_fields(uint8_t *decoded_buf, int decoded_buf_len)
597 {
598  int byte_offset = 0;
599  struct ch347_scan *scan;
600  struct ch347_scan *tmp;
601  int bit_offset = 0;
603  for (int i = 0; i < scan->fields_len; i++) {
604  int num_bits = scan->fields[i].num_bits;
605  LOG_DEBUG("fields[%d].in_value[%d], read from bit offset: %d", i, num_bits, bit_offset);
606  // only if we need the value
607  if (scan->fields[i].in_value) {
608  uint8_t *capture_buf = malloc(DIV_ROUND_UP(num_bits, 8));
609  if (!capture_buf) {
610  LOG_ERROR("malloc failed");
611  return ERROR_FAIL;
612  }
613  uint8_t *captured = buf_set_buf(decoded_buf, bit_offset, capture_buf, 0, num_bits);
614 
616  char *str = buf_to_hex_str(captured, num_bits);
617  LOG_DEBUG_IO("size=%d, buf=[%s]", num_bits, str);
618  free(str);
619  }
620 
621  buf_cpy(captured, scan->fields[i].in_value, num_bits);
622  free(capture_buf);
623  } else {
625  LOG_DEBUG_IO("field skipped");
626  }
627  bit_offset += num_bits;
628  }
629  list_del(&scan->queue);
630  free(scan);
631  /* after one round of scan field processing the
632  next data bits are read from the next data byte
633  => round up and calculate the next start bit */
634  byte_offset = DIV_ROUND_UP(bit_offset, 8);
635  bit_offset = byte_offset * 8;
636  }
637 
638  // if not all bytes are transferred: put the rest into single_read buffer
639  if (byte_offset < decoded_buf_len) {
640  ch347.singe_read_len = decoded_buf_len - byte_offset;
641  LOG_DEBUG("single read of %d bytes", ch347.singe_read_len);
643  LOG_ERROR("Can't read more than %d bytes for a single command!", CH347_SINGLE_CMD_MAX_READ);
645  }
646  memcpy(ch347.single_read, &decoded_buf[byte_offset], ch347.singe_read_len);
647  }
648 
649  return ERROR_OK;
650 }
651 
658 static int ch347_cmd_transmit_queue(void)
659 {
660  // queue last command
661  int retval = ch347_cmd_from_scratchpad();
662  if (retval != ERROR_OK)
663  return retval;
664 
665  // nothing to do! => done here
666  if (list_empty(&ch347.cmd_queue))
667  return ERROR_OK;
668 
669  // calculate the needed buffer length for all decoded bytes
670  struct ch347_cmd *cmd;
671  int decoded_buf_len = 0;
673  if (cmd->read_len > 0)
674  decoded_buf_len += ch347_is_single_cmd_type(cmd->type) ?
675  cmd->read_len : DIV_ROUND_UP(cmd->tdo_bit_count, 8);
676 
677  // create the buffer for all decoded bytes
678  uint8_t *decoded_buf = NULL;
679  int decoded_buf_idx = 0;
680  if (decoded_buf_len > 0) {
681  decoded_buf = malloc(decoded_buf_len);
682  if (!decoded_buf) {
683  LOG_ERROR("malloc failed");
684  return ERROR_FAIL;
685  }
686  }
687 
688  while (!list_empty(&ch347.cmd_queue)) {
689  struct ch347_cmd *last_cmd = NULL;
690  int total_len = 0;
691  int total_tdo_count = 0;
692  int bytes_to_write = 0;
693 
695  total_len += CH347_CMD_HEADER + cmd->write_data_len;
696  total_tdo_count += cmd->tdo_bit_count;
697  // don't exceed max length or max TDO bit count
698  if (total_len >= ch347.max_len || total_tdo_count >= HW_TDO_BUF_SIZE)
699  break;
700  // remember the last cmd to send and bytes to send
701  last_cmd = cmd;
702  bytes_to_write = total_len;
703  }
704 
705  // sanity checks
706  if (!last_cmd || bytes_to_write == 0) {
707  LOG_ERROR("Nothing to send!");
708  free(decoded_buf);
709  return ERROR_FAIL;
710  }
711 
712  // create the write buffer
713  uint8_t *write_buf = malloc(bytes_to_write);
714  if (!write_buf) {
715  LOG_ERROR("malloc failed");
716  free(decoded_buf);
717  return ERROR_FAIL;
718  }
719 
720  int idx = 0;
721  int bytes_to_read = 0;
722  int current_decoded_buf_len = 0;
723  struct ch347_cmd *tmp;
724 
726  // copy command to buffer
727  write_buf[idx++] = cmd->type;
728  h_u16_to_le(&write_buf[idx], cmd->write_data_len);
729  idx += 2;
730  memcpy(&write_buf[idx], cmd->write_data, cmd->write_data_len);
731  idx += cmd->write_data_len;
732  // need to read something back?
733  if (cmd->read_len > 0) {
734  bytes_to_read += CH347_CMD_HEADER + cmd->read_len;
735  current_decoded_buf_len += ch347_is_single_cmd_type(cmd->type) ?
736  cmd->read_len : DIV_ROUND_UP(cmd->tdo_bit_count, 8);
737  }
738 
739  // cmd data no longer needed
740  list_del(&cmd->queue);
741  free(cmd->write_data);
742  free(cmd);
743 
744  if (cmd == last_cmd)
745  break;
746  }
747 
748  // write data to device
749  retval = ch347_write_data(write_buf, &idx);
750  free(write_buf);
751  if (retval != ERROR_OK) {
752  free(decoded_buf);
753  return retval;
754  }
755 
756  if (!bytes_to_read)
757  continue;
758 
759  // Need only to execute a read without decoding the data to make the CH347 happy?
760  if (!current_decoded_buf_len) {
761  // read but don't decode anything
762  retval = ch347_read_scan(NULL, 0, bytes_to_read);
763  } else {
764  retval = ch347_read_scan(&decoded_buf[decoded_buf_idx], current_decoded_buf_len, bytes_to_read);
765  decoded_buf_idx += current_decoded_buf_len;
766  }
767 
768  if (retval != ERROR_OK) {
769  free(decoded_buf);
770  return retval;
771  }
772  }
773 
774  // something decoded from the data read back from CH347?
775  if (decoded_buf) {
776  // put the decoded data into the scan fields or single_read buffer
777  retval = ch347_scan_data_to_fields(decoded_buf, decoded_buf_len);
778  free(decoded_buf);
779  }
780 
781  return retval;
782 }
783 
790 static int ch347_cmd_start_next(uint8_t type)
791 {
792  // different command type or non chainable command? (GPIO commands can't be concat)
793  uint8_t prev_type = ch347.scratchpad_cmd_type;
794  if (prev_type != type || ch347_is_single_cmd_type(type)) {
795  // something written in the scratchpad? => store it as command
796  if (prev_type != 0 && ch347.scratchpad_idx > 0) {
797  int retval = ch347_cmd_from_scratchpad();
798  if (retval != ERROR_OK)
799  return retval;
800 
801  /* if the last queued command is not chainable we should send it immediately
802  because e.g. the GPIO command can't be combined with any other command */
803  if (ch347_is_single_cmd_type(prev_type)) {
804  retval = ch347_cmd_transmit_queue();
805  if (retval != ERROR_OK)
806  return retval;
807  }
808  }
809 
810  /* before we can send non chainable command ("single" like GPIO command) we should send all
811  other commands because we can't send it together with other commands */
813  int retval = ch347_cmd_transmit_queue();
814  if (retval != ERROR_OK)
815  return retval;
816  }
817 
818  // store the next command type
820  }
821 
822  return ERROR_OK;
823 }
824 
832 static int ch347_scan_queue_fields(struct scan_field *scan_fields, int scan_fields_len)
833 {
834  // malloc for the scan struct
835  struct ch347_scan *scan = malloc(sizeof(struct ch347_scan));
836  if (!scan) {
837  LOG_ERROR("malloc failed");
838  return ERROR_FAIL;
839  }
840 
841  scan->fields = scan_fields;
842  scan->fields_len = scan_fields_len;
843  list_add_tail(&scan->queue, &ch347.scan_queue);
844  return ERROR_OK;
845 }
846 
855 static int ch347_single_read_get_byte(int read_buf_idx, uint8_t *byte)
856 {
857  int retval = ch347_cmd_transmit_queue();
858  if (retval != ERROR_OK)
859  return retval;
860 
861  if (read_buf_idx > CH347_SINGLE_CMD_MAX_READ || read_buf_idx < 0) {
862  LOG_ERROR("read_buf_idx out of range");
863  return ERROR_FAIL;
864  }
865 
866  *byte = ch347.single_read[read_buf_idx];
867  return ERROR_OK;
868 }
869 
875 {
876  // if full create a new command in the queue
878  uint8_t type = ch347.scratchpad_cmd_type;
881  }
882 }
883 
892 static int ch347_scratchpad_add_byte(uint8_t byte)
893 {
894  if (ch347.scratchpad_cmd_type == 0) {
895  LOG_ERROR("call ch347_next_cmd first!");
896  return ERROR_FAIL;
897  }
898 
901  return ERROR_OK;
902 }
903 
912 {
914 }
915 
926 static int ch347_scratchpad_add_bytes(uint8_t *bytes, int count)
927 {
928  if (ch347.scratchpad_cmd_type == 0) {
929  LOG_ERROR("call ch347_next_cmd first!");
930  return ERROR_FAIL;
931  }
932 
933  int remaining = count;
934  int bytes_idx = 0;
935  while (remaining > 0) {
936  int bytes_to_store = ch347.scratchpad_idx + remaining <= UCMDPKT_DATA_MAX_BYTES_USBHS ?
938 
939  if (bytes)
940  memcpy(&ch347.scratchpad[ch347.scratchpad_idx], &bytes[bytes_idx], bytes_to_store);
941  else
942  memset(&ch347.scratchpad[ch347.scratchpad_idx], 0, bytes_to_store);
943 
944  ch347.scratchpad_idx += bytes_to_store;
945  bytes_idx += bytes_to_store;
946  remaining -= bytes_to_store;
948  }
949 
950  return ERROR_OK;
951 }
952 
960 static int ch347_scratchpad_add_clock_tms(bool tms)
961 {
962  ch347.tms_pin = tms ? TMS_H : TMS_L;
963  ch347.tck_pin = TCK_L;
964  int retval = ch347_scratchpad_add_pin_byte();
965  if (retval != ERROR_OK)
966  return retval;
967 
968  ch347.tck_pin = TCK_H;
970 }
971 
979 {
980  if (ch347.scratchpad_cmd_type == 0) {
982  if (retval != ERROR_OK)
983  return retval;
984  }
985 
986  bool tms = ch347.tms_pin == TMS_H;
987  for (int i = 0; i < count; i++) {
988  int retval = ch347_scratchpad_add_clock_tms(tms);
989  if (retval != ERROR_OK)
990  return retval;
991  }
992 
993  return ERROR_OK;
994 }
995 
1002 {
1003  ch347.tck_pin = TCK_L;
1005 }
1006 
1015 static int ch347_scratchpad_add_tms_change(const uint8_t *tms_value, int step, int skip)
1016 {
1017  LOG_DEBUG_IO("TMS Value: %02x..., step = %d, skip = %d", tms_value[0], step, skip);
1018 
1020  if (retval != ERROR_OK)
1021  return retval;
1022 
1023  for (int i = skip; i < step; i++) {
1024  retval = ch347_scratchpad_add_clock_tms((tms_value[i / 8] >> (i % 8)) & BIT(0));
1025  if (retval != ERROR_OK)
1026  return retval;
1027  }
1028 
1030 }
1031 
1039 {
1040  LOG_DEBUG_IO("num_states=%d, last_state=%d", cmd->num_states, cmd->path[cmd->num_states - 1]);
1041 
1043  if (retval != ERROR_OK)
1044  return retval;
1045 
1046  for (unsigned int i = 0; i < cmd->num_states; i++) {
1047  if (tap_state_transition(tap_get_state(), false) == cmd->path[i]) {
1048  retval = ch347_scratchpad_add_clock_tms(0);
1049  if (retval != ERROR_OK)
1050  return retval;
1051  } else if (tap_state_transition(tap_get_state(), true) == cmd->path[i]) {
1052  retval = ch347_scratchpad_add_clock_tms(1);
1053  if (retval != ERROR_OK)
1054  return retval;
1055  } else {
1056  LOG_ERROR("No transition possible!");
1058  }
1059  tap_set_state(cmd->path[i]);
1060  }
1061 
1063 }
1064 
1073 {
1074  uint8_t tms_scan;
1075  int tms_len;
1076 
1078  // don't do anything if we are already in the right state; but do execute always the TAP_RESET
1079  if (tap_get_state() == state && state != TAP_RESET)
1080  return ERROR_OK;
1081 
1082  tms_scan = tap_get_tms_path(tap_get_state(), state);
1084  int retval = ch347_scratchpad_add_tms_change(&tms_scan, tms_len, skip);
1086  return retval;
1087 }
1088 
1098 static int ch347_scratchpad_add_write_read(struct scan_command *cmd, uint8_t *bits, int bits_len, enum scan_type scan)
1099 {
1100  // the bits and bytes to transfer
1101  int byte_count = bits_len / 8;
1102  int bit_count = bits_len % 8;
1103 
1104  // only bytes are not possible because we need to set TMS high for the last bit
1105  if (byte_count > 0 && bit_count == 0) {
1106  // make one byte to eight bits
1107  byte_count--;
1108  bit_count = 8;
1109  }
1110 
1111  // in bitwise mode only bits are allowed
1112  if (ch347.use_bitwise_mode) {
1113  byte_count = 0;
1114  bit_count = bits_len;
1115  }
1116 
1117  bool is_read = (scan == SCAN_IN || scan == SCAN_IO);
1118 
1119  // if we need to send bytes
1120  if (byte_count > 0) {
1121  // start the next cmd and copy the data out bytes to it
1123  if (retval != ERROR_OK)
1124  return retval;
1125 
1126  if (bits)
1127  retval = ch347_scratchpad_add_bytes(bits, byte_count);
1128  else
1129  retval = ch347_scratchpad_add_bytes(NULL, byte_count);
1130 
1131  if (retval != ERROR_OK)
1132  return retval;
1133  }
1134 
1135  // bits are always need to send; no possibility to not send bits
1137  if (retval != ERROR_OK)
1138  return retval;
1139 
1140  ch347.tms_pin = TMS_L;
1141  ch347.tdi_pin = TDI_L;
1142 
1143  for (int i = 0; i < bit_count; i++) {
1144  if (bits)
1145  ch347.tdi_pin = ((bits[byte_count + i / 8] >> i % 8) & BIT(0)) ? TDI_H : TDI_L;
1146 
1147  // for the last bit set TMS high to exit the shift state
1148  if (i + 1 == bit_count)
1149  ch347.tms_pin = TMS_H;
1150 
1151  ch347.tck_pin = TCK_L;
1152  retval = ch347_scratchpad_add_pin_byte();
1153  if (retval != ERROR_OK)
1154  return retval;
1155 
1156  ch347.tck_pin = TCK_H;
1157  retval = ch347_scratchpad_add_pin_byte();
1158  if (retval != ERROR_OK)
1159  return retval;
1160 
1161  /* cut the package after each MAX_BITS_PER_BIT_OP bits because it
1162  needs a dividable by 8 bits package */
1163  if (i > 0 && (i + 1) % MAX_BITS_PER_BIT_OP == 0) {
1164  retval = ch347_cmd_from_scratchpad();
1165  if (retval != ERROR_OK)
1166  return retval;
1167 
1169  if (retval != ERROR_OK)
1170  return retval;
1171  }
1172  }
1173 
1174  // one TCK_L after the last bit
1176  if (retval != ERROR_OK)
1177  return retval;
1178 
1179  // if read is involved we need to queue the scan fields
1180  if (is_read)
1181  return ch347_scan_queue_fields(cmd->fields, cmd->num_fields);
1182 
1183  return ERROR_OK;
1184 }
1185 
1193 static int ch347_scratchpad_add_run_test(int cycles, enum tap_state state)
1194 {
1195  LOG_DEBUG_IO("cycles=%d, end_state=%d", cycles, state);
1196  int retval;
1197  if (tap_get_state() != TAP_IDLE) {
1199  if (retval != ERROR_OK)
1200  return retval;
1201  }
1202 
1203  retval = ch347_scratchpad_add_stableclocks(cycles);
1204  if (retval != ERROR_OK)
1205  return retval;
1206 
1208 }
1209 
1217 {
1218  static const char *const type2str[] = {"", "SCAN_IN", "SCAN_OUT", "SCAN_IO"};
1219 
1220  enum scan_type type = jtag_scan_type(cmd);
1221  uint8_t *buf = NULL;
1222  int scan_bits = jtag_build_buffer(cmd, &buf);
1223 
1224  // add a move to IRSHIFT or DRSHIFT state
1225  int retval;
1226  if (cmd->ir_scan)
1228  else
1230 
1231  if (retval != ERROR_OK) {
1232  free(buf);
1233  return retval;
1234  }
1235 
1237  char *log_buf = buf_to_hex_str(buf, scan_bits);
1238  LOG_DEBUG_IO("scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d",
1239  cmd->ir_scan ? "IRSCAN" : "DRSCAN",
1240  type2str[type],
1241  scan_bits, log_buf, cmd->end_state);
1242  free(log_buf);
1243  }
1244 
1245  retval = ch347_scratchpad_add_write_read(cmd, buf, scan_bits, type);
1246  free(buf);
1247  if (retval != ERROR_OK)
1248  return retval;
1249 
1250  // add a move to the final state
1251  return ch347_scratchpad_add_move_state(cmd->end_state, 1);
1252 }
1253 
1261 static int ch347_gpio_set(int gpio, bool data)
1262 {
1263  int retval = ch347_cmd_start_next(CH347_CMD_GPIO);
1264  if (retval != ERROR_OK)
1265  return retval;
1266 
1267  uint8_t gpios[GPIO_CNT];
1268  memset(gpios, 0, GPIO_CNT);
1269  /* always set bits 7 and 6 for GPIO enable
1270  bits 5 and 4 for pin direction output
1271  bit 3 is the data bit */
1272  gpios[gpio] = data == 0 ? GPIO_SET_L : GPIO_SET_H;
1274  if (retval != ERROR_OK)
1275  return retval;
1276 
1277  // check in the read if the bit is set/cleared correctly
1278  uint8_t byte;
1279  retval = ch347_single_read_get_byte(gpio, &byte);
1280  if (retval != ERROR_OK)
1281  return retval;
1282 
1283  if ((byte & BIT(6)) >> 6 != data) {
1284  LOG_ERROR("Output not set.");
1285  return ERROR_FAIL;
1286  }
1287 
1288  return ERROR_OK;
1289 }
1290 
1297 static int ch347_activity_led_set(int led_state)
1298 {
1299  if (ch347_activity_led_gpio_pin != 0xFF)
1300  return ch347_gpio_set(ch347_activity_led_gpio_pin, ch347_activity_led_active_high ? led_state : 1 - led_state);
1301 
1302  // not configured => also OK
1303  return ERROR_OK;
1304 }
1305 
1314 static int ch347_reset(int trst, int srst)
1315 {
1316  LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst);
1317  if (srst) {
1318  LOG_ERROR("Asserting SRST not supported!");
1319  return ERROR_FAIL;
1320  }
1321 
1322  if (swd_mode) {
1323  if (trst)
1324  LOG_WARNING("Asserting TRST not supported in SWD mode!");
1325  return ERROR_OK;
1326  }
1327 
1329  if (retval != ERROR_OK)
1330  return retval;
1331 
1332  ch347.trst_pin = trst ? TRST_L : TRST_H;
1333  retval = ch347_scratchpad_add_pin_byte();
1334  if (retval != ERROR_OK)
1335  return retval;
1336 
1338  if (retval != ERROR_OK)
1339  return retval;
1340 
1341  return ch347_cmd_transmit_queue();
1342 }
1343 
1350 static int ch347_sleep(int us)
1351 {
1352  LOG_DEBUG_IO("us=%d", us);
1353  int retval = ch347_cmd_transmit_queue();
1354  jtag_sleep(us);
1355  return retval;
1356 }
1357 
1363 static int ch347_execute_queue(struct jtag_command *cmd_queue)
1364 {
1365  struct jtag_command *cmd = cmd_queue;
1366 
1367  int retval = ch347_activity_led_set(LED_ON);
1368  if (retval != ERROR_OK)
1369  return retval;
1370 
1371  while (retval == ERROR_OK && cmd) {
1372  switch (cmd->type) {
1373  case JTAG_RUNTEST:
1374  retval = ch347_scratchpad_add_run_test(cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
1375  break;
1376  case JTAG_STABLECLOCKS:
1377  retval = ch347_scratchpad_add_stableclocks(cmd->cmd.stableclocks->num_cycles);
1378  break;
1379  case JTAG_TLR_RESET:
1380  retval = ch347_scratchpad_add_move_state(cmd->cmd.statemove->end_state, 0);
1381  break;
1382  case JTAG_PATHMOVE:
1383  retval = ch347_scratchpad_add_move_path(cmd->cmd.pathmove);
1384  break;
1385  case JTAG_TMS:
1386  retval = ch347_scratchpad_add_tms_change(cmd->cmd.tms->bits, cmd->cmd.tms->num_bits, 0);
1387  break;
1388  case JTAG_SLEEP:
1389  retval = ch347_sleep(cmd->cmd.sleep->us);
1390  break;
1391  case JTAG_SCAN:
1392  retval = ch347_scratchpad_add_scan(cmd->cmd.scan);
1393  break;
1394  default:
1395  LOG_ERROR("BUG: unknown JTAG command type 0x%X", cmd->type);
1396  retval = ERROR_FAIL;
1397  break;
1398  }
1399 
1400  cmd = cmd->next;
1401  }
1402 
1403  if (retval != ERROR_OK)
1404  return retval;
1405 
1406  retval = ch347_cmd_transmit_queue();
1407  if (retval != ERROR_OK)
1408  return retval;
1409 
1411 }
1412 
1418 static int ch347_open_device(void)
1419 {
1420  const uint16_t *ch347_vids = default_ch347_vids;
1421  const uint16_t *ch347_pids = default_ch347_pids;
1422 
1423  if (adapter_usb_get_vids()[0] != 0) {
1424  ch347_vids = adapter_usb_get_vids();
1425  ch347_pids = adapter_usb_get_pids();
1426  }
1427 
1428  int retval = jtag_libusb_open(ch347_vids, ch347_pids,
1430  if (retval != ERROR_OK) {
1431  char error_message[256];
1432  snprintf(error_message, sizeof(error_message), "CH347 not found. Tried VID/PID pairs: ");
1433  for (int i = 0; ch347_vids[i] != 0; i++)
1434  snprintf(error_message + strlen(error_message), sizeof(error_message) - strlen(error_message),
1435  "%04x:%04x ", ch347_vids[i], ch347_pids[i]);
1436 
1437  LOG_ERROR("%s", error_message);
1438  return retval;
1439  }
1440 
1441  struct libusb_device_descriptor ch347_device_descriptor;
1442  libusb_device *device = libusb_get_device(ch347_handle);
1443  if (!device) {
1444  LOG_ERROR("CH347 error calling libusb_get_device");
1446  return ERROR_FAIL;
1447  }
1448 
1449  retval = libusb_get_device_descriptor(device, &ch347_device_descriptor);
1450  if (retval != ERROR_OK) {
1451  LOG_ERROR("CH347 error getting device descriptor: %s", libusb_error_name(retval));
1453  return retval;
1454  }
1455 
1456  // CH347T / CH347F detection
1457  // if we can claim interface 4 we found a CH347F chip; if we can claim interface 2 we found CH347T chip
1458  retval = libusb_claim_interface(ch347_handle, CH347F_MPHSI_INTERFACE);
1459  if (retval != ERROR_OK) {
1460  retval = libusb_claim_interface(ch347_handle, CH347T_MPHSI_INTERFACE);
1461  if (retval != ERROR_OK) {
1462  LOG_ERROR("CH347 unable to claim interface: %s", libusb_error_name(retval));
1464  return retval;
1465  }
1467  } else {
1469  }
1470 
1471  char firmware_version;
1473  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1474  VENDOR_VERSION, 0, 0, &firmware_version, sizeof(firmware_version),
1476  if (retval != ERROR_OK) {
1477  LOG_ERROR("CH347 unable to get firmware version");
1479  return retval;
1480  }
1481 
1482  char manufacturer[256 + 1];
1483  if (libusb_get_string_descriptor_ascii(ch347_handle, ch347_device_descriptor.iManufacturer,
1484  (unsigned char *)manufacturer, sizeof(manufacturer) - 1) < 0) {
1485  strcpy(manufacturer, "(unknown)");
1486  }
1487  char product[256 + 1];
1488  if (libusb_get_string_descriptor_ascii(ch347_handle, ch347_device_descriptor.iProduct,
1489  (unsigned char *)product, sizeof(product) - 1) < 0) {
1490  strcpy(product, "(unknown)");
1491  }
1492  char serial_number[256 + 1];
1493  if (libusb_get_string_descriptor_ascii(ch347_handle, ch347_device_descriptor.iSerialNumber,
1494  (unsigned char *)serial_number, sizeof(serial_number) - 1) < 0) {
1495  strcpy(serial_number, "(unknown)");
1496  }
1497 
1498  LOG_INFO("CH347 %s from vendor %s with serial number %s found. (Chip version=%X.%2X, Firmware=0x%02X)",
1499  product,
1500  manufacturer,
1501  serial_number,
1502  (ch347_device_descriptor.bcdDevice >> 8) & 0xFF,
1503  ch347_device_descriptor.bcdDevice & 0xFF,
1504  firmware_version);
1505 
1506  if (ch347.chip_variant == CH347T && ch347_device_descriptor.bcdDevice < BYTEWISE_MODE_VERSION) {
1507  LOG_INFO("CH347T old version of the chip, JTAG only working in bitwise mode. For bytewise mode at least version %X.%X is needed.",
1508  (BYTEWISE_MODE_VERSION >> 8) & 0xFF,
1509  BYTEWISE_MODE_VERSION & 0xFF);
1510  ch347.use_bitwise_mode = true;
1511  } else {
1512  ch347.use_bitwise_mode = false;
1513  }
1514 
1515  if (ch347.chip_variant == CH347T) {
1516  if (swd_mode) {
1517  ch347.swclk_5mhz_supported = ch347_device_descriptor.bcdDevice >= 0x544;
1518 
1519  if (ch347_device_descriptor.bcdDevice < 0x441)
1520  LOG_WARNING("CH347T version older than 4.41 probably does not support SWD transport");
1521 
1522  if (ch347_device_descriptor.bcdDevice == 0x441)
1523  LOG_WARNING("If CH347T version 4.41 cannot connect or SWD fails often, insert a resistor to SWDIO circuit");
1524 
1525  } else if (ch347_device_descriptor.bcdDevice == 0x241) {
1526  LOG_WARNING("CH347T version 2.41 has very weird clock timing, may not work with a slower JTAG device");
1527  }
1528 
1529  if (ch347_device_descriptor.bcdDevice < 0x544)
1530  LOG_INFO("Please upgrade CH347T firmware to a production version >= 5.44");
1531 
1532  } else if (ch347.chip_variant == CH347F) {
1533  ch347.swclk_5mhz_supported = ch347_device_descriptor.bcdDevice >= 0x101;
1534 
1535  if (ch347_device_descriptor.bcdDevice < 0x101)
1536  LOG_INFO("Please upgrade CH347F firmware to a production version >= 1.1");
1537  }
1538 
1539  return ERROR_OK;
1540 }
1541 
1547 static int ch347_quit(void)
1548 {
1549  // on close set the LED on, because the state without JTAG is on
1551  int retval = ch347_cmd_transmit_queue();
1553  LOG_DEBUG_IO("CH347 close");
1554  return retval;
1555 }
1556 
1568 static int ch347_adapter_init(uint8_t clock_index, bool *supports_larger_pack_mode)
1569 {
1571  if (retval != ERROR_OK)
1572  return retval;
1573 
1574  retval = ch347_scratchpad_add_byte(0);
1575  if (retval != ERROR_OK)
1576  return retval;
1577 
1578  retval = ch347_scratchpad_add_byte(clock_index);
1579  if (retval != ERROR_OK)
1580  return retval;
1581 
1582  for (int i = 0; i < 4; i++) {
1583  retval = ch347_scratchpad_add_pin_byte();
1584  if (retval != ERROR_OK)
1585  return retval;
1586  }
1587 
1588  uint8_t mode;
1589  retval = ch347_single_read_get_byte(0, &mode);
1590  if (retval != ERROR_OK)
1591  return retval;
1592 
1593  *supports_larger_pack_mode = mode != 0;
1594  return ERROR_OK;
1595 }
1596 
1605 static int ch347_adapter_supports_larger_pack_mode(bool *supports_larger_pack_mode)
1606 {
1607  return ch347_adapter_init(CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE, supports_larger_pack_mode);
1608 }
1609 
1617 static int ch347_adapter_set_speed(uint8_t clock_index)
1618 {
1619  bool unused;
1620  return ch347_adapter_init(clock_index, &unused);
1621 }
1622 
1629 static int ch347_swd_init_cmd(uint8_t clock_divisor)
1630 {
1632  if (retval != ERROR_OK)
1633  return retval;
1634 
1635  uint8_t cmd_data[] = {0x40, 0x42, 0x0f, 0x00, clock_divisor, 0x00, 0x00, 0x00 };
1636  retval = ch347_scratchpad_add_bytes(cmd_data, ARRAY_SIZE(cmd_data));
1637  if (retval != ERROR_OK)
1638  return retval;
1639 
1640  /* TODO: CH347_CMD_SWD_INIT reads one data byte.
1641  But how can we decide if SWD init was successfully executed?
1642  Return an error code if init was failed */
1643  uint8_t init_result = 0;
1644  retval = ch347_single_read_get_byte(0, &init_result);
1645  LOG_DEBUG("SWD init clk div %" PRIu8 ", result %02" PRIx8,
1646  clock_divisor, init_result);
1647  ch347_swd_context.clk_divisor = clock_divisor;
1648  return retval;
1649 }
1650 
1657 static int ch347_speed_set(int speed_index)
1658 {
1659  if (swd_mode)
1660  return ch347_swd_init_cmd(speed_index);
1661 
1662  int retval = ch347_adapter_set_speed(speed_index);
1663  if (retval != ERROR_OK) {
1664  LOG_ERROR("Couldn't set CH347 speed");
1665  return retval;
1666  }
1667 
1668  return ERROR_OK;
1669 }
1670 
1676 static int ch347_init_pack_size(void)
1677 {
1678  // already set?
1679  if (ch347.pack_size != UNSET)
1680  return ERROR_OK;
1681 
1682  // set the lower limit for starting
1684  bool supports_larger_pack_mode;
1685  int retval = ch347_adapter_supports_larger_pack_mode(&supports_larger_pack_mode);
1686  if (retval != ERROR_OK)
1687  return retval;
1688 
1689  ch347.pack_size = supports_larger_pack_mode ? LARGER_PACK : STANDARD_PACK;
1692  return ERROR_OK;
1693 }
1694 
1702 static int ch347_speed_get(int speed_idx, int *khz)
1703 {
1704  if (swd_mode) {
1705  if (speed_idx)
1706  *khz = DIV_ROUND_UP(CH347_SWD_CLOCK_BASE, speed_idx);
1707  else
1708  *khz = CH347_SWD_CLOCK_MAX;
1709  return ERROR_OK;
1710  }
1711 
1712  int retval = ch347_init_pack_size();
1713  if (retval != ERROR_OK)
1714  return retval;
1715  const int *speeds = ch347.pack_size == STANDARD_PACK ?
1717  *khz = speeds[speed_idx];
1718  return ERROR_OK;
1719 }
1720 
1728 static int ch347_speed_get_index(int khz, int *speed_idx)
1729 {
1730  if (khz == 0) {
1731  LOG_ERROR("Adaptive clocking not supported");
1732  return ERROR_FAIL;
1733  }
1734 
1735  if (swd_mode) {
1737  *speed_idx = 0;
1738  } else {
1739  // Don't allow too low clk speeds: packet processing is limited to ~8 msec
1740  // or triggers host USB disconnect
1741  *speed_idx = MIN(DIV_ROUND_UP(CH347_SWD_CLOCK_BASE, khz),
1743  }
1744  return ERROR_OK;
1745  }
1746 
1747  // when checking with speed index 9 we can see if the device supports STANDARD_PACK or LARGER_PACK mode
1748  int retval = ch347_init_pack_size();
1749  if (retval != ERROR_OK)
1750  return retval;
1751  // depending on pack size there are different fixed clock speeds possible
1752  const int *speeds = ch347.pack_size == STANDARD_PACK ?
1754  int length = ch347.pack_size == STANDARD_PACK ?
1756  int idx = -1;
1757  int lower_bound = 0;
1758  // find the suitable speed index
1759  for (int i = 0; i < length; i++) {
1760  if (khz >= lower_bound && khz <= speeds[i]) {
1761  idx = i;
1762  break;
1763  }
1764  lower_bound = speeds[i];
1765  }
1766  // too high! => use max possible speed
1767  if (idx == -1) {
1768  LOG_INFO("Speed %d kHz is higher than highest speed of %d kHz. Using %d khz!",
1769  khz, speeds[length - 1], speeds[length - 1]);
1770  idx = length - 1;
1771  } else if (speeds[idx] != khz) {
1772  LOG_INFO("Requested speed of %d kHz is not possible. Using the next higher speed of %d kHz!",
1773  khz, speeds[idx]);
1774  }
1775  *speed_idx = idx;
1776  return ERROR_OK;
1777 }
1778 
1779 static const struct command_registration ch347_command_handlers[] = {
1780  {
1781  .name = "ch347",
1782  .mode = COMMAND_ANY,
1783  .help = "perform ch347 management",
1784  .usage = "",
1785  },
1787 };
1788 
1798 static void ch347_configure_activity_led(const struct adapter_gpio_config *led_config)
1799 {
1800  uint8_t gpio = led_config->gpio_num;
1801  if (gpio >= GPIO_CNT || (BIT(gpio) & USEABLE_GPIOS) == 0)
1802  return;
1803 
1806 }
1807 
1813 static int ch347_init(void)
1814 {
1815  int retval = ch347_open_device();
1816 
1817  if (retval != ERROR_OK) {
1818  LOG_ERROR("CH347 open error");
1819  return retval;
1820  }
1821 
1822  LOG_DEBUG_IO("CH347 open success");
1823 
1824  // CH347 JTAG init
1825  ch347.tck_pin = TCK_L;
1826  ch347.tms_pin = TMS_H;
1827  ch347.tdi_pin = TDI_L;
1828  ch347.trst_pin = TRST_H;
1831 
1832  ch347.pack_size = UNSET;
1833 
1835 
1836  if (!swd_mode) {
1838  } else {
1839  retval = ch347_init_pack_size();
1840  if (retval != ERROR_OK)
1841  return retval;
1842 
1843  retval = ch347_swd_init_cmd(1);
1844  }
1845  return retval;
1846 }
1847 
1853 static int ch347_swd_init(void)
1854 {
1855  LOG_INFO("CH347 SWD mode enabled");
1856  swd_mode = true;
1857  memset(&ch347_swd_context, 0, sizeof(ch347_swd_context));
1858 
1861 
1863  // 0XE8 + 2byte len + N byte cmds
1865  // 0XE8 + 2byte len + N byte ack + data
1867  struct ch347_swd_io *pswd_io = ch347_swd_context.ch347_cmd_buf;
1868  for (int i = 0; i < CH347_MAX_CMD_BUF; i++, pswd_io++) {
1869  INIT_LIST_HEAD(&pswd_io->list_entry);
1871  }
1872  return ERROR_OK;
1873 }
1874 
1876 {
1877  struct ch347_swd_io *pswd_io;
1879  return NULL;
1880 
1882  struct ch347_swd_io, list_entry);
1883  list_del_init(&pswd_io->list_entry);
1884  pswd_io->cmd = 0;
1885  pswd_io->usb_cmd = CH347_CMD_SWD_SEQ_W;
1886  pswd_io->dst = NULL;
1887  return pswd_io;
1888 }
1889 
1890 static int ch347_swd_queue_flush(void)
1891 {
1896  if (retval != ERROR_OK) {
1898  LOG_DEBUG("CH347WriteData error");
1899  return retval;
1900  }
1901 
1905  if (retval != ERROR_OK) {
1907  LOG_DEBUG("CH347ReadData error");
1908  return retval;
1909  }
1910 
1913  LOG_ERROR("write/read failed %d %d",
1916  retval = ERROR_FAIL;
1917  }
1918 
1919  return retval;
1920 }
1921 
1922 static void ch347_write_swd_reg(uint8_t cmd, const uint32_t out)
1923 {
1925  // 8bit + 32bit +1bit
1932  // 0xA0 + 1 byte(3bit ACK)
1933  ch347_swd_context.need_recv_len += (1 + 1);
1935 }
1936 
1937 static void ch347_write_spec_seq(const uint8_t *out, uint8_t out_len)
1938 {
1943  for (uint8_t i = 0; i < DIV_ROUND_UP(out_len, 8); i++)
1944  ch347_swd_context.send_buf[ch347_swd_context.send_len++] = out ? out[i] : 0x00;
1945  ch347_swd_context.need_recv_len += 1; // 0xA1
1946  ch347_swd_context.total_swd_clk += out_len;
1947 }
1948 
1949 static void ch347_read_swd_reg(uint8_t cmd)
1950 {
1955  // 0xA2 + 1 byte(3bit ACK) + 4 byte(data) + 1 byte(1bit parity+1bit trn)
1956  ch347_swd_context.need_recv_len += 1 + 1 + 4 + 1;
1958 }
1959 
1960 static int ch347_swd_switch_out(enum swd_special_seq seq, const uint8_t *out, unsigned int out_len)
1961 {
1962  if ((ch347_swd_context.send_len + (1 + 2 + DIV_ROUND_UP(out_len, 8))) > CH347_MAX_SEND_BUF)
1963  return ERROR_FAIL;
1965  return ERROR_FAIL;
1966 
1967  struct ch347_swd_io *pswd_io = ch347_get_one_swd_io();
1968  if (pswd_io) {
1969  ch347_write_spec_seq(out, out_len);
1971  return ERROR_OK;
1972  } else {
1973  return ERROR_FAIL;
1974  }
1975 }
1976 
1977 // check read/write REG can fill in remaining buff
1978 static bool ch347_chk_buf_size(uint8_t cmd, uint32_t ap_delay_clk)
1979 {
1980  bool flush = false;
1981  int send_len = ch347_swd_context.send_len;
1982  int recv_len = ch347_swd_context.need_recv_len;
1983  int len;
1984  do {
1985  if (cmd & SWD_CMD_RNW) {
1986  len = 1 + 1 + 1 + 1; // 0xA2 + len + rev + cmd
1987  if (send_len + len > CH347_MAX_SEND_BUF)
1988  break;
1989  send_len += len;
1990  len = 1 + 1 + 4 + 1;
1991  /* 0xA2 + 1byte(3bit ack) + 4byte(data) +
1992  1byte(1bit parity+1bit trn) */
1993  if (recv_len + len > CH347_MAX_RECV_BUF)
1994  break;
1995  recv_len += len;
1996  } else { // write reg
1997  len = 1 + 1 + 1 + 1 + 4 + 1;
1998  // 0xA0 + len + rev + cmd +data + parity
1999  if (send_len + len > CH347_MAX_SEND_BUF)
2000  break;
2001  send_len += len;
2002  len = 1 + 1; // 0xA0 + 1byte(3bit ack)
2003  if (recv_len + len > CH347_MAX_RECV_BUF)
2004  break;
2005  recv_len += len;
2006  }
2007  if (cmd & SWD_CMD_APNDP) {
2008  len = 1 + 1 + 1 + DIV_ROUND_UP(ap_delay_clk, 8);
2009  // 0xA1 + Len + rev + n byte(delay)
2010  if (send_len + len > CH347_MAX_SEND_BUF)
2011  break;
2012  len = 1; // 0xA1
2013  if ((recv_len + len) > CH347_MAX_RECV_BUF)
2014  break;
2015  }
2016  // swd packet requests
2017  flush = true;
2018  } while (false);
2019 
2020  return flush;
2021 }
2022 
2023 static int ch347_swd_run_queue_inner(void);
2024 
2025 static int ch347_swd_send_idle(uint32_t ap_delay_clk)
2026 {
2027  bool run_q = false;
2028  struct ch347_swd_io *pswd_io = NULL;
2029  unsigned int max_processing_clk = ch347_swd_context.clk_divisor
2032  bool more_q_runs =
2033  1 + 1 + 1 + DIV_ROUND_UP(ap_delay_clk, 8) > CH347_MAX_SEND_BUF
2034  && ap_delay_clk > max_processing_clk;
2035 
2037  unsigned int expected_total_clk = ch347_swd_context.total_swd_clk + ap_delay_clk;
2038  unsigned int expected_send_len = ch347_swd_context.send_len
2039  + 1 + 1 + 1 + DIV_ROUND_UP(ap_delay_clk, 8);
2040  // 0xA1 + Len + rev + n byte(delay)
2041  unsigned int expected_recv_len = ch347_swd_context.need_recv_len + 1;
2042  // 0xA1
2043  unsigned int expected_time = ch347_swd_context.clk_divisor
2044  ? expected_total_clk * ch347_swd_context.clk_divisor
2045  : expected_total_clk / 5;
2046  if (expected_time > CH347_MAX_PROCESSING_US
2047  || expected_send_len > CH347_MAX_SEND_BUF
2048  || expected_recv_len > CH347_MAX_RECV_BUF) {
2049  int send_room = CH347_MAX_SEND_BUF - ch347_swd_context.send_len - 1 - 1 - 1;
2050  if (more_q_runs
2051  && send_room > 0
2052  && expected_recv_len <= CH347_MAX_RECV_BUF
2053  && ch347_swd_context.total_swd_clk < max_processing_clk) {
2054  pswd_io = ch347_get_one_swd_io();
2055  if (pswd_io) {
2056  // fill the rest of queue/time by part of delay
2057  unsigned int this_delay_clk = MIN(ap_delay_clk, 255);
2058  if ((unsigned int)send_room * 8 < this_delay_clk)
2059  this_delay_clk = send_room * 8;
2060  if (max_processing_clk - ch347_swd_context.total_swd_clk < this_delay_clk)
2061  this_delay_clk = max_processing_clk - ch347_swd_context.total_swd_clk;
2062  LOG_DEBUG_IO("partial delay %u clk", this_delay_clk);
2063  ch347_write_spec_seq(NULL, this_delay_clk);
2065  ap_delay_clk -= this_delay_clk;
2066  }
2067  }
2068  run_q = true;
2069  }
2070  }
2071 
2072  do {
2073  if (!run_q)
2074  pswd_io = ch347_get_one_swd_io();
2075 
2076  if (!pswd_io) {
2077  int retval = ch347_swd_run_queue_inner();
2078  if (retval != ERROR_OK)
2079  return retval;
2080 
2081  pswd_io = ch347_get_one_swd_io();
2082  if (!pswd_io) {
2083  LOG_ERROR("ch347 SWD queue not empty after ch347_swd_run_queue");
2085  return ERROR_FAIL;
2086  }
2087  }
2088 
2089  unsigned int send_room = CH347_MAX_SEND_BUF - 1 - 1 - 1;
2090  unsigned int this_delay_clk = MIN(ap_delay_clk, 255);
2091  if (send_room * 8 < this_delay_clk)
2092  this_delay_clk = send_room * 8;
2093  if (max_processing_clk < this_delay_clk)
2094  this_delay_clk = max_processing_clk;
2095  LOG_DEBUG_IO("delay %u clk", this_delay_clk);
2096  ch347_write_spec_seq(NULL, this_delay_clk);
2098  ap_delay_clk -= this_delay_clk;
2099  run_q = true;
2100  pswd_io = NULL;
2101  } while (ap_delay_clk);
2102  return ERROR_OK;
2103 }
2104 
2106 {
2107  LOG_DEBUG_IO("Executing %u queued transactions", ch347_swd_context.sent_cmd_count);
2109  LOG_DEBUG_IO("Skipping due to previous errors: %d", ch347_swd_context.queued_retval);
2110  goto skip;
2111  }
2112 
2113  int retval = ch347_swd_queue_flush();
2114  if (retval != ERROR_OK)
2115  return retval;
2116 
2118  LOG_ERROR("CH347 usb write/read failed - queued_retval");
2119  goto skip;
2120  }
2121  uint8_t *recv_buf = ch347_swd_context.recv_buf;
2122  int recv_len = 0;
2123  if (recv_buf[recv_len++] != CH347_CMD_SWD) { // 0XE8
2125  LOG_ERROR("CH347 usb write/read failed - not CH347_CMD_SWD");
2126  goto skip;
2127  }
2128 
2129  int cmds_len = le_to_h_u16(&recv_buf[recv_len]);
2130  recv_len += 2; // cmds_len
2131  if ((cmds_len + CH347_CMD_HEADER) > ch347_swd_context.recv_len) {
2133  LOG_ERROR("CH347 usb write/read failed - too long");
2134  goto skip;
2135  }
2136 
2137  struct list_head *tmp;
2138  struct list_head *pos;
2139  struct ch347_swd_io *pswd_io;
2140 
2142  pswd_io = list_entry(pos, struct ch347_swd_io, list_entry);
2143  if (pswd_io->usb_cmd == CH347_CMD_SWD_SEQ_W) {
2144  if (recv_buf[recv_len++] != CH347_CMD_SWD_SEQ_W) {
2146  LOG_ERROR("CH347 usb write/read failed - not CH347_CMD_SWD_SEQ_W");
2147  goto skip;
2148  }
2149  } else { // read/write Reg
2150  uint32_t ack;
2151  bool check_ack;
2152  // read Reg
2153  if (recv_buf[recv_len] == CH347_CMD_SWD_REG_R) {
2154  recv_len++;
2155  ack = buf_get_u32(&recv_buf[recv_len++], 0, 3);
2156  /* Devices do not reply to DP_TARGETSEL write
2157  cmd, ignore received ack */
2158  check_ack = swd_cmd_returns_ack(pswd_io->cmd);
2159  if (pswd_io->cmd & SWD_CMD_RNW) {
2160  uint32_t data = buf_get_u32(&recv_buf[recv_len], 0, 32);
2161 
2162  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK)
2164  "%s%s %s read reg %X = %08" PRIx32,
2165  check_ack ? "" : "ack ignored ",
2166  ack == SWD_ACK_OK ? "OK" :
2167  ack == SWD_ACK_WAIT ? "WAIT" :
2168  ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
2169  pswd_io->cmd & SWD_CMD_APNDP ? "AP" : "DP",
2170  (pswd_io->cmd & SWD_CMD_A32) >> 1,
2171  data);
2172 
2173  if (ack != SWD_ACK_OK && check_ack) {
2175  goto skip;
2176  }
2177 
2178  uint32_t parity = buf_get_u32(&recv_buf[recv_len], 32, 1);
2179  if (parity != (uint32_t)parity_u32(data)) {
2180  LOG_ERROR("SWD Read data parity mismatch");
2182  goto skip;
2183  }
2184 
2185  if (pswd_io->dst)
2186  *pswd_io->dst = data;
2187  } else {
2189  LOG_ERROR("CH347 usb write/read failed - not SWD_CMD_RNW");
2190  goto skip;
2191  }
2192  recv_len += 5;
2193  } else if (recv_buf[recv_len] == CH347_CMD_SWD_REG_W) {
2194  recv_len++;
2195  ack = buf_get_u32(&recv_buf[recv_len++], 0, 3);
2196  /* Devices do not reply to DP_TARGETSEL write
2197  cmd, ignore received ack */
2198  check_ack = swd_cmd_returns_ack(pswd_io->cmd);
2199 
2200  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK)
2202  "%s%s %s write reg %X = %08" PRIx32,
2203  check_ack ? "" : "ack ignored ",
2204  ack == SWD_ACK_OK ? "OK" :
2205  ack == SWD_ACK_WAIT ? "WAIT" :
2206  ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
2207  pswd_io->cmd & SWD_CMD_APNDP ? "AP" : "DP",
2208  (pswd_io->cmd & SWD_CMD_A32) >> 1,
2209  pswd_io->value);
2210 
2211  if (ack != SWD_ACK_OK && check_ack) {
2213  goto skip;
2214  }
2215  } else {
2217  LOG_ERROR("CH347 usb write/read failed recv_len = %d", recv_len);
2218  goto skip;
2219  }
2220  }
2221  list_del_init(&pswd_io->list_entry);
2222  list_add_tail(&pswd_io->list_entry,
2224  }
2225 
2226 skip:
2229  pswd_io = list_entry(pos, struct ch347_swd_io, list_entry);
2230  list_del_init(&pswd_io->list_entry);
2231  list_add_tail(&pswd_io->list_entry,
2233  }
2234  }
2235 
2236  // 0xE8 + 2byte len
2238  // 0xE8 + 2byte len
2245  return retval;
2246 }
2247 
2248 static int ch347_swd_run_queue(void)
2249 {
2250  /* A transaction must be followed by another transaction or at least 8
2251  idle cycles to ensure that data is clocked through the AP. */
2252  int retval = ch347_swd_send_idle(8);
2253  if (retval != ERROR_OK)
2254  return retval;
2255 
2256  return ch347_swd_run_queue_inner();
2257 }
2258 
2259 static int ch347_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2260 {
2261  int retval = ERROR_OK;
2262  bool run_q = false;
2264  unsigned int expected_total_clk = ch347_swd_context.total_swd_clk
2265  + 46 // SWD transaction
2266  + ap_delay_clk
2267  + 8; // 8 idle cycles at the end of queue
2268  unsigned int expected_time = ch347_swd_context.clk_divisor
2269  ? expected_total_clk * ch347_swd_context.clk_divisor
2270  : expected_total_clk / 5;
2271  if (expected_time > CH347_MAX_PROCESSING_US) {
2272  LOG_DEBUG_IO("Expected queue run %u cycles, with this cmd %u",
2273  ch347_swd_context.total_swd_clk, expected_total_clk);
2274  run_q = true;
2275  } else if (!ch347_chk_buf_size(cmd, ap_delay_clk)) {
2276  run_q = true;
2277  }
2278  }
2279 
2280  struct ch347_swd_io *pswd_io = NULL;
2281  if (!run_q)
2282  pswd_io = ch347_get_one_swd_io();
2283 
2284  if (!pswd_io) {
2285  retval = ch347_swd_run_queue_inner();
2286  if (retval != ERROR_OK)
2287  return retval;
2288 
2289  pswd_io = ch347_get_one_swd_io();
2290  if (!pswd_io) {
2291  LOG_ERROR("ch347 SWD queue not empty after ch347_swd_run_queue");
2293  return ERROR_FAIL;
2294  }
2295  }
2296 
2297  pswd_io->cmd = cmd | SWD_CMD_START | SWD_CMD_PARK;
2298 
2299  if (pswd_io->cmd & SWD_CMD_RNW) {
2300  pswd_io->usb_cmd = CH347_CMD_SWD_REG_R;
2301  pswd_io->dst = dst;
2302  ch347_read_swd_reg(pswd_io->cmd);
2303  } else {
2304  pswd_io->usb_cmd = CH347_CMD_SWD_REG_W;
2305  pswd_io->value = data;
2306  ch347_write_swd_reg(pswd_io->cmd, data);
2307  }
2308 
2311 
2312  // Insert idle cycles after AP accesses to avoid WAIT
2313  if (ap_delay_clk)
2314  retval = ch347_swd_send_idle(ap_delay_clk);
2315 
2316  return retval;
2317 }
2318 
2320 {
2321  switch (seq) {
2322  case LINE_RESET:
2323  LOG_DEBUG("SWD line reset");
2325  case JTAG_TO_SWD:
2326  LOG_DEBUG("JTAG-to-SWD");
2328  case JTAG_TO_DORMANT:
2329  LOG_DEBUG("JTAG-to-DORMANT");
2331  case SWD_TO_JTAG:
2332  LOG_DEBUG("SWD-to-JTAG");
2334  case SWD_TO_DORMANT:
2335  LOG_DEBUG("SWD-to-DORMANT");
2337  case DORMANT_TO_SWD:
2338  LOG_DEBUG("DORMANT-to-SWD");
2340  case DORMANT_TO_JTAG:
2341  LOG_DEBUG("DORMANT-to-JTAG");
2343  default:
2344  LOG_ERROR("Sequence %d not supported", seq);
2345  return ERROR_FAIL;
2346  }
2347 }
2348 
2349 static void ch347_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
2350 {
2351  assert(cmd & SWD_CMD_RNW);
2352  int retval = ch347_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
2353  if (retval != ERROR_OK)
2355 }
2356 
2357 static void ch347_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
2358 {
2359  assert(!(cmd & SWD_CMD_RNW));
2360  int retval = ch347_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
2361  if (retval != ERROR_OK)
2363 }
2364 
2365 static const struct swd_driver ch347_swd = {
2366  .init = ch347_swd_init,
2367  .switch_seq = ch347_swd_switch_seq,
2368  .read_reg = ch347_swd_read_reg,
2369  .write_reg = ch347_swd_write_reg,
2370  .run = ch347_swd_run_queue,
2371 };
2372 
2373 static struct jtag_interface ch347_interface = {
2375  .execute_queue = ch347_execute_queue,
2376 };
2377 
2379  .name = "ch347",
2380  .transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
2381  .transport_preferred_id = TRANSPORT_JTAG,
2382  .commands = ch347_command_handlers,
2383 
2384  .init = ch347_init,
2385  .quit = ch347_quit,
2386  .reset = ch347_reset,
2387  .speed = ch347_speed_set,
2388  .khz = ch347_speed_get_index,
2389  .speed_div = ch347_speed_get,
2390 
2391  .jtag_ops = &ch347_interface,
2392  .swd_ops = &ch347_swd,
2393 };
const struct adapter_gpio_config * adapter_gpio_get_config(void)
Retrieves gpio configuration set with command "adapter gpio <signal_name>".
Definition: adapter.c:1339
const uint16_t * adapter_usb_get_pids(void)
Definition: adapter.c:340
const char * adapter_usb_get_product_name(void)
Definition: adapter.c:350
const uint16_t * adapter_usb_get_vids(void)
Definition: adapter.c:335
struct adapter_gpio_config gpios[ADAPTER_GPIO_IDX_NUM]
Definition: adapter.c:53
@ 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:1001
static int ch347_scratchpad_add_move_state(enum tap_state state, int skip)
Toggle the tap state to the target state.
Definition: ch347.c:1072
static int ch347_cmd_from_scratchpad(void)
copy the scratchpad content into a new command in the command queue
Definition: ch347.c:485
struct adapter_driver ch347_adapter_driver
Definition: ch347.c:2378
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:926
#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:1676
#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:1960
#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:409
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:596
#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:2349
static int ch347_quit(void)
CH347 Device Release Function.
Definition: ch347.c:1547
#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:1875
#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:1038
#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:445
#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:1798
#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:1779
#define DEFAULT_VENDOR_ID
Definition: ch347.c:139
static void ch347_write_swd_reg(uint8_t cmd, const uint32_t out)
Definition: ch347.c:1922
#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:790
#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:265
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:2248
#define USEABLE_GPIOS
Definition: ch347.c:79
static const struct swd_driver ch347_swd
Definition: ch347.c:2365
#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:1728
#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:1617
static int ch347_open_device(void)
opens the CH347 device via libusb driver
Definition: ch347.c:1418
static int ch347_scratchpad_add_stableclocks(int count)
Function adds a certain amount of TCK pulses without changing the TMS pin.
Definition: ch347.c:978
#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:892
static int ch347_execute_queue(struct jtag_command *cmd_queue)
Executes the command queue.
Definition: ch347.c:1363
static int ch347_swd_send_idle(uint32_t ap_delay_clk)
Definition: ch347.c:2025
#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:658
static void log_buf_dump(const uint8_t *data, unsigned int size, bool recv)
Definition: ch347.c:270
static int ch347_activity_led_set(int led_state)
Turn the activity LED on or off.
Definition: ch347.c:1297
#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:1261
static int ch347_swd_queue_flush(void)
Definition: ch347.c:1890
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:911
static void ch347_write_spec_seq(const uint8_t *out, uint8_t out_len)
Definition: ch347.c:1937
static struct libusb_device_handle * ch347_handle
Definition: ch347.c:261
#define UCMDPKT_DATA_MAX_BYTES_USBHS
Definition: ch347.c:92
static uint16_t default_ch347_pids[]
Definition: ch347.c:256
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:832
static int ch347_scratchpad_add_run_test(int cycles, enum tap_state state)
Toggle the Tap state to run test/idle.
Definition: ch347.c:1193
#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:1702
#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:2259
static int ch347_swd_switch_seq(enum swd_special_seq seq)
Definition: ch347.c:2319
static struct jtag_interface ch347_interface
Definition: ch347.c:2373
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:1978
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:1098
#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:373
static struct ch347_info ch347
Definition: ch347.c:260
#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:523
#define CH347_CMD_SWD_REG_R
Definition: ch347.c:118
static uint8_t ch347_activity_led_gpio_pin
Definition: ch347.c:258
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:1015
static int ch347_swd_init(void)
Initialization for the swd mode.
Definition: ch347.c:1853
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:855
#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:1350
static void ch347_read_swd_reg(uint8_t cmd)
Definition: ch347.c:1949
#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:1605
static int ch347_init(void)
CH347 Initialization function.
Definition: ch347.c:1813
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:1568
#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:960
static int ch347_swd_run_queue_inner(void)
Definition: ch347.c:2105
#define CH347_CMD_SWD_SEQ_W
Definition: ch347.c:117
#define CH347_CMD_SWD_REG_W
Definition: ch347.c:116
static void ch347_scratchpad_check_full(void)
checks if the scratchpad is full.
Definition: ch347.c:874
static void ch347_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: ch347.c:2357
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:1629
#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:1657
#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:1314
static int ch347_scratchpad_add_scan(struct scan_command *cmd)
Switch to SHIFT-DR or SHIFT-IR status for scanning.
Definition: ch347.c:1216
#define MAX_BITS_PER_BIT_OP
Definition: ch347.c:99
#define LED_ON
Definition: ch347.c:73
static bool ch347_activity_led_active_high
Definition: ch347.c:259
#define CH347_CMD_JTAG_DATA_SHIFT_RD
Definition: ch347.c:109
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ 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:1070
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:132
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_LEVEL_IS(FOO)
Definition: log.h:112
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_DEBUG
Definition: log.h:55
@ LOG_LVL_DEBUG_IO
Definition: log.h:56
const unsigned char bit_offset
Definition: mspm0.c:628
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:1467
#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: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