OpenOCD
kitprog.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
5  * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
6  * *
7  * Copyright (C) 2008 by Spencer Oliver *
8  * spen@spen-soft.co.uk *
9  * *
10  * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
11  * plagnioj@jcrosoft.com *
12  * *
13  * Copyright (C) 2015 by Marc Schink *
14  * openocd-dev@marcschink.de *
15  * *
16  * Copyright (C) 2015 by Paul Fertser *
17  * fercerpav@gmail.com *
18  * *
19  * Copyright (C) 2015-2017 by Forest Crossman *
20  * cyrozap@gmail.com *
21  ***************************************************************************/
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <stdint.h>
28 
29 #include <hidapi.h>
30 
31 #include <jtag/interface.h>
32 #include <jtag/swd.h>
33 #include <jtag/commands.h>
34 
35 #include "libusb_helper.h"
36 
37 #define VID 0x04b4
38 #define PID 0xf139
39 
40 #define BULK_EP_IN 1
41 #define BULK_EP_OUT 2
42 
43 #define CONTROL_TYPE_READ 0x01
44 #define CONTROL_TYPE_WRITE 0x02
45 
46 #define CONTROL_COMMAND_PROGRAM 0x07
47 
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
54 
55 #define PROTOCOL_JTAG 0x00
56 #define PROTOCOL_SWD 0x01
57 
58 #define DEVICE_PSOC4 0x00
59 #define DEVICE_PSOC3 0x01
60 #define DEVICE_UNKNOWN 0x02
61 #define DEVICE_PSOC5 0x03
62 
63 #define ACQUIRE_MODE_RESET 0x00
64 #define ACQUIRE_MODE_POWER_CYCLE 0x01
65 
66 #define SEQUENCE_LINE_RESET 0x00
67 #define SEQUENCE_JTAG_TO_SWD 0x01
68 
69 #define PROGRAMMER_NOK_NACK 0x00
70 #define PROGRAMMER_OK_ACK 0x01
71 
72 #define HID_TYPE_WRITE 0x00
73 #define HID_TYPE_READ 0x01
74 #define HID_TYPE_START 0x02
75 
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
81 
82 /* 512 bytes seemed to work reliably.
83  * It works with both full queue of mostly reads or mostly writes.
84  *
85  * Unfortunately the commit 88f429ead019fd6df96ec15f0d897385f3cef0d0
86  * 5321: target/cortex_m: faster reading of all CPU registers
87  * revealed a serious Kitprog firmware problem:
88  * If the queue contains more than 63 transactions in the repeated pattern
89  * one write, two reads, the firmware fails badly.
90  * Sending 64 transactions makes the adapter to loose the connection with the
91  * device. Sending 65 or more transactions causes the adapter to stop
92  * receiving USB HID commands, next kitprog_hid_command() stops in hid_write().
93  *
94  * The problem was detected with KitProg v2.12 and v2.16.
95  * We can guess the problem is something like a buffer or stack overflow.
96  *
97  * Use shorter buffer as a workaround. 300 bytes (= 60 transactions) works.
98  */
99 #define SWD_MAX_BUFFER_LENGTH 300
100 
101 struct kitprog {
102  hid_device *hid_handle;
103  struct libusb_device_handle *usb_handle;
104  uint16_t packet_size;
105  uint16_t packet_index;
106  uint8_t *packet_buffer;
107  char *serial;
109  uint8_t minor_version;
110  uint8_t major_version;
111  uint16_t millivolts;
112 
114 };
115 
117  uint8_t cmd;
118  uint32_t data;
119  void *buffer;
120 };
121 
123 
126 
127 static int queued_retval;
128 
129 static struct kitprog *kitprog_handle;
130 
131 static int kitprog_usb_open(void);
132 static void kitprog_usb_close(void);
133 
134 static int kitprog_hid_command(uint8_t *command, size_t command_length,
135  uint8_t *data, size_t data_length);
136 static int kitprog_get_version(void);
137 static int kitprog_get_millivolts(void);
138 static int kitprog_get_info(void);
139 static int kitprog_set_protocol(uint8_t protocol);
140 static int kitprog_get_status(void);
141 static int kitprog_set_unknown(void);
142 static int kitprog_acquire_psoc(uint8_t psoc_type, uint8_t acquire_mode,
143  uint8_t max_attempts);
144 static int kitprog_reset_target(void);
145 static int kitprog_swd_sync(void);
146 static int kitprog_swd_seq(uint8_t seq_type);
147 
148 static int kitprog_generic_acquire(void);
149 
150 static int kitprog_swd_run_queue(void);
151 static void kitprog_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data);
152 static int kitprog_swd_switch_seq(enum swd_special_seq seq);
153 
154 
155 static inline int mm_to_version(uint8_t major, uint8_t minor)
156 {
157  return (major << 8) | minor;
158 }
159 
160 static int kitprog_init(void)
161 {
162  int retval;
163 
164  kitprog_handle = malloc(sizeof(struct kitprog));
165  if (!kitprog_handle) {
166  LOG_ERROR("Failed to allocate memory");
167  return ERROR_FAIL;
168  }
169 
170  if (kitprog_usb_open() != ERROR_OK) {
171  LOG_ERROR("Can't find a KitProg device! Please check device connections and permissions.");
172  return ERROR_JTAG_INIT_FAILED;
173  }
174 
175  /* Get the current KitProg version and target voltage */
176  if (kitprog_get_info() != ERROR_OK)
177  return ERROR_FAIL;
178 
179  /* Compatibility check */
182  if (kitprog_version < mm_to_version(2, 14)) {
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.");
185  }
186 
187  /* I have no idea what this does */
188  if (kitprog_set_unknown() != ERROR_OK)
189  return ERROR_FAIL;
190 
191  /* SWD won't work unless we do this */
192  if (kitprog_swd_sync() != ERROR_OK)
193  return ERROR_FAIL;
194 
195  /* Set the protocol to SWD */
197  return ERROR_FAIL;
198 
199  /* Reset the SWD bus */
201  return ERROR_FAIL;
202 
204  /* Try to acquire any device that will respond */
205  retval = kitprog_generic_acquire();
206  if (retval != ERROR_OK) {
207  LOG_ERROR("No PSoC devices found");
208  return retval;
209  }
210  }
211 
212  /* Allocate packet buffers and queues */
216  LOG_ERROR("Failed to allocate memory for the packet buffer");
217  return ERROR_FAIL;
218  }
219 
222  if (!pending_transfers) {
223  LOG_ERROR("Failed to allocate memory for the SWD transfer queue");
224  return ERROR_FAIL;
225  }
226 
227  return ERROR_OK;
228 }
229 
230 static int kitprog_quit(void)
231 {
233 
235  free(kitprog_handle->serial);
236  free(kitprog_handle);
237  free(pending_transfers);
238 
239  return ERROR_OK;
240 }
241 
242 /*************** kitprog usb functions *********************/
243 
244 static int kitprog_get_usb_serial(void)
245 {
246  int retval;
247  const uint8_t str_index = 128; /* This seems to be a constant */
248  char desc_string[256+1]; /* Max size of string descriptor */
249 
250  retval = libusb_get_string_descriptor_ascii(kitprog_handle->usb_handle,
251  str_index, (unsigned char *)desc_string, sizeof(desc_string)-1);
252  if (retval < 0) {
253  LOG_ERROR("libusb_get_string_descriptor_ascii() failed with %d", retval);
254  return ERROR_FAIL;
255  }
256 
257  /* Null terminate descriptor string */
258  desc_string[retval] = '\0';
259 
260  /* Allocate memory for the serial number */
261  kitprog_handle->serial = calloc(retval + 1, sizeof(char));
262  if (!kitprog_handle->serial) {
263  LOG_ERROR("Failed to allocate memory for the serial number");
264  return ERROR_FAIL;
265  }
266 
267  /* Store the serial number */
268  strncpy(kitprog_handle->serial, desc_string, retval + 1);
269 
270  return ERROR_OK;
271 }
272 
273 static int kitprog_usb_open(void)
274 {
275  const uint16_t vids[] = { VID, 0 };
276  const uint16_t pids[] = { PID, 0 };
277 
278  if (jtag_libusb_open(vids, pids, NULL, &kitprog_handle->usb_handle, NULL) != ERROR_OK) {
279  LOG_ERROR("Failed to open or find the device");
280  return ERROR_FAIL;
281  }
282 
283  /* Get the serial number for the device */
285  LOG_WARNING("Failed to get KitProg serial number");
286 
287  /* Convert the ASCII serial number into a (wchar_t *) */
288  size_t len = strlen(kitprog_handle->serial);
289  wchar_t *hid_serial = calloc(len + 1, sizeof(wchar_t));
290  if (!hid_serial) {
291  LOG_ERROR("Failed to allocate memory for the serial number");
292  return ERROR_FAIL;
293  }
294  if (mbstowcs(hid_serial, kitprog_handle->serial, len + 1) == (size_t)-1) {
295  free(hid_serial);
296  LOG_ERROR("Failed to convert serial number");
297  return ERROR_FAIL;
298  }
299 
300  /* Use HID for the KitBridge interface */
301  kitprog_handle->hid_handle = hid_open(VID, PID, hid_serial);
302  free(hid_serial);
303  if (!kitprog_handle->hid_handle) {
304  LOG_ERROR("Failed to open KitBridge (HID) interface");
305  return ERROR_FAIL;
306  }
307 
308  /* Claim the KitProg Programmer (bulk transfer) interface */
309  if (libusb_claim_interface(kitprog_handle->usb_handle, 1) != ERROR_OK) {
310  LOG_ERROR("Failed to claim KitProg Programmer (bulk transfer) interface");
311  return ERROR_FAIL;
312  }
313 
314  return ERROR_OK;
315 }
316 
317 static void kitprog_usb_close(void)
318 {
319  if (kitprog_handle->hid_handle) {
320  hid_close(kitprog_handle->hid_handle);
321  hid_exit();
322  }
323 
325 }
326 
327 /*************** kitprog lowlevel functions *********************/
328 
329 static int kitprog_hid_command(uint8_t *command, size_t command_length,
330  uint8_t *data, size_t data_length)
331 {
332  int ret;
333 
334  ret = hid_write(kitprog_handle->hid_handle, command, command_length);
335  if (ret < 0) {
336  LOG_DEBUG("HID write returned %i", ret);
337  return ERROR_FAIL;
338  }
339 
340  ret = hid_read_timeout(kitprog_handle->hid_handle,
341  data, data_length, LIBUSB_TIMEOUT_MS);
342  if (ret == 0) {
343  LOG_ERROR("HID read timed out");
344  return ERROR_TIMEOUT_REACHED;
345  } else if (ret < 0) {
346  LOG_ERROR("HID read error %ls", hid_error(kitprog_handle->hid_handle));
347  return ERROR_FAIL;
348  }
349 
350  return ERROR_OK;
351 }
352 
353 static int kitprog_get_version(void)
354 {
355  int ret;
356 
357  unsigned char command[3] = {HID_TYPE_START | HID_TYPE_WRITE, 0x00, HID_COMMAND_VERSION};
358  unsigned char data[64];
359 
360  ret = kitprog_hid_command(command, sizeof(command), data, sizeof(data));
361  if (ret != ERROR_OK)
362  return ret;
363 
364  kitprog_handle->hardware_version = data[1];
365  kitprog_handle->minor_version = data[2];
366  kitprog_handle->major_version = data[3];
367 
368  return ERROR_OK;
369 }
370 
371 static int kitprog_get_millivolts(void)
372 {
373  int ret;
374 
375  unsigned char command[3] = {HID_TYPE_START | HID_TYPE_READ, 0x00, HID_COMMAND_POWER};
376  unsigned char data[64];
377 
378  ret = kitprog_hid_command(command, sizeof(command), data, sizeof(data));
379  if (ret != ERROR_OK)
380  return ret;
381 
382  kitprog_handle->millivolts = (data[4] << 8) | data[3];
383 
384  return ERROR_OK;
385 }
386 
387 static int kitprog_get_info(void)
388 {
389  /* Get the device version information */
390  if (kitprog_get_version() == ERROR_OK) {
391  LOG_INFO("KitProg v%u.%02u",
393  LOG_INFO("Hardware version: %u",
395  } else {
396  LOG_ERROR("Failed to get KitProg version");
397  return ERROR_FAIL;
398  }
399 
400  /* Get the current reported target voltage */
401  if (kitprog_get_millivolts() == ERROR_OK) {
402  LOG_INFO("VTARG = %u.%03u V",
404  } else {
405  LOG_ERROR("Failed to get target voltage");
406  return ERROR_FAIL;
407  }
408 
409  return ERROR_OK;
410 }
411 
412 static int kitprog_set_protocol(uint8_t protocol)
413 {
414  int transferred;
416 
418  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
421  protocol, &status, 1, 0, &transferred);
422 
423  if (retval != ERROR_OK || transferred == 0) {
424  LOG_DEBUG("Zero bytes transferred");
425  return ERROR_FAIL;
426  }
427 
428  if (status != PROGRAMMER_OK_ACK) {
429  LOG_DEBUG("Programmer did not respond OK");
430  return ERROR_FAIL;
431  }
432 
433  return ERROR_OK;
434 }
435 
436 static int kitprog_get_status(void)
437 {
438  int transferred = 0;
440 
441  /* Try a maximum of three times */
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);
448  jtag_sleep(1000);
449  }
450 
451  if (transferred == 0) {
452  LOG_DEBUG("Zero bytes transferred");
453  return ERROR_FAIL;
454  }
455 
456  if (status != PROGRAMMER_OK_ACK) {
457  LOG_DEBUG("Programmer did not respond OK");
458  return ERROR_FAIL;
459  }
460 
461  return ERROR_OK;
462 }
463 
464 static int kitprog_set_unknown(void)
465 {
466  int transferred;
468 
470  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
472  (0x03 << 8) | 0x04,
473  0, &status, 1, 0, &transferred);
474 
475  if (retval != ERROR_OK || transferred == 0) {
476  LOG_DEBUG("Zero bytes transferred");
477  return ERROR_FAIL;
478  }
479 
480  if (status != PROGRAMMER_OK_ACK) {
481  LOG_DEBUG("Programmer did not respond OK");
482  return ERROR_FAIL;
483  }
484 
485  return ERROR_OK;
486 }
487 
488 static int kitprog_acquire_psoc(uint8_t psoc_type, uint8_t acquire_mode,
489  uint8_t max_attempts)
490 {
491  int transferred;
493 
495  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
498  (max_attempts << 8) | (acquire_mode << 4) | psoc_type, &status, 1, 0, &transferred);
499 
500  if (retval != ERROR_OK || transferred == 0) {
501  LOG_DEBUG("Zero bytes transferred");
502  return ERROR_FAIL;
503  }
504 
505  if (status != PROGRAMMER_OK_ACK) {
506  LOG_DEBUG("Programmer did not respond OK");
507  return ERROR_FAIL;
508  }
509 
510  return ERROR_OK;
511 }
512 
513 static int kitprog_reset_target(void)
514 {
515  int transferred;
517 
519  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
522  0, &status, 1, 0, &transferred);
523 
524  if (retval != ERROR_OK || transferred == 0) {
525  LOG_DEBUG("Zero bytes transferred");
526  return ERROR_FAIL;
527  }
528 
529  if (status != PROGRAMMER_OK_ACK) {
530  LOG_DEBUG("Programmer did not respond OK");
531  return ERROR_FAIL;
532  }
533 
534  return ERROR_OK;
535 }
536 
537 static int kitprog_swd_sync(void)
538 {
539  int transferred;
541 
543  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
546  0, &status, 1, 0, &transferred);
547 
548  if (retval != ERROR_OK || transferred == 0) {
549  LOG_DEBUG("Zero bytes transferred");
550  return ERROR_FAIL;
551  }
552 
553  if (status != PROGRAMMER_OK_ACK) {
554  LOG_DEBUG("Programmer did not respond OK");
555  return ERROR_FAIL;
556  }
557 
558  return ERROR_OK;
559 }
560 
561 static int kitprog_swd_seq(uint8_t seq_type)
562 {
563  int transferred;
565 
567  LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
570  seq_type, &status, 1, 0, &transferred);
571 
572  if (retval != ERROR_OK || transferred == 0) {
573  LOG_DEBUG("Zero bytes transferred");
574  return ERROR_FAIL;
575  }
576 
577  if (status != PROGRAMMER_OK_ACK) {
578  LOG_DEBUG("Programmer did not respond OK");
579  return ERROR_FAIL;
580  }
581 
582  return ERROR_OK;
583 }
584 
585 static int kitprog_generic_acquire(void)
586 {
587  const uint8_t devices[] = {DEVICE_PSOC4, DEVICE_PSOC3, DEVICE_PSOC5};
588 
589  int retval;
590  int acquire_count = 0;
591 
592  /* Due to the way the SWD port is shared between the Test Controller (TC)
593  * and the Cortex-M3 DAP on the PSoC 5LP, the TC is the default SWD target
594  * after power is applied. To access the DAP, the PSoC 5LP requires at least
595  * one acquisition sequence to be run (which switches the SWD mux from the
596  * TC to the DAP). However, after the mux is switched, the Cortex-M3 will be
597  * held in reset until a series of registers are written to (see section 5.2
598  * of the PSoC 5LP Device Programming Specifications for details).
599  *
600  * Instead of writing the registers in this function, we just do what the
601  * Cypress tools do and run the acquisition sequence a second time. This
602  * will take the Cortex-M3 out of reset and enable debugging.
603  */
604  for (int i = 0; i < 2; i++) {
605  for (uint8_t j = 0; j < sizeof(devices) && acquire_count == i; j++) {
607  if (retval != ERROR_OK) {
608  LOG_DEBUG("Acquisition function failed for device 0x%02x.", devices[j]);
609  return retval;
610  }
611 
612  if (kitprog_get_status() == ERROR_OK)
613  acquire_count++;
614  }
615 
616  jtag_sleep(10);
617  }
618 
619  if (acquire_count < 2)
620  return ERROR_FAIL;
621 
622  return ERROR_OK;
623 }
624 
625 /*************** swd wrapper functions *********************/
626 
627 static int kitprog_swd_init(void)
628 {
629  return ERROR_OK;
630 }
631 
632 static void kitprog_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
633 {
634  assert(!(cmd & SWD_CMD_RNW));
635  kitprog_swd_queue_cmd(cmd, NULL, value);
636 }
637 
638 static void kitprog_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
639 {
640  assert(cmd & SWD_CMD_RNW);
641  kitprog_swd_queue_cmd(cmd, value, 0);
642 }
643 
644 /*************** swd lowlevel functions ********************/
645 
647 {
648  switch (seq) {
649  case JTAG_TO_SWD:
651  LOG_DEBUG("JTAG to SWD");
653  return ERROR_FAIL;
654  break;
655  } else {
656  LOG_DEBUG("JTAG to SWD not supported");
657  /* Fall through to fix target reset issue */
658  }
659  /* fallthrough */
660  case LINE_RESET:
661  LOG_DEBUG("SWD line reset");
663  return ERROR_FAIL;
664  break;
665  default:
666  LOG_ERROR("Sequence %d not supported.", seq);
667  return ERROR_FAIL;
668  }
669 
670  return ERROR_OK;
671 }
672 
673 static int kitprog_swd_run_queue(void)
674 {
675  int ret;
676 
677  size_t read_count = 0;
678  size_t read_index = 0;
679  size_t write_count = 0;
680  uint8_t *buffer = kitprog_handle->packet_buffer;
681 
682  do {
683  LOG_DEBUG_IO("Executing %d queued transactions", pending_transfer_count);
684 
685  if (queued_retval != ERROR_OK) {
686  LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
687  break;
688  }
689 
691  break;
692 
693  for (int i = 0; i < pending_transfer_count; i++) {
694  uint8_t cmd = pending_transfers[i].cmd;
695  uint32_t data = pending_transfers[i].data;
696 
697  /* When proper WAIT handling is implemented in the
698  * common SWD framework, this kludge can be
699  * removed. However, this might lead to minor
700  * performance degradation as the adapter wouldn't be
701  * able to automatically retry anything (because ARM
702  * has forgotten to implement sticky error flags
703  * clearing). See also comments regarding
704  * cmsis_dap_cmd_DAP_TFER_Configure() and
705  * cmsis_dap_cmd_DAP_SWD_Configure() in
706  * cmsis_dap_init().
707  */
708  if (!(cmd & SWD_CMD_RNW) &&
709  !(cmd & SWD_CMD_APNDP) &&
710  (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT &&
711  (data & CORUNDETECT)) {
712  LOG_DEBUG("refusing to enable sticky overrun detection");
713  data &= ~CORUNDETECT;
714  }
715 
716  LOG_DEBUG_IO("%s %s reg %x %"PRIx32,
717  cmd & SWD_CMD_APNDP ? "AP" : "DP",
718  cmd & SWD_CMD_RNW ? "read" : "write",
719  (cmd & SWD_CMD_A32) >> 1, data);
720 
721  buffer[write_count++] = (cmd | SWD_CMD_START | SWD_CMD_PARK) & ~SWD_CMD_STOP;
722  read_count++;
723  if (!(cmd & SWD_CMD_RNW)) {
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;
728  } else {
729  read_count += 4;
730  }
731  }
732 
734  BULK_EP_OUT, (char *)buffer,
735  write_count, 0, &ret)) {
736  LOG_ERROR("Bulk write failed");
738  break;
739  } else {
741  }
742 
743  /* KitProg firmware does not send a zero length packet
744  * after the bulk-in transmission of a length divisible by bulk packet
745  * size (64 bytes) as required by the USB specification.
746  * Therefore libusb would wait for continuation of transmission.
747  * Workaround: Limit bulk read size to expected number of bytes
748  * for problematic transfer sizes. Otherwise use the maximum buffer
749  * size here because the KitProg sometimes doesn't like bulk reads
750  * of fewer than 62 bytes. (?!?!)
751  */
752  size_t read_count_workaround = SWD_MAX_BUFFER_LENGTH;
753  if (read_count % 64 == 0)
754  read_count_workaround = read_count;
755 
757  BULK_EP_IN | LIBUSB_ENDPOINT_IN, (char *)buffer,
758  read_count_workaround, 1000, &ret)) {
759  LOG_ERROR("Bulk read failed");
761  break;
762  } else {
763  /* Handle garbage data by offsetting the initial read index */
764  if ((unsigned int)ret > read_count)
765  read_index = ret - read_count;
767  }
768 
769  for (int i = 0; i < pending_transfer_count; i++) {
770  if (pending_transfers[i].cmd & SWD_CMD_RNW) {
771  uint32_t data = le_to_h_u32(&buffer[read_index]);
772 
773  LOG_DEBUG_IO("Read result: %"PRIx32, data);
774 
775  if (pending_transfers[i].buffer)
776  *(uint32_t *)pending_transfers[i].buffer = data;
777 
778  read_index += 4;
779  }
780 
781  uint8_t ack = buffer[read_index] & 0x07;
782  if (ack != SWD_ACK_OK || (buffer[read_index] & 0x08)) {
783  LOG_DEBUG("SWD ack not OK: %d %s", i,
784  ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
786  break;
787  }
788  read_index++;
789  }
790  } while (0);
791 
793  int retval = queued_retval;
795 
796  return retval;
797 }
798 
799 static void kitprog_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
800 {
802  /* Not enough room in the queue. Run the queue. */
804  }
805 
806  if (queued_retval != ERROR_OK)
807  return;
808 
811  if (cmd & SWD_CMD_RNW) {
812  /* Queue a read transaction */
814  }
816 }
817 
818 /*************** jtag lowlevel functions ********************/
819 
820 static int kitprog_reset(int trst, int srst)
821 {
822  int retval = ERROR_OK;
823 
824  if (trst == 1) {
825  LOG_ERROR("KitProg: Interface has no TRST");
826  return ERROR_FAIL;
827  }
828 
829  if (srst == 1) {
830  retval = kitprog_reset_target();
831  /* Since the previous command also disables SWCLK output, we need to send an
832  * SWD bus reset command to re-enable it. For some reason, running
833  * kitprog_swd_seq() immediately after kitprog_reset_target() won't
834  * actually fix this. Instead, kitprog_swd_seq() will be run once OpenOCD
835  * tries to send a JTAG-to-SWD sequence, which should happen during
836  * swd_check_reconnect (see the JTAG_TO_SWD case in kitprog_swd_switch_seq).
837  */
838  }
839 
840  if (retval != ERROR_OK)
841  LOG_ERROR("KitProg: Interface reset failed");
842  return retval;
843 }
844 
845 COMMAND_HANDLER(kitprog_handle_info_command)
846 {
847  int retval = kitprog_get_info();
848 
849  return retval;
850 }
851 
852 
853 COMMAND_HANDLER(kitprog_handle_acquire_psoc_command)
854 {
855  int retval = kitprog_generic_acquire();
856 
857  return retval;
858 }
859 
860 COMMAND_HANDLER(kitprog_handle_init_acquire_psoc_command)
861 {
863 
864  return ERROR_OK;
865 }
866 
867 static const struct command_registration kitprog_subcommand_handlers[] = {
868  {
869  .name = "info",
870  .handler = &kitprog_handle_info_command,
871  .mode = COMMAND_EXEC,
872  .usage = "",
873  .help = "show KitProg info",
874  },
875  {
876  .name = "acquire_psoc",
877  .handler = &kitprog_handle_acquire_psoc_command,
878  .mode = COMMAND_EXEC,
879  .usage = "",
880  .help = "try to acquire a PSoC",
881  },
882  {
883  .name = "init_acquire_psoc",
884  .handler = &kitprog_handle_init_acquire_psoc_command,
885  .mode = COMMAND_CONFIG,
886  .help = "try to acquire a PSoC during init",
887  .usage = "",
888  },
890 };
891 
892 static const struct command_registration kitprog_command_handlers[] = {
893  {
894  .name = "kitprog",
895  .mode = COMMAND_ANY,
896  .help = "perform KitProg management",
897  .usage = "<cmd>",
899  },
901 };
902 
903 static const struct swd_driver kitprog_swd = {
905  .switch_seq = kitprog_swd_switch_seq,
906  .read_reg = kitprog_swd_read_reg,
907  .write_reg = kitprog_swd_write_reg,
908  .run = kitprog_swd_run_queue,
909 };
910 
911 static const char * const kitprog_transports[] = { "swd", NULL };
912 
914  .name = "kitprog",
915  .transports = kitprog_transports,
916  .commands = kitprog_command_handlers,
917 
918  .init = kitprog_init,
919  .quit = kitprog_quit,
920  .reset = kitprog_reset,
921 
922  .swd_ops = &kitprog_swd,
923 };
#define SWD_ACK_FAULT
Definition: arm_adi_v5.h:33
#define DP_CTRL_STAT
Definition: arm_adi_v5.h:50
#define CORUNDETECT
Definition: arm_adi_v5.h:82
swd_special_seq
Definition: arm_adi_v5.h:236
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ LINE_RESET
Definition: arm_adi_v5.h:237
#define SWD_ACK_WAIT
Definition: arm_adi_v5.h:32
#define SWD_ACK_OK
Definition: arm_adi_v5.h:31
static const struct device_t devices[]
Definition: at91rm9200.c:84
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1062
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:553
static int kitprog_init(void)
Definition: kitprog.c:160
static const struct command_registration kitprog_command_handlers[]
Definition: kitprog.c:892
static struct kitprog * kitprog_handle
Definition: kitprog.c:129
static int queued_retval
Definition: kitprog.c:127
#define CONTROL_MODE_RESET_TARGET
Definition: kitprog.c:49
#define PID
Definition: kitprog.c:38
static int kitprog_swd_sync(void)
Definition: kitprog.c:537
#define CONTROL_MODE_ACQUIRE_SWD_TARGET
Definition: kitprog.c:52
static void kitprog_usb_close(void)
Definition: kitprog.c:317
static const struct swd_driver kitprog_swd
Definition: kitprog.c:903
static const struct command_registration kitprog_subcommand_handlers[]
Definition: kitprog.c:867
#define SEQUENCE_JTAG_TO_SWD
Definition: kitprog.c:67
static int kitprog_acquire_psoc(uint8_t psoc_type, uint8_t acquire_mode, uint8_t max_attempts)
Definition: kitprog.c:488
#define PROGRAMMER_NOK_NACK
Definition: kitprog.c:69
#define CONTROL_MODE_SYNCHRONIZE_TRANSFER
Definition: kitprog.c:51
#define BULK_EP_OUT
Definition: kitprog.c:41
#define SWD_MAX_BUFFER_LENGTH
Definition: kitprog.c:99
#define CONTROL_COMMAND_PROGRAM
Definition: kitprog.c:46
static int kitprog_swd_seq(uint8_t seq_type)
Definition: kitprog.c:561
static int kitprog_usb_open(void)
Definition: kitprog.c:273
static int kitprog_get_status(void)
Definition: kitprog.c:436
#define PROGRAMMER_OK_ACK
Definition: kitprog.c:70
static bool kitprog_init_acquire_psoc
Definition: kitprog.c:122
#define SEQUENCE_LINE_RESET
Definition: kitprog.c:66
#define HID_TYPE_WRITE
Definition: kitprog.c:72
static int mm_to_version(uint8_t major, uint8_t minor)
Definition: kitprog.c:155
static int kitprog_swd_switch_seq(enum swd_special_seq seq)
Definition: kitprog.c:646
COMMAND_HANDLER(kitprog_handle_info_command)
Definition: kitprog.c:845
static int kitprog_get_version(void)
Definition: kitprog.c:353
#define HID_TYPE_START
Definition: kitprog.c:74
#define HID_TYPE_READ
Definition: kitprog.c:73
#define CONTROL_TYPE_READ
Definition: kitprog.c:43
static struct pending_transfer_result * pending_transfers
Definition: kitprog.c:125
static int kitprog_reset_target(void)
Definition: kitprog.c:513
static int kitprog_hid_command(uint8_t *command, size_t command_length, uint8_t *data, size_t data_length)
Definition: kitprog.c:329
static int kitprog_get_info(void)
Definition: kitprog.c:387
#define HID_COMMAND_POWER
Definition: kitprog.c:76
static int pending_transfer_count
Definition: kitprog.c:124
static const char *const kitprog_transports[]
Definition: kitprog.c:911
struct adapter_driver kitprog_adapter_driver
Definition: kitprog.c:913
static int kitprog_set_protocol(uint8_t protocol)
Definition: kitprog.c:412
static int kitprog_get_millivolts(void)
Definition: kitprog.c:371
static void kitprog_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: kitprog.c:638
static void kitprog_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: kitprog.c:632
static int kitprog_generic_acquire(void)
Definition: kitprog.c:585
static int kitprog_get_usb_serial(void)
Definition: kitprog.c:244
#define ACQUIRE_MODE_RESET
Definition: kitprog.c:63
static int kitprog_set_unknown(void)
Definition: kitprog.c:464
static int pending_queue_len
Definition: kitprog.c:124
static int kitprog_swd_init(void)
Definition: kitprog.c:627
#define BULK_EP_IN
Definition: kitprog.c:40
#define CONTROL_TYPE_WRITE
Definition: kitprog.c:44
static int kitprog_swd_run_queue(void)
Definition: kitprog.c:673
static int kitprog_reset(int trst, int srst)
Definition: kitprog.c:820
#define PROTOCOL_SWD
Definition: kitprog.c:56
static int kitprog_quit(void)
Definition: kitprog.c:230
#define CONTROL_MODE_SET_PROGRAMMER_PROTOCOL
Definition: kitprog.c:50
#define CONTROL_MODE_POLL_PROGRAMMER_STATUS
Definition: kitprog.c:48
#define DEVICE_PSOC3
Definition: kitprog.c:59
#define CONTROL_MODE_SEND_SWD_SEQUENCE
Definition: kitprog.c:53
#define DEVICE_PSOC5
Definition: kitprog.c:61
#define HID_COMMAND_VERSION
Definition: kitprog.c:77
#define VID
Definition: kitprog.c:37
static void kitprog_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
Definition: kitprog.c:799
#define DEVICE_PSOC4
Definition: kitprog.c:58
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
Definition: libusb_helper.h:26
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:101
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define ERROR_TIMEOUT_REACHED
Definition: log.h:173
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
Represents a driver for a debugging interface.
Definition: interface.h:207
const char *const name
The name of the interface driver.
Definition: interface.h:209
const char * name
Definition: command.h:235
struct libusb_device_handle * usb_handle
Definition: kitprog.c:103
uint8_t hardware_version
Definition: kitprog.c:108
uint8_t major_version
Definition: kitprog.c:110
uint8_t minor_version
Definition: kitprog.c:109
uint16_t packet_index
Definition: kitprog.c:105
bool supports_jtag_to_swd
Definition: kitprog.c:113
uint8_t * packet_buffer
Definition: kitprog.c:106
uint16_t packet_size
Definition: kitprog.c:104
uint16_t millivolts
Definition: kitprog.c:111
hid_device * hid_handle
Definition: kitprog.c:102
char * serial
Definition: kitprog.c:107
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
#define SWD_CMD_A32
Definition: swd.h:19
#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
#define SWD_CMD_APNDP
Definition: swd.h:17
#define SWD_CMD_START
Definition: swd.h:16
#define SWD_CMD_RNW
Definition: swd.h:18
#define SWD_CMD_STOP
Definition: swd.h:21
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t cmd
Definition: vdebug.c:1