OpenOCD
ch347.c File Reference
Include dependency graph for ch347.c:

Go to the source code of this file.

Data Structures

struct  ch347_bit_op
 
struct  ch347_cmd
 
struct  ch347_info
 
struct  ch347_scan
 
struct  ch347_swd_context
 
struct  ch347_swd_io
 

Macros

#define BYTEWISE_MODE_VERSION   0x241
 
#define CH347_CMD_GPIO   0xCC
 
#define CH347_CMD_HEADER   3
 
#define CH347_CMD_INFO_RD   0xCA
 
#define CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE   9
 
#define CH347_CMD_INIT_READ_LEN   1
 
#define CH347_CMD_JTAG_BIT_OP   0xD1
 
#define CH347_CMD_JTAG_BIT_OP_RD   0xD2
 
#define CH347_CMD_JTAG_DATA_SHIFT   0xD3
 
#define CH347_CMD_JTAG_DATA_SHIFT_RD   0xD4
 
#define CH347_CMD_JTAG_INIT   0xD0
 
#define CH347_CMD_PIN_SET   0xE2
 
#define CH347_CMD_SWD   0xE8
 
#define CH347_CMD_SWD_INIT   0xE5
 
#define CH347_CMD_SWD_REG_R   0xA2
 
#define CH347_CMD_SWD_REG_W   0xA0
 
#define CH347_CMD_SWD_SEQ_W   0xA1
 
#define CH347_EPIN   0x86u
 
#define CH347_EPOUT   0x06u
 
#define CH347_MAX_CMD_BUF   128
 
#define CH347_MAX_PROCESSING_US   7000
 
#define CH347_MAX_RECV_BUF   (USBC_PACKET_USBHS - CH347_CMD_HEADER)
 
#define CH347_MAX_SEND_BUF   UCMDPKT_DATA_MAX_BYTES_USBHS
 
#define CH347_SWD_CLOCK_BASE   1000
 
#define CH347_SWD_CLOCK_MAX   5000
 
#define CH347_SWD_CLOCK_MAX_DIVISOR   (CH347_MAX_PROCESSING_US / swd_seq_dormant_to_swd_len)
 
#define CH347F_MPHSI_INTERFACE   4
 
#define CH347F_SRST_FAKE_GPIO   GPIO_CNT
 
#define CH347F_TRST_GPIO   3
 
#define CH347T_MPHSI_INTERFACE   2
 
#define CH347T_TRST_GPIO   5
 
#define DEFAULT_CH347F_PRODUCT_ID   0x55de
 
#define DEFAULT_CH347T_PRODUCT_ID   0x55dd
 
#define DEFAULT_OTHER_PRODUCT_ID   0x55e7
 
#define DEFAULT_VENDOR_ID   0x1a86
 
#define GPIO_CNT   8
 
#define GPIO_CNT_EXT   (GPIO_CNT + 1)
 
#define GPIO_SET_H   (BIT(3) | BIT(4) | BIT(5) | BIT(6) | BIT(7))
 
#define GPIO_SET_INPUT   (BIT(6) | BIT(7))
 
#define GPIO_SET_L   (BIT(4) | BIT(5) | BIT(6) | BIT(7))
 
#define HW_TDO_BUF_SIZE   4096
 
#define LARGER_PACK_MAX_SIZE   51200
 
#define MAX_BITS_PER_BIT_OP   248
 
#define SRST_H   BIT(6)
 
#define SRST_L   0
 
#define TCK_H   BIT(0)
 
#define TCK_L   0
 
#define TDI_H   BIT(4)
 
#define TDI_L   0
 
#define TMS_H   BIT(1)
 
#define TMS_L   0
 
#define TRST_H   BIT(5)
 
#define TRST_L   0
 
#define UCMDPKT_DATA_MAX_BYTES_USBHS   507
 
#define USB_READ_TIMEOUT   500
 
#define USB_WRITE_TIMEOUT   500
 
#define USBC_PACKET_USBHS   512
 
#define VENDOR_VERSION   0x5F
 

Enumerations

enum  ch347_variant { CH347T = 0 , CH347F = 1 , OTHER_PRODUCT_ID = 2 }
 
enum  pack_size { UNSET = -1 , STANDARD_PACK = 0 , LARGER_PACK = 1 }
 

Functions

static void ch347_activity_led_set (bool led_state, bool flush_now)
 Turn the activity LED on or off Errors are intentionally ignored. More...
 
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 or asking with special clock index 9 for the LARGER_PACK mode support. More...
 
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. More...
 
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 the LARGER_PACK mode support. More...
 
static int ch347_check_gpio_unique (enum adapter_gpio_config_index gpio_idx, unsigned int *gpios_used)
 
static bool ch347_chk_buf_size (uint8_t cmd, uint32_t ap_delay_clk)
 
static void ch347_cmd_calc_reads (struct ch347_cmd *cmd)
 calculates the amount of bits and bytes that should be read for this command More...
 
static int ch347_cmd_from_scratchpad (void)
 copy the scratchpad content into a new command in the command queue More...
 
static bool ch347_cmd_has_variable_data_size (uint8_t type)
 
static int ch347_cmd_start_next (uint8_t type)
 starts the next command in the scratchpad. More...
 
static int ch347_cmd_transmit_queue (void)
 Sends all queued commands and puts the data read back into the scan fields. More...
 
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. More...
 
static int ch347_execute_queue (struct jtag_command *cmd_queue)
 Executes the command queue. More...
 
static int ch347_exit_gpio (enum adapter_gpio_config_index gpio_idx, uint8_t *gpios_data, uint8_t *bit_op)
 
static unsigned int ch347_get_gpio_num_or_default (enum adapter_gpio_config_index gpio_idx)
 
static struct ch347_swd_io * ch347_get_one_swd_io (void)
 
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. More...
 
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 succeed. More...
 
static int ch347_init (void)
 CH347 Initialization function. More...
 
static int ch347_init_gpio (enum adapter_gpio_config_index gpio_idx, uint8_t *gpios_data, uint8_t *bit_op)
 
static int ch347_init_pack_size (void)
 inits ch347.pack_size and ch347.max_len More...
 
static bool ch347_is_single_cmd_type (uint8_t type)
 
static int ch347_open_device (void)
 opens the CH347 device via libusb driver More...
 
static void ch347_queues_discard (void)
 Drops everything queued but not transmitted yet: the scratchpad, the command queue and the scan fields waiting for data. More...
 
static int ch347_quit (void)
 CH347 Device Release Function. More...
 
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.0/libusb_packetoverflow.html for explanation of avoiding LIBUSB_TRANSFER_OVERFLOW More...
 
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. More...
 
static void ch347_read_swd_reg (uint8_t cmd)
 
static int ch347_reset (int trst, int srst)
 Control (assert/deassert) the signals SRST and TRST on the interface. More...
 
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. More...
 
static int ch347_scan_queue_fields (struct scan_field *scan_fields, int scan_fields_len)
 queue the scan fields into the scan queue More...
 
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 from the scratchpad and the scratchpad is cleared for the next command More...
 
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 created from the scratchpad and the scratchpad is cleared for the next command More...
 
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. More...
 
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. More...
 
