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 tested with the CH347T chip in mode 3 and *
30  * with the CH347F chip. *
31  * *
32  * The datasheet for the wch-ic.com's CH347 part is here: *
33  * https://www.wch-ic.com/downloads/CH347DS1_PDF.html *
34  * *
35  ***************************************************************************/
36 
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40 
41 #if IS_CYGWIN == 1
42 #include "windows.h"
43 #undef LOG_ERROR
44 #endif
45 
46 // project specific includes
47 #include <jtag/interface.h>
48 #include <jtag/commands.h>
49 #include <jtag/swd.h>
50 #include <jtag/adapter.h>
51 #include <helper/time_support.h>
52 #include <helper/replacements.h>
53 #include <helper/list.h>
54 #include <helper/binarybuffer.h>
55 #include <helper/align.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 SRST_H BIT(6)
74 #define SRST_L 0
75 
76 #define GPIO_CNT 8 // the CH347 has 8 GPIO's
77 // SRST pin can be configured as CH347F_SRST_FAKE_GPIO
78 #define GPIO_CNT_EXT (GPIO_CNT + 1)
79 #define CH347F_SRST_FAKE_GPIO GPIO_CNT
80 #define CH347T_TRST_GPIO 5
81 #define CH347F_TRST_GPIO 3
82 
83 /* For GPIO command: always set bits 7 and 6 for GPIO enable
84  bits 5 and 4 for pin direction output bit 3 is the data bit */
85 #define GPIO_SET_L (BIT(4) | BIT(5) | BIT(6) | BIT(7)) // value for setting a GPIO to low
86 #define GPIO_SET_H (BIT(3) | BIT(4) | BIT(5) | BIT(6) | BIT(7)) // value for setting a GPIO to high
87 // enable the pin and set the direction to input, so the pin gets released
88 #define GPIO_SET_INPUT (BIT(6) | BIT(7))
89 
90 #define VENDOR_VERSION 0x5F // for getting the chip version
91 
92 // maybe the hardware of the CH347 chip can capture only this amount of TDO bits
93 #define HW_TDO_BUF_SIZE 4096
94 // Don't send more than this amount of bytes via libusb in one packet. Also that is the limit for LARGER_PACK mode.
95 #define LARGER_PACK_MAX_SIZE 51200
96 // The data length contained in each command packet during USB high-speed operation
97 // REVISIT: could be USBC_PACKET_USBHS - CH347_CMD_HEADER ?
98 #define UCMDPKT_DATA_MAX_BYTES_USBHS 507
99 #define USBC_PACKET_USBHS 512 // Maximum data length per packet at USB high speed
100 #define CH347_CMD_HEADER 3 // Protocol header length (1 byte command type + 2 bytes data length)
101 #define CH347_CMD_INIT_READ_LEN 1 // JTAG_INIT/SWD_INIT/PIN_SET read one data byte
102 // if we send 9 in the init command we get the STANDARD mode or LARGER_PACK mode flag
103 #define CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE 9
104 // No more bits are allowed per CH347_CMD_JTAG_BIT_OP command; this should be dividable by 8
105 #define MAX_BITS_PER_BIT_OP 248
106 
107 /* Protocol transmission format: CMD (1 byte) + Length (2 bytes) + Data
108  Parameter acquisition, used to obtain firmware version, JTAG interface related parameters, etc */
109 #define CH347_CMD_INFO_RD 0xCA
110 #define CH347_CMD_GPIO 0xCC // GPIO Command
111 #define CH347_CMD_JTAG_INIT 0xD0 // JTAG Interface initialization command
112 #define CH347_CMD_JTAG_BIT_OP 0xD1 // JTAG interface pin bit control command
113 #define CH347_CMD_JTAG_BIT_OP_RD 0xD2 // JTAG interface pin bit control and read commands
114 #define CH347_CMD_JTAG_DATA_SHIFT 0xD3 // JTAG interface data shift command
115 #define CH347_CMD_JTAG_DATA_SHIFT_RD 0xD4 // JTAG interface data shift and read command
116 #define CH347_CMD_PIN_SET 0xE2 // set pin functions (CH347F)
117 
118 // for SWD
119 #define CH347_CMD_SWD_INIT 0xE5 // SWD Interface Initialization Command
120 #define CH347_CMD_SWD 0xE8 // SWD Command group header
121 #define CH347_CMD_SWD_REG_W 0xA0 // SWD Interface write reg
122 #define CH347_CMD_SWD_SEQ_W 0xA1 // SWD Interface write spec seq
123 #define CH347_CMD_SWD_REG_R 0xA2 // SWD Interface read reg
124 #define CH347_MAX_PROCESSING_US 7000 // max time in us for packet processing
125  // USB hosts disconnect the adapter if SWD processing takes more!
126 #define CH347_MAX_SEND_BUF UCMDPKT_DATA_MAX_BYTES_USBHS
127 #define CH347_MAX_RECV_BUF (USBC_PACKET_USBHS - CH347_CMD_HEADER)
128 #define CH347_MAX_CMD_BUF 128
129 #define CH347_SWD_CLOCK_MAX 5000
130 #define CH347_SWD_CLOCK_BASE 1000
131 // limited by the longest sequence processing time
132 #define CH347_SWD_CLOCK_MAX_DIVISOR (CH347_MAX_PROCESSING_US / swd_seq_dormant_to_swd_len)
133 
134 #define CH347_EPOUT 0x06u // the usb endpoint number for writing
135 #define CH347_EPIN 0x86u // the usb endpoint number for reading
136 #define CH347T_MPHSI_INTERFACE 2 // the CH347T JTAG interface is number 2
137 #define CH347F_MPHSI_INTERFACE 4 // the CH347F JTAG interface is number 4
138 #define USB_WRITE_TIMEOUT 500 // write timeout in milliseconds
139 #define USB_READ_TIMEOUT 500 // read timeout in milliseconds
140 // BCD version for devices that can use bytewise mode below this version only bitwise mode can be used
141 #define BYTEWISE_MODE_VERSION 0x241
142 /* if the configuration is not setting the vendor id / product id we search for vendor id 1a86
143  and product id's 0x55dd (CH347T chip in mode 3), 0x55de (CH347F chip) and 0x55e7 (other chip) */
144 #define DEFAULT_VENDOR_ID 0x1a86
145 #define DEFAULT_CH347T_PRODUCT_ID 0x55dd
146 #define DEFAULT_CH347F_PRODUCT_ID 0x55de
147 #define DEFAULT_OTHER_PRODUCT_ID 0x55e7
148 
149 /* There are 3 different CH347 variants. The datasheet mentions the CH347T chip which has the product id 0x55dd
150  as default when it's configured to mode 3. The CH347F chip is also mentioned in the datasheet and has the
151  default product id 0x55de. The 3rd variant is not mentioned in a datasheet but was found here
152  https://github.com/WCHSoftGroup/ch347
153  The code from WCHSoftGroup is also searching for this product id */
155  CH347T = 0, // The CH347T chip
156  CH347F = 1, // The CH347F chip
157  OTHER_PRODUCT_ID = 2, // other product id not mentioned in the datasheet
158 };
159 
160 /* STANDARD_PACK means that we can send only one USBC_PACKET_USBHS-sized USB packet
161  and then read data back. LARGER_PACK means, we can send packets as large as
162  LARGER_PACK_MAX_SIZE. libusb splits there large packets into smaller USB packets and
163  transmit the data. Then we read back the data in a bigger packet. */
164 enum pack_size {
165  UNSET = -1,
168 };
169 
170 // for STANDARD_PACK mode: these are the 6 possible speeds; values in kHz
171 static const int ch347_standard_pack_clock_speeds[] = {
172  1875, // 1.875 MHz (60000 : 32)
173  3750, // 3.75 MHz (60000 : 16)
174  7500, // 7.5 MHz (60000 : 8)
175  15000, // 15 MHz (60000 : 4)
176  30000, // 30 MHz (60000 : 2)
177  60000 // 60 MHz
178 };
179 
180 // for LARGER_PACK mode: these are the 8 possible speeds; values in kHz
181 static const int ch347_larger_pack_clock_speeds[] = {
182  469, // 468.75 kHz (60000 : 128)
183  938, // 937.5 kHz (60000 : 64)
184  1875, // 1.875 MHz (60000 : 32)
185  3750, // 3.75 MHz (60000 : 16)
186  7500, // 7.5 MHz (60000 : 8)
187  15000, // 15 MHz (60000 : 4)
188  30000, // 30 MHz (60000 : 2)
189  60000 // 60 MHz
190 };
191 
192 struct ch347_cmd {
193  uint8_t type; // the command type
194  uint8_t *write_data; // data bytes for write
195  uint16_t write_data_len; // count of data bytes in the write_data buffer
196  uint16_t read_len; // if >0 a read is needed after this command
197  uint16_t tdo_bit_count; // how many TDO bits are needed to shift in by this read
198  struct list_head queue; // for handling a queue (list)
199 };
200 
201 struct ch347_scan {
202  struct scan_field *fields; // array of scan_field's for data from the device
203  int fields_len; // scan_fields array length
204  struct list_head queue; // for handling a queue (list)
205 };
206 
207 struct ch347_info {
208  // Record the CH347 pin status
209  int tms_pin;
210  int tdi_pin;
211  int tck_pin;
212  int trst_pin;
213  int srst_pin;
214 
215  enum ch347_variant chip_variant; // ch347_variant for explanation
216  // if true then we can't us the bytewise commands due to a bug of the chip; depends on BYTEWISE_MODE_VERSION
218  enum pack_size pack_size; // see: pack_size for explanation
219  int max_len; // in STANDARD_PACK or bitwise mode we can send only one USBC_PACKET_USBHS sized package
223  bool led_is_on;
226 
227  // a "scratchpad" where we record all bytes for one command
228  uint8_t scratchpad_cmd_type; // command type
229  uint8_t scratchpad[UCMDPKT_DATA_MAX_BYTES_USBHS]; // scratchpad buffer
230  int scratchpad_idx; // current index in scratchpad
231 
232  // after a command is complete it will be stored for later processing
233  struct list_head cmd_queue;
234  // all data input scan fields are queued here
235  struct list_head scan_queue;
236  // read buffer for the single commands like CH347_CMD_JTAG_INIT
239  // GPIO status returned by CH347_CMD_GPIO
241 };
242 
243 struct ch347_swd_io {
244  uint8_t usb_cmd; // 0xA0, 0xA1, 0xA2
245  uint8_t cmd;
246  uint32_t *dst;
247  uint32_t value;
248  struct list_head list_entry;
249 };
250 
254  int send_len;
255  int recv_len;
258  unsigned int clk_divisor;
259  unsigned int total_swd_clk;
260  struct list_head send_cmd_head;
261  struct list_head free_cmd_head;
263 };
264 
266 static bool swd_mode;
267 
271 
272 /* Set after the CH347F dedicated SRST output is enabled and its pin byte
273  * has been transmitted. JTAG pin bytes carry bit 6 only then. SWD leaves
274  * this clear. */
276 static struct ch347_info ch347;
277 static struct libusb_device_handle *ch347_handle;
278 
279 struct ch347_bit_op {
280  uint8_t bit;
282  int *ptr;
283 };
284 static const struct ch347_bit_op ch347t_gpio_to_bit_op[GPIO_CNT_EXT] = {
285  [0] = { .bit = TCK_H, .jtag_use = true, .swd_use = true, .ptr = &ch347.tck_pin },
286  [1] = { .jtag_use = true },
287  [2] = { .bit = TMS_H, .jtag_use = true, .swd_use = true, .ptr = &ch347.tms_pin },
288  [CH347T_TRST_GPIO] = { .bit = TRST_H, .ptr = &ch347.trst_pin }, // GPIO 5
289 };
290 
291 static const struct ch347_bit_op ch347f_gpio_to_bit_op[GPIO_CNT_EXT] = {
292  [CH347F_TRST_GPIO] = { .bit = TRST_H, .ptr = &ch347.trst_pin }, // GPIO 3
293  [4] = { .bit = TCK_H, .jtag_use = true, .swd_use = true, .ptr = &ch347.tck_pin },
294  [5] = { .jtag_use = true },
295  [6] = { .bit = TDI_H, .jtag_use = true, .ptr = &ch347.tdi_pin },
296  [7] = { .bit = TMS_H, .jtag_use = true, .swd_use = true, .ptr = &ch347.tms_pin },
297  [CH347F_SRST_FAKE_GPIO] = { .bit = SRST_H, .ptr = &ch347.srst_pin},
298 };
299 
300 /* there are "single" commands. These commands can't be chained together and
301  need to be send as single command and need a read after write */
302 static inline bool ch347_is_single_cmd_type(uint8_t type)
303 {
305  || type == CH347_CMD_PIN_SET;
306 }
307 
308 /* The commands with variable data size can be concatenated in the scratchpad
309  * and split if the scratchpad overflows */
310 static inline bool ch347_cmd_has_variable_data_size(uint8_t type)
311 {
314 }
315 
316 static void log_buf_dump(const uint8_t *data, unsigned int size, bool recv)
317 {
318  unsigned int i = 0, j = 0;
319  unsigned int n = size * 3 + 1;
320  char *str = malloc(n);
321  if (!str)
322  return;
323 
324  while (i < size && j < n) {
325  uint8_t cmd = data[i++];
326  hexify(str + j, &cmd, 1, n - j);
327  j += 2;
328  str[j++] = ' ';
329 
330  unsigned int cmd_payload_size = le_to_h_u16(data + i);
331  if (i + 2 <= size && j + 4 < n) {
332  hexify(str + j, data + i, 2, n - j);
333  i += 2;
334  j += 4;
335  str[j++] = ' ';
336  }
337 
338  if (cmd == CH347_CMD_SWD) {
339  // nested SWD commands
340  str[j] = '\0';
341  if (i + cmd_payload_size > size) {
342  LOG_DEBUG_IO("%s - bad size", str);
343  cmd_payload_size = size - i;
344  } else {
345  LOG_DEBUG_IO("%s", str);
346  }
347  j = 0;
348  unsigned int swd_base_i = i;
349 
350  while (i < swd_base_i + cmd_payload_size) {
351  uint8_t swd_cmd = data[i++];
352  hexify(str + j, &swd_cmd, 1, n - j);
353  j += 2;
354  str[j++] = ' ';
355 
356  unsigned int swd_bits = 0;
357  unsigned int swd_payload_size = 0;
358  if (!recv) {
359  if (i + 2 <= size) {
360  swd_bits = le_to_h_u16(data + i);
361  hexify(str + j, data + i, 2, n - j);
362  i += 2;
363  j += 4;
364  str[j++] = ' ';
365  }
366  }
367 
368  switch (swd_cmd) {
369  case CH347_CMD_SWD_REG_W:
370  if (recv)
371  swd_payload_size = 1;
372  else
373  swd_payload_size = DIV_ROUND_UP(swd_bits, 8);
374  break;
375  case CH347_CMD_SWD_SEQ_W:
376  if (!recv)
377  swd_payload_size = DIV_ROUND_UP(swd_bits, 8);
378  break;
379  case CH347_CMD_SWD_REG_R:
380  if (recv)
381  swd_payload_size = 1 + 4 + 1;
382  else
383  swd_payload_size = 1;
384  break;
385  }
386 
387  hexify(str + j, data + i, MIN(swd_payload_size, size - i), n - j);
388  i += swd_payload_size;
389  j += 2 * swd_payload_size;
390  str[j] = '\0';
391  if (i > size)
392  LOG_DEBUG_IO(" %s - bad size", str);
393  else
394  LOG_DEBUG_IO(" %s", str);
395  j = 0;
396  }
397  } else {
398  hexify(str + j, data + i, MIN(cmd_payload_size, size - i), n - j);
399  i += cmd_payload_size;
400  j += 2 * cmd_payload_size;
401  str[j] = '\0';
402  if (i > size)
403  LOG_DEBUG_IO("%s - bad size", str);
404  else
405  LOG_DEBUG_IO("%s", str);
406  j = 0;
407  }
408  }
409  free(str);
410 }
411 
419 static int ch347_write_data(uint8_t *data, int *length)
420 {
421  int write_len = *length;
422  int i = 0;
423  int transferred = 0;
424 
425  while (true) {
426  int retval = jtag_libusb_bulk_write(ch347_handle, CH347_EPOUT, (char *)&data[i],
427  write_len, USB_WRITE_TIMEOUT, &transferred);
428  if (retval != ERROR_OK) {
429  LOG_ERROR("CH347 write fail (size %d)", write_len);
430  log_buf_dump(data, *length, false);
431  *length = 0;
432  return retval;
433  }
434  i += transferred;
435  if (i >= *length)
436  break;
437  write_len = *length - i;
438  }
439 
441  LOG_DEBUG_IO("size=%d, buf=", i);
442  log_buf_dump(data, i, false);
443  }
444 
445  *length = i;
446  return ERROR_OK;
447 }
448 
459 static int ch347_read_data(uint8_t *data, int buf_length, int *length)
460 {
461  int done = 0;
462 
463  do {
464  int transferred = 0;
465  int read_len = buf_length - done;
466  int retval = jtag_libusb_bulk_read(ch347_handle, CH347_EPIN, (char *)&data[done],
467  read_len, USB_READ_TIMEOUT, &transferred);
468  if (retval != ERROR_OK) {
469  LOG_ERROR("CH347 read fail");
470  *length = 0;
471  return retval;
472  }
473 
474  done += transferred;
475  } while (done < *length);
476 
478  LOG_DEBUG_IO("size=%d, buf=", done);
479  log_buf_dump(data, done, true);
480  }
481 
482  *length = done;
483  return ERROR_OK;
484 }
485 
492 static void ch347_cmd_calc_reads(struct ch347_cmd *cmd)
493 {
494  cmd->read_len = 0;
495  cmd->tdo_bit_count = 0;
496 
497  switch (cmd->type) {
498  case CH347_CMD_GPIO:
499  // for GPIO we need to read back the same amount of data that we had send
500  cmd->read_len = cmd->write_data_len;
501  break;
502  case CH347_CMD_SWD:
503  cmd->read_len = ch347_swd_context.need_recv_len;
504  break;
505  case CH347_CMD_JTAG_INIT:
506  case CH347_CMD_SWD_INIT:
507  case CH347_CMD_PIN_SET:
508  // for JTAG_INIT/SWD_INIT/PIN_SET the amount is fixed
509  cmd->read_len = CH347_CMD_INIT_READ_LEN;
510  break;
512  // for bit operations we need to count the TCK high edges
513  for (int i = 0; i < cmd->write_data_len; i++) {
514  if ((cmd->write_data[i] & TCK_H) == TCK_H) {
515  cmd->read_len++;
516  cmd->tdo_bit_count++;
517  }
518  }
519  break;
521  // for byte operations: need to read one byte back for each data byte
522  cmd->read_len = cmd->write_data_len;
523  // we occupy 8 bits per byte in the TDO hardware buffer
524  cmd->tdo_bit_count = cmd->read_len * 8;
525  break;
526  }
527 }
528 
537 {
538  // nothing to do if no bytes are recorded
539  if (!ch347.scratchpad_idx)
540  return ERROR_OK;
541 
542  // malloc for the command and data bytes
543  struct ch347_cmd *cmd = malloc(sizeof(struct ch347_cmd));
544  if (cmd)
545  cmd->write_data = malloc(ch347.scratchpad_idx);
546  if (!cmd || !cmd->write_data) {
547  LOG_ERROR("malloc failed");
548  free(cmd);
549  return ERROR_FAIL;
550  }
551 
552  // copy data, calculate the reads and add to the command queue
553  cmd->type = ch347.scratchpad_cmd_type;
554  cmd->write_data_len = ch347.scratchpad_idx;
555  memcpy(cmd->write_data, ch347.scratchpad, ch347.scratchpad_idx);
557  list_add_tail(&cmd->queue, &ch347.cmd_queue);
558 
559  // cleanup the scratchpad for the next command
561  ch347.scratchpad_idx = 0;
562  return ERROR_OK;
563 }
564 
574 static int ch347_read_scan(uint8_t *decoded_buf, int decoded_buf_len, int raw_read_len)
575 {
576  int read_bf_len = ALIGN_UP(raw_read_len, USBC_PACKET_USBHS);
577  uint8_t *read_buf = malloc(read_bf_len);
578  if (!read_buf) {
579  LOG_ERROR("malloc failed");
580  return ERROR_FAIL;
581  }
582 
583  int read_len = raw_read_len;
584  int retval = ch347_read_data(read_buf, read_bf_len, &read_len);
585  if (retval != ERROR_OK) {
586  free(read_buf);
587  return retval;
588  }
589 
590  int rd_idx = 0;
591  int decoded_buf_idx = 0;
592 
593  while (rd_idx < read_len) {
594  unsigned int type = read_buf[rd_idx++];
595  uint16_t data_len = le_to_h_u16(&read_buf[rd_idx]);
596  rd_idx += 2;
597 
598  switch (type) {
599  case CH347_CMD_JTAG_INIT:
600  case CH347_CMD_SWD_INIT:
601  case CH347_CMD_PIN_SET:
602  if (data_len != CH347_CMD_INIT_READ_LEN) {
603  LOG_ERROR("CH347 unexpected returned data length from init CMD");
604  free(read_buf);
605  return ERROR_FAIL;
606  }
607  memcpy(ch347.init_result_buf, &read_buf[rd_idx], data_len);
608  ch347.init_result_len = data_len;
609  rd_idx += data_len;
610  break;
611  case CH347_CMD_GPIO:
612  if (data_len != GPIO_CNT) {
613  LOG_ERROR("CH347 unexpected returned data length from CMD GPIO");
614  free(read_buf);
615  return ERROR_FAIL;
616  }
617  memcpy(ch347.gpio_status_buf, &read_buf[rd_idx], data_len);
618  rd_idx += data_len;
619  break;
621  // for bytewise commands: copy the data bytes
622  if (!decoded_buf || decoded_buf_idx + data_len > decoded_buf_len) {
623  LOG_ERROR("CH347 no decoded_buf or too small");
624  free(read_buf);
625  return ERROR_FAIL;
626  }
627  memcpy(&decoded_buf[decoded_buf_idx], &read_buf[rd_idx], data_len);
628  decoded_buf_idx += data_len;
629  rd_idx += data_len;
630  break;
632  // for CH347_CMD_JTAG_BIT_OP_RD we need to copy bit by bit
633  if (!decoded_buf || decoded_buf_idx + DIV_ROUND_UP(data_len, 8) > decoded_buf_len) {
634  LOG_ERROR("CH347 no decoded_buf or too small");
635  free(read_buf);
636  return ERROR_FAIL;
637  }
638  for (int i = 0; i < data_len; i++) {
639  if (read_buf[rd_idx + i] & BIT(0))
640  decoded_buf[decoded_buf_idx + i / 8] |= BIT(i % 8);
641  else
642  decoded_buf[decoded_buf_idx + i / 8] &= ~(BIT(i % 8));
643  }
644  decoded_buf_idx += DIV_ROUND_UP(data_len, 8);
645  rd_idx += data_len;
646  break;
647  case CH347_CMD_SWD:
648  if (data_len > sizeof(ch347_swd_context.recv_buf)) {
649  LOG_ERROR("CH347 SWD recv buf too small");
650  free(read_buf);
651  return ERROR_FAIL;
652  }
653  memcpy(ch347_swd_context.recv_buf, &read_buf[rd_idx], data_len);
654  ch347_swd_context.recv_len = data_len;
655  rd_idx += data_len;
656  break;
657  default:
658  LOG_ERROR("CH347 read command fail");
659  free(read_buf);
660  return ERROR_FAIL;
661  }
662  }
663 
664  free(read_buf);
665  return ERROR_OK;
666 }
667 
675 static int ch347_scan_data_to_fields(uint8_t *decoded_buf, int decoded_buf_len)
676 {
677  int byte_offset = 0;
678  struct ch347_scan *scan;
679  struct ch347_scan *tmp;
680  int bit_offset = 0;
682  for (int i = 0; i < scan->fields_len; i++) {
683  int num_bits = scan->fields[i].num_bits;
684  LOG_DEBUG_IO("fields[%d].in_value[%d], read from bit offset: %d", i, num_bits, bit_offset);
685  // only if we need the value
686  if (scan->fields[i].in_value) {
687  uint8_t *capture_buf = malloc(DIV_ROUND_UP(num_bits, 8));
688  if (!capture_buf) {
689  LOG_ERROR("malloc failed");
690  return ERROR_FAIL;
691  }
692  uint8_t *captured = buf_set_buf(decoded_buf, bit_offset, capture_buf, 0, num_bits);
693 
695  char *str = buf_to_hex_str(captured, num_bits);
696  LOG_DEBUG_IO("size=%d, buf=[%s]", num_bits, str);
697  free(str);
698  }
699 
700  buf_cpy(captured, scan->fields[i].in_value, num_bits);
701  free(capture_buf);
702  } else {
703  LOG_DEBUG_IO("field skipped");
704  }
705  bit_offset += num_bits;
706  }
707  list_del(&scan->queue);
708  free(scan);
709  /* after one round of scan field processing the
710  next data bits are read from the next data byte
711  => round up and calculate the next start bit */
712  byte_offset = DIV_ROUND_UP(bit_offset, 8);
713  bit_offset = byte_offset * 8;
714  }
715 
716  if (byte_offset < decoded_buf_len)
717  LOG_DEBUG("decoded %d of %d bytes", byte_offset, decoded_buf_len);
718 
719  return ERROR_OK;
720 }
721 
729 static void ch347_queues_discard(void)
730 {
731  struct ch347_cmd *cmd, *tmp_cmd;
733  list_del(&cmd->queue);
734  free(cmd->write_data);
735  free(cmd);
736  }
737 
739  ch347.scratchpad_idx = 0;
740 
741  struct ch347_scan *scan, *tmp;
743  list_del(&scan->queue);
744  free(scan);
745  }
746 }
747 
755 {
756  // queue last command
757  int retval = ch347_cmd_from_scratchpad();
758  if (retval != ERROR_OK)
759  return retval;
760 
761  // nothing to do! => done here
762  if (list_empty(&ch347.cmd_queue))
763  return ERROR_OK;
764 
765  // calculate the needed buffer length for all decoded bytes
766  struct ch347_cmd *cmd;
767  int decoded_buf_len = 0;
769  if (cmd->read_len > 0 && ch347_cmd_has_variable_data_size(cmd->type))
770  decoded_buf_len += DIV_ROUND_UP(cmd->tdo_bit_count, 8);
771 
772  // create the buffer for all decoded bytes
773  uint8_t *decoded_buf = NULL;
774  int decoded_buf_idx = 0;
775  if (decoded_buf_len > 0) {
776  decoded_buf = malloc(decoded_buf_len);
777  if (!decoded_buf) {
778  LOG_ERROR("malloc failed");
779  return ERROR_FAIL;
780  }
781  }
782 
783  while (!list_empty(&ch347.cmd_queue)) {
784  struct ch347_cmd *last_cmd = NULL;
785  int total_len = 0;
786  int total_tdo_count = 0;
787  int bytes_to_write = 0;
788  int max_len = ch347.max_len;
789 
791  total_len += CH347_CMD_HEADER + cmd->write_data_len;
792  total_tdo_count += cmd->tdo_bit_count;
793  if (cmd->type == CH347_CMD_GPIO)
794  max_len = USBC_PACKET_USBHS;
795  // don't exceed max length or max TDO bit count
796  if (total_len >= max_len || total_tdo_count >= HW_TDO_BUF_SIZE)
797  break;
798  // remember the last cmd to send and bytes to send
799  last_cmd = cmd;
800  bytes_to_write = total_len;
801  }
802 
803  // sanity checks
804  if (!last_cmd || bytes_to_write == 0) {
805  LOG_ERROR("Nothing to send!");
806  free(decoded_buf);
807  return ERROR_FAIL;
808  }
809 
810  // create the write buffer
811  uint8_t *write_buf = malloc(bytes_to_write);
812  if (!write_buf) {
813  LOG_ERROR("malloc failed");
814  free(decoded_buf);
815  return ERROR_FAIL;
816  }
817 
818  int idx = 0;
819  int bytes_to_read = 0;
820  int current_decoded_buf_len = 0;
821  struct ch347_cmd *tmp;
822 
824  // copy command to buffer
825  write_buf[idx++] = cmd->type;
826  h_u16_to_le(&write_buf[idx], cmd->write_data_len);
827  idx += 2;
828  memcpy(&write_buf[idx], cmd->write_data, cmd->write_data_len);
829  idx += cmd->write_data_len;
830  // need to read something back?
831  if (cmd->read_len > 0) {
832  bytes_to_read += CH347_CMD_HEADER + cmd->read_len;
834  current_decoded_buf_len += DIV_ROUND_UP(cmd->tdo_bit_count, 8);
835  }
836 
837  // cmd data no longer needed
838  list_del(&cmd->queue);
839  free(cmd->write_data);
840  free(cmd);
841 
842  if (cmd == last_cmd)
843  break;
844  }
845 
846  // write data to device
847  retval = ch347_write_data(write_buf, &idx);
848  free(write_buf);
849  if (retval != ERROR_OK) {
850  free(decoded_buf);
851  return retval;
852  }
853 
854  if (!bytes_to_read)
855  continue;
856 
857  // Need only to execute a read without decoding the data to make the CH347 happy?
858  if (!current_decoded_buf_len) {
859  // read but don't decode anything
860  retval = ch347_read_scan(NULL, 0, bytes_to_read);
861  } else {
862  retval = ch347_read_scan(&decoded_buf[decoded_buf_idx], current_decoded_buf_len, bytes_to_read);
863  decoded_buf_idx += current_decoded_buf_len;
864  }
865 
866  if (retval != ERROR_OK) {
867  free(decoded_buf);
868  return retval;
869  }
870  }
871 
872  // something decoded from the data read back from CH347?
873  if (decoded_buf) {
874  // put the decoded data into the scan fields
875  retval = ch347_scan_data_to_fields(decoded_buf, decoded_buf_len);
876  free(decoded_buf);
877  }
878 
879  return retval;
880 }
881 
888 static int ch347_cmd_transmit_queue(void)
889 {
890  int retval = ch347_cmd_transmit_queue_inner();
891  if (retval != ERROR_OK)
893 
894  return retval;
895 }
896 
903 static int ch347_cmd_start_next(uint8_t type)
904 {
905  // different command type or non chainable command?
906  uint8_t prev_type = ch347.scratchpad_cmd_type;
907  if (prev_type != type || !ch347_cmd_has_variable_data_size(type)) {
908  // something written in the scratchpad? => store it as command
909  if (prev_type != 0 && ch347.scratchpad_idx > 0) {
910  int retval = ch347_cmd_from_scratchpad();
911  if (retval != ERROR_OK)
912  return retval;
913 
914  /* if the last queued command is not chainable we should send it immediately
915  because e.g. the init commands can't be combined with any other command */
916  if (ch347_is_single_cmd_type(prev_type)) {
917  retval = ch347_cmd_transmit_queue();
918  if (retval != ERROR_OK)
919  return retval;
920  }
921  }
922 
923  /* before we can send non chainable command we should send all
924  other commands because we can't send it together with other commands */
926  int retval = ch347_cmd_transmit_queue();
927  if (retval != ERROR_OK)
928  return retval;
929  }
930 
931  // store the next command type
933  }
934 
935  return ERROR_OK;
936 }
937 
945 static int ch347_scan_queue_fields(struct scan_field *scan_fields, int scan_fields_len)
946 {
947  // malloc for the scan struct
948  struct ch347_scan *scan = malloc(sizeof(struct ch347_scan));
949  if (!scan) {
950  LOG_ERROR("malloc failed");
951  return ERROR_FAIL;
952  }
953 
954  scan->fields = scan_fields;
955  scan->fields_len = scan_fields_len;
956  list_add_tail(&scan->queue, &ch347.scan_queue);
957  return ERROR_OK;
958 }
959 
967 static int ch347_single_read_get_byte(uint8_t *byte)
968 {
970  int retval = ch347_cmd_transmit_queue();
971  if (retval != ERROR_OK)
972  return retval;
973 
974  if (ch347.init_result_len < 1) {
975  LOG_ERROR("no init cmd result");
976  return ERROR_FAIL;
977  }
978 
979  *byte = ch347.init_result_buf[0];
980  return ERROR_OK;
981 }
982 
988 {
989  // if full create a new command in the queue
991  uint8_t type = ch347.scratchpad_cmd_type;
994  }
995 }
996 
1005 static int ch347_scratchpad_add_byte(uint8_t byte)
1006 {
1007  if (ch347.scratchpad_cmd_type == 0) {
1008  LOG_ERROR("call ch347_next_cmd first!");
1009  return ERROR_FAIL;
1010  }
1011 
1014  return ERROR_OK;
1015 }
1016 
1025 {
1026  uint8_t pins = ch347.tms_pin | ch347.tdi_pin | ch347.tck_pin | ch347.trst_pin;
1027 
1029  pins |= ch347.srst_pin;
1030 
1031  return ch347_scratchpad_add_byte(pins);
1032 }
1033 
1044 static int ch347_scratchpad_add_bytes(uint8_t *bytes, int count)
1045 {
1046  if (ch347.scratchpad_cmd_type == 0) {
1047  LOG_ERROR("call ch347_next_cmd first!");
1048  return ERROR_FAIL;
1049  }
1050 
1053  LOG_ERROR("BUG: CH347 command 0x%02" PRIx8
1054  " is not variable data size type and payload is too long",
1056  return ERROR_FAIL;
1057  }
1058 
1059  int remaining = count;
1060  int bytes_idx = 0;
1061  while (remaining > 0) {
1062  int bytes_to_store = ch347.scratchpad_idx + remaining <= UCMDPKT_DATA_MAX_BYTES_USBHS ?
1064 
1065  if (bytes)
1066  memcpy(&ch347.scratchpad[ch347.scratchpad_idx], &bytes[bytes_idx], bytes_to_store);
1067  else
1068  memset(&ch347.scratchpad[ch347.scratchpad_idx], 0, bytes_to_store);
1069 
1070  ch347.scratchpad_idx += bytes_to_store;
1071  bytes_idx += bytes_to_store;
1072  remaining -= bytes_to_store;
1074  }
1075 
1076  return ERROR_OK;
1077 }
1078 
1099 static int ch347_srst_enable(void)
1100 {
1102  if (retval != ERROR_OK)
1103  return retval;
1104 
1105  uint8_t cmd_data[] = {0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1106  retval = ch347_scratchpad_add_bytes(cmd_data, ARRAY_SIZE(cmd_data));
1107  if (retval != ERROR_OK)
1108  return retval;
1109 
1110  uint8_t init_result;
1111  retval = ch347_single_read_get_byte(&init_result);
1112  if (retval != ERROR_OK)
1113  return retval;
1114 
1115  LOG_DEBUG("CH347F PIN_SET result %02" PRIx8, init_result);
1116 
1118  return ERROR_OK;
1119 }
1120 
1129 {
1130  ch347.tms_pin = tms ? TMS_H : TMS_L;
1131  ch347.tck_pin = TCK_L;
1132  int retval = ch347_scratchpad_add_pin_byte();
1133  if (retval != ERROR_OK)
1134  return retval;
1135 
1136  ch347.tck_pin = TCK_H;
1138 }
1139 
1147 {
1149  if (retval != ERROR_OK)
1150  return retval;
1151 
1152  bool tms = ch347.tms_pin == TMS_H;
1153  for (int i = 0; i < count; i++) {
1154  retval = ch347_scratchpad_add_clock_tms(tms);
1155  if (retval != ERROR_OK)
1156  return retval;
1157  }
1158 
1159  return ERROR_OK;
1160 }
1161 
1169 {
1170  if (ch347.tck_pin == TCK_L)
1171  return ERROR_OK;
1172 
1174  if (retval != ERROR_OK)
1175  return retval;
1176 
1177  ch347.tck_pin = TCK_L;
1179 }
1180 
1189 static int ch347_scratchpad_add_tms_change(const uint8_t *tms_value, int step, int skip)
1190 {
1191  LOG_DEBUG_IO("TMS Value: %02x..., step = %d, skip = %d", tms_value[0], step, skip);
1192 
1194  if (retval != ERROR_OK)
1195  return retval;
1196 
1197  for (int i = skip; i < step; i++) {
1198  retval = ch347_scratchpad_add_clock_tms((tms_value[i / 8] >> (i % 8)) & BIT(0));
1199  if (retval != ERROR_OK)
1200  return retval;
1201  }
1202 
1203  return ERROR_OK;
1204 }
1205 
1213 {
1214  LOG_DEBUG_IO("num_states=%d, last_state=%d", cmd->num_states, cmd->path[cmd->num_states - 1]);
1215 
1217  if (retval != ERROR_OK)
1218  return retval;
1219 
1220  for (unsigned int i = 0; i < cmd->num_states; i++) {
1221  if (tap_state_transition(tap_get_state(), false) == cmd->path[i]) {
1222  retval = ch347_scratchpad_add_clock_tms(0);
1223  if (retval != ERROR_OK)
1224  return retval;
1225  } else if (tap_state_transition(tap_get_state(), true) == cmd->path[i]) {
1226  retval = ch347_scratchpad_add_clock_tms(1);
1227  if (retval != ERROR_OK)
1228  return retval;
1229  } else {
1230  LOG_ERROR("No transition possible!");
1232  }
1233  tap_set_state(cmd->path[i]);
1234  }
1235 
1236  return ERROR_OK;
1237 }
1238 
1247 {
1248  uint8_t tms_scan;
1249  int tms_len;
1250 
1252  // don't do anything if we are already in the right state; but do execute always the TAP_RESET
1253  if (tap_get_state() == state && state != TAP_RESET)
1254  return ERROR_OK;
1255 
1256  tms_scan = tap_get_tms_path(tap_get_state(), state);
1258  int retval = ch347_scratchpad_add_tms_change(&tms_scan, tms_len, skip);
1260  return retval;
1261 }
1262 
1272 static int ch347_scratchpad_add_write_read(struct scan_command *cmd, uint8_t *bits, int bits_len, enum scan_type scan)
1273 {
1274  // the bits and bytes to transfer
1275  int byte_count = bits_len / 8;
1276  int bit_count = bits_len % 8;
1277 
1278  // only bytes are not possible because we need to set TMS high for the last bit
1279  if (byte_count > 0 && bit_count == 0) {
1280  // make one byte to eight bits
1281  byte_count--;
1282  bit_count = 8;
1283  }
1284 
1285  // in bitwise mode only bits are allowed
1286  if (ch347.use_bitwise_mode) {
1287  byte_count = 0;
1288  bit_count = bits_len;
1289  }
1290 
1291  bool is_read = (scan == SCAN_IN || scan == SCAN_IO);
1292 
1293  // if we need to send bytes
1294  if (byte_count > 0) {
1295  // a byte shift started with TCK high corrupts the scan
1296  int retval = ch347_scratchpad_add_tck_idle();
1297  if (retval != ERROR_OK)
1298  return retval;
1299 
1300  // start the next cmd and copy the data out bytes to it
1302  if (retval != ERROR_OK)
1303  return retval;
1304 
1305  if (bits)
1306  retval = ch347_scratchpad_add_bytes(bits, byte_count);
1307  else
1308  retval = ch347_scratchpad_add_bytes(NULL, byte_count);
1309 
1310  if (retval != ERROR_OK)
1311  return retval;
1312  }
1313 
1314  // bits are always need to send; no possibility to not send bits
1316  if (retval != ERROR_OK)
1317  return retval;
1318 
1319  ch347.tms_pin = TMS_L;
1320  ch347.tdi_pin = TDI_L;
1321 
1322  for (int i = 0; i < bit_count; i++) {
1323  if (bits)
1324  ch347.tdi_pin = ((bits[byte_count + i / 8] >> i % 8) & BIT(0)) ? TDI_H : TDI_L;
1325 
1326  // for the last bit set TMS high to exit the shift state
1327  if (i + 1 == bit_count)
1328  ch347.tms_pin = TMS_H;
1329 
1330  ch347.tck_pin = TCK_L;
1331  retval = ch347_scratchpad_add_pin_byte();
1332  if (retval != ERROR_OK)
1333  return retval;
1334 
1335  ch347.tck_pin = TCK_H;
1336  retval = ch347_scratchpad_add_pin_byte();
1337  if (retval != ERROR_OK)
1338  return retval;
1339 
1340  /* cut the package after each MAX_BITS_PER_BIT_OP bits because it
1341  needs a dividable by 8 bits package */
1342  if (i > 0 && (i + 1) % MAX_BITS_PER_BIT_OP == 0) {
1343  retval = ch347_cmd_from_scratchpad();
1344  if (retval != ERROR_OK)
1345  return retval;
1346 
1348  if (retval != ERROR_OK)
1349  return retval;
1350  }
1351  }
1352 
1353  // if read is involved we need to queue the scan fields
1354  if (is_read)
1355  return ch347_scan_queue_fields(cmd->fields, cmd->num_fields);
1356 
1357  return ERROR_OK;
1358 }
1359 
1367 static int ch347_scratchpad_add_run_test(int cycles, enum tap_state state)
1368 {
1369  LOG_DEBUG_IO("cycles=%d, end_state=%d", cycles, state);
1370  int retval;
1371  if (tap_get_state() != TAP_IDLE) {
1373  if (retval != ERROR_OK)
1374  return retval;
1375  }
1376 
1377  retval = ch347_scratchpad_add_stableclocks(cycles);
1378  if (retval != ERROR_OK)
1379  return retval;
1380 
1382 }
1383 
1391 {
1392  static const char *const type2str[] = {"", "SCAN_IN", "SCAN_OUT", "SCAN_IO"};
1393 
1394  enum scan_type type = jtag_scan_type(cmd);
1395  uint8_t *buf = NULL;
1396  int scan_bits = jtag_build_buffer(cmd, &buf);
1397 
1398  // add a move to IRSHIFT or DRSHIFT state
1399  int retval;
1400  if (cmd->ir_scan)
1402  else
1404 
1405  if (retval != ERROR_OK) {
1406  free(buf);
1407  return retval;
1408  }
1409 
1411  char *log_buf = buf_to_hex_str(buf, scan_bits);
1412  LOG_DEBUG_IO("scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d",
1413  cmd->ir_scan ? "IRSCAN" : "DRSCAN",
1414  type2str[type],
1415  scan_bits, log_buf, cmd->end_state);
1416  free(log_buf);
1417  }
1418 
1419  retval = ch347_scratchpad_add_write_read(cmd, buf, scan_bits, type);
1420  free(buf);
1421  if (retval != ERROR_OK)
1422  return retval;
1423 
1424  // add a move to the final state
1425  return ch347_scratchpad_add_move_state(cmd->end_state, 1);
1426 }
1427 
1433 static inline unsigned int ch347_trst_gpio(void)
1434 {
1436 }
1437 
1449 static uint8_t ch347_gpio_drive_value(bool level, enum adapter_gpio_drive_mode drive)
1450 {
1451  switch (drive) {
1453  return level ? GPIO_SET_INPUT : GPIO_SET_L;
1455  return level ? GPIO_SET_H : GPIO_SET_INPUT;
1457  break;
1458  }
1459 
1460  return level ? GPIO_SET_H : GPIO_SET_L;
1461 }
1462 
1464  unsigned int *gpios_used)
1465 {
1466  const struct adapter_gpio_config *gpio_config = &adapter_gpio_get_config()[gpio_idx];
1467  if (gpio_config->gpio_num == ADAPTER_GPIO_NOT_SET)
1468  return ERROR_OK;
1469 
1470  if (gpio_config->gpio_num >= GPIO_CNT_EXT)
1471  return ERROR_OK; // invalid GPIO num will be reported later
1472 
1473  if (BIT(gpio_config->gpio_num) & *gpios_used) {
1474  LOG_ERROR("%s GPIO %u already used",
1475  adapter_gpio_get_name(gpio_idx),
1476  gpio_config->gpio_num);
1477  return ERROR_FAIL;
1478  }
1479 
1480  *gpios_used |= BIT(gpio_config->gpio_num);
1481  return ERROR_OK;
1482 }
1483 
1485 {
1486  const struct adapter_gpio_config *gpio_config = &adapter_gpio_get_config()[gpio_idx];
1487  if (gpio_config->gpio_num == ADAPTER_GPIO_NOT_SET) {
1488  if (gpio_idx == ADAPTER_GPIO_IDX_SRST)
1489  return ch347.srst_default_gpio;
1490 
1491  if (gpio_idx == ADAPTER_GPIO_IDX_TRST)
1492  return ch347.trst_default_gpio;
1493  }
1494  return gpio_config->gpio_num;
1495 }
1496 
1497 static int ch347_set_gpio(enum adapter_gpio_config_index gpio_idx, bool active,
1498  uint8_t *gpios_data, uint8_t *bit_op)
1499 {
1500  const struct adapter_gpio_config *gpio_config = &adapter_gpio_get_config()[gpio_idx];
1501  unsigned int gpio_num = ch347_get_gpio_num_or_default(gpio_idx);
1502  const struct ch347_bit_op *gpio_to_bit_op = ch347.chip_variant == CH347F ?
1504  bool value = active ^ gpio_config->active_low;
1505  uint8_t gpio_val = ch347_gpio_drive_value(value, gpio_config->drive);
1506 
1507  /* Using both CH347_CMD_GPIO and CH347_CMD_JTAG_BIT_OP is tricky
1508  * but the only way to use GPIO capabilities (simulated open-drain)
1509  * together with JTAG and/or with the CH347F dedicated SRST pin.
1510  *
1511  * CH347T Chip version=2.41 does not support CH347_CMD_GPIO at all
1512  * - GPIO support blocked by cmd_gpio_allowed = false.
1513  *
1514  * CH347T Chip version=4.41 honours CH347_CMD_GPIO setting
1515  * to input and prevents CH347_CMD_JTAG_BIT_OP from changing the same
1516  * pin to output. Pin previously set by CH347_CMD_GPIO to output
1517  * gets updated by CH347_CMD_JTAG_BIT_OP.
1518  *
1519  * CH347T Chip version=5.44 and CH347F Chip version=1.20:
1520  * The pin previously set by CH347_CMD_GPIO to input works as open-drain
1521  * with respect to CH347_CMD_JTAG_BIT_OP. Bit op setting to L propagates
1522  * low level to the pin, setting to H switches the pin to Hi-Z.
1523  * Pin previously set by CH347_CMD_GPIO to output gets updated
1524  * by CH347_CMD_JTAG_BIT_OP.
1525  *
1526  * It means for all pins controlled by both CH347_CMD_JTAG_BIT_OP
1527  * and CH347_CMD_GPIO: the bit keeping the pin state
1528  * for CH347_CMD_JTAG_BIT_OP must follow CH347_CMD_GPIO output state
1529  * and must be set to H if GPIO is set to input. */
1530  if (gpio_num < GPIO_CNT && ch347.cmd_gpio_allowed) {
1531  gpios_data[gpio_num] = gpio_val;
1532 
1533  // set the corresponding bit for CH347_CMD_JTAG_BIT_OP
1534  if (gpio_to_bit_op[gpio_num].ptr) {
1535  if (gpio_val == GPIO_SET_INPUT)
1536  value = true; /* input setting by CH347_CMD_GPIO keeps
1537  * precedence while CH347_CMD_JTAG_BIT_OP
1538  * sets the same pin to output H */
1539  *gpio_to_bit_op[gpio_num].ptr = value ? gpio_to_bit_op[gpio_num].bit : 0;
1540  }
1541 
1542  return ERROR_OK;
1543  }
1544 
1545  if (gpio_num < GPIO_CNT_EXT) {
1546  if (gpio_to_bit_op[gpio_num].ptr) {
1547  *gpio_to_bit_op[gpio_num].ptr = value ? gpio_to_bit_op[gpio_num].bit : 0;
1548  *bit_op |= gpio_to_bit_op[gpio_num].bit;
1549 
1550  if (gpio_val == GPIO_SET_INPUT && gpio_to_bit_op[gpio_num].bit & ~ch347.bit_op_log_once) {
1551  LOG_WARNING("%s drive mode ignored, GPIO %u is push-pull only",
1552  adapter_gpio_get_name(gpio_idx),
1553  gpio_num);
1554  ch347.bit_op_log_once |= gpio_to_bit_op[gpio_num].bit;
1555  }
1556  return ERROR_OK;
1557  }
1558  }
1559 
1560  // Settings failed, reaction is GPIO specific
1561  if (gpio_idx == ADAPTER_GPIO_IDX_LED) {
1562  if (gpio_num == ADAPTER_GPIO_NOT_SET)
1563  return ERROR_OK;
1564  else if (ch347.led_log_once)
1565  return ERROR_FAIL;
1566 
1567  ch347.led_log_once = true;
1568  } else if (!active) {
1569  return ERROR_OK;
1570  }
1571  if (gpio_num == ADAPTER_GPIO_NOT_SET)
1572  LOG_ERROR("%s GPIO not configured", adapter_gpio_get_name(gpio_idx));
1573  else if (gpio_num < GPIO_CNT && !ch347.cmd_gpio_allowed)
1574  LOG_ERROR("Too old CH347 version, %s GPIO configuration not supported",
1575  adapter_gpio_get_name(gpio_idx));
1576  else
1577  LOG_ERROR("%s GPIO configuration invalid", adapter_gpio_get_name(gpio_idx));
1578 
1579  return ERROR_FAIL;
1580 }
1581 
1583  uint8_t *gpios_data, uint8_t *bit_op)
1584 {
1585  const struct adapter_gpio_config *gpio_config = &adapter_gpio_get_config()[gpio_idx];
1586  if (gpio_config->init_state == ADAPTER_GPIO_INIT_STATE_INPUT) {
1587  unsigned int gpio_num = ch347_get_gpio_num_or_default(gpio_idx);
1589  gpios_data[gpio_num] = GPIO_SET_INPUT;
1590  return ERROR_OK;
1591  }
1592  LOG_WARNING("Cannot set %s GPIO to initial input state",
1593  adapter_gpio_get_name(gpio_idx));
1594  }
1595  bool active = gpio_config->init_state == ADAPTER_GPIO_INIT_STATE_ACTIVE;
1596  return ch347_set_gpio(gpio_idx, active, gpios_data, bit_op);
1597 }
1598 
1600  uint8_t *gpios_data, uint8_t *bit_op)
1601 {
1602  const struct adapter_gpio_config *gpio_config = &adapter_gpio_get_config()[gpio_idx];
1603  if (gpio_config->exit_state == ADAPTER_GPIO_EXIT_STATE_NO_CHANGE)
1604  return ERROR_OK;
1605 
1606  if (gpio_config->exit_state == ADAPTER_GPIO_EXIT_STATE_INPUT) {
1607  unsigned int gpio_num = ch347_get_gpio_num_or_default(gpio_idx);
1609  gpios_data[gpio_num] = GPIO_SET_INPUT;
1610  return ERROR_OK;
1611  }
1612  LOG_WARNING("Cannot set %s GPIO to exit input state",
1613  adapter_gpio_get_name(gpio_idx));
1614  }
1615  bool active = gpio_config->exit_state == ADAPTER_GPIO_EXIT_STATE_ACTIVE;
1616  return ch347_set_gpio(gpio_idx, active, gpios_data, bit_op);
1617 }
1618 
1625 static int ch347_gpios_update(uint8_t *gpios_data, bool bit_op, bool flush_now)
1626 {
1627  int retval1 = ERROR_OK;
1628  if (bit_op) {
1630  if (retval1 == ERROR_OK)
1631  retval1 = ch347_scratchpad_add_pin_byte();
1632  }
1633 
1634  unsigned int i;
1635  for (i = 0; i < GPIO_CNT; i++)
1636  if (gpios_data[i])
1637  break;
1638 
1639  bool any_gpio_set = i < GPIO_CNT;
1640  int retval2 = ERROR_OK;
1641  if (any_gpio_set) {
1643  if (retval2 == ERROR_OK)
1644  retval2 = ch347_scratchpad_add_bytes(gpios_data, GPIO_CNT);
1645  }
1646  if (retval1 == ERROR_OK)
1647  retval1 = retval2;
1648 
1649  if (flush_now && (bit_op || any_gpio_set))
1650  retval2 = ch347_cmd_transmit_queue();
1651 
1652  return retval1 == ERROR_OK ? retval2 : retval1;
1653 }
1654 
1659 static void ch347_activity_led_set(bool led_state, bool flush_now)
1660 {
1661  uint8_t gpios[GPIO_CNT] = { 0 };
1662  uint8_t bit_op = 0;
1663 
1664  ch347_set_gpio(ADAPTER_GPIO_IDX_LED, led_state, gpios, &bit_op);
1665  int retval = ch347_gpios_update(gpios, bit_op, flush_now);
1666  if (retval != ERROR_OK)
1667  ch347.led_is_on = false;
1668  else
1669  ch347.led_is_on = led_state;
1670 }
1671 
1683 static int ch347_reset(int trst, int srst)
1684 {
1685  LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst);
1686 
1687  uint8_t gpios[GPIO_CNT] = { 0 };
1688  uint8_t bit_op = 0;
1689  int retval1 = ch347_set_gpio(ADAPTER_GPIO_IDX_SRST, srst, gpios, &bit_op);
1690  int retval2 = ch347_set_gpio(ADAPTER_GPIO_IDX_TRST, trst, gpios, &bit_op);
1691  if (retval1 == ERROR_OK)
1692  retval1 = retval2;
1693 
1694  retval2 = ch347_gpios_update(gpios, bit_op, true);
1695  return retval1 != ERROR_OK ? retval1 : retval2;
1696 }
1697 
1704 static int ch347_sleep(int us)
1705 {
1706  LOG_DEBUG_IO("us=%d", us);
1707  int retval = ch347_scratchpad_add_tck_idle();
1708  if (retval == ERROR_OK)
1709  retval = ch347_cmd_transmit_queue();
1710  jtag_sleep(us);
1711  return retval;
1712 }
1713 
1719 static int ch347_execute_queue(struct jtag_command *cmd_queue)
1720 {
1721  struct jtag_command *cmd = cmd_queue;
1722  int retval = ERROR_OK;
1723 
1724  ch347_activity_led_set(true, false);
1725 
1726  while (retval == ERROR_OK && cmd) {
1727  switch (cmd->type) {
1728  case JTAG_RUNTEST:
1729  retval = ch347_scratchpad_add_run_test(cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
1730  break;
1731  case JTAG_STABLECLOCKS:
1732  retval = ch347_scratchpad_add_stableclocks(cmd->cmd.stableclocks->num_cycles);
1733  break;
1734  case JTAG_TLR_RESET:
1735  retval = ch347_scratchpad_add_move_state(cmd->cmd.statemove->end_state, 0);
1736  break;
1737  case JTAG_PATHMOVE:
1738  retval = ch347_scratchpad_add_move_path(cmd->cmd.pathmove);
1739  break;
1740  case JTAG_TMS:
1741  retval = ch347_scratchpad_add_tms_change(cmd->cmd.tms->bits, cmd->cmd.tms->num_bits, 0);
1742  break;
1743  case JTAG_SLEEP:
1744  retval = ch347_sleep(cmd->cmd.sleep->us);
1745  break;
1746  case JTAG_SCAN:
1747  retval = ch347_scratchpad_add_scan(cmd->cmd.scan);
1748  break;
1749  default:
1750  LOG_ERROR("BUG: unknown JTAG command type 0x%X", cmd->type);
1751  retval = ERROR_FAIL;
1752  break;
1753  }
1754 
1755  cmd = cmd->next;
1756  }
1757 
1758  if (retval == ERROR_OK)
1759  retval = ch347_scratchpad_add_tck_idle();
1760 
1761  /* drop the partially queued commands, the LED off goes with the next queue */
1762  if (retval != ERROR_OK)
1764 
1765  ch347_activity_led_set(false, false);
1766 
1767  if (retval != ERROR_OK)
1768  return retval;
1769 
1770  retval = ch347_cmd_transmit_queue();
1771  return retval;
1772 }
1773 
1779 static int ch347_open_device(void)
1780 {
1781  const uint16_t *ch347_vids = default_ch347_vids;
1782  const uint16_t *ch347_pids = default_ch347_pids;
1783 
1784  if (adapter_usb_get_vids()[0] != 0) {
1785  ch347_vids = adapter_usb_get_vids();
1786  ch347_pids = adapter_usb_get_pids();
1787  }
1788 
1789  int retval = jtag_libusb_open(ch347_vids, ch347_pids,
1791  if (retval != ERROR_OK) {
1792  char error_message[256];
1793  snprintf(error_message, sizeof(error_message), "CH347 not found. Tried VID/PID pairs: ");
1794  for (int i = 0; ch347_vids[i] != 0; i++)
1795  snprintf(error_message + strlen(error_message), sizeof(error_message) - strlen(error_message),
1796  "%04x:%04x ", ch347_vids[i], ch347_pids[i]);
1797 
1798  LOG_ERROR("%s", error_message);
1799  return retval;
1800  }
1801 
1802  struct libusb_device_descriptor ch347_device_descriptor;
1803  libusb_device *device = libusb_get_device(ch347_handle);
1804  if (!device) {
1805  LOG_ERROR("CH347 error calling libusb_get_device");
1807  return ERROR_FAIL;
1808  }
1809 
1810  retval = libusb_get_device_descriptor(device, &ch347_device_descriptor);
1811  if (retval != ERROR_OK) {
1812  LOG_ERROR("CH347 error getting device descriptor: %s", libusb_error_name(retval));
1814  return retval;
1815  }
1816 
1817  // Fix for CH347F on ARM64
1818  // Force set configuration 1 (required on some ARM64 platforms)
1819  retval = libusb_set_configuration(ch347_handle, 1);
1820  if (retval != LIBUSB_SUCCESS && retval != LIBUSB_ERROR_BUSY)
1821  LOG_WARNING("CH347 set_configuration failed: %s", libusb_error_name(retval));
1822 
1823  // CH347T / CH347F detection
1824  // if we can claim interface 4 we found a CH347F chip; if we can claim interface 2 we found CH347T chip
1825  retval = libusb_claim_interface(ch347_handle, CH347F_MPHSI_INTERFACE);
1826  if (retval != ERROR_OK) {
1827  retval = libusb_claim_interface(ch347_handle, CH347T_MPHSI_INTERFACE);
1828  if (retval != ERROR_OK) {
1829  LOG_ERROR("CH347 unable to claim interface: %s", libusb_error_name(retval));
1831  return retval;
1832  }
1834  } else {
1836  }
1837 
1838  char firmware_version;
1840  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
1841  VENDOR_VERSION, 0, 0, &firmware_version, sizeof(firmware_version),
1843  if (retval != ERROR_OK) {
1844  LOG_ERROR("CH347 unable to get firmware version");
1846  return retval;
1847  }
1848 
1849  char manufacturer[256 + 1];
1850  if (libusb_get_string_descriptor_ascii(ch347_handle, ch347_device_descriptor.iManufacturer,
1851  (unsigned char *)manufacturer, sizeof(manufacturer) - 1) < 0) {
1852  strcpy(manufacturer, "(unknown)");
1853  }
1854  char product[256 + 1];
1855  if (libusb_get_string_descriptor_ascii(ch347_handle, ch347_device_descriptor.iProduct,
1856  (unsigned char *)product, sizeof(product) - 1) < 0) {
1857  strcpy(product, "(unknown)");
1858  }
1859  char serial_number[256 + 1];
1860  if (libusb_get_string_descriptor_ascii(ch347_handle, ch347_device_descriptor.iSerialNumber,
1861  (unsigned char *)serial_number, sizeof(serial_number) - 1) < 0) {
1862  strcpy(serial_number, "(unknown)");
1863  }
1864 
1865  LOG_INFO("CH347%c %s from vendor %s with serial number %s found. (Chip version=%X.%02X, Firmware=0x%02X)",
1866  ch347.chip_variant == CH347T ? 'T' : ch347.chip_variant == CH347F ? 'F' : '?',
1867  product,
1868  manufacturer,
1869  serial_number,
1870  (ch347_device_descriptor.bcdDevice >> 8) & 0xFF,
1871  ch347_device_descriptor.bcdDevice & 0xFF,
1872  firmware_version);
1873 
1874  ch347.use_bitwise_mode = false;
1875  ch347.cmd_gpio_allowed = true;
1876 
1877  if (ch347.chip_variant == CH347T) {
1878  if (ch347_device_descriptor.bcdDevice < BYTEWISE_MODE_VERSION) {
1879  ch347.use_bitwise_mode = true;
1880  LOG_INFO("CH347T old version of the chip, JTAG only working in bitwise mode. For bytewise mode at least version %X.%X is needed.",
1881  (BYTEWISE_MODE_VERSION >> 8) & 0xFF,
1882  BYTEWISE_MODE_VERSION & 0xFF);
1883  }
1884 
1885  if (ch347_device_descriptor.bcdDevice < 0x441)
1886  ch347.cmd_gpio_allowed = false;
1887 
1888  if (swd_mode) {
1889  ch347.swclk_5mhz_supported = ch347_device_descriptor.bcdDevice >= 0x544;
1890 
1891  if (ch347_device_descriptor.bcdDevice < 0x441)
1892  LOG_WARNING("CH347T version older than 4.41 probably does not support SWD transport");
1893 
1894  if (ch347_device_descriptor.bcdDevice == 0x441)
1895  LOG_WARNING("If CH347T version 4.41 cannot connect or SWD fails often, insert a resistor to SWDIO circuit");
1896 
1897  } else if (ch347_device_descriptor.bcdDevice == 0x241) {
1898  LOG_WARNING("CH347T version 2.41 has very weird clock timing, may not work with a slower JTAG device");
1899  }
1900 
1901  if (ch347_device_descriptor.bcdDevice < 0x544)
1902  LOG_INFO("Please upgrade CH347T firmware to a production version >= 5.44");
1903 
1904  } else if (ch347.chip_variant == CH347F) {
1905  ch347.swclk_5mhz_supported = ch347_device_descriptor.bcdDevice >= 0x101;
1906 
1907  if (ch347_device_descriptor.bcdDevice < 0x101)
1908  LOG_INFO("Please upgrade CH347F firmware to a production version >= 1.1");
1909  }
1910 
1911  return ERROR_OK;
1912 }
1913 
1919 static int ch347_quit(void)
1920 {
1921  int retval;
1922  uint8_t gpios[GPIO_CNT] = { 0 };
1923  uint8_t bit_op = 0;
1927 
1928  retval = ch347_gpios_update(gpios, bit_op, true);
1929 
1931  LOG_DEBUG("CH347 close");
1932  return retval;
1933 }
1934 
1946 static int ch347_adapter_init(uint8_t clock_index, bool *supports_larger_pack_mode)
1947 {
1949  if (retval != ERROR_OK)
1950  return retval;
1951 
1952  retval = ch347_scratchpad_add_byte(0);
1953  if (retval != ERROR_OK)
1954  return retval;
1955 
1956  retval = ch347_scratchpad_add_byte(clock_index);
1957  if (retval != ERROR_OK)
1958  return retval;
1959 
1960  for (int i = 0; i < 4; i++) {
1961  retval = ch347_scratchpad_add_pin_byte();
1962  if (retval != ERROR_OK)
1963  return retval;
1964  }
1965 
1966  uint8_t mode;
1967  retval = ch347_single_read_get_byte(&mode);
1968  if (retval != ERROR_OK)
1969  return retval;
1970 
1971  *supports_larger_pack_mode = mode != 0;
1972  return ERROR_OK;
1973 }
1974 
1983 static int ch347_adapter_supports_larger_pack_mode(bool *supports_larger_pack_mode)
1984 {
1985  return ch347_adapter_init(CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE, supports_larger_pack_mode);
1986 }
1987 
1995 static int ch347_adapter_set_speed(uint8_t clock_index)
1996 {
1997  bool unused;
1998  return ch347_adapter_init(clock_index, &unused);
1999 }
2000 
2007 static int ch347_swd_init_cmd(uint8_t clock_divisor)
2008 {
2010  if (retval != ERROR_OK)
2011  return retval;
2012 
2013  uint8_t cmd_data[] = {0x40, 0x42, 0x0f, 0x00, clock_divisor, 0x00, 0x00, 0x00 };
2014  retval = ch347_scratchpad_add_bytes(cmd_data, ARRAY_SIZE(cmd_data));
2015  if (retval != ERROR_OK)
2016  return retval;
2017 
2018  /* TODO: CH347_CMD_SWD_INIT reads one data byte.
2019  But how can we decide if SWD init was successfully executed?
2020  Return an error code if init was failed */
2021  uint8_t init_result = 0;
2022  retval = ch347_single_read_get_byte(&init_result);
2023  LOG_DEBUG("SWD init clk div %" PRIu8 ", result %02" PRIx8,
2024  clock_divisor, init_result);
2025  ch347_swd_context.clk_divisor = clock_divisor;
2026  return retval;
2027 }
2028 
2035 static int ch347_speed_set(int speed_index)
2036 {
2037  if (swd_mode)
2038  return ch347_swd_init_cmd(speed_index);
2039 
2040  int retval = ch347_adapter_set_speed(speed_index);
2041  if (retval != ERROR_OK) {
2042  LOG_ERROR("Couldn't set CH347 speed");
2043  return retval;
2044  }
2045 
2046  return ERROR_OK;
2047 }
2048 
2054 static int ch347_init_pack_size(void)
2055 {
2056  // already set?
2057  if (ch347.pack_size != UNSET)
2058  return ERROR_OK;
2059 
2060  // set the lower limit for starting
2062  bool supports_larger_pack_mode;
2063  int retval = ch347_adapter_supports_larger_pack_mode(&supports_larger_pack_mode);
2064  if (retval != ERROR_OK)
2065  return retval;
2066 
2067  ch347.pack_size = supports_larger_pack_mode ? LARGER_PACK : STANDARD_PACK;
2070  return ERROR_OK;
2071 }
2072 
2080 static int ch347_speed_get(int speed_idx, int *khz)
2081 {
2082  if (swd_mode) {
2083  if (speed_idx)
2084  *khz = DIV_ROUND_UP(CH347_SWD_CLOCK_BASE, speed_idx);
2085  else
2086  *khz = CH347_SWD_CLOCK_MAX;
2087  return ERROR_OK;
2088  }
2089 
2090  int retval = ch347_init_pack_size();
2091  if (retval != ERROR_OK)
2092  return retval;
2093  const int *speeds = ch347.pack_size == STANDARD_PACK ?
2095  *khz = speeds[speed_idx];
2096  return ERROR_OK;
2097 }
2098 
2108 static int ch347_speed_get_index(int khz, int *speed_idx)
2109 {
2110  if (khz == 0) {
2111  LOG_ERROR("Adaptive clocking not supported");
2112  return ERROR_FAIL;
2113  }
2114 
2115  if (swd_mode) {
2117  *speed_idx = 0;
2118  } else {
2119  // Don't allow too low clk speeds: packet processing is limited to ~8 msec
2120  // or triggers host USB disconnect
2121  *speed_idx = MIN(DIV_ROUND_UP(CH347_SWD_CLOCK_BASE, khz),
2123  }
2124  return ERROR_OK;
2125  }
2126 
2127  // the available clock speeds depend on STANDARD_PACK or LARGER_PACK mode support
2128  int retval = ch347_init_pack_size();
2129  if (retval != ERROR_OK)
2130  return retval;
2131  // depending on pack size there are different fixed clock speeds possible
2132  const int *speeds = ch347.pack_size == STANDARD_PACK ?
2134  int length = ch347.pack_size == STANDARD_PACK ?
2136  // find the suitable speed index
2137  int idx = length - 1;
2138  while (idx > 0 && speeds[idx] > khz)
2139  idx--;
2140 
2141  if (speeds[idx] > khz) {
2142  LOG_WARNING("Requested speed of %d kHz is not possible. Using %d kHz",
2143  khz, speeds[idx]);
2144  }
2145  *speed_idx = idx;
2146  return ERROR_OK;
2147 }
2148 
2149 static const struct command_registration ch347_command_handlers[] = {
2150  {
2151  .name = "ch347",
2152  .mode = COMMAND_ANY,
2153  .help = "perform ch347 management",
2154  .usage = "",
2155  },
2157 };
2158 
2164 static int ch347_init(void)
2165 {
2166  int retval = ch347_open_device();
2167 
2168  if (retval != ERROR_OK) {
2169  LOG_ERROR("CH347 open error");
2170  return retval;
2171  }
2172 
2173  LOG_DEBUG("CH347 open success");
2174 
2175  if (swd_mode) {
2176  /* Keep idle SWCLK and SWDIO state if CH347_CMD_JTAG_BIT_OP
2177  * is used for SRST handling */
2178  ch347.tck_pin = TCK_H;
2179  ch347.tms_pin = TMS_L;
2180  } else {
2181  ch347.tck_pin = TCK_L;
2182  ch347.tms_pin = TMS_H;
2183  }
2184  ch347.tdi_pin = TDI_L;
2185  ch347.trst_pin = TRST_H;
2186  ch347.srst_pin = SRST_H;
2190 
2191  unsigned int gpios_used = 0;
2192  const struct ch347_bit_op *gpio_to_bit_op = ch347.chip_variant == CH347F
2194 
2195  for (unsigned int i = 0; i < GPIO_CNT_EXT; i++) {
2196  if (swd_mode ? gpio_to_bit_op[i].swd_use : gpio_to_bit_op[i].jtag_use)
2197  gpios_used |= BIT(i);
2198  }
2199 
2200  retval = ch347_check_gpio_unique(ADAPTER_GPIO_IDX_SRST, &gpios_used);
2201  if (retval == ERROR_OK)
2202  retval = ch347_check_gpio_unique(ADAPTER_GPIO_IDX_TRST, &gpios_used);
2203  if (retval == ERROR_OK)
2204  retval = ch347_check_gpio_unique(ADAPTER_GPIO_IDX_LED, &gpios_used);
2205  if (retval != ERROR_OK)
2206  return retval;
2207 
2209  if (gpios_used & BIT(ch347.trst_default_gpio))
2211 
2213  if (ch347.chip_variant == CH347F
2214  && (gpios_used & BIT(CH347F_SRST_FAKE_GPIO)) == 0)
2216 
2217  /* Static initial pack_size is STANDARD_PACK (0), which would look already
2218  * probed. max_len stays 0 until the probe, and PIN_SET uses this queue. */
2219  ch347.pack_size = UNSET;
2220  retval = ch347_init_pack_size();
2221  if (retval != ERROR_OK)
2222  return retval;
2223 
2224  uint8_t gpios[GPIO_CNT] = { 0 };
2225  uint8_t bit_op = 0;
2229 
2230  if (bit_op & SRST_H) {
2231  retval = ch347_srst_enable();
2232  if (retval != ERROR_OK) {
2233  LOG_WARNING("CH347F dedicated SRST enable failed");
2234  // Do not fail adapter init, partial functionality possible
2235  }
2236  }
2237 
2238  retval = ch347_gpios_update(gpios, bit_op, true);
2239  if (retval != ERROR_OK) {
2240  LOG_WARNING("CH347 initial GPIO setting failed");
2241  // Do not fail adapter init, partial functionality possible
2242  }
2243 
2244  if (swd_mode)
2245  return ch347_swd_init_cmd(1);
2246 
2248  return ERROR_OK;
2249 }
2250 
2256 static int ch347_swd_init(void)
2257 {
2258  LOG_INFO("CH347 SWD mode enabled");
2259  swd_mode = true;
2260  memset(&ch347_swd_context, 0, sizeof(ch347_swd_context));
2261 
2264 
2267  struct ch347_swd_io *pswd_io = ch347_swd_context.ch347_cmd_buf;
2268  for (int i = 0; i < CH347_MAX_CMD_BUF; i++, pswd_io++) {
2269  INIT_LIST_HEAD(&pswd_io->list_entry);
2271  }
2272  return ERROR_OK;
2273 }
2274 
2276 {
2277  struct ch347_swd_io *pswd_io;
2279  return NULL;
2280 
2282  struct ch347_swd_io, list_entry);
2283  list_del_init(&pswd_io->list_entry);
2284  pswd_io->cmd = 0;
2285  pswd_io->usb_cmd = CH347_CMD_SWD_SEQ_W;
2286  pswd_io->dst = NULL;
2287  return pswd_io;
2288 }
2289 
2290 static int ch347_swd_queue_flush(void)
2291 {
2293 
2294  int retval = ch347_cmd_start_next(CH347_CMD_SWD);
2295  if (retval == ERROR_OK)
2297  if (retval != ERROR_OK) {
2299  return retval;
2300  }
2301 
2302  retval = ch347_cmd_transmit_queue();
2303  if (retval != ERROR_OK)
2304  return retval;
2305 
2307  LOG_ERROR("write/read failed %d %d",
2310  retval = ERROR_FAIL;
2311  }
2312 
2313  return retval;
2314 }
2315 
2316 static void ch347_write_swd_reg(uint8_t cmd, const uint32_t out)
2317 {
2319  // 8bit + 32bit +1bit
2326  // 0xA0 + 1 byte(3bit ACK)
2327  ch347_swd_context.need_recv_len += (1 + 1);
2329 }
2330 
2331 static void ch347_write_spec_seq(const uint8_t *out, uint8_t out_len)
2332 {
2337  for (uint8_t i = 0; i < DIV_ROUND_UP(out_len, 8); i++)
2338  ch347_swd_context.send_buf[ch347_swd_context.send_len++] = out ? out[i] : 0x00;
2339  ch347_swd_context.need_recv_len += 1; // 0xA1
2340  ch347_swd_context.total_swd_clk += out_len;
2341 }
2342 
2343 static void ch347_read_swd_reg(uint8_t cmd)
2344 {
2349  // 0xA2 + 1 byte(3bit ACK) + 4 byte(data) + 1 byte(1bit parity+1bit trn)
2350  ch347_swd_context.need_recv_len += 1 + 1 + 4 + 1;
2352 }
2353 
2354 static int ch347_swd_switch_out(enum swd_special_seq seq, const uint8_t *out, unsigned int out_len)
2355 {
2356  if ((ch347_swd_context.send_len + (1 + 2 + DIV_ROUND_UP(out_len, 8))) > CH347_MAX_SEND_BUF)
2357  return ERROR_FAIL;
2359  return ERROR_FAIL;
2360 
2361  struct ch347_swd_io *pswd_io = ch347_get_one_swd_io();
2362  if (pswd_io) {
2363  ch347_write_spec_seq(out, out_len);
2365  return ERROR_OK;
2366  } else {
2367  return ERROR_FAIL;
2368  }
2369 }
2370 
2371 // check read/write REG can fill in remaining buff
2372 static bool ch347_chk_buf_size(uint8_t cmd, uint32_t ap_delay_clk)
2373 {
2374  bool flush = false;
2375  int send_len = ch347_swd_context.send_len;
2376  int recv_len = ch347_swd_context.need_recv_len;
2377  int len;
2378  do {
2379  if (cmd & SWD_CMD_RNW) {
2380  len = 1 + 1 + 1 + 1; // 0xA2 + len + rev + cmd
2381  if (send_len + len > CH347_MAX_SEND_BUF)
2382  break;
2383  send_len += len;
2384  len = 1 + 1 + 4 + 1;
2385  /* 0xA2 + 1byte(3bit ack) + 4byte(data) +
2386  1byte(1bit parity+1bit trn) */
2387  if (recv_len + len > CH347_MAX_RECV_BUF)
2388  break;
2389  recv_len += len;
2390  } else { // write reg
2391  len = 1 + 1 + 1 + 1 + 4 + 1;
2392  // 0xA0 + len + rev + cmd +data + parity
2393  if (send_len + len > CH347_MAX_SEND_BUF)
2394  break;
2395  send_len += len;
2396  len = 1 + 1; // 0xA0 + 1byte(3bit ack)
2397  if (recv_len + len > CH347_MAX_RECV_BUF)
2398  break;
2399  recv_len += len;
2400  }
2401  if (cmd & SWD_CMD_APNDP) {
2402  len = 1 + 1 + 1 + DIV_ROUND_UP(ap_delay_clk, 8);
2403  // 0xA1 + Len + rev + n byte(delay)
2404  if (send_len + len > CH347_MAX_SEND_BUF)
2405  break;
2406  len = 1; // 0xA1
2407  if ((recv_len + len) > CH347_MAX_RECV_BUF)
2408  break;
2409  }
2410  // swd packet requests
2411  flush = true;
2412  } while (false);
2413 
2414  return flush;
2415 }
2416 
2417 static int ch347_swd_run_queue_inner(void);
2418 
2419 static int ch347_swd_send_idle(uint32_t ap_delay_clk)
2420 {
2421  bool run_q = false;
2422  struct ch347_swd_io *pswd_io = NULL;
2423  unsigned int max_processing_clk = ch347_swd_context.clk_divisor
2426  bool more_q_runs =
2427  1 + 1 + 1 + DIV_ROUND_UP(ap_delay_clk, 8) > CH347_MAX_SEND_BUF
2428  && ap_delay_clk > max_processing_clk;
2429 
2431  unsigned int expected_total_clk = ch347_swd_context.total_swd_clk + ap_delay_clk;
2432  unsigned int expected_send_len = ch347_swd_context.send_len
2433  + 1 + 1 + 1 + DIV_ROUND_UP(ap_delay_clk, 8);
2434  // 0xA1 + Len + rev + n byte(delay)
2435  unsigned int expected_recv_len = ch347_swd_context.need_recv_len + 1;
2436  // 0xA1
2437  unsigned int expected_time = ch347_swd_context.clk_divisor
2438  ? expected_total_clk * ch347_swd_context.clk_divisor
2439  : expected_total_clk / 5;
2440  if (expected_time > CH347_MAX_PROCESSING_US
2441  || expected_send_len > CH347_MAX_SEND_BUF
2442  || expected_recv_len > CH347_MAX_RECV_BUF) {
2443  int send_room = CH347_MAX_SEND_BUF - ch347_swd_context.send_len - 1 - 1 - 1;
2444  if (more_q_runs
2445  && send_room > 0
2446  && expected_recv_len <= CH347_MAX_RECV_BUF
2447  && ch347_swd_context.total_swd_clk < max_processing_clk) {
2448  pswd_io = ch347_get_one_swd_io();
2449  if (pswd_io) {
2450  // fill the rest of queue/time by part of delay
2451  unsigned int this_delay_clk = MIN(ap_delay_clk, 255);
2452  if ((unsigned int)send_room * 8 < this_delay_clk)
2453  this_delay_clk = send_room * 8;
2454  if (max_processing_clk - ch347_swd_context.total_swd_clk < this_delay_clk)
2455  this_delay_clk = max_processing_clk - ch347_swd_context.total_swd_clk;
2456  LOG_DEBUG_IO("partial delay %u clk", this_delay_clk);
2457  ch347_write_spec_seq(NULL, this_delay_clk);
2459  ap_delay_clk -= this_delay_clk;
2460  }
2461  }
2462  run_q = true;
2463  }
2464  }
2465 
2466  do {
2467  if (!run_q)
2468  pswd_io = ch347_get_one_swd_io();
2469 
2470  if (!pswd_io) {
2471  int retval = ch347_swd_run_queue_inner();
2472  if (retval != ERROR_OK)
2473  return retval;
2474 
2475  pswd_io = ch347_get_one_swd_io();
2476  if (!pswd_io) {
2477  LOG_ERROR("ch347 SWD queue not empty after ch347_swd_run_queue");
2478  return ERROR_FAIL;
2479  }
2480  }
2481 
2482  unsigned int send_room = CH347_MAX_SEND_BUF - 1 - 1 - 1;
2483  unsigned int this_delay_clk = MIN(ap_delay_clk, 255);
2484  if (send_room * 8 < this_delay_clk)
2485  this_delay_clk = send_room * 8;
2486  if (max_processing_clk < this_delay_clk)
2487  this_delay_clk = max_processing_clk;
2488  LOG_DEBUG_IO("delay %u clk", this_delay_clk);
2489  ch347_write_spec_seq(NULL, this_delay_clk);
2491  ap_delay_clk -= this_delay_clk;
2492  run_q = true;
2493  pswd_io = NULL;
2494  } while (ap_delay_clk);
2495  return ERROR_OK;
2496 }
2497 
2499 {
2500  LOG_DEBUG_IO("Executing %u queued transactions", ch347_swd_context.sent_cmd_count);
2501 
2502  /* CMD_GPIO or CMD_JTAG_BIT_OP can take place before CMD_SWD
2503  * in the same USB packet */
2504  if (!ch347.led_is_on)
2505  ch347_activity_led_set(true, false);
2506 
2507  int retval = ch347_swd_queue_flush();
2508  if (retval != ERROR_OK) {
2509  LOG_ERROR("CH347 usb write/read failed");
2510  goto skip;
2511  }
2512 
2513  uint8_t *recv_buf = ch347_swd_context.recv_buf;
2514  int recv_len = 0;
2515  struct list_head *tmp;
2516  struct list_head *pos;
2517  struct ch347_swd_io *pswd_io;
2518 
2520  pswd_io = list_entry(pos, struct ch347_swd_io, list_entry);
2521  if (pswd_io->usb_cmd == CH347_CMD_SWD_SEQ_W) {
2522  if (recv_buf[recv_len++] != CH347_CMD_SWD_SEQ_W) {
2523  LOG_ERROR("CH347 usb write/read failed - not CH347_CMD_SWD_SEQ_W");
2524  retval = ERROR_FAIL;
2525  goto skip;
2526  }
2527  } else { // read/write Reg
2528  uint32_t ack;
2529  bool check_ack;
2530  // read Reg
2531  if (recv_buf[recv_len] == CH347_CMD_SWD_REG_R) {
2532  recv_len++;
2533  ack = buf_get_u32(&recv_buf[recv_len++], 0, 3);
2534  /* Devices do not reply to DP_TARGETSEL write
2535  cmd, ignore received ack */
2536  check_ack = swd_cmd_returns_ack(pswd_io->cmd);
2537  if (pswd_io->cmd & SWD_CMD_RNW) {
2538  uint32_t data = buf_get_u32(&recv_buf[recv_len], 0, 32);
2539 
2540  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK)
2542  "%s%s %s read reg %X = %08" PRIx32,
2543  check_ack ? "" : "ack ignored ",
2544  ack == SWD_ACK_OK ? "OK" :
2545  ack == SWD_ACK_WAIT ? "WAIT" :
2546  ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
2547  pswd_io->cmd & SWD_CMD_APNDP ? "AP" : "DP",
2548  (pswd_io->cmd & SWD_CMD_A32) >> 1,
2549  data);
2550 
2551  if (ack != SWD_ACK_OK && check_ack) {
2552  retval = swd_ack_to_error_code(ack);
2553  goto skip;
2554  }
2555 
2556  uint32_t parity = buf_get_u32(&recv_buf[recv_len], 32, 1);
2557  if (parity != (uint32_t)parity_u32(data)) {
2558  LOG_ERROR("SWD Read data parity mismatch");
2559  retval = ERROR_SWD_FAIL;
2560  goto skip;
2561  }
2562 
2563  if (pswd_io->dst)
2564  *pswd_io->dst = data;
2565  } else {
2566  retval = ERROR_FAIL;
2567  LOG_ERROR("CH347 usb write/read failed - not SWD_CMD_RNW");
2568  goto skip;
2569  }
2570  recv_len += 5;
2571  } else if (recv_buf[recv_len] == CH347_CMD_SWD_REG_W) {
2572  recv_len++;
2573  ack = buf_get_u32(&recv_buf[recv_len++], 0, 3);
2574  /* Devices do not reply to DP_TARGETSEL write
2575  cmd, ignore received ack */
2576  check_ack = swd_cmd_returns_ack(pswd_io->cmd);
2577 
2578  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK)
2580  "%s%s %s write reg %X = %08" PRIx32,
2581  check_ack ? "" : "ack ignored ",
2582  ack == SWD_ACK_OK ? "OK" :
2583  ack == SWD_ACK_WAIT ? "WAIT" :
2584  ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
2585  pswd_io->cmd & SWD_CMD_APNDP ? "AP" : "DP",
2586  (pswd_io->cmd & SWD_CMD_A32) >> 1,
2587  pswd_io->value);
2588 
2589  if (ack != SWD_ACK_OK && check_ack) {
2590  retval = swd_ack_to_error_code(ack);
2591  goto skip;
2592  }
2593  } else {
2594  retval = ERROR_FAIL;
2595  LOG_ERROR("CH347 usb write/read failed recv_len = %d", recv_len);
2596  goto skip;
2597  }
2598  }
2599  list_del_init(&pswd_io->list_entry);
2600  list_add_tail(&pswd_io->list_entry,
2602  }
2603 
2604 skip:
2607  pswd_io = list_entry(pos, struct ch347_swd_io, list_entry);
2608  list_del_init(&pswd_io->list_entry);
2609  list_add_tail(&pswd_io->list_entry,
2611  }
2612  }
2613 
2619  return retval;
2620 }
2621 
2622 static int ch347_swd_run_queue(void)
2623 {
2624  /* A transaction must be followed by another transaction or at least 8
2625  idle cycles to ensure that data is clocked through the AP. */
2626  int retval = ch347_swd_send_idle(8);
2627  if (retval != ERROR_OK)
2628  return retval;
2629 
2630  retval = ch347_swd_run_queue_inner();
2631 
2632  /* CMD_GPIO or CMD_JTAG_BIT_OP cannot follow CMD_SWD in the same
2633  * USB packet. Flush the queue first then send eventual LED off
2634  * in a separate USB packet */
2635  ch347_activity_led_set(false, true);
2636 
2637  return retval;
2638 }
2639 
2640 static int ch347_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2641 {
2642  int retval = ERROR_OK;
2643  bool run_q = false;
2645  unsigned int expected_total_clk = ch347_swd_context.total_swd_clk
2646  + 46 // SWD transaction
2647  + ap_delay_clk
2648  + 8; // 8 idle cycles at the end of queue
2649  unsigned int expected_time = ch347_swd_context.clk_divisor
2650  ? expected_total_clk * ch347_swd_context.clk_divisor
2651  : expected_total_clk / 5;
2652  if (expected_time > CH347_MAX_PROCESSING_US) {
2653  LOG_DEBUG_IO("Expected queue run %u cycles, with this cmd %u",
2654  ch347_swd_context.total_swd_clk, expected_total_clk);
2655  run_q = true;
2656  } else if (!ch347_chk_buf_size(cmd, ap_delay_clk)) {
2657  run_q = true;
2658  }
2659  }
2660 
2661  struct ch347_swd_io *pswd_io = NULL;
2662  if (!run_q)
2663  pswd_io = ch347_get_one_swd_io();
2664 
2665  if (!pswd_io) {
2666  retval = ch347_swd_run_queue_inner();
2667  if (retval != ERROR_OK)
2668  return retval;
2669 
2670  pswd_io = ch347_get_one_swd_io();
2671  if (!pswd_io) {
2672  LOG_ERROR("ch347 SWD queue not empty after ch347_swd_run_queue");
2673  return ERROR_FAIL;
2674  }
2675  }
2676 
2677  pswd_io->cmd = cmd | SWD_CMD_START | SWD_CMD_PARK;
2678 
2679  if (pswd_io->cmd & SWD_CMD_RNW) {
2680  pswd_io->usb_cmd = CH347_CMD_SWD_REG_R;
2681  pswd_io->dst = dst;
2682  ch347_read_swd_reg(pswd_io->cmd);
2683  } else {
2684  pswd_io->usb_cmd = CH347_CMD_SWD_REG_W;
2685  pswd_io->value = data;
2686  ch347_write_swd_reg(pswd_io->cmd, data);
2687  }
2688 
2691 
2692  // Insert idle cycles after AP accesses to avoid WAIT
2693  if (ap_delay_clk)
2694  retval = ch347_swd_send_idle(ap_delay_clk);
2695 
2696  return retval;
2697 }
2698 
2700 {
2701  switch (seq) {
2702  case LINE_RESET:
2703  LOG_DEBUG("SWD line reset");
2705  case JTAG_TO_SWD:
2706  LOG_DEBUG("JTAG-to-SWD");
2708  case JTAG_TO_DORMANT:
2709  LOG_DEBUG("JTAG-to-DORMANT");
2711  case SWD_TO_JTAG:
2712  LOG_DEBUG("SWD-to-JTAG");
2714  case SWD_TO_DORMANT:
2715  LOG_DEBUG("SWD-to-DORMANT");
2717  case DORMANT_TO_SWD:
2718  LOG_DEBUG("DORMANT-to-SWD");
2720  case DORMANT_TO_JTAG:
2721  LOG_DEBUG("DORMANT-to-JTAG");
2723  default:
2724  LOG_ERROR("Sequence %d not supported", seq);
2725  return ERROR_FAIL;
2726  }
2727 }
2728 
2729 static int ch347_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
2730 {
2731  assert(cmd & SWD_CMD_RNW);
2732  return ch347_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
2733 }
2734 
2735 static int ch347_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
2736 {
2737  assert(!(cmd & SWD_CMD_RNW));
2738  return ch347_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
2739 }
2740 
2741 static const struct swd_driver ch347_swd = {
2742  .init = ch347_swd_init,
2743  .switch_seq = ch347_swd_switch_seq,
2744  .read_reg = ch347_swd_read_reg,
2745  .write_reg = ch347_swd_write_reg,
2746  .run = ch347_swd_run_queue,
2747 };
2748 
2749 static struct jtag_interface ch347_interface = {
2751  .execute_queue = ch347_execute_queue,
2752 };
2753 
2755  .name = "ch347",
2756  .transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
2757  .transport_preferred_id = TRANSPORT_JTAG,
2758  .commands = ch347_command_handlers,
2759 
2760  .init = ch347_init,
2761  .quit = ch347_quit,
2762  .reset = ch347_reset,
2763  .speed = ch347_speed_set,
2764  .khz = ch347_speed_get_index,
2765  .speed_div = ch347_speed_get,
2766 
2767  .jtag_ops = &ch347_interface,
2768  .swd_ops = &ch347_swd,
2769 };
const struct adapter_gpio_config * adapter_gpio_get_config(void)
Retrieves gpio configuration set with command "adapter gpio <signal_name>".
Definition: adapter.c:1349
const uint16_t * adapter_usb_get_pids(void)
Definition: adapter.c:344
const char * adapter_usb_get_product_name(void)
Definition: adapter.c:354
const uint16_t * adapter_usb_get_vids(void)
Definition: adapter.c:339
struct adapter_gpio_config gpios[ADAPTER_GPIO_IDX_NUM]
Definition: adapter.c:53
const char * adapter_gpio_get_name(enum adapter_gpio_config_index idx)
Retrieves gpio name.
Definition: adapter.c:1343
@ ADAPTER_GPIO_INIT_STATE_ACTIVE
Definition: adapter.h:32
@ ADAPTER_GPIO_INIT_STATE_INPUT
Definition: adapter.h:33
@ ADAPTER_GPIO_EXIT_STATE_NO_CHANGE
Definition: adapter.h:38
@ ADAPTER_GPIO_EXIT_STATE_INPUT
Definition: adapter.h:41
@ ADAPTER_GPIO_EXIT_STATE_ACTIVE
Definition: adapter.h:40
adapter_gpio_config_index
Adapter GPIO.
Definition: adapter.h:52
@ ADAPTER_GPIO_IDX_LED
Definition: adapter.h:62
@ ADAPTER_GPIO_IDX_SRST
Definition: adapter.h:61
@ ADAPTER_GPIO_IDX_TRST
Definition: adapter.h:57
adapter_gpio_drive_mode
Supported output drive modes for adaptor GPIO.
Definition: adapter.h:16
@ ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE
Definition: adapter.h:19
@ ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN
Definition: adapter.h:18
@ ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL
Definition: adapter.h:17
#define ADAPTER_GPIO_NOT_SET
Definition: adapter.h:142
#define ALIGN_UP(x, a)
Definition: align.h:20
unsigned char bit
Definition: am13e230x.c:166
#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:280
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_srst_enable(void)
Enable the dedicated SRST output on CH347F devices.
Definition: ch347.c:1099
static int ch347_scratchpad_add_move_state(enum tap_state state, int skip)
Toggle the tap state to the target state.
Definition: ch347.c:1246
static int ch347_cmd_from_scratchpad(void)
copy the scratchpad content into a new command in the command queue
Definition: ch347.c:536
struct adapter_driver ch347_adapter_driver
Definition: ch347.c:2754
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:1044
#define CH347_MAX_CMD_BUF
Definition: ch347.c:128
static int ch347_init_pack_size(void)
inits ch347.pack_size and ch347.max_len
Definition: ch347.c:2054
static int ch347_check_gpio_unique(enum adapter_gpio_config_index gpio_idx, unsigned int *gpios_used)
Definition: ch347.c:1463
#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:2354
#define HW_TDO_BUF_SIZE
Definition: ch347.c:93
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:675
#define CH347_CMD_SWD
Definition: ch347.c:120
static const int ch347_larger_pack_clock_speeds[]
Definition: ch347.c:181
#define CH347_EPOUT
Definition: ch347.c:134
static int ch347_quit(void)
CH347 Device Release Function.
Definition: ch347.c:1919
static int ch347_single_read_get_byte(uint8_t *byte)
Function executes the single command and deliver one byte from the buffer that's read back from USB.
Definition: ch347.c:967
#define CH347F_MPHSI_INTERFACE
Definition: ch347.c:137
#define DEFAULT_CH347F_PRODUCT_ID
Definition: ch347.c:146
static struct ch347_swd_io * ch347_get_one_swd_io(void)
Definition: ch347.c:2275
#define CH347_MAX_PROCESSING_US
Definition: ch347.c:124
static int ch347_read_data(uint8_t *data, int buf_length, int *length)
reads data from the CH347 via libusb driver See https://libusb.sourceforge.io/api-1....
Definition: ch347.c:459
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:1212
#define CH347_SWD_CLOCK_MAX
Definition: ch347.c:129
#define DEFAULT_OTHER_PRODUCT_ID
Definition: ch347.c:147
#define CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE
Definition: ch347.c:103
#define CH347_EPIN
Definition: ch347.c:135
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:492
#define CH347T_TRST_GPIO
Definition: ch347.c:80
#define VENDOR_VERSION
Definition: ch347.c:90
#define GPIO_SET_INPUT
Definition: ch347.c:88
#define TMS_L
Definition: ch347.c:68
#define GPIO_CNT_EXT
Definition: ch347.c:78
#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:2149
#define DEFAULT_VENDOR_ID
Definition: ch347.c:144
static void ch347_write_swd_reg(uint8_t cmd, const uint32_t out)
Definition: ch347.c:2316
static bool ch347_dedicated_srst_enabled
Definition: ch347.c:275
#define CH347_CMD_JTAG_INIT
Definition: ch347.c:111
static int ch347_cmd_start_next(uint8_t type)
starts the next command in the scratchpad.
Definition: ch347.c:903
#define CH347_SWD_CLOCK_BASE
Definition: ch347.c:130
#define USB_READ_TIMEOUT
Definition: ch347.c:139
#define CH347_MAX_SEND_BUF
Definition: ch347.c:126
static bool ch347_is_single_cmd_type(uint8_t type)
Definition: ch347.c:302
static bool swd_mode
Definition: ch347.c:266
#define SRST_H
Definition: ch347.c:73
#define CH347_CMD_INIT_READ_LEN
Definition: ch347.c:101
static int ch347_swd_run_queue(void)
Definition: ch347.c:2622
static const struct swd_driver ch347_swd
Definition: ch347.c:2741
static int ch347_speed_get_index(int khz, int *speed_idx)
converts the requested speed to a CH347 speed index
Definition: ch347.c:2108
#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:1995
static int ch347_open_device(void)
opens the CH347 device via libusb driver
Definition: ch347.c:1779
static int ch347_scratchpad_add_stableclocks(int count)
Function adds a certain amount of TCK pulses without changing the TMS pin.
Definition: ch347.c:1146
#define GPIO_SET_H
Definition: ch347.c:86
static const uint16_t default_ch347_vids[]
Definition: ch347.c:268
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:1005
static int ch347_set_gpio(enum adapter_gpio_config_index gpio_idx, bool active, uint8_t *gpios_data, uint8_t *bit_op)
Definition: ch347.c:1497
static const struct ch347_bit_op ch347t_gpio_to_bit_op[GPIO_CNT_EXT]
Definition: ch347.c:284
static int ch347_execute_queue(struct jtag_command *cmd_queue)
Executes the command queue.
Definition: ch347.c:1719
static int ch347_swd_send_idle(uint32_t ap_delay_clk)
Definition: ch347.c:2419
static int ch347_cmd_transmit_queue(void)
Sends all queued commands and puts the data read back into the scan fields.
Definition: ch347.c:888
static void log_buf_dump(const uint8_t *data, unsigned int size, bool recv)
Definition: ch347.c:316
#define CH347_CMD_SWD_INIT
Definition: ch347.c:119
static bool ch347_cmd_has_variable_data_size(uint8_t type)
Definition: ch347.c:310
#define CH347F_TRST_GPIO
Definition: ch347.c:81
static int ch347_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: ch347.c:2735
static int ch347_gpios_update(uint8_t *gpios_data, bool bit_op, bool flush_now)
Update GPIOS or JTAG_BIT_OP controlled outputs Avoid failing on the first error - the second part may...
Definition: ch347.c:1625
static int ch347_swd_queue_flush(void)
Definition: ch347.c:2290
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:1024
static void ch347_write_spec_seq(const uint8_t *out, uint8_t out_len)
Definition: ch347.c:2331
static struct libusb_device_handle * ch347_handle
Definition: ch347.c:277
#define UCMDPKT_DATA_MAX_BYTES_USBHS
Definition: ch347.c:98
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:945
static int ch347_scratchpad_add_run_test(int cycles, enum tap_state state)
Toggle the Tap state to run test/idle.
Definition: ch347.c:1367
#define CH347_MAX_RECV_BUF
Definition: ch347.c:127
static int ch347_init_gpio(enum adapter_gpio_config_index gpio_idx, uint8_t *gpios_data, uint8_t *bit_op)
Definition: ch347.c:1582
static unsigned int ch347_trst_gpio(void)
GPIO number of the TRST pin.
Definition: ch347.c:1433
static int ch347_speed_get(int speed_idx, int *khz)
returns the speed in kHz for the given speed index
Definition: ch347.c:2080
#define CH347_CMD_GPIO
Definition: ch347.c:110
#define CH347_CMD_PIN_SET
Definition: ch347.c:116
static int ch347_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
Definition: ch347.c:2640
static int ch347_swd_switch_seq(enum swd_special_seq seq)
Definition: ch347.c:2699
static unsigned int ch347_get_gpio_num_or_default(enum adapter_gpio_config_index gpio_idx)
Definition: ch347.c:1484
static struct jtag_interface ch347_interface
Definition: ch347.c:2749
ch347_variant
Definition: ch347.c:154
@ CH347F
Definition: ch347.c:156
@ OTHER_PRODUCT_ID
Definition: ch347.c:157
@ CH347T
Definition: ch347.c:155
static bool ch347_chk_buf_size(uint8_t cmd, uint32_t ap_delay_clk)
Definition: ch347.c:2372
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:1272
#define USB_WRITE_TIMEOUT
Definition: ch347.c:138
static int ch347_write_data(uint8_t *data, int *length)
writes data to the CH347 via libusb driver
Definition: ch347.c:419
static struct ch347_info ch347
Definition: ch347.c:276
#define CH347_SWD_CLOCK_MAX_DIVISOR
Definition: ch347.c:132
#define DEFAULT_CH347T_PRODUCT_ID
Definition: ch347.c:145
#define CH347_CMD_JTAG_DATA_SHIFT
Definition: ch347.c:114
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:574
#define CH347_CMD_SWD_REG_R
Definition: ch347.c:123
static uint8_t ch347_gpio_drive_value(bool level, enum adapter_gpio_drive_mode drive)
Translates an output level into a GPIO command byte for a drive mode.
Definition: ch347.c:1449
#define CH347F_SRST_FAKE_GPIO
Definition: ch347.c:79
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:1189
static int ch347_swd_init(void)
Initialization for the swd mode.
Definition: ch347.c:2256
#define TCK_L
Definition: ch347.c:70
static const struct ch347_bit_op ch347f_gpio_to_bit_op[GPIO_CNT_EXT]
Definition: ch347.c:291
#define CH347T_MPHSI_INTERFACE
Definition: ch347.c:136
#define GPIO_CNT
Definition: ch347.c:76
static int ch347_exit_gpio(enum adapter_gpio_config_index gpio_idx, uint8_t *gpios_data, uint8_t *bit_op)
Definition: ch347.c:1599
#define TRST_H
Definition: ch347.c:71
#define GPIO_SET_L
Definition: ch347.c:85
static int ch347_sleep(int us)
Flushes the command buffer and sleeps for a specific timespan.
Definition: ch347.c:1704
static void ch347_read_swd_reg(uint8_t cmd)
Definition: ch347.c:2343
#define CH347_CMD_JTAG_BIT_OP
Definition: ch347.c:112
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:1983
static int ch347_init(void)
CH347 Initialization function.
Definition: ch347.c:2164
static int ch347_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: ch347.c:2729
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:1946
#define LARGER_PACK_MAX_SIZE
Definition: ch347.c:95
#define CH347_CMD_HEADER
Definition: ch347.c:100
static const int ch347_standard_pack_clock_speeds[]
Definition: ch347.c:171
static const uint16_t default_ch347_pids[]
Definition: ch347.c:269
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:1128
static int ch347_cmd_transmit_queue_inner(void)
Sends the write buffer via libusb and if LARGER_PACK mode is active read also data back.
Definition: ch347.c:754
static int ch347_swd_run_queue_inner(void)
Definition: ch347.c:2498
static void ch347_activity_led_set(bool led_state, bool flush_now)
Turn the activity LED on or off Errors are intentionally ignored.
Definition: ch347.c:1659
#define CH347_CMD_SWD_SEQ_W
Definition: ch347.c:122
#define CH347_CMD_SWD_REG_W
Definition: ch347.c:121
static void ch347_scratchpad_check_full(void)
checks if the scratchpad is full.
Definition: ch347.c:987
static void ch347_queues_discard(void)
Drops everything queued but not transmitted yet: the scratchpad, the command queue and the scan field...
Definition: ch347.c:729
pack_size
Definition: ch347.c:164
@ STANDARD_PACK
Definition: ch347.c:166
@ LARGER_PACK
Definition: ch347.c:167
@ UNSET
Definition: ch347.c:165
static int ch347_swd_init_cmd(uint8_t clock_divisor)
swd init function
Definition: ch347.c:2007
#define CH347_CMD_JTAG_BIT_OP_RD
Definition: ch347.c:113
static int ch347_speed_set(int speed_index)
Initializes the JTAG interface and set CH347 TCK frequency.
Definition: ch347.c:2035
#define BYTEWISE_MODE_VERSION
Definition: ch347.c:141
#define USBC_PACKET_USBHS
Definition: ch347.c:99
static int ch347_reset(int trst, int srst)
Control (assert/deassert) the signals SRST and TRST on the interface.
Definition: ch347.c:1683
static int ch347_scratchpad_add_scan(struct scan_command *cmd)
Switch to SHIFT-DR or SHIFT-IR status for scanning.
Definition: ch347.c:1390
#define MAX_BITS_PER_BIT_OP
Definition: ch347.c:105
#define CH347_CMD_JTAG_DATA_SHIFT_RD
Definition: ch347.c:115
static int ch347_scratchpad_add_tck_idle(void)
Return TCK to its low idle level, if it isn't there already.
Definition: ch347.c:1168
#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 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:234
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:355
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:206
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:201
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:199
#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:1058
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:706
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
static int step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: riscv-011.c:1477
enum stldr_func_type type
Definition: stldr.c:80
#define BIT(nr)
Definition: stm32l4x.h:18
Represents a driver for a debugging interface.
Definition: interface.h:219
const char *const name
The name of the interface driver.
Definition: interface.h:221
Configuration options for a single GPIO.
Definition: adapter.h:68
unsigned int gpio_num
Definition: adapter.h:69
enum adapter_gpio_exit_state exit_state
Definition: adapter.h:73
enum adapter_gpio_init_state init_state
Definition: adapter.h:72
enum adapter_gpio_drive_mode drive
Definition: adapter.h:71
bool jtag_use
Definition: ch347.c:281
int * ptr
Definition: ch347.c:282
uint8_t bit
Definition: ch347.c:280
bool swd_use
Definition: ch347.c:281
uint16_t read_len
Definition: ch347.c:196
uint8_t type
Definition: ch347.c:193
uint16_t tdo_bit_count
Definition: ch347.c:197
uint16_t write_data_len
Definition: ch347.c:195
uint8_t * write_data
Definition: ch347.c:194
uint8_t bit_op_log_once
Definition: ch347.c:225
unsigned int srst_default_gpio
Definition: ch347.c:222
struct list_head scan_queue
Definition: ch347.c:235
int max_len
Definition: ch347.c:219
bool use_bitwise_mode
Definition: ch347.c:217
struct list_head cmd_queue
Definition: ch347.c:233
bool led_is_on
Definition: ch347.c:223
int init_result_len
Definition: ch347.c:238
bool led_log_once
Definition: ch347.c:224
int tms_pin
Definition: ch347.c:209
bool swclk_5mhz_supported
Definition: ch347.c:220
int tdi_pin
Definition: ch347.c:210
unsigned int trst_default_gpio
Definition: ch347.c:222
int trst_pin
Definition: ch347.c:212
enum ch347_variant chip_variant
Definition: ch347.c:215
uint8_t scratchpad[UCMDPKT_DATA_MAX_BYTES_USBHS]
Definition: ch347.c:229
uint8_t gpio_status_buf[GPIO_CNT]
Definition: ch347.c:240
int scratchpad_idx
Definition: ch347.c:230
int tck_pin
Definition: ch347.c:211
bool cmd_gpio_allowed
Definition: ch347.c:221
enum pack_size pack_size
Definition: ch347.c:218
uint8_t scratchpad_cmd_type
Definition: ch347.c:228
uint8_t init_result_buf[CH347_CMD_INIT_READ_LEN]
Definition: ch347.c:237
int srst_pin
Definition: ch347.c:213
int fields_len
Definition: ch347.c:203
struct scan_field * fields
Definition: ch347.c:202
struct list_head free_cmd_head
Definition: ch347.c:261
struct ch347_swd_io ch347_cmd_buf[CH347_MAX_CMD_BUF]
Definition: ch347.c:262
int sent_cmd_count
Definition: ch347.c:257
unsigned int clk_divisor
Definition: ch347.c:258
int need_recv_len
Definition: ch347.c:256
uint8_t send_buf[CH347_MAX_SEND_BUF]
Definition: ch347.c:252
uint8_t recv_buf[CH347_MAX_RECV_BUF]
Definition: ch347.c:253
struct list_head send_cmd_head
Definition: ch347.c:260
unsigned int total_swd_clk
Definition: ch347.c:259
uint32_t * dst
Definition: ch347.c:246
struct list_head list_entry
Definition: ch347.c:248
uint8_t usb_cmd
Definition: ch347.c:244
uint8_t cmd
Definition: ch347.c:245
uint32_t value
Definition: ch347.c:247
const char * name
Definition: command.h:239
Represents a driver for a debugging interface.
Definition: interface.h:194
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:198
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
#define ERROR_SWD_FAIL
Definition: swd.h:28
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:619