1 /***************************************************************************
3 * Copyright (C) 2012 by Spencer Oliver *
4 * spen@spen-soft.co.uk *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
20 ***************************************************************************/
26 /* project specific includes */
27 #include <helper/binarybuffer.h>
28 #include <jtag/interface.h>
29 #include <jtag/hla/hla_layout.h>
30 #include <jtag/hla/hla_transport.h>
31 #include <jtag/hla/hla_interface.h>
32 #include <target/target.h>
34 #include <target/cortex_m.h>
38 #define ICDI_WRITE_ENDPOINT 0x02
39 #define ICDI_READ_ENDPOINT 0x83
41 #define ICDI_WRITE_TIMEOUT 1000
42 #define ICDI_READ_TIMEOUT 1000
43 #define ICDI_PACKET_SIZE 2048
45 #define PACKET_START "$"
46 #define PACKET_END "#"
48 struct icdi_usb_handle_s
{
49 libusb_context
*usb_ctx
;
50 libusb_device_handle
*usb_dev
;
56 uint32_t max_rw_packet
; /* max X packet (read/write memory) transfers */
59 static int icdi_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
60 uint32_t count
, uint8_t *buffer
);
61 static int icdi_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
62 uint32_t count
, const uint8_t *buffer
);
64 static int remote_escape_output(const char *buffer
, int len
, char *out_buf
, int *out_len
, int out_maxlen
)
66 int input_index
, output_index
;
70 for (input_index
= 0; input_index
< len
; input_index
++) {
72 char b
= buffer
[input_index
];
74 if (b
== '$' || b
== '#' || b
== '}' || b
== '*') {
75 /* These must be escaped. */
76 if (output_index
+ 2 > out_maxlen
)
78 out_buf
[output_index
++] = '}';
79 out_buf
[output_index
++] = b
^ 0x20;
81 if (output_index
+ 1 > out_maxlen
)
83 out_buf
[output_index
++] = b
;
87 *out_len
= input_index
;
91 static int remote_unescape_input(const char *buffer
, int len
, char *out_buf
, int out_maxlen
)
93 int input_index
, output_index
;
99 for (input_index
= 0; input_index
< len
; input_index
++) {
101 char b
= buffer
[input_index
];
103 if (output_index
+ 1 > out_maxlen
)
104 LOG_ERROR("Received too much data from the target.");
107 out_buf
[output_index
++] = b
^ 0x20;
112 out_buf
[output_index
++] = b
;
116 LOG_ERROR("Unmatched escape character in target response.");
121 static int icdi_send_packet(void *handle
, int len
)
123 unsigned char cksum
= 0;
124 struct icdi_usb_handle_s
*h
;
125 int result
, retry
= 0;
128 assert(handle
!= NULL
);
129 h
= (struct icdi_usb_handle_s
*)handle
;
131 /* check we have a large enough buffer for checksum "#00" */
132 if (len
+ 3 > h
->max_packet
) {
133 LOG_ERROR("packet buffer too small");
137 /* calculate checksum - offset start of packet */
138 for (int i
= 1; i
< len
; i
++)
139 cksum
+= h
->write_buffer
[i
];
141 len
+= sprintf(&h
->write_buffer
[len
], PACKET_END
"%02x", cksum
);
143 #ifdef _DEBUG_USB_COMMS_
145 char ch
= h
->write_buffer
[1];
146 if (ch
== 'x' || ch
== 'X')
147 LOG_DEBUG("writing packet: <binary>");
149 memcpy(buffer
, h
->write_buffer
, len
>= 50 ? 50-1 : len
);
151 LOG_DEBUG("writing packet: %s", buffer
);
157 result
= libusb_bulk_transfer(h
->usb_dev
, ICDI_WRITE_ENDPOINT
, (unsigned char *)h
->write_buffer
, len
,
158 &transferred
, ICDI_WRITE_TIMEOUT
);
159 if (result
!= 0 || transferred
!= len
) {
160 LOG_DEBUG("Error TX Data %d", result
);
164 /* check that the client got the message ok, or shall we resend */
165 result
= libusb_bulk_transfer(h
->usb_dev
, ICDI_READ_ENDPOINT
, (unsigned char *)h
->read_buffer
, h
->max_packet
,
166 &transferred
, ICDI_READ_TIMEOUT
);
167 if (result
!= 0 || transferred
< 1) {
168 LOG_DEBUG("Error RX Data %d", result
);
172 #ifdef _DEBUG_USB_COMMS_
173 LOG_DEBUG("received reply: '%c' : count %d", h
->read_buffer
[0], transferred
);
176 if (h
->read_buffer
[0] == '-') {
177 LOG_DEBUG("Resending packet %d", ++retry
);
179 if (h
->read_buffer
[0] != '+')
180 LOG_DEBUG("Unexpected Reply from ICDI: %c", h
->read_buffer
[0]);
185 LOG_DEBUG("maximum nack retries attempted");
191 h
->read_count
= transferred
;
195 /* read reply from icdi */
196 result
= libusb_bulk_transfer(h
->usb_dev
, ICDI_READ_ENDPOINT
, (unsigned char *)h
->read_buffer
+ h
->read_count
,
197 h
->max_packet
- h
->read_count
, &transferred
, ICDI_READ_TIMEOUT
);
199 #ifdef _DEBUG_USB_COMMS_
200 LOG_DEBUG("received data: count %d", transferred
);
203 /* check for errors but retry for timeout */
206 if (result
== LIBUSB_ERROR_TIMEOUT
) {
207 LOG_DEBUG("Error RX timeout %d", result
);
209 LOG_DEBUG("Error RX Data %d", result
);
214 h
->read_count
+= transferred
;
216 /* we need to make sure we have a full packet, including checksum */
217 if (h
->read_count
> 5) {
219 /* check that we have received an packet delimiter
220 * we do not validate the checksum
221 * reply should contain $...#AA - so we check for # */
222 if (h
->read_buffer
[h
->read_count
- 3] == '#')
227 LOG_DEBUG("maximum data retries attempted");
235 static int icdi_send_cmd(void *handle
, const char *cmd
)
237 struct icdi_usb_handle_s
*h
;
238 h
= (struct icdi_usb_handle_s
*)handle
;
240 int cmd_len
= snprintf(h
->write_buffer
, h
->max_packet
, PACKET_START
"%s", cmd
);
241 return icdi_send_packet(handle
, cmd_len
);
244 static int icdi_send_remote_cmd(void *handle
, const char *data
)
246 struct icdi_usb_handle_s
*h
;
247 h
= (struct icdi_usb_handle_s
*)handle
;
249 size_t cmd_len
= sprintf(h
->write_buffer
, PACKET_START
"qRcmd,");
250 cmd_len
+= hexify(h
->write_buffer
+ cmd_len
, data
, 0, h
->max_packet
- cmd_len
);
252 return icdi_send_packet(handle
, cmd_len
);
255 static int icdi_get_cmd_result(void *handle
)
257 struct icdi_usb_handle_s
*h
;
261 assert(handle
!= NULL
);
262 h
= (struct icdi_usb_handle_s
*)handle
;
265 ch
= h
->read_buffer
[offset
++];
266 if (offset
> h
->read_count
)
270 if (memcmp("OK", h
->read_buffer
+ offset
, 2) == 0)
273 if (h
->read_buffer
[offset
] == 'E') {
276 if (unhexify(&result
, h
->read_buffer
+ offset
+ 1, 1) != 1)
281 /* for now we assume everything else is ok */
285 static int icdi_usb_idcode(void *handle
, uint32_t *idcode
)
291 static int icdi_usb_write_debug_reg(void *handle
, uint32_t addr
, uint32_t val
)
293 return icdi_usb_write_mem(handle
, addr
, 4, 1, (uint8_t *)&val
);
296 static enum target_state
icdi_usb_state(void *handle
)
299 struct icdi_usb_handle_s
*h
;
302 h
= (struct icdi_usb_handle_s
*)handle
;
304 result
= icdi_usb_read_mem(h
, DCB_DHCSR
, 4, 1, (uint8_t *)&dhcsr
);
305 if (result
!= ERROR_OK
)
306 return TARGET_UNKNOWN
;
309 return TARGET_HALTED
;
311 return TARGET_RUNNING
;
314 static int icdi_usb_version(void *handle
)
316 struct icdi_usb_handle_s
*h
;
317 h
= (struct icdi_usb_handle_s
*)handle
;
321 /* get info about icdi */
322 int result
= icdi_send_remote_cmd(handle
, "version");
323 if (result
!= ERROR_OK
)
326 if (h
->read_count
< 8) {
327 LOG_ERROR("Invalid Reply Received");
332 if (unhexify(version
, h
->read_buffer
+ 2, 4) != 4) {
333 LOG_WARNING("unable to get ICDI version");
337 /* null terminate and print info */
340 LOG_INFO("ICDI Firmware version: %s", version
);
345 static int icdi_usb_query(void *handle
)
349 struct icdi_usb_handle_s
*h
;
350 h
= (struct icdi_usb_handle_s
*)handle
;
352 result
= icdi_send_cmd(handle
, "qSupported");
353 if (result
!= ERROR_OK
)
357 result
= icdi_get_cmd_result(handle
);
358 if (result
!= ERROR_OK
) {
359 LOG_ERROR("query supported failed: 0x%x", result
);
363 /* from this we can get the max packet supported */
365 /* query packet buffer size */
366 char *offset
= strstr(h
->read_buffer
, "PacketSize");
371 max_packet
= strtoul(offset
+ 11, &separator
, 16);
373 LOG_ERROR("invalid max packet, using defaults");
375 h
->max_packet
= max_packet
;
376 LOG_DEBUG("max packet supported : %" PRIu32
" bytes", h
->max_packet
);
380 /* if required re allocate packet buffer */
381 if (h
->max_packet
!= ICDI_PACKET_SIZE
) {
382 h
->read_buffer
= realloc(h
->read_buffer
, h
->max_packet
);
383 h
->write_buffer
= realloc(h
->write_buffer
, h
->max_packet
);
384 if (h
->read_buffer
== 0 || h
->write_buffer
== 0) {
385 LOG_ERROR("unable to reallocate memory");
390 /* set extended mode */
391 result
= icdi_send_cmd(handle
, "!");
392 if (result
!= ERROR_OK
)
396 result
= icdi_get_cmd_result(handle
);
397 if (result
!= ERROR_OK
) {
398 LOG_ERROR("unable to enable extended mode: 0x%x", result
);
405 static int icdi_usb_reset(void *handle
)
407 /* we do this in hla_target.c */
411 static int icdi_usb_assert_srst(void *handle
, int srst
)
413 /* TODO not supported yet */
414 return ERROR_COMMAND_NOTFOUND
;
417 static int icdi_usb_run(void *handle
)
421 /* resume target at current address */
422 result
= icdi_send_cmd(handle
, "c");
423 if (result
!= ERROR_OK
)
427 result
= icdi_get_cmd_result(handle
);
428 if (result
!= ERROR_OK
) {
429 LOG_ERROR("continue failed: 0x%x", result
);
436 static int icdi_usb_halt(void *handle
)
440 /* this query halts the target ?? */
441 result
= icdi_send_cmd(handle
, "?");
442 if (result
!= ERROR_OK
)
446 result
= icdi_get_cmd_result(handle
);
447 if (result
!= ERROR_OK
) {
448 LOG_ERROR("halt failed: 0x%x", result
);
455 static int icdi_usb_step(void *handle
)
459 /* step target at current address */
460 result
= icdi_send_cmd(handle
, "s");
461 if (result
!= ERROR_OK
)
465 result
= icdi_get_cmd_result(handle
);
466 if (result
!= ERROR_OK
) {
467 LOG_ERROR("step failed: 0x%x", result
);
474 static int icdi_usb_read_regs(void *handle
)
476 /* currently unsupported */
480 static int icdi_usb_read_reg(void *handle
, int num
, uint32_t *val
)
483 struct icdi_usb_handle_s
*h
;
486 h
= (struct icdi_usb_handle_s
*)handle
;
488 snprintf(cmd
, sizeof(cmd
), "p%x", num
);
489 result
= icdi_send_cmd(handle
, cmd
);
490 if (result
!= ERROR_OK
)
494 result
= icdi_get_cmd_result(handle
);
495 if (result
!= ERROR_OK
) {
496 LOG_ERROR("register read failed: 0x%x", result
);
501 if (unhexify((char *)val
, h
->read_buffer
+ 2, 4) != 4) {
502 LOG_ERROR("failed to convert result");
509 static int icdi_usb_write_reg(void *handle
, int num
, uint32_t val
)
514 int cmd_len
= snprintf(cmd
, sizeof(cmd
), "P%x=", num
);
515 hexify(cmd
+ cmd_len
, (char *)&val
, 4, sizeof(cmd
));
517 result
= icdi_send_cmd(handle
, cmd
);
518 if (result
!= ERROR_OK
)
522 result
= icdi_get_cmd_result(handle
);
523 if (result
!= ERROR_OK
) {
524 LOG_ERROR("register write failed: 0x%x", result
);
531 static int icdi_usb_read_mem_int(void *handle
, uint32_t addr
, uint32_t len
, uint8_t *buffer
)
534 struct icdi_usb_handle_s
*h
;
537 h
= (struct icdi_usb_handle_s
*)handle
;
539 snprintf(cmd
, sizeof(cmd
), "x%x,%x", addr
, len
);
540 result
= icdi_send_cmd(handle
, cmd
);
541 if (result
!= ERROR_OK
)
545 result
= icdi_get_cmd_result(handle
);
546 if (result
!= ERROR_OK
) {
547 LOG_ERROR("memory read failed: 0x%x", result
);
552 int read_len
= remote_unescape_input(h
->read_buffer
+ 5, h
->read_count
- 8, (char *)buffer
, len
);
553 if (read_len
!= (int)len
) {
554 LOG_ERROR("read more bytes than expected: actual 0x%" PRIx32
" expected 0x%" PRIx32
, read_len
, len
);
561 static int icdi_usb_write_mem_int(void *handle
, uint32_t addr
, uint32_t len
, const uint8_t *buffer
)
564 struct icdi_usb_handle_s
*h
;
566 h
= (struct icdi_usb_handle_s
*)handle
;
568 size_t cmd_len
= snprintf(h
->write_buffer
, h
->max_packet
, PACKET_START
"X%x,%x:", addr
, len
);
571 cmd_len
+= remote_escape_output((char *)buffer
, len
, h
->write_buffer
+ cmd_len
,
572 &out_len
, h
->max_packet
- cmd_len
);
574 if (out_len
< (int)len
) {
575 /* for now issue a error as we have no way of allocating a larger buffer */
576 LOG_ERROR("memory buffer too small: requires 0x%" PRIx32
" actual 0x%" PRIx32
, out_len
, len
);
580 result
= icdi_send_packet(handle
, cmd_len
);
581 if (result
!= ERROR_OK
)
585 result
= icdi_get_cmd_result(handle
);
586 if (result
!= ERROR_OK
) {
587 LOG_ERROR("memory write failed: 0x%x", result
);
594 static int icdi_usb_read_mem(void *handle
, uint32_t addr
, uint32_t size
,
595 uint32_t count
, uint8_t *buffer
)
597 int retval
= ERROR_OK
;
598 struct icdi_usb_handle_s
*h
= (struct icdi_usb_handle_s
*)handle
;
599 uint32_t bytes_remaining
;
601 /* calculate byte count */
606 bytes_remaining
= h
->max_rw_packet
;
607 if (count
< bytes_remaining
)
608 bytes_remaining
= count
;
610 retval
= icdi_usb_read_mem_int(handle
, addr
, bytes_remaining
, buffer
);
611 if (retval
!= ERROR_OK
)
614 buffer
+= bytes_remaining
;
615 addr
+= bytes_remaining
;
616 count
-= bytes_remaining
;
622 static int icdi_usb_write_mem(void *handle
, uint32_t addr
, uint32_t size
,
623 uint32_t count
, const uint8_t *buffer
)
625 int retval
= ERROR_OK
;
626 struct icdi_usb_handle_s
*h
= (struct icdi_usb_handle_s
*)handle
;
627 uint32_t bytes_remaining
;
629 /* calculate byte count */
634 bytes_remaining
= h
->max_rw_packet
;
635 if (count
< bytes_remaining
)
636 bytes_remaining
= count
;
638 retval
= icdi_usb_write_mem_int(handle
, addr
, bytes_remaining
, buffer
);
639 if (retval
!= ERROR_OK
)
642 buffer
+= bytes_remaining
;
643 addr
+= bytes_remaining
;
644 count
-= bytes_remaining
;
650 static int icdi_usb_close(void *handle
)
652 struct icdi_usb_handle_s
*h
;
654 h
= (struct icdi_usb_handle_s
*)handle
;
657 libusb_close(h
->usb_dev
);
660 libusb_exit(h
->usb_ctx
);
663 free(h
->read_buffer
);
666 free(h
->write_buffer
);
673 static int icdi_usb_open(struct hl_interface_param_s
*param
, void **fd
)
676 struct icdi_usb_handle_s
*h
;
678 LOG_DEBUG("icdi_usb_open");
680 h
= calloc(1, sizeof(struct icdi_usb_handle_s
));
683 LOG_ERROR("unable to allocate memory");
687 LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param
->transport
,
688 param
->vid
, param
->pid
);
690 if (libusb_init(&h
->usb_ctx
) != 0) {
691 LOG_ERROR("libusb init failed");
695 h
->usb_dev
= libusb_open_device_with_vid_pid(h
->usb_ctx
, param
->vid
, param
->pid
);
697 LOG_ERROR("open failed");
701 if (libusb_claim_interface(h
->usb_dev
, 2)) {
702 LOG_DEBUG("claim interface failed");
706 /* check if mode is supported */
709 switch (param
->transport
) {
711 /* TODO place holder as swd is not currently supported */
712 case HL_TRANSPORT_SWD
:
714 case HL_TRANSPORT_JTAG
:
721 if (retval
!= ERROR_OK
) {
722 LOG_ERROR("mode (transport) not supported by device");
726 /* allocate buffer */
727 h
->read_buffer
= malloc(ICDI_PACKET_SIZE
);
728 h
->write_buffer
= malloc(ICDI_PACKET_SIZE
);
729 h
->max_packet
= ICDI_PACKET_SIZE
;
731 if (h
->read_buffer
== 0 || h
->write_buffer
== 0) {
732 LOG_DEBUG("malloc failed");
736 /* query icdi version etc */
737 retval
= icdi_usb_version(h
);
738 if (retval
!= ERROR_OK
)
741 /* query icdi support */
742 retval
= icdi_usb_query(h
);
743 if (retval
!= ERROR_OK
)
748 /* set the max target read/write buffer in bytes
749 * as we are using gdb binary packets to transfer memory we have to
750 * reserve half the buffer for any possible escape chars plus
751 * at least 64 bytes for the gdb packet header */
752 h
->max_rw_packet
= (((h
->max_packet
- 64) / 4) * 4) / 2;
762 struct hl_layout_api_s icdi_usb_layout_api
= {
763 .open
= icdi_usb_open
,
764 .close
= icdi_usb_close
,
765 .idcode
= icdi_usb_idcode
,
766 .state
= icdi_usb_state
,
767 .reset
= icdi_usb_reset
,
768 .assert_srst
= icdi_usb_assert_srst
,
770 .halt
= icdi_usb_halt
,
771 .step
= icdi_usb_step
,
772 .read_regs
= icdi_usb_read_regs
,
773 .read_reg
= icdi_usb_read_reg
,
774 .write_reg
= icdi_usb_write_reg
,
775 .read_mem
= icdi_usb_read_mem
,
776 .write_mem
= icdi_usb_write_mem
,
777 .write_debug_reg
= icdi_usb_write_debug_reg
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)