forked from myelin/arduino-cmsis-dap
-
Notifications
You must be signed in to change notification settings - Fork 39
/
DAP.cpp
1364 lines (1192 loc) · 38.2 KB
/
DAP.cpp
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
/* CMSIS-DAP Interface Firmware
* Copyright (c) 2009-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#include "DAP_config.h"
#include "DAP.h"
#include "semihost.h"
#define DAP_FW_VER "1.0" // Firmware Version
#if (DAP_PACKET_SIZE < 64)
#error "Minimum Packet Size is 64"
#endif
#if (DAP_PACKET_SIZE > 32768)
#error "Maximum Packet Size is 32768"
#endif
#if (DAP_PACKET_COUNT < 1)
#error "Minimum Packet Count is 1"
#endif
#if (DAP_PACKET_COUNT > 255)
#error "Maximum Packet Count is 255"
#endif
// Clock Macros
#define MAX_SWJ_CLOCK(delay_cycles) \
(CPU_CLOCK/2 / (IO_PORT_WRITE_CYCLES + delay_cycles))
#define CLOCK_DELAY(swj_clock) \
((CPU_CLOCK/2 / swj_clock) - IO_PORT_WRITE_CYCLES)
DAP_Data_t DAP_Data; // DAP Data
volatile uint8_t DAP_TransferAbort; // Trasfer Abort Flag
#ifdef DAP_VENDOR
const char DAP_Vendor [] = DAP_VENDOR;
#endif
#ifdef DAP_PRODUCT
const char DAP_Product[] = DAP_PRODUCT;
#endif
#ifdef DAP_SER_NUM
const char DAP_SerNum [] = DAP_SER_NUM;
#endif
const char DAP_FW_Ver [] = DAP_FW_VER;
#if TARGET_DEVICE_FIXED
const char TargetDeviceVendor [] = TARGET_DEVICE_VENDOR;
const char TargetDeviceName [] = TARGET_DEVICE_NAME;
#endif
// Get DAP Information
// id: info identifier
// info: pointer to info data
// return: number of bytes in info data
static uint8_t DAP_Info(uint8_t id, uint8_t *info) {
uint8_t length = 0;
switch (id) {
case DAP_ID_VENDOR:
#ifdef DAP_VENDOR
memcpy(info, DAP_Vendor, sizeof(DAP_Vendor));
length = sizeof(DAP_Vendor);
#endif
break;
case DAP_ID_PRODUCT:
#ifdef DAP_PRODUCT
memcpy(info, DAP_Product, sizeof(DAP_Product));
length = sizeof(DAP_Product);
#endif
break;
case DAP_ID_SER_NUM:
#ifdef DAP_SER_NUM
memcpy(info, DAP_SerNum, sizeof(DAP_SerNum));
length = sizeof(DAP_SerNum);
#endif
break;
case DAP_ID_FW_VER:
memcpy(info, DAP_FW_Ver, sizeof(DAP_FW_Ver));
length = sizeof(DAP_FW_Ver);
break;
case DAP_ID_DEVICE_VENDOR:
#if TARGET_DEVICE_FIXED
memcpy(info, TargetDeviceVendor, sizeof(TargetDeviceVendor));
length = sizeof(TargetDeviceVendor);
#endif
break;
case DAP_ID_DEVICE_NAME:
#if TARGET_DEVICE_FIXED
memcpy(info, TargetDeviceName, sizeof(TargetDeviceName));
length = sizeof(TargetDeviceName);
#endif
break;
case DAP_ID_CAPABILITIES:
info[0] = ((DAP_SWD != 0) ? (1 << 0) : 0) |
((DAP_JTAG != 0) ? (1 << 1) : 0);
length = 1;
break;
case DAP_ID_PACKET_SIZE:
info[0] = (uint8_t)(DAP_PACKET_SIZE >> 0);
info[1] = (uint8_t)(DAP_PACKET_SIZE >> 8);
length = 2;
break;
case DAP_ID_PACKET_COUNT:
info[0] = DAP_PACKET_COUNT;
length = 1;
break;
}
return (length);
}
// Timer Functions
#if ((DAP_SWD != 0) || (DAP_JTAG != 0))
// Start Timer
static __inline void TIMER_START (uint32_t usec) {
//TODO
// SysTick->VAL = 0;
// SysTick->LOAD = usec * CPU_CLOCK/1000000;
// SysTick->CTRL = (1 << SysTick_CTRL_ENABLE_Pos) |
// (1 << SysTick_CTRL_CLKSOURCE_Pos);
}
// Stop Timer
static __inline void TIMER_STOP (void) {
//TODO SysTick->CTRL = 0;
}
// Check if Timer expired
static __inline uint32_t TIMER_EXPIRED (void) {
return 0; //TODO return ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) ? 1 : 0);
}
#endif
// Delay for specified time
// delay: delay time in ms
void Delayms(uint32_t delay) {
delay *= (CPU_CLOCK/1000 + (DELAY_SLOW_CYCLES-1)) / DELAY_SLOW_CYCLES;
PIN_DELAY_SLOW(delay);
}
// Process Delay command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
static uint32_t DAP_Delay(uint8_t *request, uint8_t *response) {
uint32_t delay;
delay = *(request+0) | (*(request+1) << 8);
delay *= (CPU_CLOCK/1000000 + (DELAY_SLOW_CYCLES-1)) / DELAY_SLOW_CYCLES;
PIN_DELAY_SLOW(delay);
*response = DAP_OK;
return (1);
}
// Process Host Status command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
static uint32_t DAP_HostStatus(uint8_t *request, uint8_t *response) {
switch (*request) {
case DAP_DEBUGGER_CONNECTED:
LED_CONNECTED_OUT((*(request+1) & 1));
break;
case DAP_TARGET_RUNNING:
LED_RUNNING_OUT((*(request+1) & 1));
break;
default:
*response = DAP_ERROR;
return (1);
}
*response = DAP_OK;
return (1);
}
// Process Connect command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
static uint32_t DAP_Connect(uint8_t *request, uint8_t *response) {
uint32_t port;
if (*request == DAP_PORT_AUTODETECT) {
port = DAP_DEFAULT_PORT;
} else {
port = *request;
}
semihost_disable();
switch (port) {
#if (DAP_SWD != 0)
case DAP_PORT_SWD:
DAP_Data.debug_port = DAP_PORT_SWD;
PORT_SWD_SETUP();
break;
#endif
#if (DAP_JTAG != 0)
case DAP_PORT_JTAG:
DAP_Data.debug_port = DAP_PORT_JTAG;
PORT_JTAG_SETUP();
break;
#endif
default:
*response = DAP_PORT_DISABLED;
return (1);
}
*response = port;
return (1);
}
// Process Disconnect command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
static uint32_t DAP_Disconnect(uint8_t *response) {
DAP_Data.debug_port = DAP_PORT_DISABLED;
PORT_OFF();
semihost_enable();
*response = DAP_OK;
return (1);
}
// Process Reset Target command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
static uint32_t DAP_ResetTarget(uint8_t *response) {
*(response+1) = RESET_TARGET();
*(response+0) = DAP_OK;
return (2);
}
// Process SWJ Pins command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if ((DAP_SWD != 0) || (DAP_JTAG != 0))
static uint32_t DAP_SWJ_Pins(uint8_t *request, uint8_t *response) {
uint32_t value;
uint32_t select;
uint32_t wait;
value = *(request+0);
select = *(request+1);
wait = (*(request+2) << 0) |
(*(request+3) << 8) |
(*(request+4) << 16) |
(*(request+5) << 24);
if (select & (1 << DAP_SWJ_SWCLK_TCK)) {
if (value & (1 << DAP_SWJ_SWCLK_TCK)) {
PIN_SWCLK_TCK_SET();
} else {
PIN_SWCLK_TCK_CLR();
}
}
if (select & (1 << DAP_SWJ_SWDIO_TMS)) {
if (value & (1 << DAP_SWJ_SWDIO_TMS)) {
PIN_SWDIO_TMS_SET();
} else {
PIN_SWDIO_TMS_CLR();
}
}
if (select & (1 << DAP_SWJ_TDI)) {
PIN_TDI_OUT(value >> DAP_SWJ_TDI);
}
if (select & (1 << DAP_SWJ_nTRST)) {
PIN_nTRST_OUT(value >> DAP_SWJ_nTRST);
}
if (select & (1 << DAP_SWJ_nRESET)) {
PIN_nRESET_OUT(value >> DAP_SWJ_nRESET);
}
if (wait) {
if (wait > 3000000) wait = 3000000;
TIMER_START(wait);
do {
if (select & (1 << DAP_SWJ_SWCLK_TCK)) {
if ((value >> DAP_SWJ_SWCLK_TCK) ^ PIN_SWCLK_TCK_IN()) continue;
}
if (select & (1 << DAP_SWJ_SWDIO_TMS)) {
if ((value >> DAP_SWJ_SWDIO_TMS) ^ PIN_SWDIO_TMS_IN()) continue;
}
if (select & (1 << DAP_SWJ_TDI)) {
if ((value >> DAP_SWJ_TDI) ^ PIN_TDI_IN()) continue;
}
if (select & (1 << DAP_SWJ_nTRST)) {
if ((value >> DAP_SWJ_nTRST) ^ PIN_nTRST_IN()) continue;
}
if (select & (1 << DAP_SWJ_nRESET)) {
if ((value >> DAP_SWJ_nRESET) ^ PIN_nRESET_IN()) continue;
}
break;
} while (!TIMER_EXPIRED());
TIMER_STOP();
}
value = (PIN_SWCLK_TCK_IN() << DAP_SWJ_SWCLK_TCK) |
(PIN_SWDIO_TMS_IN() << DAP_SWJ_SWDIO_TMS) |
(PIN_TDI_IN() << DAP_SWJ_TDI) |
(PIN_TDO_IN() << DAP_SWJ_TDO) |
(PIN_nTRST_IN() << DAP_SWJ_nTRST) |
(PIN_nRESET_IN() << DAP_SWJ_nRESET);
*response = (uint8_t)value;
return (1);
}
#endif
// Process SWJ Clock command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if ((DAP_SWD != 0) || (DAP_JTAG != 0))
static uint32_t DAP_SWJ_Clock(uint8_t *request, uint8_t *response) {
uint32_t clock;
uint32_t delay;
clock = (*(request+0) << 0) |
(*(request+1) << 8) |
(*(request+2) << 16) |
(*(request+3) << 24);
if (clock == 0) {
*response = DAP_ERROR;
return (1);
}
if (clock >= MAX_SWJ_CLOCK(DELAY_FAST_CYCLES)) {
DAP_Data.fast_clock = 1;
DAP_Data.clock_delay = 1;
} else {
DAP_Data.fast_clock = 0;
delay = (CPU_CLOCK/2 + (clock - 1)) / clock;
if (delay > IO_PORT_WRITE_CYCLES) {
delay -= IO_PORT_WRITE_CYCLES;
delay = (delay + (DELAY_SLOW_CYCLES - 1)) / DELAY_SLOW_CYCLES;
} else {
delay = 1;
}
DAP_Data.clock_delay = delay;
}
*response = DAP_OK;
return (1);
}
#endif
// Process SWJ Sequence command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if ((DAP_SWD != 0) || (DAP_JTAG != 0))
static uint32_t DAP_SWJ_Sequence(uint8_t *request, uint8_t *response) {
uint32_t count;
count = *request++;
if (count == 0) count = 256;
SWJ_Sequence(count, request);
*response = DAP_OK;
return (1);
}
#endif
// Process SWD Configure command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if (DAP_SWD != 0)
static uint32_t DAP_SWD_Configure(uint8_t *request, uint8_t *response) {
uint8_t value;
value = *request;
DAP_Data.swd_conf.turnaround = (value & 0x03) + 1;
DAP_Data.swd_conf.data_phase = (value & 0x04) ? 1 : 0;
*response = DAP_OK;
return (1);
}
#endif
// Process SWD Abort command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if (DAP_SWD != 0)
static uint32_t DAP_SWD_Abort(uint8_t *request, uint8_t *response) {
uint32_t data;
if (DAP_Data.debug_port != DAP_PORT_SWD) {
*response = DAP_ERROR;
return (1);
}
// Load data (Ignore DAP index)
data = (*(request+1) << 0) |
(*(request+2) << 8) |
(*(request+3) << 16) |
(*(request+4) << 24);
// Write Abort register
SWD_Transfer(DP_ABORT, &data);
*response = DAP_OK;
return (1);
}
#endif
// Process JTAG Sequence command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if (DAP_JTAG != 0)
static uint32_t DAP_JTAG_Sequence(uint8_t *request, uint8_t *response) {
uint32_t sequence_info;
uint32_t sequence_count;
uint32_t response_count;
uint32_t count;
*response++ = DAP_OK;
response_count = 1;
sequence_count = *request++;
while (sequence_count--) {
sequence_info = *request++;
JTAG_Sequence(sequence_info, request, response);
count = sequence_info & JTAG_SEQUENCE_TCK;
if (count == 0) count = 64;
count = (count + 7) / 8;
request += count;
if (sequence_info & JTAG_SEQUENCE_TDO) {
response += count;
response_count += count;
}
}
return (response_count);
}
#endif
// Process JTAG Configure command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if (DAP_JTAG != 0)
static uint32_t DAP_JTAG_Configure(uint8_t *request, uint8_t *response) {
uint32_t count;
uint32_t length;
uint32_t bits;
uint32_t n;
count = *request++;
DAP_Data.jtag_dev.count = count;
bits = 0;
for (n = 0; n < count; n++) {
length = *request++;
DAP_Data.jtag_dev.ir_length[n] = length;
DAP_Data.jtag_dev.ir_before[n] = bits;
bits += length;
}
for (n = 0; n < count; n++) {
bits -= DAP_Data.jtag_dev.ir_length[n];
DAP_Data.jtag_dev.ir_after[n] = bits;
}
*response = DAP_OK;
return (1);
}
#endif
// Process JTAG IDCODE command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if (DAP_JTAG != 0)
static uint32_t DAP_JTAG_IDCode(uint8_t *request, uint8_t *response) {
uint32_t data;
if (DAP_Data.debug_port != DAP_PORT_JTAG) {
err:*response = DAP_ERROR;
return (1);
}
// Device index (JTAP TAP)
DAP_Data.jtag_dev.index = *request;
if (DAP_Data.jtag_dev.index >= DAP_Data.jtag_dev.count) goto err;
// Select JTAG chain
JTAG_IR(JTAG_IDCODE);
// Read IDCODE register
data = JTAG_ReadIDCode();
// Store Data
*(response+0) = DAP_OK;
*(response+1) = (uint8_t)(data >> 0);
*(response+2) = (uint8_t)(data >> 8);
*(response+3) = (uint8_t)(data >> 16);
*(response+4) = (uint8_t)(data >> 24);
return (1+4);
}
#endif
// Process JTAG Abort command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if (DAP_JTAG != 0)
static uint32_t DAP_JTAG_Abort(uint8_t *request, uint8_t *response) {
uint32_t data;
if (DAP_Data.debug_port != DAP_PORT_JTAG) {
err:*response = DAP_ERROR;
return (1);
}
// Device index (JTAP TAP)
DAP_Data.jtag_dev.index = *request;
if (DAP_Data.jtag_dev.index >= DAP_Data.jtag_dev.count) goto err;
// Select JTAG chain
JTAG_IR(JTAG_ABORT);
// Load data
data = (*(request+1) << 0) |
(*(request+2) << 8) |
(*(request+3) << 16) |
(*(request+4) << 24);
// Write Abort register
JTAG_WriteAbort(data);
*response = DAP_OK;
return (1);
}
#endif
// Process Transfer Configure command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
static uint32_t DAP_TransferConfigure(uint8_t *request, uint8_t *response) {
DAP_Data.transfer.idle_cycles = *(request+0);
DAP_Data.transfer.retry_count = *(request+1) | (*(request+2) << 8);
DAP_Data.transfer.match_retry = *(request+3) | (*(request+4) << 8);
*response = DAP_OK;
return (1);
}
// Process SWD Transfer command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if (DAP_SWD != 0)
static uint32_t DAP_SWD_Transfer(uint8_t *request, uint8_t *response) {
uint32_t request_count;
uint32_t request_value;
uint32_t response_count;
uint32_t response_value;
uint8_t *response_head;
uint32_t post_read;
uint32_t check_write;
uint32_t match_value;
uint32_t match_retry;
uint32_t retry;
uint32_t data;
response_count = 0;
response_value = 0;
response_head = response;
response += 2;
DAP_TransferAbort = 0;
post_read = 0;
check_write = 0;
request++; // Ignore DAP index
request_count = *request++;
while (request_count--) {
request_value = *request++;
if (request_value & DAP_TRANSFER_RnW) {
// Read register
if (post_read) {
// Read was posted before
retry = DAP_Data.transfer.retry_count;
if ((request_value & (DAP_TRANSFER_APnDP | DAP_TRANSFER_MATCH_VALUE)) == DAP_TRANSFER_APnDP) {
// Read previous AP data and post next AP read
do {
response_value = SWD_Transfer(request_value, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
} else {
// Read previous AP data
do {
response_value = SWD_Transfer(DP_RDBUFF | DAP_TRANSFER_RnW, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
post_read = 0;
}
if (response_value != DAP_TRANSFER_OK) break;
// Store previous AP data
*response++ = (uint8_t) data;
*response++ = (uint8_t)(data >> 8);
*response++ = (uint8_t)(data >> 16);
*response++ = (uint8_t)(data >> 24);
}
if (request_value & DAP_TRANSFER_MATCH_VALUE) {
// Read with value match
match_value = (*(request+0) << 0) |
(*(request+1) << 8) |
(*(request+2) << 16) |
(*(request+3) << 24);
request += 4;
match_retry = DAP_Data.transfer.match_retry;
if (request_value & DAP_TRANSFER_APnDP) {
// Post AP read
retry = DAP_Data.transfer.retry_count;
do {
response_value = SWD_Transfer(request_value, NULL);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
}
do {
// Read register until its value matches or retry counter expires
retry = DAP_Data.transfer.retry_count;
do {
response_value = SWD_Transfer(request_value, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
} while (((data & DAP_Data.transfer.match_mask) != match_value) && match_retry-- && !DAP_TransferAbort);
if ((data & DAP_Data.transfer.match_mask) != match_value) {
response_value |= DAP_TRANSFER_MISMATCH;
}
if (response_value != DAP_TRANSFER_OK) break;
} else {
// Normal read
retry = DAP_Data.transfer.retry_count;
if (request_value & DAP_TRANSFER_APnDP) {
// Read AP register
if (post_read == 0) {
// Post AP read
do {
response_value = SWD_Transfer(request_value, NULL);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
post_read = 1;
}
} else {
// Read DP register
do {
response_value = SWD_Transfer(request_value, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
// Store data
*response++ = (uint8_t) data;
*response++ = (uint8_t)(data >> 8);
*response++ = (uint8_t)(data >> 16);
*response++ = (uint8_t)(data >> 24);
}
}
check_write = 0;
} else {
// Write register
if (post_read) {
// Read previous data
retry = DAP_Data.transfer.retry_count;
do {
response_value = SWD_Transfer(DP_RDBUFF | DAP_TRANSFER_RnW, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
// Store previous data
*response++ = (uint8_t) data;
*response++ = (uint8_t)(data >> 8);
*response++ = (uint8_t)(data >> 16);
*response++ = (uint8_t)(data >> 24);
post_read = 0;
}
// Load data
data = (((uint32_t)*(request+0)) << 0) |
(((uint32_t)*(request+1)) << 8) |
(((uint32_t)*(request+2)) << 16) |
(((uint32_t)*(request+3)) << 24);
request += 4;
if (request_value & DAP_TRANSFER_MATCH_MASK) {
// Write match mask
DAP_Data.transfer.match_mask = data;
response_value = DAP_TRANSFER_OK;
} else {
// Write DP/AP register
retry = DAP_Data.transfer.retry_count;
do {
response_value = SWD_Transfer(request_value, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
check_write = 1;
}
}
response_count++;
if (DAP_TransferAbort) break;
}
if (response_value == DAP_TRANSFER_OK) {
if (post_read) {
// Read previous data
retry = DAP_Data.transfer.retry_count;
do {
response_value = SWD_Transfer(DP_RDBUFF | DAP_TRANSFER_RnW, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) goto end;
// Store previous data
*response++ = (uint8_t) data;
*response++ = (uint8_t)(data >> 8);
*response++ = (uint8_t)(data >> 16);
*response++ = (uint8_t)(data >> 24);
} else if (check_write) {
// Check last write
retry = DAP_Data.transfer.retry_count;
do {
response_value = SWD_Transfer(DP_RDBUFF | DAP_TRANSFER_RnW, NULL);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
}
}
end:
*(response_head+0) = (uint8_t)response_count;
*(response_head+1) = (uint8_t)response_value;
return (response - response_head);
}
#endif
// Process JTAG Transfer command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if (DAP_JTAG != 0)
static uint32_t DAP_JTAG_Transfer(uint8_t *request, uint8_t *response) {
uint32_t request_count;
uint32_t request_value;
uint32_t request_ir;
uint32_t response_count;
uint32_t response_value;
uint8_t *response_head;
uint32_t post_read;
uint32_t match_value;
uint32_t match_retry;
uint32_t retry;
uint32_t data;
uint32_t ir;
response_count = 0;
response_value = 0;
response_head = response;
response += 2;
DAP_TransferAbort = 0;
ir = 0;
post_read = 0;
// Device index (JTAP TAP)
DAP_Data.jtag_dev.index = *request++;
if (DAP_Data.jtag_dev.index >= DAP_Data.jtag_dev.count) goto end;
request_count = *request++;
while (request_count--) {
request_value = *request++;
request_ir = (request_value & DAP_TRANSFER_APnDP) ? JTAG_APACC : JTAG_DPACC;
if (request_value & DAP_TRANSFER_RnW) {
// Read register
if (post_read) {
// Read was posted before
retry = DAP_Data.transfer.retry_count;
if ((ir == request_ir) && ((request_value & DAP_TRANSFER_MATCH_VALUE) == 0)) {
// Read previous data and post next read
do {
response_value = JTAG_Transfer(request_value, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
} else {
// Select JTAG chain
if (ir != JTAG_DPACC) {
ir = JTAG_DPACC;
JTAG_IR(ir);
}
// Read previous data
do {
response_value = JTAG_Transfer(DP_RDBUFF | DAP_TRANSFER_RnW, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
post_read = 0;
}
if (response_value != DAP_TRANSFER_OK) break;
// Store previous data
*response++ = (uint8_t) data;
*response++ = (uint8_t)(data >> 8);
*response++ = (uint8_t)(data >> 16);
*response++ = (uint8_t)(data >> 24);
}
if (request_value & DAP_TRANSFER_MATCH_VALUE) {
// Read with value match
match_value = (*(request+0) << 0) |
(*(request+1) << 8) |
(*(request+2) << 16) |
(*(request+3) << 24);
request += 4;
match_retry = DAP_Data.transfer.match_retry;
// Select JTAG chain
if (ir != request_ir) {
ir = request_ir;
JTAG_IR(ir);
}
// Post DP/AP read
retry = DAP_Data.transfer.retry_count;
do {
response_value = JTAG_Transfer(request_value, NULL);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
do {
// Read register until its value matches or retry counter expires
retry = DAP_Data.transfer.retry_count;
do {
response_value = JTAG_Transfer(request_value, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
} while (((data & DAP_Data.transfer.match_mask) != match_value) && match_retry-- && !DAP_TransferAbort);
if ((data & DAP_Data.transfer.match_mask) != match_value) {
response_value |= DAP_TRANSFER_MISMATCH;
}
if (response_value != DAP_TRANSFER_OK) break;
} else {
// Normal read
if (post_read == 0) {
// Select JTAG chain
if (ir != request_ir) {
ir = request_ir;
JTAG_IR(ir);
}
// Post DP/AP read
retry = DAP_Data.transfer.retry_count;
do {
response_value = JTAG_Transfer(request_value, NULL);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
post_read = 1;
}
}
} else {
// Write register
if (post_read) {
// Select JTAG chain
if (ir != JTAG_DPACC) {
ir = JTAG_DPACC;
JTAG_IR(ir);
}
// Read previous data
retry = DAP_Data.transfer.retry_count;
do {
response_value = JTAG_Transfer(DP_RDBUFF | DAP_TRANSFER_RnW, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
// Store previous data
*response++ = (uint8_t) data;
*response++ = (uint8_t)(data >> 8);
*response++ = (uint8_t)(data >> 16);
*response++ = (uint8_t)(data >> 24);
post_read = 0;
}
// Load data
data = (*(request+0) << 0) |
(*(request+1) << 8) |
(*(request+2) << 16) |
(*(request+3) << 24);
request += 4;
if (request_value & DAP_TRANSFER_MATCH_MASK) {
// Write match mask
DAP_Data.transfer.match_mask = data;
response_value = DAP_TRANSFER_OK;
} else {
// Select JTAG chain
if (ir != request_ir) {
ir = request_ir;
JTAG_IR(ir);
}
// Write DP/AP register
retry = DAP_Data.transfer.retry_count;
do {
response_value = JTAG_Transfer(request_value, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) break;
}
}
response_count++;
if (DAP_TransferAbort) break;
}
if (response_value == DAP_TRANSFER_OK) {
// Select JTAG chain
if (ir != JTAG_DPACC) {
ir = JTAG_DPACC;
JTAG_IR(ir);
}
if (post_read) {
// Read previous data
retry = DAP_Data.transfer.retry_count;
do {
response_value = JTAG_Transfer(DP_RDBUFF | DAP_TRANSFER_RnW, &data);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
if (response_value != DAP_TRANSFER_OK) goto end;
// Store previous data
*response++ = (uint8_t) data;
*response++ = (uint8_t)(data >> 8);
*response++ = (uint8_t)(data >> 16);
*response++ = (uint8_t)(data >> 24);
} else {
// Check last write
retry = DAP_Data.transfer.retry_count;
do {
response_value = JTAG_Transfer(DP_RDBUFF | DAP_TRANSFER_RnW, NULL);
} while ((response_value == DAP_TRANSFER_WAIT) && retry-- && !DAP_TransferAbort);
}
}
end:
*(response_head+0) = (uint8_t)response_count;
*(response_head+1) = (uint8_t)response_value;
return (response - response_head);
}
#endif
// Process SWD Transfer Block command and prepare response
// request: pointer to request data
// response: pointer to response data
// return: number of bytes in response
#if (DAP_SWD != 0)
static uint32_t DAP_SWD_TransferBlock(uint8_t *request, uint8_t *response) {
uint32_t request_count;
uint32_t request_value;
uint32_t response_count;
uint32_t response_value;