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
46
47
52
53
54
55
56
57
58
59
60
61
68
69
74
75
78
79
81
82
89
90
91
92
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
132
133
134
135
136
137
138
157
158
159
160
161
162
163
164
165
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
236
237
238
239
240
241
242
243
244
245
246
247
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
274
275
276
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
302
303
304
305
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
348
349
350
351
352
353
354
355
356
357
358
359
360
366
367
368
369
374
375
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
422
423
428
429
433
434
435
436
437
438
439
443
447
451
455
459
462
463
464
465
466
467
471
472
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
505
506
510
511
512
513
514
518
519
520
521
522
526
527
531
532
533
534
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <jtag/interface.h>
#include <jtag/swd.h>
#include <jtag/commands.h>
#include "libusb_helper.h"
#define VID 0x2E8A
#define PID 0x0004
#define BULK_EP_OUT 4
#define BULK_EP_IN 5
#define PICOPROBE_INTERFACE 2
#define PICOPROBE_MAX_PACKET_LENGTH 512
#define LIBUSB_TIMEOUT 10000
7 defines
struct picoprobe {
struct libusb_device_handle *usb_handle;
uint8_t *packet_buffer;
int freq;
...};
static struct swd_cmd_queue_entry {
uint8_t cmd;
uint32_t *dst;
uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
...} *swd_cmd_queue;
static size_t swd_cmd_queue_length;
static size_t swd_cmd_queue_alloced;
static int queued_retval;
static struct picoprobe *picoprobe_handle;
static int picoprobe_init(void);
static int picoprobe_quit(void);
enum PROBE_CMDS {
PROBE_INVALID = 0,
PROBE_WRITE_BITS = 1,
PROBE_READ_BITS = 2,
PROBE_SET_FREQ = 3,
PROBE_RESET = 4
...};
struct __attribute__((__packed__)) probe_cmd_hdr {
uint8_t id;
uint8_t cmd;
uint32_t bits;
...};
struct __attribute__((__packed__)) probe_pkt_hdr {
uint32_t total_packet_length;
...};
/* ... */
#define PICOPROBE_QUEUE_SIZE 64
static struct picoprobe_queue_entry {
uint8_t id;
uint8_t cmd;
unsigned bits;
unsigned offset;
const uint8_t *buf;
...} *picoprobe_queue;
static size_t picoprobe_queue_length;
static size_t picoprobe_queue_alloced;
static inline unsigned packet_length(uint8_t *pkt)
{
return pkt - picoprobe_handle->packet_buffer;
}{ ... }
static int picoprobe_bulk_write(struct probe_pkt_hdr *pkt_hdr, uint8_t *pkt)
{
pkt_hdr->total_packet_length = packet_length(pkt);
assert(pkt_hdr->total_packet_length <= PICOPROBE_MAX_PACKET_LENGTH);
int ret = 0;
jtag_libusb_bulk_write(picoprobe_handle->usb_handle,
BULK_EP_OUT, (char *)picoprobe_handle->packet_buffer, packet_length(pkt), LIBUSB_TIMEOUT,
&ret);
if (ret < 0)
return ERROR_JTAG_DEVICE_ERROR;
return ERROR_OK;
}{ ... }
static int picoprobe_flush(void)
{
LOG_DEBUG_IO("Flush %d transactions", (int)picoprobe_queue_length);
int ret = ERROR_OK;
struct probe_pkt_hdr *pkt_hdr = (struct probe_pkt_hdr *)picoprobe_handle->packet_buffer;
uint8_t *pkt = picoprobe_handle->packet_buffer + sizeof(struct probe_pkt_hdr);
unsigned total_reads = 0;
unsigned total_read_bytes = 0;
for (unsigned i = 0; i < picoprobe_queue_length; i++) {
struct picoprobe_queue_entry *q = &picoprobe_queue[i];
struct probe_cmd_hdr *hdr = (struct probe_cmd_hdr *)pkt;
if (q->id != i) {
LOG_ERROR("Wrong queue id. q->id %d != %d", q->id, i);
return ERROR_JTAG_DEVICE_ERROR;
}if (q->id != i) { ... }
hdr->id = q->id;
hdr->cmd = q->cmd;
hdr->bits = q->bits;
pkt += sizeof(struct probe_cmd_hdr);
unsigned length_bytes = DIV_ROUND_UP(q->bits, 8);
if (q->cmd == PROBE_WRITE_BITS) {
if (q->buf) {
bit_copy(pkt, 0, q->buf, q->offset, q->bits);
}if (q->buf) { ... } else {
assert(q->offset == 0);
memset(pkt, 0, length_bytes);
}else { ... }
pkt += length_bytes;
}if (q->cmd == PROBE_WRITE_BITS) { ... } else if (q->cmd == PROBE_READ_BITS) {
/* ... */
total_reads++;
total_read_bytes += length_bytes;
}else if (q->cmd == PROBE_READ_BITS) { ... } else {
return ERROR_FAIL;
}else { ... }
}for (unsigned i = 0; i < picoprobe_queue_length; i++) { ... }
ret = picoprobe_bulk_write(pkt_hdr, pkt);
if (ret < 0)
return ERROR_JTAG_DEVICE_ERROR;
if (total_reads == 0) {
picoprobe_queue_length = 0;
return ret;
}if (total_reads == 0) { ... }
unsigned rx_pkt_len = sizeof(struct probe_pkt_hdr) +
(sizeof(struct probe_cmd_hdr) * total_reads) +
total_read_bytes;
jtag_libusb_bulk_read(picoprobe_handle->usb_handle,
BULK_EP_IN | LIBUSB_ENDPOINT_IN, (char *)picoprobe_handle->packet_buffer,
rx_pkt_len, LIBUSB_TIMEOUT, &ret);
if (ret < 0)
return ERROR_JTAG_DEVICE_ERROR;
LOG_DEBUG_IO("Read %d bytes from probe", ret);
if ((int)rx_pkt_len != ret)
return ERROR_JTAG_DEVICE_ERROR;
struct probe_pkt_hdr *response_hdr = (struct probe_pkt_hdr *)picoprobe_handle->packet_buffer;
if (rx_pkt_len != response_hdr->total_packet_length)
return ERROR_JTAG_DEVICE_ERROR;
pkt = picoprobe_handle->packet_buffer + sizeof(struct probe_pkt_hdr);
for (unsigned i = 0; i < total_reads; i++) {
struct probe_cmd_hdr *read_hdr = (struct probe_cmd_hdr *)pkt;
pkt += sizeof(struct probe_cmd_hdr);
unsigned read_bytes = DIV_ROUND_UP(read_hdr->bits, 8);
if (read_hdr->cmd != PROBE_READ_BITS)
return ERROR_JTAG_DEVICE_ERROR;
uint8_t id = read_hdr->id;
struct picoprobe_queue_entry *q = &picoprobe_queue[id];
assert(read_hdr->cmd == q->cmd);
assert(read_hdr->id == q->id);
assert(read_hdr->bits == q->bits);
LOG_DEBUG_IO("Processing read of %d bits", read_hdr->bits);
memcpy((void *)q->buf, pkt, read_bytes);
pkt += read_bytes;
}for (unsigned i = 0; i < total_reads; i++) { ... }
unsigned processed_len = (pkt - picoprobe_handle->packet_buffer);
if (processed_len != rx_pkt_len)
return ERROR_JTAG_DEVICE_ERROR;
picoprobe_queue_length = 0;
keep_alive();
return ERROR_OK;
}{ ... }
static int picoprobe_read_write_bits(const uint8_t *buf, unsigned offset, unsigned length, uint8_t cmd)
{
if (picoprobe_queue_length == picoprobe_queue_alloced) {
LOG_ERROR("Picoprobe queue full");
return ERROR_BUF_TOO_SMALL;
}if (picoprobe_queue_length == picoprobe_queue_alloced) { ... } else {
LOG_DEBUG_IO("Picoprobe queue len %d -> %d", (int)picoprobe_queue_length,
(int)picoprobe_queue_length + 1);
}else { ... }
struct picoprobe_queue_entry *q = &picoprobe_queue[picoprobe_queue_length];
q->id = picoprobe_queue_length++;
q->cmd = cmd;
q->bits = length;
q->offset = offset;
q->buf = buf;
return ERROR_OK;
}{ ... }
static int picoprobe_write_bits(const uint8_t *buf, unsigned offset, unsigned length)
{
LOG_DEBUG_IO("Write %d bits @ offset %d", length, offset);
return picoprobe_read_write_bits(buf, offset, length, PROBE_WRITE_BITS);
}{ ... }
static int picoprobe_read_bits(const uint8_t *buf, unsigned offset, unsigned length)
{
LOG_DEBUG_IO("Read %d bits @ offset %d", length, offset);
if (picoprobe_queue_length == picoprobe_queue_alloced)
return ERROR_BUF_TOO_SMALL;
return picoprobe_read_write_bits(buf, offset, length, PROBE_READ_BITS);
}{ ... }
static int picoprobe_swd_run_queue(void)
{
LOG_DEBUG_IO("Executing %zu queued transactions", swd_cmd_queue_length);
int retval;
queued_retval = picoprobe_flush();
if (queued_retval != ERROR_OK) {
LOG_DEBUG_IO("Skipping due to previous errors: %d", queued_retval);
goto skip;
}if (queued_retval != ERROR_OK) { ... }
for (size_t i = 0; i < swd_cmd_queue_length; i++) {
if (0 == ((swd_cmd_queue[i].cmd ^ swd_cmd(false, false, DP_TARGETSEL)) &
(SWD_CMD_APNDP|SWD_CMD_RNW|SWD_CMD_A32))) {
buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1, 3, SWD_ACK_OK);
}if (0 == ((swd_cmd_queue[i].cmd ^ swd_cmd(false, false, DP_TARGETSEL)) & (SWD_CMD_APNDP|SWD_CMD_RNW|SWD_CMD_A32))) { ... }
LOG_DEBUG_IO("trn_ack_data_parity_trn:");
for (size_t y = 0; y < sizeof(swd_cmd_queue[i].trn_ack_data_parity_trn); y++)
LOG_DEBUG_IO("BYTE %d 0x%x", (int)y, swd_cmd_queue[i].trn_ack_data_parity_trn[y]);
int ack = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1, 3);
LOG_DEBUG_IO("%s %s %s reg %X = %08"PRIx32,
ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
swd_cmd_queue[i].cmd & SWD_CMD_APNDP ? "AP" : "DP",
swd_cmd_queue[i].cmd & SWD_CMD_RNW ? "read" : "write",
(swd_cmd_queue[i].cmd & SWD_CMD_A32) >> 1,
buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn,
1 + 3 + (swd_cmd_queue[i].cmd & SWD_CMD_RNW ? 0 : 1), 32));
if (ack != SWD_ACK_OK) {
queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
goto skip;
}if (ack != SWD_ACK_OK) { ... } else if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
uint32_t data = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3, 32);
int parity = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 32, 1);
if (parity != parity_u32(data)) {
LOG_ERROR("SWD Read data parity mismatch");
queued_retval = ERROR_FAIL;
goto skip;
}if (parity != parity_u32(data)) { ... }
if (swd_cmd_queue[i].dst != NULL)
*swd_cmd_queue[i].dst = data;
}else if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) { ... }
}for (size_t i = 0; i < swd_cmd_queue_length; i++) { ... }
skip:
for (size_t i = 0; i < swd_cmd_queue_length; i++)
swd_cmd_queue[i].dst = NULL;
swd_cmd_queue_length = 0;
retval = queued_retval;
queued_retval = ERROR_OK;
return retval;
}{ ... }
static void picoprobe_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
{
if (swd_cmd_queue_length == swd_cmd_queue_alloced)
queued_retval = picoprobe_swd_run_queue();
if (queued_retval != ERROR_OK)
return;
size_t i = swd_cmd_queue_length++;
swd_cmd_queue[i].cmd = cmd | SWD_CMD_START | SWD_CMD_PARK;
picoprobe_write_bits(&swd_cmd_queue[i].cmd, 0, 8);
if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
swd_cmd_queue[i].dst = dst;
picoprobe_read_bits(swd_cmd_queue[i].trn_ack_data_parity_trn,
0, 1 + 3 + 32 + 1 + 1);
}if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) { ... } else {
picoprobe_read_bits(swd_cmd_queue[i].trn_ack_data_parity_trn,
0, 1 + 3 + 1);
buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1, 32, data);
buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(data));
picoprobe_write_bits(swd_cmd_queue[i].trn_ack_data_parity_trn,
1 + 3 + 1, 32 + 1);
}else { ... }
if (cmd & SWD_CMD_APNDP) {
if (ap_delay_clk == 0)
return;
LOG_DEBUG("Add %d idle cycles", ap_delay_clk);
picoprobe_write_bits(NULL, 0, ap_delay_clk);
}if (cmd & SWD_CMD_APNDP) { ... }
}{ ... }
static void picoprobe_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
{
assert(cmd & SWD_CMD_RNW);
picoprobe_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
}{ ... }
static void picoprobe_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
{
assert(!(cmd & SWD_CMD_RNW));
picoprobe_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
}{ ... }
static int_least32_t picoprobe_set_frequency(int_least32_t hz)
{
int ret;
struct probe_pkt_hdr *pkt_hdr = (struct probe_pkt_hdr *)picoprobe_handle->packet_buffer;
assert(picoprobe_queue_length == 0);
uint8_t *pkt = picoprobe_handle->packet_buffer + sizeof(struct probe_pkt_hdr);
struct probe_cmd_hdr *hdr = (struct probe_cmd_hdr *)pkt;
hdr->id = 0;
hdr->cmd = PROBE_SET_FREQ;
hdr->bits = hz / 1000;
pkt += sizeof(struct probe_cmd_hdr);
ret = picoprobe_bulk_write(pkt_hdr, pkt);
if (ret < 0)
return ERROR_JTAG_DEVICE_ERROR;
return hz;
}{ ... }
static int_least32_t picoprobe_speed(int_least32_t hz)
{
int ret = picoprobe_set_frequency(hz);
if (ret < 0)
LOG_ERROR("Couldn't set picoprobe speed");
else
picoprobe_handle->freq = ret;
return ERROR_OK;
}{ ... }
static int picoprobe_khz(int khz, int *jtag_speed)
{
*jtag_speed = khz * 1000;
return ERROR_OK;
}{ ... }
static int picoprobe_speed_div(int speed, int *khz)
{
*khz = speed / 1000;
return ERROR_OK;
}{ ... }
static int picoprobe_swd_init(void)
{
return ERROR_OK;
}{ ... }
static int picoprobe_swd_switch_seq(enum swd_special_seq seq)
{
int ret = ERROR_OK;
switch (seq) {
case LINE_RESET:
LOG_DEBUG_IO("SWD line reset");
ret = picoprobe_write_bits(swd_seq_line_reset, 0, swd_seq_line_reset_len);
break;case LINE_RESET:
case JTAG_TO_SWD:
LOG_DEBUG("JTAG-to-SWD");
ret = picoprobe_write_bits(swd_seq_jtag_to_swd, 0, swd_seq_jtag_to_swd_len);
break;case JTAG_TO_SWD:
case SWD_TO_JTAG:
LOG_DEBUG("SWD-to-JTAG");
ret = picoprobe_write_bits(swd_seq_swd_to_jtag, 0, swd_seq_swd_to_jtag_len);
break;case SWD_TO_JTAG:
case DORMANT_TO_SWD:
LOG_DEBUG("DORMANT-to-SWD");
ret = picoprobe_write_bits(swd_seq_dormant_to_swd, 0, swd_seq_dormant_to_swd_len);
break;case DORMANT_TO_SWD:
case SWD_TO_DORMANT:
LOG_DEBUG("SWD-to-DORMANT");
ret = picoprobe_write_bits(swd_seq_swd_to_dormant, 0, swd_seq_swd_to_dormant_len);
break;case SWD_TO_DORMANT:
default:
LOG_ERROR("Sequence %d not supported", seq);
return ERROR_FAIL;default
}switch (seq) { ... }
return ret;
}{ ... }
static int picoprobe_reset(int trst, int srst)
{
return ERROR_OK;
}{ ... }
static const struct swd_driver picoprobe_swd = {
.init = picoprobe_swd_init,
.switch_seq = picoprobe_swd_switch_seq,
.read_reg = picoprobe_swd_read_reg,
.write_reg = picoprobe_swd_write_reg,
.run = picoprobe_swd_run_queue,
...};
static const char * const picoprobe_transports[] = { "swd", NULL };
struct adapter_driver picoprobe_adapter_driver = {
.name = "picoprobe",
.commands = NULL,
.transports = picoprobe_transports,
.swd_ops = &picoprobe_swd,
.init = picoprobe_init,
.quit = picoprobe_quit,
.reset = picoprobe_reset,
.speed = picoprobe_speed,
.speed_div = picoprobe_speed_div,
.khz = picoprobe_khz,
...};
static int picoprobe_usb_open(void)
{
const uint16_t vids[] = { VID, 0 };
const uint16_t pids[] = { PID, 0 };
if (jtag_libusb_open(vids, pids, NULL,
&picoprobe_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, &picoprobe_handle->usb_handle, NULL) != ERROR_OK) { ... }
if (libusb_claim_interface(picoprobe_handle->usb_handle, PICOPROBE_INTERFACE) != ERROR_OK) {
LOG_ERROR("Failed to claim picoprobe interface");
return ERROR_FAIL;
}if (libusb_claim_interface(picoprobe_handle->usb_handle, PICOPROBE_INTERFACE) != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static void picoprobe_usb_close(void)
{
jtag_libusb_close(picoprobe_handle->usb_handle);
}{ ... }
static int picoprobe_init(void)
{
picoprobe_handle = malloc(sizeof(struct picoprobe));
if (picoprobe_handle == NULL) {
LOG_ERROR("Failed to allocate memory");
return ERROR_FAIL;
}if (picoprobe_handle == NULL) { ... }
if (picoprobe_usb_open() != ERROR_OK) {
LOG_ERROR("Can't find a picoprobe device! Please check device connections and permissions.");
return ERROR_JTAG_INIT_FAILED;
}if (picoprobe_usb_open() != ERROR_OK) { ... }
picoprobe_handle->packet_buffer = malloc(PICOPROBE_MAX_PACKET_LENGTH);
if (picoprobe_handle->packet_buffer == NULL) {
LOG_ERROR("Failed to allocate memory for the packet buffer");
return ERROR_FAIL;
}if (picoprobe_handle->packet_buffer == NULL) { ... }
picoprobe_queue_alloced = PICOPROBE_QUEUE_SIZE;
picoprobe_queue_length = 0;
picoprobe_queue = malloc(picoprobe_queue_alloced * sizeof(*picoprobe_queue));
if (picoprobe_queue == NULL)
return ERROR_FAIL;
swd_cmd_queue_alloced = 10;
swd_cmd_queue = malloc(swd_cmd_queue_alloced * sizeof(*swd_cmd_queue));
return swd_cmd_queue != NULL ? ERROR_OK : ERROR_FAIL;
}{ ... }
static int picoprobe_quit(void)
{
picoprobe_usb_close();
return ERROR_OK;
}{ ... }