1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
159
160
161
162
163
164
165
169
170
174
175
176
177
178
179
180
181
182
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
211
212
213
214
215
219
220
221
222
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
256
257
258
259
260
261
262
266
267
268
269
270
271
272
273
274
275
276
277
278
282
283
284
285
286
287
288
289
290
294
299
300
301
302
303
307
308
309
313
314
315
316
317
326
327
328
329
330
331
332
333
334
335
339
340
341
342
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
399
400
401
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
427
428
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
455
456
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
479
480
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
504
505
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
528
529
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
552
553
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
576
577
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
618
619
620
621
622
623
624
625
626
627
631
632
637
638
643
644
645
646
672
673
674
675
676
677
678
679
680
681
682
683
684
685
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
715
716
717
718
719
720
721
722
723
731
732
733
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
788
789
790
791
792
793
794
795
796
797
798
799
800
801
805
806
807
808
809
810
811
815
816
817
818
819
820
821
822
823
824
828
829
839
840
841
842
843
844
845
851
852
853
859
860
866
867
891
892
902
903
910
911
912
913
914
915
916
917
918
919
920
921
922
923
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
#include <hidapi.h>
#include <jtag/interface.h>
#include <jtag/swd.h>
#include <jtag/commands.h>
#include "libusb_helper.h"
6 includes
#define VID 0x04b4
#define PID 0xf139
#define BULK_EP_IN 1
#define BULK_EP_OUT 2
#define CONTROL_TYPE_READ 0x01
#define CONTROL_TYPE_WRITE 0x02
#define CONTROL_COMMAND_PROGRAM 0x07
#define CONTROL_MODE_POLL_PROGRAMMER_STATUS 0x01
#define CONTROL_MODE_RESET_TARGET 0x04
#define CONTROL_MODE_SET_PROGRAMMER_PROTOCOL 0x40
#define CONTROL_MODE_SYNCHRONIZE_TRANSFER 0x41
#define CONTROL_MODE_ACQUIRE_SWD_TARGET 0x42
#define CONTROL_MODE_SEND_SWD_SEQUENCE 0x43
#define PROTOCOL_JTAG 0x00
#define PROTOCOL_SWD 0x01
#define DEVICE_PSOC4 0x00
#define DEVICE_PSOC3 0x01
#define DEVICE_UNKNOWN 0x02
#define DEVICE_PSOC5 0x03
#define ACQUIRE_MODE_RESET 0x00
#define ACQUIRE_MODE_POWER_CYCLE 0x01
#define SEQUENCE_LINE_RESET 0x00
#define SEQUENCE_JTAG_TO_SWD 0x01
#define PROGRAMMER_NOK_NACK 0x00
#define PROGRAMMER_OK_ACK 0x01
#define HID_TYPE_WRITE 0x00
#define HID_TYPE_READ 0x01
#define HID_TYPE_START 0x02
#define HID_COMMAND_POWER 0x80
#define HID_COMMAND_VERSION 0x81
#define HID_COMMAND_RESET 0x82
#define HID_COMMAND_CONFIGURE 0x8f
#define HID_COMMAND_BOOTLOADER 0xa0
/* ... */
#define SWD_MAX_BUFFER_LENGTH 300
34 defines
struct kitprog {
hid_device *hid_handle;
struct libusb_device_handle *usb_handle;
uint16_t packet_size;
uint16_t packet_index;
uint8_t *packet_buffer;
char *serial;
uint8_t hardware_version;
uint8_t minor_version;
uint8_t major_version;
uint16_t millivolts;
bool supports_jtag_to_swd;
...};
struct pending_transfer_result {
uint8_t cmd;
uint32_t data;
void *buffer;
...};
static bool kitprog_init_acquire_psoc;
static int pending_transfer_count, pending_queue_len;
static struct pending_transfer_result *pending_transfers;
static int queued_retval;
static struct kitprog *kitprog_handle;
static int kitprog_usb_open(void);
static void kitprog_usb_close(void);
static int kitprog_hid_command(uint8_t *command, size_t command_length,
uint8_t *data, size_t data_length);
static int kitprog_get_version(void);
static int kitprog_get_millivolts(void);
static int kitprog_get_info(void);
static int kitprog_set_protocol(uint8_t protocol);
static int kitprog_get_status(void);
static int kitprog_set_unknown(void);
static int kitprog_acquire_psoc(uint8_t psoc_type, uint8_t acquire_mode,
uint8_t max_attempts);
static int kitprog_reset_target(void);
static int kitprog_swd_sync(void);
static int kitprog_swd_seq(uint8_t seq_type);
static int kitprog_generic_acquire(void);
static int kitprog_swd_run_queue(void);
static void kitprog_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data);
static int kitprog_swd_switch_seq(enum swd_special_seq seq);
static inline int mm_to_version(uint8_t major, uint8_t minor)
{
return (major << 8) | minor;
}{ ... }
static int kitprog_init(void)
{
int retval;
kitprog_handle = malloc(sizeof(struct kitprog));
if (!kitprog_handle) {
LOG_ERROR("Failed to allocate memory");
return ERROR_FAIL;
}if (!kitprog_handle) { ... }
if (kitprog_usb_open() != ERROR_OK) {
LOG_ERROR("Can't find a KitProg device! Please check device connections and permissions.");
return ERROR_JTAG_INIT_FAILED;
}if (kitprog_usb_open() != ERROR_OK) { ... }
if (kitprog_get_info() != ERROR_OK)
return ERROR_FAIL;
kitprog_handle->supports_jtag_to_swd = true;
int kitprog_version = mm_to_version(kitprog_handle->major_version, kitprog_handle->minor_version);
if (kitprog_version < mm_to_version(2, 14)) {
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.");
kitprog_handle->supports_jtag_to_swd = false;
}if (kitprog_version < mm_to_version(2, 14)) { ... }
if (kitprog_set_unknown() != ERROR_OK)
return ERROR_FAIL;
if (kitprog_swd_sync() != ERROR_OK)
return ERROR_FAIL;
if (kitprog_set_protocol(PROTOCOL_SWD) != ERROR_OK)
return ERROR_FAIL;
if (kitprog_swd_seq(SEQUENCE_LINE_RESET) != ERROR_OK)
return ERROR_FAIL;
if (kitprog_init_acquire_psoc) {
retval = kitprog_generic_acquire();
if (retval != ERROR_OK) {
LOG_ERROR("No PSoC devices found");
return retval;
}if (retval != ERROR_OK) { ... }
}if (kitprog_init_acquire_psoc) { ... }
kitprog_handle->packet_size = SWD_MAX_BUFFER_LENGTH;
kitprog_handle->packet_buffer = malloc(SWD_MAX_BUFFER_LENGTH);
if (!kitprog_handle->packet_buffer) {
LOG_ERROR("Failed to allocate memory for the packet buffer");
return ERROR_FAIL;
}if (!kitprog_handle->packet_buffer) { ... }
pending_queue_len = SWD_MAX_BUFFER_LENGTH / 5;
pending_transfers = malloc(pending_queue_len * sizeof(*pending_transfers));
if (!pending_transfers) {
LOG_ERROR("Failed to allocate memory for the SWD transfer queue");
return ERROR_FAIL;
}if (!pending_transfers) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_quit(void)
{
kitprog_usb_close();
free(kitprog_handle->packet_buffer);
free(kitprog_handle->serial);
free(kitprog_handle);
free(pending_transfers);
return ERROR_OK;
}{ ... }
static int kitprog_get_usb_serial(void)
{
int retval;
const uint8_t str_index = 128;
char desc_string[256+1];
retval = libusb_get_string_descriptor_ascii(kitprog_handle->usb_handle,
str_index, (unsigned char *)desc_string, sizeof(desc_string)-1);
if (retval < 0) {
LOG_ERROR("libusb_get_string_descriptor_ascii() failed with %d", retval);
return ERROR_FAIL;
}if (retval < 0) { ... }
desc_string[retval] = '\0';
kitprog_handle->serial = calloc(retval + 1, sizeof(char));
if (!kitprog_handle->serial) {
LOG_ERROR("Failed to allocate memory for the serial number");
return ERROR_FAIL;
}if (!kitprog_handle->serial) { ... }
strncpy(kitprog_handle->serial, desc_string, retval + 1);
return ERROR_OK;
}{ ... }
static int kitprog_usb_open(void)
{
const uint16_t vids[] = { VID, 0 };
const uint16_t pids[] = { PID, 0 };
if (jtag_libusb_open(vids, pids, NULL, &kitprog_handle->usb_handle, NULL) != ERROR_OK) {
LOG_ERROR("Failed to open or find the device");
return ERROR_FAIL;
}if (jtag_libusb_open(vids, pids, NULL, &kitprog_handle->usb_handle, NULL) != ERROR_OK) { ... }
if (kitprog_get_usb_serial() != ERROR_OK)
LOG_WARNING("Failed to get KitProg serial number");
size_t len = strlen(kitprog_handle->serial);
wchar_t *hid_serial = calloc(len + 1, sizeof(wchar_t));
if (!hid_serial) {
LOG_ERROR("Failed to allocate memory for the serial number");
return ERROR_FAIL;
}if (!hid_serial) { ... }
if (mbstowcs(hid_serial, kitprog_handle->serial, len + 1) == (size_t)-1) {
free(hid_serial);
LOG_ERROR("Failed to convert serial number");
return ERROR_FAIL;
}if (mbstowcs(hid_serial, kitprog_handle->serial, len + 1) == (size_t)-1) { ... }
kitprog_handle->hid_handle = hid_open(VID, PID, hid_serial);
free(hid_serial);
if (!kitprog_handle->hid_handle) {
LOG_ERROR("Failed to open KitBridge (HID) interface");
return ERROR_FAIL;
}if (!kitprog_handle->hid_handle) { ... }
if (libusb_claim_interface(kitprog_handle->usb_handle, 1) != ERROR_OK) {
LOG_ERROR("Failed to claim KitProg Programmer (bulk transfer) interface");
return ERROR_FAIL;
}if (libusb_claim_interface(kitprog_handle->usb_handle, 1) != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static void kitprog_usb_close(void)
{
if (kitprog_handle->hid_handle) {
hid_close(kitprog_handle->hid_handle);
hid_exit();
}if (kitprog_handle->hid_handle) { ... }
jtag_libusb_close(kitprog_handle->usb_handle);
}{ ... }
kitprog usb functions
static int kitprog_hid_command(uint8_t *command, size_t command_length,
uint8_t *data, size_t data_length)
{
int ret;
ret = hid_write(kitprog_handle->hid_handle, command, command_length);
if (ret < 0) {
LOG_DEBUG("HID write returned %i", ret);
return ERROR_FAIL;
}if (ret < 0) { ... }
ret = hid_read_timeout(kitprog_handle->hid_handle,
data, data_length, LIBUSB_TIMEOUT_MS);
if (ret == 0) {
LOG_ERROR("HID read timed out");
return ERROR_TIMEOUT_REACHED;
}if (ret == 0) { ... } else if (ret < 0) {
LOG_ERROR("HID read error %ls", hid_error(kitprog_handle->hid_handle));
return ERROR_FAIL;
}else if (ret < 0) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_get_version(void)
{
int ret;
unsigned char command[3] = {HID_TYPE_START | HID_TYPE_WRITE, 0x00, HID_COMMAND_VERSION};
unsigned char data[64];
ret = kitprog_hid_command(command, sizeof(command), data, sizeof(data));
if (ret != ERROR_OK)
return ret;
kitprog_handle->hardware_version = data[1];
kitprog_handle->minor_version = data[2];
kitprog_handle->major_version = data[3];
return ERROR_OK;
}{ ... }
static int kitprog_get_millivolts(void)
{
int ret;
unsigned char command[3] = {HID_TYPE_START | HID_TYPE_READ, 0x00, HID_COMMAND_POWER};
unsigned char data[64];
ret = kitprog_hid_command(command, sizeof(command), data, sizeof(data));
if (ret != ERROR_OK)
return ret;
kitprog_handle->millivolts = (data[4] << 8) | data[3];
return ERROR_OK;
}{ ... }
static int kitprog_get_info(void)
{
if (kitprog_get_version() == ERROR_OK) {
LOG_INFO("KitProg v%u.%02u",
kitprog_handle->major_version, kitprog_handle->minor_version);
LOG_INFO("Hardware version: %u",
kitprog_handle->hardware_version);
}if (kitprog_get_version() == ERROR_OK) { ... } else {
LOG_ERROR("Failed to get KitProg version");
return ERROR_FAIL;
}else { ... }
if (kitprog_get_millivolts() == ERROR_OK) {
LOG_INFO("VTARG = %u.%03u V",
kitprog_handle->millivolts / 1000, kitprog_handle->millivolts % 1000);
}if (kitprog_get_millivolts() == ERROR_OK) { ... } else {
LOG_ERROR("Failed to get target voltage");
return ERROR_FAIL;
}else { ... }
return ERROR_OK;
}{ ... }
static int kitprog_set_protocol(uint8_t protocol)
{
int transferred;
char status = PROGRAMMER_NOK_NACK;
int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
CONTROL_TYPE_WRITE,
(CONTROL_MODE_SET_PROGRAMMER_PROTOCOL << 8) | CONTROL_COMMAND_PROGRAM,
protocol, &status, 1, 0, &transferred);
if (retval != ERROR_OK || transferred == 0) {
LOG_DEBUG("Zero bytes transferred");
return ERROR_FAIL;
}if (retval != ERROR_OK || transferred == 0) { ... }
if (status != PROGRAMMER_OK_ACK) {
LOG_DEBUG("Programmer did not respond OK");
return ERROR_FAIL;
}if (status != PROGRAMMER_OK_ACK) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_get_status(void)
{
int transferred = 0;
char status = PROGRAMMER_NOK_NACK;
for (int i = 0; (i < 3) && (transferred == 0); i++) {
jtag_libusb_control_transfer(kitprog_handle->usb_handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
CONTROL_TYPE_READ,
(CONTROL_MODE_POLL_PROGRAMMER_STATUS << 8) | CONTROL_COMMAND_PROGRAM,
0, &status, 1, 0, &transferred);
jtag_sleep(1000);
}for (int i = 0; (i < 3) && (transferred == 0); i++) { ... }
if (transferred == 0) {
LOG_DEBUG("Zero bytes transferred");
return ERROR_FAIL;
}if (transferred == 0) { ... }
if (status != PROGRAMMER_OK_ACK) {
LOG_DEBUG("Programmer did not respond OK");
return ERROR_FAIL;
}if (status != PROGRAMMER_OK_ACK) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_set_unknown(void)
{
int transferred;
char status = PROGRAMMER_NOK_NACK;
int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
CONTROL_TYPE_WRITE,
(0x03 << 8) | 0x04,
0, &status, 1, 0, &transferred);
if (retval != ERROR_OK || transferred == 0) {
LOG_DEBUG("Zero bytes transferred");
return ERROR_FAIL;
}if (retval != ERROR_OK || transferred == 0) { ... }
if (status != PROGRAMMER_OK_ACK) {
LOG_DEBUG("Programmer did not respond OK");
return ERROR_FAIL;
}if (status != PROGRAMMER_OK_ACK) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_acquire_psoc(uint8_t psoc_type, uint8_t acquire_mode,
uint8_t max_attempts)
{
int transferred;
char status = PROGRAMMER_NOK_NACK;
int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
CONTROL_TYPE_WRITE,
(CONTROL_MODE_ACQUIRE_SWD_TARGET << 8) | CONTROL_COMMAND_PROGRAM,
(max_attempts << 8) | (acquire_mode << 4) | psoc_type, &status, 1, 0, &transferred);
if (retval != ERROR_OK || transferred == 0) {
LOG_DEBUG("Zero bytes transferred");
return ERROR_FAIL;
}if (retval != ERROR_OK || transferred == 0) { ... }
if (status != PROGRAMMER_OK_ACK) {
LOG_DEBUG("Programmer did not respond OK");
return ERROR_FAIL;
}if (status != PROGRAMMER_OK_ACK) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_reset_target(void)
{
int transferred;
char status = PROGRAMMER_NOK_NACK;
int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
CONTROL_TYPE_WRITE,
(CONTROL_MODE_RESET_TARGET << 8) | CONTROL_COMMAND_PROGRAM,
0, &status, 1, 0, &transferred);
if (retval != ERROR_OK || transferred == 0) {
LOG_DEBUG("Zero bytes transferred");
return ERROR_FAIL;
}if (retval != ERROR_OK || transferred == 0) { ... }
if (status != PROGRAMMER_OK_ACK) {
LOG_DEBUG("Programmer did not respond OK");
return ERROR_FAIL;
}if (status != PROGRAMMER_OK_ACK) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_swd_sync(void)
{
int transferred;
char status = PROGRAMMER_NOK_NACK;
int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
CONTROL_TYPE_WRITE,
(CONTROL_MODE_SYNCHRONIZE_TRANSFER << 8) | CONTROL_COMMAND_PROGRAM,
0, &status, 1, 0, &transferred);
if (retval != ERROR_OK || transferred == 0) {
LOG_DEBUG("Zero bytes transferred");
return ERROR_FAIL;
}if (retval != ERROR_OK || transferred == 0) { ... }
if (status != PROGRAMMER_OK_ACK) {
LOG_DEBUG("Programmer did not respond OK");
return ERROR_FAIL;
}if (status != PROGRAMMER_OK_ACK) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_swd_seq(uint8_t seq_type)
{
int transferred;
char status = PROGRAMMER_NOK_NACK;
int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle,
LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
CONTROL_TYPE_WRITE,
(CONTROL_MODE_SEND_SWD_SEQUENCE << 8) | CONTROL_COMMAND_PROGRAM,
seq_type, &status, 1, 0, &transferred);
if (retval != ERROR_OK || transferred == 0) {
LOG_DEBUG("Zero bytes transferred");
return ERROR_FAIL;
}if (retval != ERROR_OK || transferred == 0) { ... }
if (status != PROGRAMMER_OK_ACK) {
LOG_DEBUG("Programmer did not respond OK");
return ERROR_FAIL;
}if (status != PROGRAMMER_OK_ACK) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_generic_acquire(void)
{
const uint8_t devices[] = {DEVICE_PSOC4, DEVICE_PSOC3, DEVICE_PSOC5};
int retval;
int acquire_count = 0;
/* ... */
for (int i = 0; i < 2; i++) {
for (uint8_t j = 0; j < sizeof(devices) && acquire_count == i; j++) {
retval = kitprog_acquire_psoc(devices[j], ACQUIRE_MODE_RESET, 3);
if (retval != ERROR_OK) {
LOG_DEBUG("Acquisition function failed for device 0x%02x.", devices[j]);
return retval;
}if (retval != ERROR_OK) { ... }
if (kitprog_get_status() == ERROR_OK)
acquire_count++;
}for (uint8_t j = 0; j < sizeof(devices) && acquire_count == i; j++) { ... }
jtag_sleep(10);
}for (int i = 0; i < 2; i++) { ... }
if (acquire_count < 2)
return ERROR_FAIL;
return ERROR_OK;
}{ ... }
kitprog lowlevel functions
static int kitprog_swd_init(void)
{
return ERROR_OK;
}{ ... }
static void kitprog_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
{
assert(!(cmd & SWD_CMD_RNW));
kitprog_swd_queue_cmd(cmd, NULL, value);
}{ ... }
static void kitprog_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
{
assert(cmd & SWD_CMD_RNW);
kitprog_swd_queue_cmd(cmd, value, 0);
}{ ... }
swd wrapper functions
static int kitprog_swd_switch_seq(enum swd_special_seq seq)
{
switch (seq) {
case JTAG_TO_SWD:
if (kitprog_handle->supports_jtag_to_swd) {
LOG_DEBUG("JTAG to SWD");
if (kitprog_swd_seq(SEQUENCE_JTAG_TO_SWD) != ERROR_OK)
return ERROR_FAIL;
break;
}if (kitprog_handle->supports_jtag_to_swd) { ... } else {
LOG_DEBUG("JTAG to SWD not supported");
}else { ... }
case JTAG_TO_SWD:
case LINE_RESET:
LOG_DEBUG("SWD line reset");
if (kitprog_swd_seq(SEQUENCE_LINE_RESET) != ERROR_OK)
return ERROR_FAIL;
break;case LINE_RESET:
default:
LOG_ERROR("Sequence %d not supported.", seq);
return ERROR_FAIL;default
}switch (seq) { ... }
return ERROR_OK;
}{ ... }
static int kitprog_swd_run_queue(void)
{
int ret;
size_t read_count = 0;
size_t read_index = 0;
size_t write_count = 0;
uint8_t *buffer = kitprog_handle->packet_buffer;
do {
LOG_DEBUG_IO("Executing %d queued transactions", pending_transfer_count);
if (queued_retval != ERROR_OK) {
LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
break;
}if (queued_retval != ERROR_OK) { ... }
if (!pending_transfer_count)
break;
for (int i = 0; i < pending_transfer_count; i++) {
uint8_t cmd = pending_transfers[i].cmd;
uint32_t data = pending_transfers[i].data;
/* ... */
if (!(cmd & SWD_CMD_RNW) &&
!(cmd & SWD_CMD_APNDP) &&
(cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT &&
(data & CORUNDETECT)) {
LOG_DEBUG("refusing to enable sticky overrun detection");
data &= ~CORUNDETECT;
}if (!(cmd & SWD_CMD_RNW) && !(cmd & SWD_CMD_APNDP) && (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT && (data & CORUNDETECT)) { ... }
LOG_DEBUG_IO("%s %s reg %x %"PRIx32,
cmd & SWD_CMD_APNDP ? "AP" : "DP",
cmd & SWD_CMD_RNW ? "read" : "write",
(cmd & SWD_CMD_A32) >> 1, data);
buffer[write_count++] = (cmd | SWD_CMD_START | SWD_CMD_PARK) & ~SWD_CMD_STOP;
read_count++;
if (!(cmd & SWD_CMD_RNW)) {
buffer[write_count++] = (data) & 0xff;
buffer[write_count++] = (data >> 8) & 0xff;
buffer[write_count++] = (data >> 16) & 0xff;
buffer[write_count++] = (data >> 24) & 0xff;
}if (!(cmd & SWD_CMD_RNW)) { ... } else {
read_count += 4;
}else { ... }
}for (int i = 0; i < pending_transfer_count; i++) { ... }
if (jtag_libusb_bulk_write(kitprog_handle->usb_handle,
BULK_EP_OUT, (char *)buffer,
write_count, 0, &ret)) {
LOG_ERROR("Bulk write failed");
queued_retval = ERROR_FAIL;
break;
}if (jtag_libusb_bulk_write(kitprog_handle->usb_handle, BULK_EP_OUT, (char *)buffer, write_count, 0, &ret)) { ... } else {
queued_retval = ERROR_OK;
}else { ... }
/* ... */
size_t read_count_workaround = SWD_MAX_BUFFER_LENGTH;
if (read_count % 64 == 0)
read_count_workaround = read_count;
if (jtag_libusb_bulk_read(kitprog_handle->usb_handle,
BULK_EP_IN | LIBUSB_ENDPOINT_IN, (char *)buffer,
read_count_workaround, 1000, &ret)) {
LOG_ERROR("Bulk read failed");
queued_retval = ERROR_FAIL;
break;
}if (jtag_libusb_bulk_read(kitprog_handle->usb_handle, BULK_EP_IN | LIBUSB_ENDPOINT_IN, (char *)buffer, read_count_workaround, 1000, &ret)) { ... } else {
if ((unsigned int)ret > read_count)
read_index = ret - read_count;
queued_retval = ERROR_OK;
}else { ... }
for (int i = 0; i < pending_transfer_count; i++) {
if (pending_transfers[i].cmd & SWD_CMD_RNW) {
uint32_t data = le_to_h_u32(&buffer[read_index]);
LOG_DEBUG_IO("Read result: %"PRIx32, data);
if (pending_transfers[i].buffer)
*(uint32_t *)pending_transfers[i].buffer = data;
read_index += 4;
}if (pending_transfers[i].cmd & SWD_CMD_RNW) { ... }
uint8_t ack = buffer[read_index] & 0x07;
if (ack != SWD_ACK_OK || (buffer[read_index] & 0x08)) {
LOG_DEBUG("SWD ack not OK: %d %s", i,
ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
queued_retval = swd_ack_to_error_code(ack);
break;
}if (ack != SWD_ACK_OK || (buffer[read_index] & 0x08)) { ... }
read_index++;
}for (int i = 0; i < pending_transfer_count; i++) { ... }
...} while (0);
pending_transfer_count = 0;
int retval = queued_retval;
queued_retval = ERROR_OK;
return retval;
}{ ... }
static void kitprog_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
{
if (pending_transfer_count == pending_queue_len) {
queued_retval = kitprog_swd_run_queue();
}if (pending_transfer_count == pending_queue_len) { ... }
if (queued_retval != ERROR_OK)
return;
pending_transfers[pending_transfer_count].data = data;
pending_transfers[pending_transfer_count].cmd = cmd;
if (cmd & SWD_CMD_RNW) {
pending_transfers[pending_transfer_count].buffer = dst;
}if (cmd & SWD_CMD_RNW) { ... }
pending_transfer_count++;
}{ ... }
swd lowlevel functions
static int kitprog_reset(int trst, int srst)
{
int retval = ERROR_OK;
if (trst == 1) {
LOG_ERROR("KitProg: Interface has no TRST");
return ERROR_FAIL;
}if (trst == 1) { ... }
if (srst == 1) {
retval = kitprog_reset_target();
/* ... */
}if (srst == 1) { ... }
if (retval != ERROR_OK)
LOG_ERROR("KitProg: Interface reset failed");
return retval;
}{ ... }
COMMAND_HANDLER(kitprog_handle_info_command)
{
int retval = kitprog_get_info();
return retval;
}{ ... }
COMMAND_HANDLER(kitprog_handle_acquire_psoc_command)
{
int retval = kitprog_generic_acquire();
return retval;
}{ ... }
COMMAND_HANDLER(kitprog_handle_init_acquire_psoc_command)
{
kitprog_init_acquire_psoc = true;
return ERROR_OK;
}{ ... }
static const struct command_registration kitprog_subcommand_handlers[] = {
{
.name = "info",
.handler = &kitprog_handle_info_command,
.mode = COMMAND_EXEC,
.usage = "",
.help = "show KitProg info",
...},
{
.name = "acquire_psoc",
.handler = &kitprog_handle_acquire_psoc_command,
.mode = COMMAND_EXEC,
.usage = "",
.help = "try to acquire a PSoC",
...},
{
.name = "init_acquire_psoc",
.handler = &kitprog_handle_init_acquire_psoc_command,
.mode = COMMAND_CONFIG,
.help = "try to acquire a PSoC during init",
.usage = "",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration kitprog_command_handlers[] = {
{
.name = "kitprog",
.mode = COMMAND_ANY,
.help = "perform KitProg management",
.usage = "<cmd>",
.chain = kitprog_subcommand_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
static const struct swd_driver kitprog_swd = {
.init = kitprog_swd_init,
.switch_seq = kitprog_swd_switch_seq,
.read_reg = kitprog_swd_read_reg,
.write_reg = kitprog_swd_write_reg,
.run = kitprog_swd_run_queue,
...};
static const char * const kitprog_transports[] = { "swd", NULL };
struct adapter_driver kitprog_adapter_driver = {
.name = "kitprog",
.transports = kitprog_transports,
.commands = kitprog_command_handlers,
.init = kitprog_init,
.quit = kitprog_quit,
.reset = kitprog_reset,
.swd_ops = &kitprog_swd,
...};