static int ch347_scratchpad_add_move_state (enum tap_state state, int skip)
 Toggle the tap state to the target state. More...
 
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 created from the scratchpad and the scratchpad is cleared for the next command More...
 
static int ch347_scratchpad_add_run_test (int cycles, enum tap_state state)
 Toggle the Tap state to run test/idle. More...
 
static int ch347_scratchpad_add_scan (struct scan_command *cmd)
 Switch to SHIFT-DR or SHIFT-IR status for scanning. More...
 
static int ch347_scratchpad_add_stableclocks (int count)
 Function adds a certain amount of TCK pulses without changing the TMS pin. More...
 
static int ch347_scratchpad_add_tck_idle (void)
 Return TCK to its low idle level, if it isn't there already. More...
 
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. More...
 
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. More...
 
static void ch347_scratchpad_check_full (void)
 checks if the scratchpad is full. More...
 
static int ch347_set_gpio (enum adapter_gpio_config_index gpio_idx, bool active, uint8_t *gpios_data, uint8_t *bit_op)
 
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. More...
 
static int ch347_sleep (int us)
 Flushes the command buffer and sleeps for a specific timespan. More...
 
static int ch347_speed_get (int speed_idx, int *khz)
 returns the speed in kHz for the given speed index More...
 
static int ch347_speed_get_index (int khz, int *speed_idx)
 converts the requested speed to a CH347 speed index More...
 
static int ch347_speed_set (int speed_index)
 Initializes the JTAG interface and set CH347 TCK frequency. More...
 
static int ch347_srst_enable (void)
 Enable the dedicated SRST output on CH347F devices. More...
 
static int ch347_swd_init (void)
 Initialization for the swd mode. More...
 
static int ch347_swd_init_cmd (uint8_t clock_divisor)
 swd init function More...
 
