43 #define CONTROL_TYPE_READ 0x01
44 #define CONTROL_TYPE_WRITE 0x02
46 #define CONTROL_COMMAND_PROGRAM 0x07
48 #define CONTROL_MODE_POLL_PROGRAMMER_STATUS 0x01
49 #define CONTROL_MODE_RESET_TARGET 0x04
50 #define CONTROL_MODE_SET_PROGRAMMER_PROTOCOL 0x40
51 #define CONTROL_MODE_SYNCHRONIZE_TRANSFER 0x41
52 #define CONTROL_MODE_ACQUIRE_SWD_TARGET 0x42
53 #define CONTROL_MODE_SEND_SWD_SEQUENCE 0x43
55 #define PROTOCOL_JTAG 0x00
56 #define PROTOCOL_SWD 0x01
58 #define DEVICE_PSOC4 0x00
59 #define DEVICE_PSOC3 0x01
60 #define DEVICE_UNKNOWN 0x02
61 #define DEVICE_PSOC5 0x03
63 #define ACQUIRE_MODE_RESET 0x00
64 #define ACQUIRE_MODE_POWER_CYCLE 0x01
66 #define SEQUENCE_LINE_RESET 0x00
67 #define SEQUENCE_JTAG_TO_SWD 0x01
69 #define PROGRAMMER_NOK_NACK 0x00
70 #define PROGRAMMER_OK_ACK 0x01
72 #define HID_TYPE_WRITE 0x00
73 #define HID_TYPE_READ 0x01
74 #define HID_TYPE_START 0x02
76 #define HID_COMMAND_POWER 0x80
77 #define HID_COMMAND_VERSION 0x81
78 #define HID_COMMAND_RESET 0x82
79 #define HID_COMMAND_CONFIGURE 0x8f
80 #define HID_COMMAND_BOOTLOADER 0xa0
99 #define SWD_MAX_BUFFER_LENGTH 300
135 uint8_t *data,
size_t data_length);
143 uint8_t max_attempts);
157 return (major << 8) | minor;
171 LOG_ERROR(
"Can't find a KitProg device! Please check device connections and permissions.");
183 LOG_WARNING(
"KitProg firmware versions below v2.14 do not support sending JTAG to SWD sequences. These sequences will be substituted with SWD line resets.");
216 LOG_ERROR(
"Failed to allocate memory for the packet buffer");
223 LOG_ERROR(
"Failed to allocate memory for the SWD transfer queue");
247 const uint8_t str_index = 128;
248 char desc_string[256+1];
251 str_index, (
unsigned char *)desc_string,
sizeof(desc_string)-1);
253 LOG_ERROR(
"libusb_get_string_descriptor_ascii() failed with %d", retval);
258 desc_string[retval] =
'\0';
263 LOG_ERROR(
"Failed to allocate memory for the serial number");
275 const uint16_t vids[] = {
VID, 0 };
276 const uint16_t pids[] = {
PID, 0 };
279 LOG_ERROR(
"Failed to open or find the device");
285 LOG_WARNING(
"Failed to get KitProg serial number");
289 wchar_t *hid_serial = calloc(len + 1,
sizeof(
wchar_t));
291 LOG_ERROR(
"Failed to allocate memory for the serial number");
296 LOG_ERROR(
"Failed to convert serial number");
304 LOG_ERROR(
"Failed to open KitBridge (HID) interface");
310 LOG_ERROR(
"Failed to claim KitProg Programmer (bulk transfer) interface");
330 uint8_t *data,
size_t data_length)
345 }
else if (ret < 0) {
358 unsigned char data[64];
376 unsigned char data[64];
396 LOG_ERROR(
"Failed to get KitProg version");
405 LOG_ERROR(
"Failed to get target voltage");
418 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
421 protocol, &
status, 1, 0, &transferred);
423 if (retval !=
ERROR_OK || transferred == 0) {
429 LOG_DEBUG(
"Programmer did not respond OK");
442 for (
int i = 0; (i < 3) && (transferred == 0); i++) {
444 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
447 0, &
status, 1, 0, &transferred);
451 if (transferred == 0) {
457 LOG_DEBUG(
"Programmer did not respond OK");
470 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
473 0, &
status, 1, 0, &transferred);
475 if (retval !=
ERROR_OK || transferred == 0) {
481 LOG_DEBUG(
"Programmer did not respond OK");
489 uint8_t max_attempts)
495 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
498 (max_attempts << 8) | (acquire_mode << 4) | psoc_type, &
status, 1, 0, &transferred);
500 if (retval !=
ERROR_OK || transferred == 0) {
506 LOG_DEBUG(
"Programmer did not respond OK");
519 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
522 0, &
status, 1, 0, &transferred);
524 if (retval !=
ERROR_OK || transferred == 0) {
530 LOG_DEBUG(
"Programmer did not respond OK");
543 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
546 0, &
status, 1, 0, &transferred);
548 if (retval !=
ERROR_OK || transferred == 0) {
554 LOG_DEBUG(
"Programmer did not respond OK");
567 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
570 seq_type, &
status, 1, 0, &transferred);
572 if (retval !=
ERROR_OK || transferred == 0) {
578 LOG_DEBUG(
"Programmer did not respond OK");
590 int acquire_count = 0;
604 for (
int i = 0; i < 2; i++) {
605 for (uint8_t j = 0; j <
sizeof(
devices) && acquire_count == i; j++) {
619 if (acquire_count < 2)
666 LOG_ERROR(
"Sequence %d not supported.", seq);
677 size_t read_count = 0;
678 size_t read_index = 0;
679 size_t write_count = 0;
712 LOG_DEBUG(
"refusing to enable sticky overrun detection");
724 buffer[write_count++] = (data) & 0xff;
725 buffer[write_count++] = (data >> 8) & 0xff;
726 buffer[write_count++] = (data >> 16) & 0xff;
727 buffer[write_count++] = (data >> 24) & 0xff;
735 write_count, 0, &ret)) {
753 if (read_count % 64 == 0)
754 read_count_workaround = read_count;
758 read_count_workaround, 1000, &ret)) {
764 if ((
unsigned int)ret > read_count)
765 read_index = ret - read_count;
781 uint8_t ack =
buffer[read_index] & 0x07;
825 LOG_ERROR(
"KitProg: Interface has no TRST");
841 LOG_ERROR(
"KitProg: Interface reset failed");
870 .handler = &kitprog_handle_info_command,
873 .help =
"show KitProg info",
876 .name =
"acquire_psoc",
877 .handler = &kitprog_handle_acquire_psoc_command,
880 .help =
"try to acquire a PSoC",
883 .name =
"init_acquire_psoc",
884 .handler = &kitprog_handle_init_acquire_psoc_command,
886 .help =
"try to acquire a PSoC during init",
896 .help =
"perform KitProg management",
static const struct device_t devices[]
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
void jtag_sleep(uint32_t us)
#define ERROR_JTAG_INIT_FAILED
static int kitprog_init(void)
static const struct command_registration kitprog_command_handlers[]
static struct kitprog * kitprog_handle
#define CONTROL_MODE_RESET_TARGET
static int kitprog_swd_sync(void)
#define CONTROL_MODE_ACQUIRE_SWD_TARGET
static void kitprog_usb_close(void)
static const struct swd_driver kitprog_swd
static const struct command_registration kitprog_subcommand_handlers[]
#define SEQUENCE_JTAG_TO_SWD
static int kitprog_acquire_psoc(uint8_t psoc_type, uint8_t acquire_mode, uint8_t max_attempts)
#define PROGRAMMER_NOK_NACK
#define CONTROL_MODE_SYNCHRONIZE_TRANSFER
#define SWD_MAX_BUFFER_LENGTH
#define CONTROL_COMMAND_PROGRAM
static int kitprog_swd_seq(uint8_t seq_type)
static int kitprog_usb_open(void)
static int kitprog_get_status(void)
#define PROGRAMMER_OK_ACK
static bool kitprog_init_acquire_psoc
#define SEQUENCE_LINE_RESET
static int mm_to_version(uint8_t major, uint8_t minor)
static int kitprog_swd_switch_seq(enum swd_special_seq seq)
COMMAND_HANDLER(kitprog_handle_info_command)
static int kitprog_get_version(void)
#define CONTROL_TYPE_READ
static struct pending_transfer_result * pending_transfers
static int kitprog_reset_target(void)
static int kitprog_hid_command(uint8_t *command, size_t command_length, uint8_t *data, size_t data_length)
static int kitprog_get_info(void)
#define HID_COMMAND_POWER
static int pending_transfer_count
static const char *const kitprog_transports[]
struct adapter_driver kitprog_adapter_driver
static int kitprog_set_protocol(uint8_t protocol)
static int kitprog_get_millivolts(void)
static void kitprog_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
static void kitprog_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
static int kitprog_generic_acquire(void)
static int kitprog_get_usb_serial(void)
#define ACQUIRE_MODE_RESET
static int kitprog_set_unknown(void)
static int pending_queue_len
static int kitprog_swd_init(void)
#define CONTROL_TYPE_WRITE
static int kitprog_swd_run_queue(void)
static int kitprog_reset(int trst, int srst)
static int kitprog_quit(void)
#define CONTROL_MODE_SET_PROGRAMMER_PROTOCOL
#define CONTROL_MODE_POLL_PROGRAMMER_STATUS
#define CONTROL_MODE_SEND_SWD_SEQUENCE
#define HID_COMMAND_VERSION
static void kitprog_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
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 LIBUSB_TIMEOUT_MS
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define ERROR_TIMEOUT_REACHED
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
struct libusb_device_handle * usb_handle
bool supports_jtag_to_swd
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
static uint32_t le_to_h_u32(const uint8_t *buf)