forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmsis_dap.c
1060 lines (936 loc) · 25.6 KB
/
cmsis_dap.c
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
117
118
119
120
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
156
157
158
159
160
161
162
163
164
165
166
167
168
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
342
343
344
345
346
347
348
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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018-2019 PHYTEC Messtechnik GmbH
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* This file is based on DAP.c from CMSIS-DAP Source (Revision: V2.0.0)
* https://github.com/ARM-software/CMSIS_5/tree/develop/CMSIS/DAP/Firmware
* Copyright (c) 2013-2017 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/drivers/swdp.h>
#include <stdint.h>
#include <cmsis_dap.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(dap, CONFIG_DAP_LOG_LEVEL);
#define DAP_STATE_CONNECTED 0
struct dap_context {
struct device *swdp_dev;
atomic_t state;
uint8_t debug_port;
uint8_t capabilities;
uint16_t pkt_size;
struct {
/* Idle cycles after transfer */
uint8_t idle_cycles;
/* Number of retries after WAIT response */
uint16_t retry_count;
/* Number of retries if read value does not match */
uint16_t match_retry;
/* Match Mask */
uint32_t match_mask;
} transfer;
};
static struct dap_context dap_ctx[1];
BUILD_ASSERT(sizeof(CONFIG_CMSIS_DAP_PROBE_VENDOR) <=
MIN(CONFIG_CMSIS_DAP_PACKET_SIZE - 2, UINT8_MAX - 2),
"PROBE_VENDOR string is too long.");
BUILD_ASSERT(sizeof(CONFIG_CMSIS_DAP_PROBE_NAME) <=
MIN(CONFIG_CMSIS_DAP_PACKET_SIZE - 2, UINT8_MAX - 2),
"PROBE_NAME string is too long.");
BUILD_ASSERT(sizeof(CONFIG_CMSIS_DAP_BOARD_VENDOR) <=
MIN(CONFIG_CMSIS_DAP_PACKET_SIZE - 2, UINT8_MAX - 2),
"BOARD_VENDOR string is too long.");
BUILD_ASSERT(sizeof(CONFIG_CMSIS_DAP_BOARD_NAME) <=
MIN(CONFIG_CMSIS_DAP_PACKET_SIZE - 2, UINT8_MAX - 2),
"BOARD_NAME string is too long.");
BUILD_ASSERT(sizeof(CONFIG_CMSIS_DAP_DEVICE_VENDOR) <=
MIN(CONFIG_CMSIS_DAP_PACKET_SIZE - 2, UINT8_MAX - 2),
"DEVICE_VENDOR string is too long.");
BUILD_ASSERT(sizeof(CONFIG_CMSIS_DAP_DEVICE_NAME) <=
MIN(CONFIG_CMSIS_DAP_PACKET_SIZE - 2, UINT8_MAX - 2),
"DEVICE_NAME string is too long.");
/* Get DAP Information */
static uint16_t dap_info(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
uint8_t *info = response + 1;
uint8_t id = request[0];
uint8_t length = 0U;
switch (id) {
case DAP_ID_VENDOR:
LOG_DBG("ID_VENDOR");
memcpy(info, CONFIG_CMSIS_DAP_PROBE_VENDOR,
sizeof(CONFIG_CMSIS_DAP_PROBE_VENDOR));
length = sizeof(CONFIG_CMSIS_DAP_PROBE_VENDOR);
break;
case DAP_ID_PRODUCT:
LOG_DBG("ID_PRODUCT");
memcpy(info, CONFIG_CMSIS_DAP_PROBE_NAME,
sizeof(CONFIG_CMSIS_DAP_PROBE_NAME));
length = sizeof(CONFIG_CMSIS_DAP_PROBE_NAME);
break;
case DAP_ID_SER_NUM:
/* optional to implement */
LOG_DBG("ID_SER_NUM unsupported");
break;
case DAP_ID_FW_VER:
LOG_DBG("ID_FW_VER");
memcpy(info, DAP_FW_VER, sizeof(DAP_FW_VER));
length = sizeof(DAP_FW_VER);
break;
case DAP_ID_DEVICE_VENDOR:
LOG_DBG("ID_DEVICE_VENDOR");
memcpy(info, CONFIG_CMSIS_DAP_DEVICE_VENDOR,
sizeof(CONFIG_CMSIS_DAP_DEVICE_VENDOR));
length = sizeof(CONFIG_CMSIS_DAP_DEVICE_VENDOR);
break;
case DAP_ID_DEVICE_NAME:
LOG_DBG("ID_DEVICE_NAME");
memcpy(info, CONFIG_CMSIS_DAP_DEVICE_NAME,
sizeof(CONFIG_CMSIS_DAP_DEVICE_NAME));
length = sizeof(CONFIG_CMSIS_DAP_DEVICE_NAME);
break;
case DAP_ID_BOARD_VENDOR:
LOG_DBG("ID_BOARD_VENDOR");
memcpy(info, CONFIG_CMSIS_DAP_BOARD_VENDOR,
sizeof(CONFIG_CMSIS_DAP_BOARD_VENDOR));
length = sizeof(CONFIG_CMSIS_DAP_BOARD_VENDOR);
break;
case DAP_ID_BOARD_NAME:
memcpy(info, CONFIG_CMSIS_DAP_BOARD_NAME,
sizeof(CONFIG_CMSIS_DAP_BOARD_NAME));
length = sizeof(CONFIG_CMSIS_DAP_BOARD_NAME);
LOG_DBG("ID_BOARD_NAME");
break;
case DAP_ID_PRODUCT_FW_VER:
/* optional to implement */
LOG_DBG("ID_PRODUCT_FW_VER unsupported");
break;
case DAP_ID_CAPABILITIES:
info[0] = ctx->capabilities;
LOG_DBG("ID_CAPABILITIES 0x%0x", info[0]);
length = 1U;
break;
case DAP_ID_TIMESTAMP_CLOCK:
LOG_DBG("ID_TIMESTAMP_CLOCK unsupported");
break;
case DAP_ID_UART_RX_BUFFER_SIZE:
LOG_DBG("ID_UART_RX_BUFFER_SIZE unsupported");
break;
case DAP_ID_UART_TX_BUFFER_SIZE:
LOG_DBG("ID_UART_TX_BUFFER_SIZE unsupported");
break;
case DAP_ID_SWO_BUFFER_SIZE:
LOG_DBG("ID_SWO_BUFFER_SIZE unsupported");
break;
case DAP_ID_PACKET_SIZE:
LOG_DBG("ID_PACKET_SIZE");
sys_put_le16(ctx->pkt_size, &info[0]);
length = 2U;
break;
case DAP_ID_PACKET_COUNT:
LOG_DBG("ID_PACKET_COUNT");
info[0] = CONFIG_CMSIS_DAP_PACKET_COUNT;
length = 1U;
break;
default:
LOG_DBG("unsupported ID");
break;
}
response[0] = length;
return length + 1U;
}
/* Process Host Status command and prepare response */
static uint16_t dap_host_status(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
switch (request[0]) {
case DAP_DEBUGGER_CONNECTED:
if (request[1]) {
LOG_INF("Debugger connected");
} else {
LOG_INF("Debugger disconnected");
}
break;
case DAP_TARGET_RUNNING:
LOG_DBG("unsupported");
break;
default:
*response = DAP_ERROR;
return 1U;
}
response[0] = DAP_OK;
return 1U;
}
/* Process Connect command and prepare response */
static uint16_t dap_connect(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
const struct swdp_api *api = ctx->swdp_dev->api;
uint8_t port;
if (request[0] == DAP_PORT_AUTODETECT) {
port = DAP_PORT_SWD;
} else {
port = request[0];
}
switch (port) {
case DAP_PORT_SWD:
LOG_INF("port swd");
ctx->debug_port = DAP_PORT_SWD;
if (atomic_test_and_set_bit(&ctx->state,
DAP_STATE_CONNECTED)) {
LOG_ERR("DAP device is already connected");
break;
}
api->swdp_port_on(ctx->swdp_dev);
break;
case DAP_PORT_JTAG:
LOG_ERR("port unsupported");
port = DAP_ERROR;
break;
default:
LOG_DBG("port disabled");
port = DAP_PORT_DISABLED;
break;
}
response[0] = port;
return 1U;
}
/* Process Disconnect command and prepare response */
static uint16_t dap_disconnect(struct dap_context *const ctx,
uint8_t *const response)
{
const struct swdp_api *api = ctx->swdp_dev->api;
LOG_DBG("");
ctx->debug_port = DAP_PORT_DISABLED;
if (atomic_test_bit(&ctx->state, DAP_STATE_CONNECTED)) {
api->swdp_port_off(ctx->swdp_dev);
} else {
LOG_WRN("DAP device is not connected");
}
response[0] = DAP_OK;
atomic_clear_bit(&ctx->state, DAP_STATE_CONNECTED);
return 1;
}
/* Process Delay command and prepare response */
static uint16_t dap_delay(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
uint16_t delay = sys_get_le16(&request[0]);
LOG_DBG("dap delay %u ms", delay);
k_busy_wait(delay * USEC_PER_MSEC);
response[0] = DAP_OK;
return 1U;
}
/* Process Reset Target command and prepare response */
static uint16_t dap_reset_target(struct dap_context *const ctx,
uint8_t *const response)
{
response[0] = DAP_OK;
response[1] = 0U;
LOG_WRN("unsupported");
return 2;
}
/* Process SWJ Pins command and prepare response */
static uint16_t dap_swj_pins(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
const struct swdp_api *api = ctx->swdp_dev->api;
uint8_t value = request[0];
uint8_t select = request[1];
uint32_t wait = sys_get_le32(&request[2]);
k_timepoint_t end = sys_timepoint_calc(K_USEC(wait));
uint8_t state;
if (!atomic_test_bit(&ctx->state, DAP_STATE_CONNECTED)) {
LOG_ERR("DAP device is not connected");
response[0] = DAP_ERROR;
return 1U;
}
/* Skip if nothing selected. */
if (select) {
api->swdp_set_pins(ctx->swdp_dev, select, value);
}
do {
api->swdp_get_pins(ctx->swdp_dev, &state);
LOG_INF("select 0x%02x, value 0x%02x, wait %u, state 0x%02x",
select, value, wait, state);
if ((value & select) == (state & select)) {
LOG_DBG("swdp_get_pins succeeded before timeout");
break;
}
} while (!sys_timepoint_expired(end));
response[0] = state;
return sizeof(state);
}
/* Process SWJ Clock command and prepare response */
static uint16_t dap_swj_clock(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
const struct swdp_api *api = ctx->swdp_dev->api;
uint32_t clk = sys_get_le32(&request[0]);
LOG_DBG("clock %d", clk);
if (atomic_test_bit(&ctx->state, DAP_STATE_CONNECTED)) {
if (clk) {
api->swdp_set_clock(ctx->swdp_dev, clk);
response[0] = DAP_OK;
} else {
response[0] = DAP_ERROR;
}
} else {
LOG_WRN("DAP device is not connected");
response[0] = DAP_OK;
}
return 1U;
}
/* Process SWJ Sequence command and prepare response */
static uint16_t dap_swj_sequence(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
const struct swdp_api *api = ctx->swdp_dev->api;
uint16_t count = request[0];
LOG_DBG("count %u", count);
if (count == 0U) {
count = 256U;
}
if (!atomic_test_bit(&ctx->state, DAP_STATE_CONNECTED)) {
LOG_ERR("DAP device is not connected");
response[0] = DAP_ERROR;
return 1U;
}
api->swdp_output_sequence(ctx->swdp_dev, count, &request[1]);
response[0] = DAP_OK;
return 1U;
}
/* Process SWD Configure command and prepare response */
static uint16_t dap_swdp_configure(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
const struct swdp_api *api = ctx->swdp_dev->api;
uint8_t turnaround = (request[0] & 0x03U) + 1U;
bool data_phase = (request[0] & 0x04U) ? true : false;
if (!atomic_test_bit(&ctx->state, DAP_STATE_CONNECTED)) {
LOG_ERR("DAP device is not connected");
response[0] = DAP_ERROR;
return 1U;
}
api->swdp_configure(ctx->swdp_dev, turnaround, data_phase);
response[0] = DAP_OK;
return 1U;
}
/* Process Transfer Configure command and prepare response */
static uint16_t dap_transfer_cfg(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
ctx->transfer.idle_cycles = request[0];
ctx->transfer.retry_count = sys_get_le16(&request[1]);
ctx->transfer.match_retry = sys_get_le16(&request[3]);
LOG_DBG("idle_cycles %d, retry_count %d, match_retry %d",
ctx->transfer.idle_cycles,
ctx->transfer.retry_count,
ctx->transfer.match_retry);
response[0] = DAP_OK;
return 1U;
}
static inline uint8_t do_swdp_transfer(struct dap_context *const ctx,
const uint8_t req_val,
uint32_t *data)
{
const struct swdp_api *api = ctx->swdp_dev->api;
uint32_t retry = ctx->transfer.retry_count;
uint8_t rspns_val;
do {
api->swdp_transfer(ctx->swdp_dev,
req_val,
data,
ctx->transfer.idle_cycles,
&rspns_val);
} while ((rspns_val == SWDP_ACK_WAIT) && retry--);
return rspns_val;
}
static uint8_t swdp_transfer_match(struct dap_context *const ctx,
const uint8_t req_val,
const uint32_t match_val)
{
uint32_t match_retry = ctx->transfer.match_retry;
uint32_t data;
uint8_t rspns_val;
if (req_val & SWDP_REQUEST_APnDP) {
/* Post AP read, result will be returned on the next transfer */
rspns_val = do_swdp_transfer(ctx, req_val, NULL);
if (rspns_val != SWDP_ACK_OK) {
return rspns_val;
}
}
do {
/*
* Read register until its value matches
* or retry counter expires
*/
rspns_val = do_swdp_transfer(ctx, req_val, &data);
if (rspns_val != SWDP_ACK_OK) {
return rspns_val;
}
} while (((data & ctx->transfer.match_mask) != match_val) &&
match_retry--);
if ((data & ctx->transfer.match_mask) != match_val) {
rspns_val |= DAP_TRANSFER_MISMATCH;
}
return rspns_val;
}
/*
* Process SWD Transfer command and prepare response
* pyOCD counterpart is _encode_transfer_data.
* Packet format: one byte DAP_index (ignored)
* one bytes transfer_count
* following by number transfer_count pairs of
* one byte request (register)
* four byte data (for write request only)
*/
static uint16_t dap_swdp_transfer(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
uint8_t *rspns_buf;
const uint8_t *req_buf;
uint8_t rspns_cnt = 0;
uint8_t rspns_val = 0;
bool post_read = false;
uint32_t check_write = 0;
uint8_t req_cnt;
uint8_t req_val;
uint32_t match_val;
uint32_t data;
/* Ignore DAP index request[0] */
req_cnt = request[1];
req_buf = request + sizeof(req_cnt) + 1;
rspns_buf = response + (sizeof(rspns_cnt) + sizeof(rspns_val));
for (; req_cnt; req_cnt--) {
req_val = *req_buf++;
if (req_val & SWDP_REQUEST_RnW) {
/* Read register */
if (post_read) {
/*
* Read was posted before, read previous AP
* data or post next AP read.
*/
if ((req_val & (SWDP_REQUEST_APnDP |
DAP_TRANSFER_MATCH_VALUE)) !=
SWDP_REQUEST_APnDP) {
req_val = DP_RDBUFF | SWDP_REQUEST_RnW;
post_read = false;
}
rspns_val = do_swdp_transfer(ctx, req_val, &data);
if (rspns_val != SWDP_ACK_OK) {
break;
}
/* Store previous AP data */
sys_put_le32(data, rspns_buf);
rspns_buf += sizeof(data);
}
if (req_val & DAP_TRANSFER_MATCH_VALUE) {
LOG_INF("match value read");
/* Read with value match */
match_val = sys_get_le32(req_buf);
req_buf += sizeof(match_val);
rspns_val = swdp_transfer_match(ctx, req_val, match_val);
if (rspns_val != SWDP_ACK_OK) {
break;
}
} else if (req_val & SWDP_REQUEST_APnDP) {
/* Normal read */
if (!post_read) {
/* Post AP read */
rspns_val = do_swdp_transfer(ctx, req_val, NULL);
if (rspns_val != SWDP_ACK_OK) {
break;
}
post_read = true;
}
} else {
/* Read DP register */
rspns_val = do_swdp_transfer(ctx, req_val, &data);
if (rspns_val != SWDP_ACK_OK) {
break;
}
/* Store data */
sys_put_le32(data, rspns_buf);
rspns_buf += sizeof(data);
}
check_write = 0U;
} else {
/* Write register */
if (post_read) {
/* Read previous data */
rspns_val = do_swdp_transfer(ctx,
DP_RDBUFF | SWDP_REQUEST_RnW,
&data);
if (rspns_val != SWDP_ACK_OK) {
break;
}
/* Store previous data */
sys_put_le32(data, rspns_buf);
rspns_buf += sizeof(data);
post_read = false;
}
/* Load data */
data = sys_get_le32(req_buf);
req_buf += sizeof(data);
if (req_val & DAP_TRANSFER_MATCH_MASK) {
/* Write match mask */
ctx->transfer.match_mask = data;
rspns_val = SWDP_ACK_OK;
} else {
/* Write DP/AP register */
rspns_val = do_swdp_transfer(ctx, req_val, &data);
if (rspns_val != SWDP_ACK_OK) {
break;
}
check_write = 1U;
}
}
rspns_cnt++;
}
if (rspns_val == SWDP_ACK_OK) {
if (post_read) {
/* Read previous data */
rspns_val = do_swdp_transfer(ctx,
DP_RDBUFF | SWDP_REQUEST_RnW,
&data);
if (rspns_val != SWDP_ACK_OK) {
goto end;
}
/* Store previous data */
sys_put_le32(data, rspns_buf);
rspns_buf += sizeof(data);
} else if (check_write) {
/* Check last write */
rspns_val = do_swdp_transfer(ctx,
DP_RDBUFF | SWDP_REQUEST_RnW,
NULL);
}
}
end:
response[0] = rspns_cnt;
response[1] = rspns_val;
return (rspns_buf - response);
}
/* Delegate DAP Transfer command */
static uint16_t dap_transfer(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
uint16_t retval;
if (!atomic_test_bit(&ctx->state, DAP_STATE_CONNECTED)) {
LOG_ERR("DAP device is not connected");
response[0] = DAP_ERROR;
return 1U;
}
switch (ctx->debug_port) {
case DAP_PORT_SWD:
retval = dap_swdp_transfer(ctx, request, response);
break;
case DAP_PORT_JTAG:
default:
LOG_ERR("port unsupported");
response[0] = DAP_ERROR;
retval = 1U;
}
return retval;
}
static uint16_t dap_swdp_sequence(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
const struct swdp_api *api = ctx->swdp_dev->api;
const uint8_t *request_data = request + 1;
uint8_t *response_data = response + 1;
uint8_t count = request[0];
uint8_t num_cycles;
uint32_t num_bytes;
bool input;
switch (ctx->debug_port) {
case DAP_PORT_SWD:
response[0] = DAP_OK;
break;
case DAP_PORT_JTAG:
default:
LOG_ERR("port unsupported");
response[0] = DAP_ERROR;
return 1U;
}
for (size_t i = 0; i < count; ++i) {
input = *request_data & BIT(7);
num_cycles = *request_data & BIT_MASK(7);
num_bytes = (num_cycles + 7) >> 3; /* rounded up to full bytes */
if (num_cycles == 0) {
num_cycles = 64;
}
request_data += 1;
if (input) {
api->swdp_input_sequence(ctx->swdp_dev, num_cycles, response_data);
response_data += num_bytes;
} else {
api->swdp_output_sequence(ctx->swdp_dev, num_cycles, request_data);
request_data += num_bytes;
}
}
return response_data - response;
}
/*
* Process SWD DAP_TransferBlock command and prepare response.
* pyOCD counterpart is _encode_transfer_block_data.
* Packet format: one byte DAP_index (ignored)
* two bytes transfer_count
* one byte block_request (register)
* data[transfer_count * sizeof(uint32_t)]
*/
static uint16_t dap_swdp_transferblock(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
uint32_t data;
uint8_t *rspns_buf;
const uint8_t *req_buf;
uint16_t rspns_cnt = 0;
uint16_t req_cnt;
uint8_t rspns_val = 0;
uint8_t req_val;
req_cnt = sys_get_le16(&request[1]);
req_val = request[3];
req_buf = request + (sizeof(req_cnt) + sizeof(req_val) + 1);
rspns_buf = response + (sizeof(rspns_cnt) + sizeof(rspns_val));
if (req_cnt == 0U) {
goto end;
}
if (req_val & SWDP_REQUEST_RnW) {
/* Read register block */
if (req_val & SWDP_REQUEST_APnDP) {
/* Post AP read */
rspns_val = do_swdp_transfer(ctx, req_val, NULL);
if (rspns_val != SWDP_ACK_OK) {
goto end;
}
}
while (req_cnt--) {
/* Read DP/AP register */
if ((req_cnt == 0U) &&
(req_val & SWDP_REQUEST_APnDP)) {
/* Last AP read */
req_val = DP_RDBUFF | SWDP_REQUEST_RnW;
}
rspns_val = do_swdp_transfer(ctx, req_val, &data);
if (rspns_val != SWDP_ACK_OK) {
goto end;
}
/* Store data */
sys_put_le32(data, rspns_buf);
rspns_buf += sizeof(data);
rspns_cnt++;
}
} else {
/* Write register block */
while (req_cnt--) {
/* Load data */
data = sys_get_le32(req_buf);
req_buf += sizeof(data);
/* Write DP/AP register */
rspns_val = do_swdp_transfer(ctx, req_val, &data);
if (rspns_val != SWDP_ACK_OK) {
goto end;
}
rspns_cnt++;
}
/* Check last write */
rspns_val = do_swdp_transfer(ctx, DP_RDBUFF | SWDP_REQUEST_RnW, NULL);
}
end:
sys_put_le16(rspns_cnt, &response[0]);
response[2] = rspns_val;
LOG_DBG("Received %u, to transmit %u, response count %u",
req_buf - request,
rspns_buf - response,
rspns_cnt * 4);
return (rspns_buf - response);
}
/* Delegate Transfer Block command */
static uint16_t dap_transferblock(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
uint16_t retval;
if (!atomic_test_bit(&ctx->state, DAP_STATE_CONNECTED)) {
LOG_ERR("DAP device is not connected");
/* Clear response count */
sys_put_le16(0U, &response[0]);
/* Clear DAP response (ACK) value */
response[2] = 0U;
return 3U;
}
switch (ctx->debug_port) {
case DAP_PORT_SWD:
retval = dap_swdp_transferblock(ctx, request, response);
break;
case DAP_PORT_JTAG:
default:
LOG_ERR("port unsupported");
/* Clear response count */
sys_put_le16(0U, &response[0]);
/* Clear DAP response (ACK) value */
response[2] = 0U;
retval = 3U;
}
return retval;
}
/* Process SWD Write ABORT command and prepare response */
static uint16_t dap_swdp_writeabort(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
const struct swdp_api *api = ctx->swdp_dev->api;
/* Load data (Ignore DAP index in request[0]) */
uint32_t data = sys_get_le32(&request[1]);
/* Write Abort register */
api->swdp_transfer(ctx->swdp_dev, DP_ABORT, &data,
ctx->transfer.idle_cycles, NULL);
response[0] = DAP_OK;
return 1U;
}
/* Delegate DAP Write ABORT command */
static uint16_t dap_writeabort(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
uint16_t retval;
if (!atomic_test_bit(&ctx->state, DAP_STATE_CONNECTED)) {
LOG_ERR("DAP device is not connected");
response[0] = DAP_ERROR;
return 1U;
}
switch (ctx->debug_port) {
case DAP_PORT_SWD:
retval = dap_swdp_writeabort(ctx, request, response);
break;
case DAP_PORT_JTAG:
default:
LOG_ERR("port unsupported");
response[0] = DAP_ERROR;
retval = 1U;
}
return retval;
}
/* Process DAP Vendor command request */
static uint16_t dap_process_vendor_cmd(struct dap_context *const ctx,
const uint8_t *const request,
uint8_t *const response)
{
response[0] = ID_DAP_INVALID;
return 1U;
}
/*
* Process DAP command request and prepare response
* request: pointer to request data
* response: pointer to response data
* return: number of bytes in response
*
* All the subsequent command functions have the same parameter
* and return value structure.
*/
static uint16_t dap_process_cmd(struct dap_context *const ctx,
const uint8_t *request,
uint8_t *response)
{
uint16_t retval;
LOG_HEXDUMP_DBG(request, 8, "req");
if ((*request >= ID_DAP_VENDOR0) && (*request <= ID_DAP_VENDOR31)) {
return dap_process_vendor_cmd(ctx, request, response);
}
*response++ = *request;
LOG_DBG("request 0x%02x", *request);
switch (*request++) {
case ID_DAP_INFO:
retval = dap_info(ctx, request, response);
break;
case ID_DAP_HOST_STATUS:
retval = dap_host_status(ctx, request, response);
break;
case ID_DAP_CONNECT:
retval = dap_connect(ctx, request, response);
break;
case ID_DAP_DISCONNECT:
retval = dap_disconnect(ctx, response);
break;
case ID_DAP_DELAY:
retval = dap_delay(ctx, request, response);
break;
case ID_DAP_RESET_TARGET:
retval = dap_reset_target(ctx, response);
break;
case ID_DAP_SWJ_PINS:
retval = dap_swj_pins(ctx, request, response);
break;
case ID_DAP_SWJ_CLOCK:
retval = dap_swj_clock(ctx, request, response);
break;
case ID_DAP_SWJ_SEQUENCE:
retval = dap_swj_sequence(ctx, request, response);
break;
case ID_DAP_SWDP_CONFIGURE:
retval = dap_swdp_configure(ctx, request, response);
break;
case ID_DAP_SWDP_SEQUENCE:
retval = dap_swdp_sequence(ctx, request, response);
break;
case ID_DAP_JTAG_SEQUENCE:
LOG_ERR("JTAG sequence unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_JTAG_CONFIGURE:
LOG_ERR("JTAG configure unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_JTAG_IDCODE:
LOG_ERR("JTAG IDCODE unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_TRANSFER_CONFIGURE:
retval = dap_transfer_cfg(ctx, request, response);
break;
case ID_DAP_TRANSFER:
retval = dap_transfer(ctx, request, response);
break;
case ID_DAP_TRANSFER_BLOCK:
retval = dap_transferblock(ctx, request, response);
break;
case ID_DAP_WRITE_ABORT:
retval = dap_writeabort(ctx, request, response);
break;
case ID_DAP_SWO_TRANSPORT:
LOG_ERR("SWO Transport unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_SWO_MODE:
LOG_ERR("SWO Mode unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_SWO_BAUDRATE:
LOG_ERR("SWO Baudrate unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_SWO_CONTROL:
LOG_ERR("SWO Control unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_SWO_STATUS:
LOG_ERR("SWO Status unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_SWO_DATA:
LOG_ERR("SWO Data unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_UART_TRANSPORT:
LOG_ERR("UART Transport unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_UART_CONFIGURE:
LOG_ERR("UART Configure unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_UART_CONTROL:
LOG_ERR("UART Control unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_UART_STATUS:
LOG_ERR("UART Status unsupported");
retval = 1;
*response = DAP_ERROR;
break;
case ID_DAP_UART_TRANSFER:
LOG_ERR("UART Transfer unsupported");
retval = 1;
*response = DAP_ERROR;
break;
default:
*(response - 1) = ID_DAP_INVALID;
return 1U;