static int ch347_swd_queue_cmd (uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
 
static int ch347_swd_queue_flush (void)
 
static int ch347_swd_read_reg (uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
 
static int ch347_swd_run_queue (void)
 
static int ch347_swd_run_queue_inner (void)
 
static int ch347_swd_send_idle (uint32_t ap_delay_clk)
 
static int ch347_swd_switch_out (enum swd_special_seq seq, const uint8_t *out, unsigned int out_len)
 
static int ch347_swd_switch_seq (enum swd_special_seq seq)
 
static int ch347_swd_write_reg (uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
 
static unsigned int ch347_trst_gpio (void)
 GPIO number of the TRST pin. More...
 
static int ch347_write_data (uint8_t *data, int *length)
 writes data to the CH347 via libusb driver More...
 
static void ch347_write_spec_seq (const uint8_t *out, uint8_t out_len)
 
static void ch347_write_swd_reg (uint8_t cmd, const uint32_t out)
 
static void log_buf_dump (const uint8_t *data, unsigned int size, bool recv)
 

Variables

static struct ch347_info ch347
 
struct adapter_driver ch347_adapter_driver
 
static const struct command_registration ch347_command_handlers []
 
static bool ch347_dedicated_srst_enabled
 
static struct libusb_device_handle * ch347_handle
 
static struct jtag_interface ch347_interface
 
static const int ch347_larger_pack_clock_speeds []
 
static const int ch347_standard_pack_clock_speeds []
 
static const struct swd_driver ch347_swd
 
static struct ch347_swd_context ch347_swd_context
 
static const struct ch347_bit_op ch347f_gpio_to_bit_op [GPIO_CNT_EXT]
 
static const struct ch347_bit_op ch347t_gpio_to_bit_op [GPIO_CNT_EXT]
 
static const uint16_t default_ch347_pids []
 
static const uint16_t default_ch347_vids [] = {DEFAULT_VENDOR_ID, DEFAULT_VENDOR_ID, DEFAULT_VENDOR_ID, 0}
 
static bool swd_mode
 

Macro Definition Documentation

◆ BYTEWISE_MODE_VERSION

#define BYTEWISE_MODE_VERSION   0x241

Definition at line 141 of file ch347.c.

◆ CH347_CMD_GPIO

#define CH347_CMD_GPIO   0xCC

Definition at line 110 of file ch347.c.

◆ CH347_CMD_HEADER

#define CH347_CMD_HEADER   3

Definition at line 100 of file ch347.c.

◆ CH347_CMD_INFO_RD

#define CH347_CMD_INFO_RD   0xCA

Definition at line 109 of file ch347.c.

◆ CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE

#define CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE   9

Definition at line 103 of file ch347.c.

◆ CH347_CMD_INIT_READ_LEN

#define CH347_CMD_INIT_READ_LEN   1

Definition at line 101 of file ch347.c.

◆ CH347_CMD_JTAG_BIT_OP

#define CH347_CMD_JTAG_BIT_OP   0xD1

Definition at line 112 of file ch347.c.

◆ CH347_CMD_JTAG_BIT_OP_RD

#define CH347_CMD_JTAG_BIT_OP_RD   0xD2

Definition at line 113 of file ch347.c.

◆ CH347_CMD_JTAG_DATA_SHIFT

#define CH347_CMD_JTAG_DATA_SHIFT   0xD3

Definition at line 114 of file ch347.c.

◆ CH347_CMD_JTAG_DATA_SHIFT_RD

#define CH347_CMD_JTAG_DATA_SHIFT_RD   0xD4

Definition at line 115 of file ch347.c.

◆ CH347_CMD_JTAG_INIT

#define CH347_CMD_JTAG_INIT   0xD0

Definition at line 111 of file ch347.c.

◆ CH347_CMD_PIN_SET

#define CH347_CMD_PIN_SET   0xE2

Definition at line 116 of file ch347.c.

◆ CH347_CMD_SWD

#define CH347_CMD_SWD   0xE8

Definition at line 120 of file ch347.c.

◆ CH347_CMD_SWD_INIT

#define CH347_CMD_SWD_INIT   0xE5

Definition at line 119 of file ch347.c.

◆ CH347_CMD_SWD_REG_R

#define CH347_CMD_SWD_REG_R   0xA2

Definition at line 123 of file ch347.c.

◆ CH347_CMD_SWD_REG_W

#define CH347_CMD_SWD_REG_W   0xA0

Definition at line 121 of file ch347.c.

◆ CH347_CMD_SWD_SEQ_W

#define CH347_CMD_SWD_SEQ_W   0xA1

Definition at line 122 of file ch347.c.

◆ CH347_EPIN

#define CH347_EPIN   0x86u

Definition at line 135 of file ch347.c.

◆ CH347_EPOUT

#define CH347_EPOUT   0x06u

Definition at line 134 of file ch347.c.

◆ CH347_MAX_CMD_BUF

#define CH347_MAX_CMD_BUF   128

Definition at line 128 of file ch347.c.

◆ CH347_MAX_PROCESSING_US

#define CH347_MAX_PROCESSING_US   7000

Definition at line 124 of file ch347.c.

◆ CH347_MAX_RECV_BUF

#define CH347_MAX_RECV_BUF   (USBC_PACKET_USBHS - CH347_CMD_HEADER)

Definition at line 127 of file ch347.c.

◆ CH347_MAX_SEND_BUF

#define CH347_MAX_SEND_BUF   UCMDPKT_DATA_MAX_BYTES_USBHS

Definition at line 126 of file ch347.c.

◆ CH347_SWD_CLOCK_BASE

#define CH347_SWD_CLOCK_BASE   1000

Definition at line 130 of file ch347.c.

◆ CH347_SWD_CLOCK_MAX

#define CH347_SWD_CLOCK_MAX   5000

Definition at line 129 of file ch347.c.

◆ CH347_SWD_CLOCK_MAX_DIVISOR

#define CH347_SWD_CLOCK_MAX_DIVISOR   (CH347_MAX_PROCESSING_US / swd_seq_dormant_to_swd_len)

Definition at line 132 of file ch347.c.

◆ CH347F_MPHSI_INTERFACE

#define CH347F_MPHSI_INTERFACE   4

Definition at line 137 of file ch347.c.

◆ CH347F_SRST_FAKE_GPIO

#define CH347F_SRST_FAKE_GPIO   GPIO_CNT

Definition at line 79 of file ch347.c.

◆ CH347F_TRST_GPIO

#define CH347F_TRST_GPIO   3

Definition at line 81 of file ch347.c.

◆ CH347T_MPHSI_INTERFACE

#define CH347T_MPHSI_INTERFACE   2

Definition at line 136 of file ch347.c.

◆ CH347T_TRST_GPIO

#define CH347T_TRST_GPIO   5

Definition at line 80 of file ch347.c.

◆ DEFAULT_CH347F_PRODUCT_ID

#define DEFAULT_CH347F_PRODUCT_ID   0x55de

Definition at line 146 of file ch347.c.

◆ DEFAULT_CH347T_PRODUCT_ID

#define DEFAULT_CH347T_PRODUCT_ID   0x55dd

Definition at line 145 of file ch347.c.

◆ DEFAULT_OTHER_PRODUCT_ID

#define DEFAULT_OTHER_PRODUCT_ID   0x55e7

Definition at line 147 of file ch347.c.

◆ DEFAULT_VENDOR_ID

#define DEFAULT_VENDOR_ID   0x1a86

Definition at line 144 of file ch347.c.

◆ GPIO_CNT

#define GPIO_CNT   8

Definition at line 76 of file ch347.c.

◆ GPIO_CNT_EXT

#define GPIO_CNT_EXT   (GPIO_CNT + 1)

Definition at line 78 of file ch347.c.

◆ GPIO_SET_H

#define GPIO_SET_H   (BIT(3) | BIT(4) | BIT(5) | BIT(6) | BIT(7))

Definition at line 86 of file ch347.c.

◆ GPIO_SET_INPUT

#define GPIO_SET_INPUT   (BIT(6) | BIT(7))

Definition at line 88 of file ch347.c.

◆ GPIO_SET_L

#define GPIO_SET_L   (BIT(4) | BIT(5) | BIT(6) | BIT(7))

Definition at line 85 of file ch347.c.

◆ HW_TDO_BUF_SIZE

#define HW_TDO_BUF_SIZE   4096

Definition at line 93 of file ch347.c.

◆ LARGER_PACK_MAX_SIZE

#define LARGER_PACK_MAX_SIZE   51200

Definition at line 95 of file ch347.c.

◆ MAX_BITS_PER_BIT_OP

#define MAX_BITS_PER_BIT_OP   248

Definition at line 105 of file ch347.c.

◆ SRST_H

#define SRST_H   BIT(6)

Definition at line 73 of file ch347.c.

◆ SRST_L

#define SRST_L   0

Definition at line 74 of file ch347.c.

◆ TCK_H

#define TCK_H   BIT(0)

Definition at line 69 of file ch347.c.

◆ TCK_L

#define TCK_L   0

Definition at line 70 of file ch347.c.

◆ TDI_H

#define TDI_H   BIT(4)

Definition at line 65 of file ch347.c.

◆ TDI_L

#define TDI_L   0

Definition at line 66 of file ch347.c.

◆ TMS_H

#define TMS_H   BIT(1)

Definition at line 67 of file ch347.c.

◆ TMS_L

#define TMS_L   0

Definition at line 68 of file ch347.c.

◆ TRST_H

#define TRST_H   BIT(5)

Definition at line 71 of file ch347.c.

◆ TRST_L

#define TRST_L   0

Definition at line 72 of file ch347.c.

◆ UCMDPKT_DATA_MAX_BYTES_USBHS

#define UCMDPKT_DATA_MAX_BYTES_USBHS   507

Definition at line 98 of file ch347.c.

◆ USB_READ_TIMEOUT

#define USB_READ_TIMEOUT   500

Definition at line 139 of file ch347.c.

◆ USB_WRITE_TIMEOUT

#define USB_WRITE_TIMEOUT   500

Definition at line 138 of file ch347.c.

◆ USBC_PACKET_USBHS

#define USBC_PACKET_USBHS   512

Definition at line 99 of file ch347.c.

◆ VENDOR_VERSION

#define VENDOR_VERSION   0x5F

Definition at line 90 of file ch347.c.

Enumeration Type Documentation

◆ ch347_variant

Enumerator
CH347T 
CH347F 
OTHER_PRODUCT_ID 

Definition at line 154 of file ch347.c.

◆ pack_size

enum pack_size
Enumerator
UNSET 
STANDARD_PACK 
LARGER_PACK 

Definition at line 164 of file ch347.c.

Function Documentation

◆ ch347_activity_led_set()

static void ch347_activity_led_set ( bool  led_state,
bool  flush_now 
)
static

Turn the activity LED on or off Errors are intentionally ignored.

Definition at line 1659 of file ch347.c.

References ADAPTER_GPIO_IDX_LED, ch347, ch347_gpios_update(), ch347_set_gpio(), ERROR_OK, GPIO_CNT, gpios, and ch347_info::led_is_on.

Referenced by ch347_execute_queue(), ch347_swd_run_queue(), and ch347_swd_run_queue_inner().

◆ ch347_adapter_init()

static int ch347_adapter_init ( uint8_t  clock_index,
bool *  supports_larger_pack_mode 
)
static

Sends the CH347_CMD_JTAG_INIT (D0) command to get the JTAG interface initialized with the speed index or asking with special clock index 9 for the LARGER_PACK mode support.

Parameters
clock_indexClock index; special clock index 9 is used for checking if the device supports the LARGER_PACK mode
supports_larger_pack_modereturns true if the device supports the LARGER_PACK mode for newer CH347 devices; false if the device supports only the older STANDARD_PACK mode
Returns
ERROR_OK on success

Definition at line 1946 of file ch347.c.

References CH347_CMD_JTAG_INIT, ch347_cmd_start_next(), ch347_scratchpad_add_byte(), ch347_scratchpad_add_pin_byte(), ch347_single_read_get_byte(), ERROR_OK, and mode.

Referenced by ch347_adapter_set_speed(), and ch347_adapter_supports_larger_pack_mode().

◆ ch347_adapter_set_speed()

static int ch347_adapter_set_speed ( uint8_t  clock_index)
static

Sends the CH347_CMD_JTAG_INIT (D0) command to get the JTAG interface initialized with the speed index.

Parameters
clock_indexClock index
Returns
ERROR_OK on success

Definition at line 1995 of file ch347.c.

References ch347_adapter_init().

Referenced by ch347_speed_set().

◆ ch347_adapter_supports_larger_pack_mode()

static int ch347_adapter_supports_larger_pack_mode ( bool *  supports_larger_pack_mode)
static

Sends the CH347_CMD_JTAG_INIT (D0) command to ask the JTAG interface with special clock index 9 for the LARGER_PACK mode support.

Parameters
supports_larger_pack_modereturns true if the device supports the LARGER_PACK mode for newer CH347 devices; false if the device supports only the older STANDARD_PACK mode
Returns
ERROR_OK on success

Definition at line 1983 of file ch347.c.

References ch347_adapter_init(), and CH347_CMD_INIT_GET_MODE_CLOCK_INDEX_VALUE.

Referenced by ch347_init_pack_size().

◆ ch347_check_gpio_unique()

static int ch347_check_gpio_unique ( enum adapter_gpio_config_index  gpio_idx,
unsigned int *  gpios_used 
)
static

◆ ch347_chk_buf_size()

static bool ch347_chk_buf_size ( uint8_t  cmd,
uint32_t  ap_delay_clk 
)
static

◆ ch347_cmd_calc_reads()

static void ch347_cmd_calc_reads ( struct ch347_cmd *  cmd)
static

calculates the amount of bits and bytes that should be read for this command

Parameters
cmdcommand for the calculation

Definition at line 492 of file ch347.c.

References CH347_CMD_GPIO, CH347_CMD_INIT_READ_LEN, CH347_CMD_JTAG_BIT_OP_RD, CH347_CMD_JTAG_DATA_SHIFT_RD, CH347_CMD_JTAG_INIT, CH347_CMD_PIN_SET, CH347_CMD_SWD, CH347_CMD_SWD_INIT, cmd, ch347_swd_context::need_recv_len, and TCK_H.

Referenced by ch347_cmd_from_scratchpad().

◆ ch347_cmd_from_scratchpad()

static int ch347_cmd_from_scratchpad ( void  )
static

copy the scratchpad content into a new command in the command queue

Parameters
scan_fieldsarray of scan_field's for data from the device
scan_fields_lenarray length
Returns
ERROR_OK at success

Definition at line 536 of file ch347.c.

References ch347, ch347_cmd_calc_reads(), cmd, ch347_info::cmd_queue, ERROR_FAIL, ERROR_OK, list_add_tail(), LOG_ERROR, ch347_info::scratchpad, ch347_info::scratchpad_cmd_type, and ch347_info::scratchpad_idx.

Referenced by ch347_cmd_start_next(), ch347_cmd_transmit_queue_inner(), ch347_scratchpad_add_write_read(), and ch347_scratchpad_check_full().

◆ ch347_cmd_has_variable_data_size()

static bool ch347_cmd_has_variable_data_size ( uint8_t  type)
inlinestatic

◆ ch347_cmd_start_next()

static int ch347_cmd_start_next ( uint8_t  type)
static

◆ ch347_cmd_transmit_queue()

static int ch347_cmd_transmit_queue ( void  )
static

Sends all queued commands and puts the data read back into the scan fields.

On error, everything not transmitted yet is discarded

Returns
ERROR_OK at success

Definition at line 888 of file ch347.c.

References ch347_cmd_transmit_queue_inner(), ch347_queues_discard(), and ERROR_OK.

Referenced by ch347_cmd_start_next(), ch347_execute_queue(), ch347_gpios_update(), ch347_single_read_get_byte(), ch347_sleep(), and ch347_swd_queue_flush().

◆ ch347_cmd_transmit_queue_inner()

static int ch347_cmd_transmit_queue_inner ( void  )
static

◆ ch347_execute_queue()

◆ ch347_exit_gpio()

◆ ch347_get_gpio_num_or_default()

◆ ch347_get_one_swd_io()

◆ ch347_gpio_drive_value()

static uint8_t ch347_gpio_drive_value ( bool  level,
enum adapter_gpio_drive_mode  drive 
)
static

Translates an output level into a GPIO command byte for a drive mode.

The CH347 has no open drain output driver, so it gets emulated by switching the pin to input instead of driving the inactive level. The line then relies on an external pull resistor or on the target driving it.

Parameters
leveltrue to output a high level, false for a low level
driveoutput drive mode to emulate
Returns
one of GPIO_SET_L, GPIO_SET_H or GPIO_SET_INPUT

Definition at line 1449 of file ch347.c.

References ADAPTER_GPIO_DRIVE_MODE_OPEN_DRAIN, ADAPTER_GPIO_DRIVE_MODE_OPEN_SOURCE, ADAPTER_GPIO_DRIVE_MODE_PUSH_PULL, GPIO_SET_H, GPIO_SET_INPUT, and GPIO_SET_L.

Referenced by ch347_set_gpio().

◆ ch347_gpios_update()

static int ch347_gpios_update ( uint8_t *  gpios_data,
bool  bit_op,
bool  flush_now 
)
static

Update GPIOS or JTAG_BIT_OP controlled outputs Avoid failing on the first error - the second part may succeed.

Returns
ERROR_OK at success or the first error

Definition at line 1625 of file ch347.c.

References CH347_CMD_GPIO, CH347_CMD_JTAG_BIT_OP, ch347_cmd_start_next(), ch347_cmd_transmit_queue(), ch347_scratchpad_add_bytes(), ch347_scratchpad_add_pin_byte(), ERROR_OK, and GPIO_CNT.

Referenced by ch347_activity_led_set(), ch347_init(), ch347_quit(), and ch347_reset().

◆ ch347_init()

◆ ch347_init_gpio()

◆ ch347_init_pack_size()

static int ch347_init_pack_size ( void  )
static

inits ch347.pack_size and ch347.max_len

Returns
ERROR_OK on success

Definition at line 2054 of file ch347.c.

References ch347, ch347_adapter_supports_larger_pack_mode(), ERROR_OK, LARGER_PACK, LARGER_PACK_MAX_SIZE, ch347_info::max_len, ch347_info::pack_size, STANDARD_PACK, UNSET, USBC_PACKET_USBHS, and ch347_info::use_bitwise_mode.

Referenced by ch347_init(), and ch347_speed_get().

◆ ch347_is_single_cmd_type()

static bool ch347_is_single_cmd_type ( uint8_t  type)
inlinestatic

Definition at line 302 of file ch347.c.

References CH347_CMD_JTAG_INIT, CH347_CMD_PIN_SET, CH347_CMD_SWD_INIT, and type.

Referenced by ch347_cmd_start_next().

◆ ch347_open_device()

◆ ch347_queues_discard()

static void ch347_queues_discard ( void  )
static

Drops everything queued but not transmitted yet: the scratchpad, the command queue and the scan fields waiting for data.

Used in error paths, so stale commands are not sent with the next queue and stale scan fields (owned by the already freed jtag_command queue) are not filled in later.

Definition at line 729 of file ch347.c.

References ch347, cmd, ch347_info::cmd_queue, list_del(), list_for_each_entry_safe, scan, ch347_info::scan_queue, ch347_info::scratchpad_cmd_type, and ch347_info::scratchpad_idx.

Referenced by ch347_cmd_transmit_queue(), ch347_execute_queue(), and ch347_swd_queue_flush().

◆ ch347_quit()

static int ch347_quit ( void  )
static

CH347 Device Release Function.

Returns
ERROR_OK on success

Definition at line 1919 of file ch347.c.

References ADAPTER_GPIO_IDX_LED, ADAPTER_GPIO_IDX_SRST, ADAPTER_GPIO_IDX_TRST, ch347_exit_gpio(), ch347_gpios_update(), ch347_handle, GPIO_CNT, gpios, jtag_libusb_close(), and LOG_DEBUG.

◆ ch347_read_data()

static int ch347_read_data ( uint8_t *  data,
int  buf_length,
int *  length 
)
static

reads data from the CH347 via libusb driver See https://libusb.sourceforge.io/api-1.0/libusb_packetoverflow.html for explanation of avoiding LIBUSB_TRANSFER_OVERFLOW

Parameters
dataPoint to the data buffer
buf_lengthMaximal length, aligned up to USB packet size
lengthMinimal requested data length in and real read length out
Returns
ERROR_OK at success

Definition at line 459 of file ch347.c.

References CH347_EPIN, ch347_handle, ERROR_OK, jtag_libusb_bulk_read(), length, log_buf_dump(), LOG_DEBUG_IO, LOG_ERROR, LOG_LEVEL_IS, LOG_LVL_DEBUG_IO, and USB_READ_TIMEOUT.

Referenced by ch347_read_scan().

◆ ch347_read_scan()

static int ch347_read_scan ( uint8_t *  decoded_buf,
int  decoded_buf_len,
int  raw_read_len 
)
static

Reads data back from CH347 and decode it byte- and bitwise into the buffer.

Parameters
decoded_bufPoint to a buffer to place the data to be decoded; need to be sized to the decoded size length; not the raw_read_len
decoded_buf_lenlength of the decoded_buf
raw_read_lenData length in bytes that should be read via libusb; the decoded length can be shorter
Returns
ERROR_OK at success

Definition at line 574 of file ch347.c.

References ALIGN_UP, BIT, ch347, CH347_CMD_GPIO, CH347_CMD_INIT_READ_LEN, CH347_CMD_JTAG_BIT_OP_RD, CH347_CMD_JTAG_DATA_SHIFT_RD, CH347_CMD_JTAG_INIT, CH347_CMD_PIN_SET, CH347_CMD_SWD, CH347_CMD_SWD_INIT, ch347_read_data(), DIV_ROUND_UP, ERROR_FAIL, ERROR_OK, GPIO_CNT, ch347_info::gpio_status_buf, ch347_info::init_result_buf, ch347_info::init_result_len, le_to_h_u16(), LOG_ERROR, ch347_cmd::read_len, ch347_swd_context::recv_buf, ch347_swd_context::recv_len, type, and USBC_PACKET_USBHS.

Referenced by ch347_cmd_transmit_queue_inner().

◆ ch347_read_swd_reg()

static void ch347_read_swd_reg ( uint8_t  cmd)
static

◆ ch347_reset()

static int ch347_reset ( int  trst,
int  srst 
)
static

Control (assert/deassert) the signals SRST and TRST on the interface.

Employs 'adapter gpio' configuration for both signals. Avoid failing on the first error - the second part may succeed

Parameters
trst1 to assert TRST, 0 to deassert TRST.
srst1 to assert SRST, 0 to deassert SRST.
Returns
error code propagated from CH347_CMD_JTAG_BIT_OP or CH347_CMD_GPIO or ERROR_OK

Definition at line 1683 of file ch347.c.

References ADAPTER_GPIO_IDX_SRST, ADAPTER_GPIO_IDX_TRST, ch347_gpios_update(), ch347_set_gpio(), ERROR_OK, GPIO_CNT, gpios, and LOG_DEBUG_IO.

◆ ch347_scan_data_to_fields()

static int ch347_scan_data_to_fields ( uint8_t *  decoded_buf,
int  decoded_buf_len 
)
static

Used to put the data from the decoded buffer into the scan command fields.

Parameters
decoded_bufPoint to a buffer for the decoded data
decoded_buf_lenlength of the decoded_buf
Returns
ERROR_OK at success

Definition at line 675 of file ch347.c.

References bit_offset, buf_cpy(), buf_set_buf(), buf_to_hex_str(), ch347, DIV_ROUND_UP, ERROR_FAIL, ERROR_OK, list_del(), list_for_each_entry_safe, LOG_DEBUG, LOG_DEBUG_IO, LOG_ERROR, LOG_LEVEL_IS, LOG_LVL_DEBUG_IO, scan, and ch347_info::scan_queue.

Referenced by ch347_cmd_transmit_queue_inner().

◆ ch347_scan_queue_fields()

static int ch347_scan_queue_fields ( struct scan_field *  scan_fields,
int  scan_fields_len 
)
static

queue the scan fields into the scan queue

Parameters
scan_fieldsarray of scan field's for data from the device
scan_fields_lenarray length
Returns
ERROR_OK at success

Definition at line 945 of file ch347.c.

References ch347, ERROR_FAIL, ERROR_OK, list_add_tail(), LOG_ERROR, scan, and ch347_info::scan_queue.

Referenced by ch347_scratchpad_add_write_read().

◆ ch347_scratchpad_add_byte()

static int ch347_scratchpad_add_byte ( uint8_t  byte)
static

adds one byte to the scratchpad if scratchpad is full after this byte the command will be created from the scratchpad and the scratchpad is cleared for the next command

Parameters
byteadd this byte
Returns
ERROR_OK at success

Definition at line 1005 of file ch347.c.

References ch347, ch347_scratchpad_check_full(), ERROR_FAIL, ERROR_OK, LOG_ERROR, ch347_info::scratchpad, ch347_info::scratchpad_cmd_type, and ch347_info::scratchpad_idx.

Referenced by ch347_adapter_init(), and ch347_scratchpad_add_pin_byte().

◆ ch347_scratchpad_add_bytes()

static int ch347_scratchpad_add_bytes ( uint8_t *  bytes,
int  count 
)
static

adds bytes from a buffer to the scratchpad if scratchpad is full after this byte the command will be created from the scratchpad and the scratchpad is cleared for the next command

Parameters
bytesadd this bytes
countbyte count
Returns
ERROR_OK at success

Definition at line 1044 of file ch347.c.

References ch347, ch347_cmd_has_variable_data_size(), ch347_scratchpad_check_full(), count, ERROR_FAIL, ERROR_OK, LOG_ERROR, ch347_info::scratchpad, ch347_info::scratchpad_cmd_type, ch347_info::scratchpad_idx, and UCMDPKT_DATA_MAX_BYTES_USBHS.

Referenced by ch347_gpios_update(), ch347_scratchpad_add_write_read(), ch347_srst_enable(), ch347_swd_init_cmd(), and ch347_swd_queue_flush().

◆ ch347_scratchpad_add_clock_tms()

static int ch347_scratchpad_add_clock_tms ( bool  tms)
static

Function used to change the TMS value at the rising edge of TCK to switch its TAP state.

Parameters
tmsTMS value to be changed; true = output TMS high; false = output TMS low
Returns
ERROR_OK at success

Definition at line 1128 of file ch347.c.

References ch347, ch347_scratchpad_add_pin_byte(), ERROR_OK, TCK_H, TCK_L, ch347_info::tck_pin, TMS_H, TMS_L, and ch347_info::tms_pin.

Referenced by ch347_scratchpad_add_move_path(), ch347_scratchpad_add_stableclocks(), and ch347_scratchpad_add_tms_change().

◆ ch347_scratchpad_add_move_path()

static int ch347_scratchpad_add_move_path ( struct pathmove_command *  cmd)
static

Obtain the current Tap status and switch to the status TMS value passed down by cmd.

Parameters
cmdUpper layer transfer command parameters
Returns
ERROR_OK at success; ERROR_JTAG_TRANSITION_INVALID if no transition is possible

Definition at line 1212 of file ch347.c.

References CH347_CMD_JTAG_BIT_OP, ch347_cmd_start_next(), ch347_scratchpad_add_clock_tms(), cmd, ERROR_JTAG_TRANSITION_INVALID, ERROR_OK, LOG_DEBUG_IO, LOG_ERROR, tap_get_state(), tap_set_state, and tap_state_transition().

Referenced by ch347_execute_queue().

◆ ch347_scratchpad_add_move_state()

static int ch347_scratchpad_add_move_state ( enum tap_state  state,
int  skip 
)
static

Toggle the tap state to the target state.

Parameters
statePre switch target path
skipNumber of digits to skip
Returns
ERROR_OK at success

Definition at line 1246 of file ch347.c.

References ch347_scratchpad_add_tms_change(), ERROR_OK, LOG_DEBUG_IO, state, tap_get_state(), tap_get_tms_path(), tap_get_tms_path_len(), TAP_RESET, tap_set_state, and tap_state_name().

Referenced by ch347_execute_queue(), ch347_scratchpad_add_run_test(), and ch347_scratchpad_add_scan().

◆ ch347_scratchpad_add_pin_byte()

static int ch347_scratchpad_add_pin_byte ( void  )
static

adds the output pin byte to the scratchpad if scratchpad is full after this byte the command will be created from the scratchpad and the scratchpad is cleared for the next command

Returns
ERROR_OK at success

Definition at line 1024 of file ch347.c.

References ch347, ch347_dedicated_srst_enabled, ch347_scratchpad_add_byte(), ch347_info::srst_pin, ch347_info::tck_pin, ch347_info::tdi_pin, ch347_info::tms_pin, and ch347_info::trst_pin.

Referenced by ch347_adapter_init(), ch347_gpios_update(), ch347_scratchpad_add_clock_tms(), ch347_scratchpad_add_tck_idle(), and ch347_scratchpad_add_write_read().

◆ ch347_scratchpad_add_run_test()

static int ch347_scratchpad_add_run_test ( int  cycles,
enum tap_state  state 
)
static

Toggle the Tap state to run test/idle.

Parameters
cycleshow many clock cycles for output
stateJTAG end state
Returns
ERROR_OK at success

Definition at line 1367 of file ch347.c.

References ch347_scratchpad_add_move_state(), ch347_scratchpad_add_stableclocks(), ERROR_OK, LOG_DEBUG_IO, state, tap_get_state(), and TAP_IDLE.

Referenced by ch347_execute_queue().

◆ ch347_scratchpad_add_scan()

static int ch347_scratchpad_add_scan ( struct scan_command *  cmd)
static

Switch to SHIFT-DR or SHIFT-IR status for scanning.

Parameters
cmdUpper layer transfer command parameters
Returns
ERROR_OK at success

Definition at line 1390 of file ch347.c.

References buf_to_hex_str(), ch347_scratchpad_add_move_state(), ch347_scratchpad_add_write_read(), cmd, ERROR_OK, jtag_build_buffer(), jtag_scan_type(), LOG_DEBUG_IO, LOG_LEVEL_IS, LOG_LVL_DEBUG_IO, NULL, TAP_DRSHIFT, TAP_IRSHIFT, and type.

Referenced by ch347_execute_queue().

◆ ch347_scratchpad_add_stableclocks()

static int ch347_scratchpad_add_stableclocks ( int  count)
static

Function adds a certain amount of TCK pulses without changing the TMS pin.

Parameters
countAmount of L/H TCK pulses to add
Returns
ERROR_OK at success

Definition at line 1146 of file ch347.c.

References ch347, CH347_CMD_JTAG_BIT_OP, ch347_cmd_start_next(), ch347_scratchpad_add_clock_tms(), count, ERROR_OK, TMS_H, and ch347_info::tms_pin.

Referenced by ch347_execute_queue(), and ch347_scratchpad_add_run_test().

◆ ch347_scratchpad_add_tck_idle()

static int ch347_scratchpad_add_tck_idle ( void  )
static

Return TCK to its low idle level, if it isn't there already.

Does not generate a clock cycle.

Returns
ERROR_OK at success

Definition at line 1168 of file ch347.c.

References ch347, CH347_CMD_JTAG_BIT_OP, ch347_cmd_start_next(), ch347_scratchpad_add_pin_byte(), ERROR_OK, TCK_L, and ch347_info::tck_pin.

Referenced by ch347_execute_queue(), ch347_scratchpad_add_write_read(), and ch347_sleep().

◆ ch347_scratchpad_add_tms_change()

static int ch347_scratchpad_add_tms_change ( const uint8_t *  tms_value,
int  step,
int  skip 
)
static

Function that performs state switching by changing the value of TMS.

Parameters
tms_valueThe TMS values that need to be switched form one byte of data in the switching order
stepThe number of bit values that need to be read from the tms_value value
skipCount from the skip bit of tms_value to step
Returns
ERROR_OK at success

Definition at line 1189 of file ch347.c.

References BIT, CH347_CMD_JTAG_BIT_OP, ch347_cmd_start_next(), ch347_scratchpad_add_clock_tms(), ERROR_OK, LOG_DEBUG_IO, and step().

Referenced by ch347_execute_queue(), and ch347_scratchpad_add_move_state().

◆ ch347_scratchpad_add_write_read()

static int ch347_scratchpad_add_write_read ( struct scan_command *  cmd,
uint8_t *  bits,
int  bits_len,
enum scan_type  scan 
)
static

CH347 Batch read/write function.

Parameters
cmdThe scan command
bitsRead and write data this time
bits_lenIncoming data length in bits
scanThe transmission method of incoming data to determine whether to perform data reading
Returns
ERROR_OK at success

Definition at line 1272 of file ch347.c.

References BIT, bits, ch347, ch347_cmd_from_scratchpad(), CH347_CMD_JTAG_BIT_OP, CH347_CMD_JTAG_BIT_OP_RD, CH347_CMD_JTAG_DATA_SHIFT, CH347_CMD_JTAG_DATA_SHIFT_RD, ch347_cmd_start_next(), ch347_scan_queue_fields(), ch347_scratchpad_add_bytes(), ch347_scratchpad_add_pin_byte(), ch347_scratchpad_add_tck_idle(), cmd, ERROR_OK, MAX_BITS_PER_BIT_OP, NULL, scan, SCAN_IN, SCAN_IO, TCK_H, TCK_L, ch347_info::tck_pin, TDI_H, TDI_L, ch347_info::tdi_pin, TMS_H, TMS_L, ch347_info::tms_pin, and ch347_info::use_bitwise_mode.

Referenced by ch347_scratchpad_add_scan().

◆ ch347_scratchpad_check_full()

static void ch347_scratchpad_check_full ( void  )
static

checks if the scratchpad is full.

If it's full the function creates a command from it and puts it into the command queue

Definition at line 987 of file ch347.c.

References ch347, ch347_cmd_from_scratchpad(), ch347_info::scratchpad_cmd_type, ch347_info::scratchpad_idx, type, and UCMDPKT_DATA_MAX_BYTES_USBHS.

Referenced by ch347_scratchpad_add_byte(), and ch347_scratchpad_add_bytes().

◆ ch347_set_gpio()

◆ ch347_single_read_get_byte()

static int ch347_single_read_get_byte ( uint8_t *  byte)
static

Function executes the single command and deliver one byte from the buffer that's read back from USB.

Parameters
bytereturns result byte
Returns
ERROR_OK at success

Definition at line 967 of file ch347.c.

References ch347, ch347_cmd_transmit_queue(), ERROR_FAIL, ERROR_OK, ch347_info::init_result_buf, ch347_info::init_result_len, and LOG_ERROR.

Referenced by ch347_adapter_init(), ch347_srst_enable(), and ch347_swd_init_cmd().

◆ ch347_sleep()

static int ch347_sleep ( int  us)
static

Flushes the command buffer and sleeps for a specific timespan.

Parameters
usSleep time in microseconds
Returns
ERROR_OK at success

Definition at line 1704 of file ch347.c.

References ch347_cmd_transmit_queue(), ch347_scratchpad_add_tck_idle(), ERROR_OK, jtag_sleep(), and LOG_DEBUG_IO.

Referenced by ch347_execute_queue().

◆ ch347_speed_get()

static int ch347_speed_get ( int  speed_idx,
int *  khz 
)
static

returns the speed in kHz for the given speed index

Parameters
speed_idxCH347 speed index (JTAG) or clock divisor (SWD)
khzOutput of the speed in kHz
Returns
ERROR_OK on success

Definition at line 2080 of file ch347.c.

References ch347, ch347_init_pack_size(), ch347_larger_pack_clock_speeds, ch347_standard_pack_clock_speeds, CH347_SWD_CLOCK_BASE, CH347_SWD_CLOCK_MAX, DIV_ROUND_UP, ERROR_OK, ch347_info::pack_size, STANDARD_PACK, and swd_mode.

◆ ch347_speed_get_index()

static int ch347_speed_get_index ( int  khz,
int *  speed_idx 
)
static

converts the requested speed to a CH347 speed index

If possible, selects the highest supported speed not exceeding the requested one.

Parameters
khzSpeed in kHz
speed_idxOutput of the CH347 speed index (JTAG) or clock divisor (SWD)
Returns
ERROR_OK at success; ERROR_FAIL if khz is zero

Definition at line 2108 of file ch347.c.

◆ ch347_speed_set()

static int ch347_speed_set ( int  speed_index)
static

Initializes the JTAG interface and set CH347 TCK frequency.

Parameters
speed_indexspeed index for JTAG_INIT command
Returns
Success returns ERROR_OK, failed returns ERROR_FAIL

Definition at line 2035 of file ch347.c.

References ch347_adapter_set_speed(), ch347_swd_init_cmd(), ERROR_OK, LOG_ERROR, and swd_mode.

◆ ch347_srst_enable()

static int ch347_srst_enable ( void  )
static

Enable the dedicated SRST output on CH347F devices.

The 0xE2 command and its payload come from WCH's fork, commit 4fd5026aabb6 ("feat: Add CH347F SRST configuration"): https://github.com/WCHSoftGroup/ch347/commit/4fd5026aabb62b941317b5d156d0eeaef4c43108 That packet is {0xe2, 0x08, 0x00, 0x00, 0x81, ...}. The command queue adds the command and the length, so only the eight payload bytes are sent here. Bit 0 of the second payload byte selects the mode: 0x81 enables it and 0x80 disables it. Disabling does not tristate pin 8, so 0x80 is never sent.

The byte read back from PIN_SET has no documented meaning, so it is logged and does not decide whether initialization succeeded.

Pin 8 strongly drives low after the adapter powers up. PIN_SET only selects the dedicated output; a JTAG pin byte should follow to set the level.

Returns
ERROR_OK on success, or the original error code when the command cannot be transmitted

Definition at line 1099 of file ch347.c.

References ARRAY_SIZE, CH347_CMD_PIN_SET, ch347_cmd_start_next(), ch347_dedicated_srst_enabled, ch347_scratchpad_add_bytes(), ch347_single_read_get_byte(), ERROR_OK, and LOG_DEBUG.

Referenced by ch347_init().

◆ ch347_swd_init()

◆ ch347_swd_init_cmd()

static int ch347_swd_init_cmd ( uint8_t  clock_divisor)
static

swd init function

Parameters
clock_divisorDivisor of base SWD frequency 1 MHz or 0 for fast 5 MHz clock
Returns
ERROR_OK on success

Definition at line 2007 of file ch347.c.

References ARRAY_SIZE, ch347_cmd_start_next(), CH347_CMD_SWD_INIT, ch347_scratchpad_add_bytes(), ch347_single_read_get_byte(), ch347_swd_context::clk_divisor, ERROR_OK, and LOG_DEBUG.

Referenced by ch347_init(), and ch347_speed_set().

◆ ch347_swd_queue_cmd()

◆ ch347_swd_queue_flush()

◆ ch347_swd_read_reg()

static int ch347_swd_read_reg ( uint8_t  cmd,
uint32_t *  value,
uint32_t  ap_delay_clk 
)
static

Definition at line 2729 of file ch347.c.

References ch347_swd_queue_cmd(), cmd, SWD_CMD_RNW, and ch347_swd_io::value.

◆ ch347_swd_run_queue()

static int ch347_swd_run_queue ( void  )
static

◆ ch347_swd_run_queue_inner()

◆ ch347_swd_send_idle()

◆ ch347_swd_switch_out()

◆ ch347_swd_switch_seq()

◆ ch347_swd_write_reg()

static int ch347_swd_write_reg ( uint8_t  cmd,
uint32_t  value,
uint32_t  ap_delay_clk 
)
static

Definition at line 2735 of file ch347.c.

◆ ch347_trst_gpio()

static unsigned int ch347_trst_gpio ( void  )
inlinestatic

GPIO number of the TRST pin.

Returns
GPIO number of the TRST pin of the chip variant

Definition at line 1433 of file ch347.c.

References ch347, CH347F, CH347F_TRST_GPIO, CH347T_TRST_GPIO, and ch347_info::chip_variant.

Referenced by ch347_init().

◆ ch347_write_data()

static int ch347_write_data ( uint8_t *  data,
int *  length 
)
static

writes data to the CH347 via libusb driver

Parameters
dataPoint to the data buffer
lengthData length in and out
Returns
ERROR_OK at success

Definition at line 419 of file ch347.c.

References CH347_EPOUT, ch347_handle, ERROR_OK, jtag_libusb_bulk_write(), length, log_buf_dump(), LOG_DEBUG_IO, LOG_ERROR, LOG_LEVEL_IS, LOG_LVL_DEBUG_IO, and USB_WRITE_TIMEOUT.

Referenced by ch347_cmd_transmit_queue_inner().

◆ ch347_write_spec_seq()

static void ch347_write_spec_seq ( const uint8_t *  out,
uint8_t  out_len 
)
static

◆ ch347_write_swd_reg()

static void ch347_write_swd_reg ( uint8_t  cmd,
const uint32_t  out 
)
static

◆ log_buf_dump()

static void log_buf_dump ( const uint8_t *  data,
unsigned int  size,
bool  recv 
)
static

Variable Documentation

◆ ch347

◆ ch347_adapter_driver

struct adapter_driver ch347_adapter_driver
Initial value:
= {
.name = "ch347",
.transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
.transport_preferred_id = TRANSPORT_JTAG,
.init = ch347_init,
.quit = ch347_quit,
.reset = ch347_reset,
.speed = ch347_speed_set,
.speed_div = ch347_speed_get,
.jtag_ops = &ch347_interface,
.swd_ops = &ch347_swd,
}
static int ch347_quit(void)
CH347 Device Release Function.
Definition: ch347.c:1919
static const struct command_registration ch347_command_handlers[]
Definition: ch347.c:2149
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
static int ch347_speed_get(int speed_idx, int *khz)
returns the speed in kHz for the given speed index
Definition: ch347.c:2080
static struct jtag_interface ch347_interface
Definition: ch347.c:2749
static int ch347_init(void)
CH347 Initialization function.
Definition: ch347.c:2164
static int ch347_speed_set(int speed_index)
Initializes the JTAG interface and set CH347 TCK frequency.
Definition: ch347.c:2035
static int ch347_reset(int trst, int srst)
Control (assert/deassert) the signals SRST and TRST on the interface.
Definition: ch347.c:1683
#define TRANSPORT_SWD
Definition: transport.h:20
#define TRANSPORT_JTAG
Definition: transport.h:19

Definition at line 2735 of file ch347.c.

◆ ch347_command_handlers

const struct command_registration ch347_command_handlers[]
static
Initial value:
= {
{
.name = "ch347",
.mode = COMMAND_ANY,
.help = "perform ch347 management",
.usage = "",
},
}
#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

Definition at line 2108 of file ch347.c.

◆ ch347_dedicated_srst_enabled

bool ch347_dedicated_srst_enabled
static

Definition at line 275 of file ch347.c.

Referenced by ch347_init(), ch347_scratchpad_add_pin_byte(), and ch347_srst_enable().

◆ ch347_handle

struct libusb_device_handle* ch347_handle
static

Definition at line 277 of file ch347.c.

Referenced by ch347_open_device(), ch347_quit(), ch347_read_data(), and ch347_write_data().

◆ ch347_interface

struct jtag_interface ch347_interface
static
Initial value:
= {
.supported = DEBUG_CAP_TMS_SEQ,
.execute_queue = ch347_execute_queue,
}
static int ch347_execute_queue(struct jtag_command *cmd_queue)
Executes the command queue.
Definition: ch347.c:1719
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:199

Definition at line 2735 of file ch347.c.

◆ ch347_larger_pack_clock_speeds

const int ch347_larger_pack_clock_speeds[]
static
Initial value:
= {
469,
938,
1875,
3750,
7500,
15000,
30000,
60000
}

Definition at line 181 of file ch347.c.

Referenced by ch347_speed_get().

◆ ch347_standard_pack_clock_speeds

const int ch347_standard_pack_clock_speeds[]
static
Initial value:
= {
1875,
3750,
7500,
15000,
30000,
60000
}

Definition at line 171 of file ch347.c.

Referenced by ch347_speed_get().

◆ ch347_swd

const struct swd_driver ch347_swd
static
Initial value:
= {
.init = ch347_swd_init,
.switch_seq = ch347_swd_switch_seq,
.read_reg = ch347_swd_read_reg,
.write_reg = ch347_swd_write_reg,
}
static int ch347_swd_run_queue(void)
Definition: ch347.c:2622
static int ch347_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: ch347.c:2735
static int ch347_swd_switch_seq(enum swd_special_seq seq)
Definition: ch347.c:2699
static int ch347_swd_init(void)
Initialization for the swd mode.
Definition: ch347.c:2256
static int ch347_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: ch347.c:2729

Definition at line 2735 of file ch347.c.

◆ ch347_swd_context

Definition at line 181 of file ch347.c.

◆ ch347f_gpio_to_bit_op

const struct ch347_bit_op ch347f_gpio_to_bit_op[GPIO_CNT_EXT]
static
Initial value:
= {
[CH347F_TRST_GPIO] = { .bit = TRST_H, .ptr = &ch347.trst_pin },
[4] = { .bit = TCK_H, .jtag_use = true, .swd_use = true, .ptr = &ch347.tck_pin },
[5] = { .jtag_use = true },
[6] = { .bit = TDI_H, .jtag_use = true, .ptr = &ch347.tdi_pin },
[7] = { .bit = TMS_H, .jtag_use = true, .swd_use = true, .ptr = &ch347.tms_pin },
[CH347F_SRST_FAKE_GPIO] = { .bit = SRST_H, .ptr = &ch347.srst_pin},
}
#define TMS_H
Definition: ch347.c:67
#define TDI_H
Definition: ch347.c:65
#define SRST_H
Definition: ch347.c:73
#define TCK_H
Definition: ch347.c:69
#define CH347F_TRST_GPIO
Definition: ch347.c:81
static struct ch347_info ch347
Definition: ch347.c:276
#define CH347F_SRST_FAKE_GPIO
Definition: ch347.c:79
#define TRST_H
Definition: ch347.c:71
int tms_pin
Definition: ch347.c:209
int tdi_pin
Definition: ch347.c:210
int trst_pin
Definition: ch347.c:212
int tck_pin
Definition: ch347.c:211
int srst_pin
Definition: ch347.c:213

Definition at line 277 of file ch347.c.

Referenced by ch347_init(), and ch347_set_gpio().

◆ ch347t_gpio_to_bit_op

const struct ch347_bit_op ch347t_gpio_to_bit_op[GPIO_CNT_EXT]
static
Initial value:
= {
[0] = { .bit = TCK_H, .jtag_use = true, .swd_use = true, .ptr = &ch347.tck_pin },
[1] = { .jtag_use = true },
[2] = { .bit = TMS_H, .jtag_use = true, .swd_use = true, .ptr = &ch347.tms_pin },
[CH347T_TRST_GPIO] = { .bit = TRST_H, .ptr = &ch347.trst_pin },
}
#define CH347T_TRST_GPIO
Definition: ch347.c:80

Definition at line 277 of file ch347.c.

Referenced by ch347_init(), and ch347_set_gpio().

◆ default_ch347_pids

const uint16_t default_ch347_pids[]
static
Initial value:
#define DEFAULT_CH347F_PRODUCT_ID
Definition: ch347.c:146
#define DEFAULT_OTHER_PRODUCT_ID
Definition: ch347.c:147
#define DEFAULT_CH347T_PRODUCT_ID
Definition: ch347.c:145

Definition at line 269 of file ch347.c.

Referenced by ch347_open_device().

◆ default_ch347_vids

const uint16_t default_ch347_vids[] = {DEFAULT_VENDOR_ID, DEFAULT_VENDOR_ID, DEFAULT_VENDOR_ID, 0}
static

Definition at line 268 of file ch347.c.

Referenced by ch347_open_device().

◆ swd_mode

bool swd_mode
static