26 #define ICDI_WRITE_ENDPOINT 0x02
27 #define ICDI_READ_ENDPOINT 0x83
29 #define ICDI_WRITE_TIMEOUT (LIBUSB_TIMEOUT_MS)
30 #define ICDI_READ_TIMEOUT (LIBUSB_TIMEOUT_MS)
31 #define ICDI_PACKET_SIZE 2048
33 #define PACKET_START "$"
34 #define PACKET_END "#"
53 int input_index, output_index;
57 for (input_index = 0; input_index < len; input_index++) {
59 char b =
buffer[input_index];
61 if (b ==
'$' || b ==
'#' || b ==
'}' || b ==
'*') {
63 if (output_index + 2 > out_maxlen)
65 out_buf[output_index++] =
'}';
66 out_buf[output_index++] = b ^ 0x20;
68 if (output_index + 1 > out_maxlen)
70 out_buf[output_index++] = b;
74 *out_len = input_index;
80 int input_index, output_index;
86 for (input_index = 0; input_index < len; input_index++) {
88 char b =
buffer[input_index];
90 if (output_index + 1 > out_maxlen)
91 LOG_ERROR(
"Received too much data from the target.");
94 out_buf[output_index++] = b ^ 0x20;
99 out_buf[output_index++] = b;
103 LOG_ERROR(
"Unmatched escape character in target response.");
110 unsigned char cksum = 0;
112 int result, retry = 0;
124 for (
int i = 1; i < len; i++)
129 #ifdef _DEBUG_USB_COMMS_
132 if (ch ==
'x' || ch ==
'X')
145 if (result != 0 || transferred != len) {
153 if (result != 0 || transferred < 1) {
158 #ifdef _DEBUG_USB_COMMS_
163 LOG_DEBUG(
"Resending packet %d", ++retry);
171 LOG_DEBUG(
"maximum nack retries attempted");
185 #ifdef _DEBUG_USB_COMMS_
186 LOG_DEBUG(
"received data: count %d", transferred);
192 if (result == LIBUSB_ERROR_TIMEOUT) {
193 LOG_DEBUG(
"Error RX timeout %d", result);
213 LOG_DEBUG(
"maximum data retries attempted");
329 LOG_INFO(
"ICDI Firmware version: %s", version);
347 LOG_ERROR(
"query supported failed: 0x%x", result);
361 LOG_ERROR(
"invalid max packet, using defaults");
373 LOG_ERROR(
"unable to reallocate memory");
386 LOG_ERROR(
"unable to enable extended mode: 0x%x", result);
417 LOG_ERROR(
"continue failed: 0x%x", result);
474 snprintf(
cmd,
sizeof(
cmd),
"p%x", regsel);
482 LOG_ERROR(
"register read failed: 0x%x", result);
504 int cmd_len = snprintf(
cmd,
sizeof(
cmd),
"P%x=", regsel);
514 LOG_ERROR(
"register write failed: 0x%x", result);
527 snprintf(
cmd,
sizeof(
cmd),
"x%" PRIx32
",%" PRIx32,
addr, len);
535 LOG_ERROR(
"memory read failed: 0x%x", result);
541 if (read_len != (
int)len) {
542 LOG_ERROR(
"read more bytes than expected: actual 0x%x expected 0x%" PRIx32, read_len, len);
560 if (out_len < (
int)len) {
562 LOG_ERROR(
"memory buffer too small: requires 0x%x actual 0x%" PRIx32, out_len, len);
573 LOG_ERROR(
"memory write failed: 0x%x", result);
585 uint32_t bytes_remaining;
593 if (
count < bytes_remaining)
594 bytes_remaining =
count;
600 buffer += bytes_remaining;
601 addr += bytes_remaining;
602 count -= bytes_remaining;
613 uint32_t bytes_remaining;
621 if (
count < bytes_remaining)
622 bytes_remaining =
count;
628 buffer += bytes_remaining;
629 addr += bytes_remaining;
630 count -= bytes_remaining;
638 return !strcmp(targetname,
"cortex_m");
672 for (uint8_t i = 0; param->
vid[i] && param->
pid[i]; ++i)
685 if (libusb_claim_interface(h->
usb_dev, 2)) {
706 LOG_ERROR(
"mode (transport) not supported by device");
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t length)
Convert binary data into a string of hexadecimal pairs.
size_t unhexify(uint8_t *bin, const char *hex, size_t count)
Convert a string of hexadecimal pairs into its binary representation.
Support functions to access arbitrary bits in a byte array.
#define ERROR_COMMAND_NOTFOUND
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)
void jtag_libusb_close(struct libusb_device_handle *dev)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
target_addr_t addr
Start address to search for the control block.
size_t size
Size of the control block search area.
enum hl_transports transport
uint16_t pid[HLA_MAX_USB_IDS+1]
List of recognised PIDs.
uint16_t vid[HLA_MAX_USB_IDS+1]
List of recognised VIDs.
int(* open)(struct hl_interface_param *param, void **handle)
struct libusb_device_handle * usb_dev
#define ICDI_WRITE_TIMEOUT
static int remote_unescape_input(const char *buffer, int len, char *out_buf, int out_maxlen)
static int icdi_send_remote_cmd(void *handle, const char *data)
static int icdi_usb_read_mem(void *handle, uint32_t addr, uint32_t size, uint32_t count, uint8_t *buffer)
static int icdi_usb_override_target(const char *targetname)
static enum target_state icdi_usb_state(void *handle)
struct hl_layout_api icdi_usb_layout_api
static int icdi_usb_assert_srst(void *handle, int srst)
#define ICDI_WRITE_ENDPOINT
static int icdi_usb_query(void *handle)
static int icdi_get_cmd_result(void *handle)
static int icdi_usb_step(void *handle)
#define ICDI_READ_TIMEOUT
#define ICDI_READ_ENDPOINT
static int icdi_usb_read_mem_int(void *handle, uint32_t addr, uint32_t len, uint8_t *buffer)
static int icdi_usb_idcode(void *handle, uint32_t *idcode)
static int icdi_send_cmd(void *handle, const char *cmd)
static int remote_escape_output(const char *buffer, int len, char *out_buf, int *out_len, int out_maxlen)
static int icdi_usb_halt(void *handle)
static int icdi_usb_write_mem(void *handle, uint32_t addr, uint32_t size, uint32_t count, const uint8_t *buffer)
static int icdi_usb_close(void *handle)
static int icdi_usb_write_reg(void *handle, unsigned int regsel, uint32_t val)
static int icdi_usb_version(void *handle)
static int icdi_send_packet(void *handle, int len)
static int icdi_usb_write_mem_int(void *handle, uint32_t addr, uint32_t len, const uint8_t *buffer)
static int icdi_usb_read_regs(void *handle)
static int icdi_usb_run(void *handle)
static int icdi_usb_open(struct hl_interface_param *param, void **fd)
static int icdi_usb_reset(void *handle)
static int icdi_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
static int icdi_usb_read_reg(void *handle, unsigned int regsel, uint32_t *val)
static void h_u32_to_le(uint8_t *buf, uint32_t val)
static uint32_t le_to_h_u32(const uint8_t *buf)