forked from ikalogic/ScanaStudio-scripts-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWD.js
8542 lines (8315 loc) · 439 KB
/
SWD.js
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
/* Protocol meta info:
<NAME> SWD </NAME>
<DESCRIPTION>
SW-DP decoder according to the ARM Debug Interface Architecture Specification ADIv6.0.
About the APACC, it can decode the MEM-AP and the JTAG-AP, others are consider unknown.
</DESCRIPTION>
<VERSION> 1.1 </VERSION>
<AUTHOR_NAME> Corentin Maravat </AUTHOR_NAME>
<AUTHOR_URL> contact@ikalogic.com </AUTHOR_URL>
<HELP_URL> https://github.com/ikalogic/ScanaStudio-scripts-v3/wiki </HELP_URL>
<COPYRIGHT> Copyright Ikalogic SAS </COPYRIGHT>
<LICENSE> This code is distributed under the terms of the GNU General Public License GPLv3 </LICENSE>
<RELEASE_NOTES>
V1.1: Bug fix : variable declaration.
V1.0: Initial release.
</RELEASE_NOTES>
*/
/*Sources : Specification du protocole et des registres 6.0 Version : https://static.docs.arm.com/ihi0074/a/debug_interface_v6_0_architecture_specification_IHI0074A.pdf
Spec mais Version 5.0 to 5.2 : https://static.docs.arm.com/ihi0031/c/IHI0031C_debug_interface_as.pdf#page=123&zoom=100,125,80
Blog qui parle du SWD, avec des exemples de trame et on voit bien le changement de front entre l'hote et la cible : https://www.cnblogs.com/shangdawei/p/4748751.html
*/
//Decoder GUI
function on_draw_gui_decoder()
{
//Define decoder configuration GUI
ScanaStudio.gui_add_ch_selector("ch_io","SWDIO Channel","SWDIO");
ScanaStudio.gui_add_ch_selector("ch_clk","SWDCLK Channel","SWDCLK");
ScanaStudio.gui_add_baud_selector("baud","Baud rate",10000);
ScanaStudio.gui_add_new_tab("SWD Mode configuration",false);
ScanaStudio.gui_add_combo_box("protocol_version", "SWD protocol Version");
ScanaStudio.gui_add_item_to_combo_box("v2", true);
ScanaStudio.gui_add_item_to_combo_box("v1");
ScanaStudio.gui_add_text_input("turnaround_period","Length of the turnaround period","1");
ScanaStudio.gui_add_info_label("Default value is 1. Call DLCR Register if you want to change the value during the capture.");
ScanaStudio.gui_add_check_box("access_dpidr_change_state","Access to the DPIDR change the protocol state",false);
ScanaStudio.gui_add_info_label("If the target detects a valid read of the DP DPIDR register, the target"
+" leaves the protocol error state, and gives an OK response. ");
ScanaStudio.gui_add_new_selectable_containers_group("access_port","Select the Access Port")
ScanaStudio.gui_add_new_container("MEM-AP",true);
ScanaStudio.gui_add_check_box("large_data_extension","Large Data Extension Implemented",false);
// ScanaStudio.gui_add_info_label("Implementing this extension changes the format of CSW/DRW/BD0-BD3/DAR0-DAR255/CFG");
ScanaStudio.gui_add_check_box("packet_transfers","Packet Transfers Implemented",false);
ScanaStudio.gui_add_check_box("bytes_lanes","Bytes Lanes Implemented",false);
// ScanaStudio.gui_add_info_label("Support Data Bus access sizes smaller than word size (32 bits)");
ScanaStudio.gui_add_check_box("large_physical_address_extension","Large Physical Address Extension Implemented",false);
// ScanaStudio.gui_add_info_label("Implementing this extension changes the format of BASE/CFG/CSW/DRW/TAR");
ScanaStudio.gui_add_check_box("barrier_operation_extension","Barrier Operation Extension Implemented",false);
ScanaStudio.gui_end_container();
ScanaStudio.gui_add_new_container("JTAG-AP",false);
ScanaStudio.gui_end_container();
ScanaStudio.gui_end_selectable_containers_group();
ScanaStudio.gui_end_tab();
//Add other gui functions...
}
function reload_dec_gui_values()
{
//get GUI values
ch_io = ScanaStudio.gui_get_value("ch_io");
ch_clk = ScanaStudio.gui_get_value("ch_clk");
Protocol_version = Number(ScanaStudio.gui_get_value("protocol_version"));
Turnaround_period = Number(ScanaStudio.gui_get_value("turnaround_period"));
DPIDR_exit_error_state = ScanaStudio.gui_get_value("access_dpidr_change_state");
AP_Memory = Number(ScanaStudio.gui_get_value("access_port"));
if (AP_Memory == 0)
{
AP_Memory = "MEM-AP";
Large_data_extension_implemented = ScanaStudio.gui_get_value("large_data_extension");
Packet_transfers_implemented = ScanaStudio.gui_get_value("packet_transfers");
Bytes_lanes_implemented = ScanaStudio.gui_get_value("bytes_lanes");
Barrier_operation_extension_implemented = ScanaStudio.gui_get_value("barrier_operation_extension");
Large_physical_adress_extension_implemented = ScanaStudio.gui_get_value("large_physical_address_extension");
}
else
{
AP_Memory = "JTAG-AP";
}
}
//Evaluate decoder GUI
function on_eval_gui_decoder()
{
if (ScanaStudio.gui_get_value("turnaround_period") > 4)
{
return "Error, turnaround period can't be larger than 4 clock cycle."
}
if(Number(ScanaStudio.gui_get_value("baud")*8) >= (ScanaStudio.get_capture_sample_rate()) )
{
return "Selected bauderate is too high compared to the sampling rate you chose. Bauderate should be at least 8 times lower than the sampling rate.";
}
return ""; //All good.
}
//Global variables
var sampling_rate;
var state_machine;
var ch_io;
var ch_clk;
var nbits;
var io_channel = [];
var item_display = [];
var parity_array = [];
var last_trans = false;
var driver = "Host";
var last_RnW = "";
var Turnaround_period;
var ACK = "";
var item_content = "";
var last_APnDP = "";
// Variables used for AP Read
var last_Register_name = "";
var last_Register_name_2 = "";
var last_AP_Adress = 0;
var last_AP_Adress_2 = 0;
var last_Adress = 0;
var last_Adress_2 = 0;
// Variables for the request
var APnDP = "";
var RnW = "";
var Adress = 0;
var AP_Adress = 0;
var DP_BANK_SEL = -1; //Undefined
var SELECT_ADDR = 0;
var SELECT1_ADDR = 0;
var SELECT_string_hexa = "";
var Register_name = "";
var DP_Version = 3;
var Protocol_version = 0;
var Large_data_extension_implemented = false;
var Packet_transfers_implemented = false;
var Bytes_lanes_implemented = false;
var Barrier_operation_extension_implemented = false;
var DPIDR_exit_error_state = false;
var CSW_ERRNPASS_implemented = true;
var CSW_ERRSTOP_implemented = true;
var TRR_implemented = true;
var AP_Memory = "MEM-AP";
var TAR_Adress_LSB = 0;
var TAR_Adress_MSB = 0;
var TAR_Adress = [];
var TAR_string_hexa = "";
var Protocol_State = "Reset"; // "Reset"(after a line reset at the start of after a protocol error)/"Error"(After an ack error or Request Parity/Stop/Park bit error)/"Transfer" (No problem)
var reset = true; // To check if we're on the line reset state
var first = true; // Know if it's the first sync_decode after entering in ENUM_STATE_RESET
var AddrInc = 0; // Not incremented
var DARSIZE = 10; // DAR register implemented
// PIDR variable
var PART_0 = 0;
var PART_1 = 0;
// ACK WAIT or FAULT variables
var ORUNDETECT = 0; // overrun is disable
// JTAG_AP variables
var WFIFOCNT = 0;
var SERACTV = 0;
// demo signal
var rng_RnW = 0;
var rng_A = 0;
var clk_lvl = 0;
var rng_data = 0;
var second_trn = false;
var trs_io;
//decoder state
const ENUM_STATE_REQUEST = 0,
ENUM_STATE_ACK = 1,
ENUM_STATE_DATA_TRANSFER = 2,
ENUM_STATE_RESET = 4,
ENUM_STATE_UNDEFINED = 10;
const SWD_PROTOCOL_VERSION_2 = 0,
SWD_PROTOCOL_VERSION_1 = 1;
function on_decode_signals(resume)
{
if (!resume) //If resume == false, it's the first call to this function.
{
//initialization code goes here, ex:
sampling_rate = ScanaStudio.get_capture_sample_rate();
reload_dec_gui_values();
io_channel.push(ch_io);
var trs_clk = ScanaStudio.trs_reset(ch_clk);
trs_clk = ScanaStudio.trs_get_next(ch_clk);
var tmp_trs_sample_index;
tmp_trs_sample_index = trs_clk.sample_index;
while( (tmp_trs_sample_index == trs_clk.sample_index) && (ScanaStudio.trs_is_not_last(ch_clk) == true) )
{
trs_clk = ScanaStudio.trs_get_next(ch_clk);
}
trs_io = ScanaStudio.trs_reset(ch_io);
trs_io = ScanaStudio.trs_get_next(ch_io);
tmp_trs_sample_index = trs_io.sample_index;
while( (tmp_trs_sample_index == trs_io.sample_index) && (ScanaStudio.trs_is_not_last(ch_io) == true) )
{
trs_io = ScanaStudio.trs_get_next(ch_io);
}
// ScanaStudio.console_info_msg("trs_io.smaple_index",trs_io.sample_index);
//init global variables
swd_simple = sync_decode_v2(io_channel,trs_clk.sample_index + 1,5);
state_machine = ENUM_STATE_REQUEST;
DP_BANK_SEL = 0;
AP_Adress = 0;
}//end if !resume
else
{
//ScanaStudio.console_info_msg("Decoding resumed");
}
while (ScanaStudio.abort_is_requested() == false)
{
// ScanaStudio.console_info_msg(".");
if (!ScanaStudio.trs_is_not_last(ch_io))
{
break;
}
// ScanaStudio.console_info_msg("Protocol_State : " + Protocol_State + ", state_machine : " + state_machine , swd_simple.end_sample);
switch(state_machine)
{
case ENUM_STATE_REQUEST : // Request to read/Write an AP/DP register of the target
{
driver = "Host"; // The host always drive the line for the request
last_Protocol_State = Protocol_State;
if (Protocol_State == "Reset" || Protocol_State == "Error") // If it's the first start
{
if (Protocol_State == "Error")
{
// Check if there is a line reset
trs_clk = ScanaStudio.trs_get_before(ch_clk, swd_simple.start_sample + 1);
//if the error come from the ACK anyway we instantly go to ENUM_STATE_RESET
nbits = 50;
reset_sequence = sync_decode_v2(io_channel,trs_clk.sample_index + 1,nbits);
reset = true;
for (i=0; i<50; i++)
{
if(reset_sequence.signed_words[i] == 0)
{
reset = false;
}
}
if (reset == true)
{
ScanaStudio.dec_item_new(ch_io,reset_sequence.start_sample + 1,reset_sequence.end_sample - 1);
ScanaStudio.dec_item_add_content("Reset Sequence, go back to normal state");
ScanaStudio.dec_item_end();
//Packet View
ScanaStudio.packet_view_add_packet(true,ch_io,reset_sequence.start_sample,reset_sequence.end_sample,"RESET SEQUENCE","50 clocks with SWDIOTMS HIGH",ScanaStudio.PacketColors.Preamble.Title, ScanaStudio.PacketColors.Preamble.Content);
// Idle cycle
trs_io = ScanaStudio.trs_get_before(ch_io,reset_sequence.start_sample);
tmp_trs_sample_index = trs_io.sample_index;
while( (tmp_trs_sample_index == trs_io.sample_index) && (ScanaStudio.trs_is_not_last(ch_io) == true) )
{
trs_io = ScanaStudio.trs_get_next(ch_io);
}
var start_idle_cycles = trs_io.sample_index;
trs_io = ScanaStudio.trs_get_next(ch_io); // rising edge because the last one was a falling
ScanaStudio.dec_item_new(ch_io,start_idle_cycles + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Idle Cycles");
ScanaStudio.dec_item_end();
// Display the driver on the clk line
ScanaStudio.dec_item_new(ch_clk,reset_sequence.start_sample + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Driver : Host");
ScanaStudio.dec_item_end();
//Packet View
ScanaStudio.packet_view_add_packet(false,ch_io,start_idle_cycles,trs_io.sample_index - 1,"RESET SEQUENCE","IDLE CYCLES",ScanaStudio.PacketColors.Misc.Title, ScanaStudio.PacketColors.Misc.Content);
Protocol_State = "Reset";
DP_BANK_SEL = 0;
Turnaround_period = 1;
break;
}
else if (reset == false) // If there isn't reset sequence, we search for DPIDR
{
//TEST IF THE REQUEST IS DPIDR
nbits = 8;
swd_simple = sync_decode_v2(io_channel,trs_clk.sample_index + 1,nbits);
if (swd_simple.unsigned_words == 165) // Correct DPIDR Request
{
tmp_trs_sample_index = trs_clk.sample_index;
while( (tmp_trs_sample_index == trs_clk.sample_index) && (ScanaStudio.trs_is_not_last(ch_clk) == true) )
{
trs_clk = ScanaStudio.trs_get_next(ch_clk);
}
var protocol_error_end = trs_clk.sample_index;
// Display protocol error
ScanaStudio.dec_item_new(ch_io,protocol_error_start + 1, protocol_error_end - 1);
ScanaStudio.dec_item_add_content("Protocol error");
ScanaStudio.dec_item_end();
// Display that we found a correct DPIDR and we go
var end_protocol_error = dec_item_new_v2 (ch_io, swd_simple)[1];
ScanaStudio.dec_item_add_content("Correct DPIDR Request, go back to normal state");
display_sample(swd_simple);
ScanaStudio.dec_item_end();
// Display the driver on the clk line
ScanaStudio.dec_item_new(ch_clk,protocol_error_start + 1,end_protocol_error - 1)
dec_item_new_v2 (ch_clk, swd_simple);
ScanaStudio.dec_item_add_content("Driver : Host");
ScanaStudio.dec_item_end();
// Packet view
ScanaStudio.packet_view_add_packet(true,ch_io,protocol_error_start,protocol_error_end,"REQUEST","Read, DP, DPIDR",ScanaStudio.get_channel_color(ch_io),ScanaStudio.get_channel_color(ch_io));
// Update variables
Protocol_State = "Transfer";
Register_name = "DPIDR";
RnW = "Read";
APnDP = "DP";
Adress = 0;
state_machine = ENUM_STATE_ACK;
break;
}
break;
}
}//end if (Protocol_State == "Error")
else //start if (Protocol_State == "Reset")
{
if (Register_name == "TARGETSEL")
{
// Check if there is a line reset
trs_clk = ScanaStudio.trs_get_before(ch_clk,swd_simple.start_sample + 1);
nbits = 50;
reset_sequence = sync_decode_v2(io_channel,swd_simple.start_sample + 1,nbits);
reset = true;
for (i=0; i<50; i++)
{
if(reset_sequence.signed_words[i] == 0)
{
reset = false;
}
}
if (reset == true)
{
ScanaStudio.dec_item_new(ch_io,reset_sequence.start_sample + 1,reset_sequence.end_sample - 1);
ScanaStudio.dec_item_add_content("Reset Sequence, go back to normal state");
ScanaStudio.dec_item_end();
// Packet view
ScanaStudio.packet_view_add_packet(true,ch_io,reset_sequence.start_sample,reset_sequence.end_sample,"RESET SEQUENCE","50 clocks with SWDIOTMS HIGH",ScanaStudio.PacketColors.Preamble.Title,ScanaStudio.PacketColors.Preamble.Content);
// Idle cycle
trs_io = ScanaStudio.trs_get_next(ch_io); // falling edge because the last one was a rising
var start_idle_cycles = trs_io.sample_index;
trs_io = ScanaStudio.trs_get_next(ch_io); // rising edge because the last one was a falling
ScanaStudio.dec_item_new(ch_io,start_idle_cycles + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Idle Cycles");
ScanaStudio.dec_item_end();
// Display the driver on the clk line
var test = ScanaStudio.dec_item_new(ch_io,reset_sequence.start_sample,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Driver : Host");
ScanaStudio.dec_item_end();
ScanaStudio.packet_view_add_packet(false,ch_io,start_idle_cycles,trs_io.sample_index - 1,"RESET SEQUENCE","IDLE CYCLES",ScanaStudio.PacketColors.Misc.Title,ScanaStudio.PacketColors.Misc.Content);
Protocol_State = "Reset";
DP_BANK_SEL = 0;
Turnaround_period = 1;
swd_simple.end_sample = trs_io.sample_index;
}
else // if there isn't a reset sequence
{
// If there is idles cycles after the transaction
nbits = 1;
idle_cycles = sync_decode_v2(io_channel, swd_simple.end_sample + 1,nbits);
if (idle_cycles.unsigned_words == 0)
{
trs_io = ScanaStudio.trs_get_before(ch_io,idle_cycles.start_sample + 1); // falling edge
tmp_trs_sample_index = trs_io.sample_index;
while( (tmp_trs_sample_index == trs_io.sample_index) && (ScanaStudio.trs_is_not_last(ch_io) == true) )
{
trs_io = ScanaStudio.trs_get_next(ch_io); // rising edge
}
trs_clk = ScanaStudio.trs_get_before(ch_clk,idle_cycles.start_sample - 1); //falling edge
var start_item = trs_clk.sample_index;
var nb_trs = 0;
while (trs_clk.sample_index <= trs_io.sample_index - 1)
{
tmp_trs_sample_index = trs_clk.sample_index;
while( (tmp_trs_sample_index == trs_clk.sample_index) && (ScanaStudio.trs_is_not_last(ch_clk) == true) )
{
trs_clk = ScanaStudio.trs_get_next(ch_clk);
}
nb_trs++;
}
if (nb_trs >= 16)
{
ScanaStudio.dec_item_new(ch_io,start_item + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Idles Cycles : the clock can be stopped");
ScanaStudio.dec_item_add_content("Idles Cycles");
ScanaStudio.dec_item_end();
}
else
{
ScanaStudio.dec_item_new(ch_io,start_item + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Idles Cycles before another packet header");
ScanaStudio.dec_item_add_content("Idles Cycles");
ScanaStudio.dec_item_end();
}
// Display the driver
ScanaStudio.dec_item_new(ch_clk,start_item + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Driver : Host");
ScanaStudio.dec_item_end();
// Packet view
ScanaStudio.packet_view_add_packet(false,ch_io,start_item + 1,trs_io.sample_index - 1,"Idles Cycles","Idles Cycles",ScanaStudio.PacketColors.Misc.Title,ScanaStudio.PacketColors.Misc.Content);
swd_simple.end_sample = trs_io.sample_index;
// Check if there is a line reset
trs_clk = ScanaStudio.trs_get_before(ch_clk, trs_io.sample_index + 1);
nbits = 50;
reset_sequence = sync_decode_v2(io_channel,trs_clk.sample_index + 1,nbits);
reset = true;
for (i=0; i<50; i++)
{
if(reset_sequence.signed_words[i] == 0)
{
reset = false;
}
}
if (reset == true)
{
ScanaStudio.dec_item_new(ch_io,reset_sequence.start_sample + 1,reset_sequence.end_sample - 1);
ScanaStudio.dec_item_add_content("Reset Sequence, go back to normal state");
ScanaStudio.dec_item_end();
//Packet View
ScanaStudio.packet_view_add_packet(true,ch_io,reset_sequence.start_sample,reset_sequence.end_sample,"RESET SEQUENCE","50 clocks with SWDIOTMS HIGH",ScanaStudio.PacketColors.Preamble.Title,ScanaStudio.PacketColors.Preamble.Content);
// Idle cycle
trs_io = ScanaStudio.trs_get_before(ch_io,reset_sequence.start_sample);
tmp_trs_sample_index = trs_io.sample_index;
while( (tmp_trs_sample_index == trs_io.sample_index) && (ScanaStudio.trs_is_not_last(ch_io) == true) )
{
trs_io = ScanaStudio.trs_get_next(ch_io);
}
var start_idle_cycles = trs_io.sample_index;
trs_io = ScanaStudio.trs_get_next(ch_io); // rising edge because the last one was a falling
ScanaStudio.dec_item_new(ch_io,start_idle_cycles + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Idle Cycles");
ScanaStudio.dec_item_end();
// Display the driver on the clk line
ScanaStudio.dec_item_new(ch_clk,reset_sequence.start_sample + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Driver : Host");
ScanaStudio.dec_item_end();
//Packet View
ScanaStudio.packet_view_add_packet(false,ch_io,start_idle_cycles,trs_io.sample_index - 1,"RESET SEQUENCE","IDLE CYCLES",ScanaStudio.PacketColors.Misc.Title,ScanaStudio.PacketColors.Misc.Content);
Protocol_State = "Reset";
DP_BANK_SEL = 0;
Turnaround_period = 1;
swd_simple.end_sample = trs_io.sample_index;
}
}
else // if (idle_cycles.unsigned_words != 0)
{
// Check if there is a line reset
trs_clk = ScanaStudio.trs_get_before(ch_clk, swd_simple.start_sample + 1);
nbits = 50;
reset_sequence = sync_decode_v2(io_channel,trs_clk.sample_index + 1,nbits);
reset = true;
for (i=0; i<50; i++)
{
if(reset_sequence.signed_words[i] == 0)
{
reset = false;
}
}
if (reset == true)
{
ScanaStudio.dec_item_new(ch_io,reset_sequence.start_sample + 1,reset_sequence.end_sample - 1);
ScanaStudio.dec_item_add_content("Reset Sequence, go back to normal state");
ScanaStudio.dec_item_end();
//Packet View
ScanaStudio.packet_view_add_packet(true,ch_io,reset_sequence.start_sample,reset_sequence.end_sample,"RESET SEQUENCE","50 clocks with SWDIOTMS HIGH",ScanaStudio.PacketColors.Preamble.Title,ScanaStudio.PacketColors.Preamble.Content);
// Idle cycle
trs_io = ScanaStudio.trs_get_before(ch_io,reset_sequence.start_sample);
tmp_trs_sample_index = trs_io.sample_index;
while( (tmp_trs_sample_index == trs_io.sample_index) && (ScanaStudio.trs_is_not_last(ch_io) == true) )
{
trs_io = ScanaStudio.trs_get_next(ch_io);
}
var start_idle_cycles = trs_io.sample_index;
trs_io = ScanaStudio.trs_get_next(ch_io); // rising edge because the last one was a falling
ScanaStudio.dec_item_new(ch_io,start_idle_cycles + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Idle Cycles");
ScanaStudio.dec_item_end();
// Display the driver on the clk line
ScanaStudio.dec_item_new(ch_clk,reset_sequence.start_sample + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Driver : Host");
ScanaStudio.dec_item_end();
//Packet View
ScanaStudio.packet_view_add_packet(false,ch_io,start_idle_cycles,trs_io.sample_index - 1,"RESET SEQUENCE","IDLE CYCLES",ScanaStudio.PacketColors.Misc.Title,ScanaStudio.PacketColors.Misc.Content);
Protocol_State = "Reset";
DP_BANK_SEL = 0;
Turnaround_period = 1;
swd_simple.end_sample = trs_io.sample_index;
}//end if (reset == true)
}//end if (idle_cycles.unsigned_words != 0)
}//end if (reset != true)
// Start bit
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample + 1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
if (swd_value == 0)
{
dec_item_new_v2(ch_io, swd_simple);
ScanaStudio.dec_item_add_content("Start");
ScanaStudio.dec_item_add_content("S");
ScanaStudio.dec_item_add_sample_point(swd_simple.start_sample,"P")
ScanaStudio.dec_item_emphasize_error();
state_machine = ENUM_STATE_REQUEST;
trs_io.value = -1;
item_display = [];
parity_array = [];
ScanaStudio.dec_item_end();
//Packet View
ScanaStudio.packet_view_add_packet(true,ch_io,swd_simple.start_sample,swd_simple.end_sample,"SWD","CH_IO",ScanaStudio.get_channel_color(ch_io),ScanaStudio.get_channel_color(ch_io));
break;
}
//Packet View
ScanaStudio.packet_view_add_packet(true,ch_io,swd_simple.start_sample,swd_simple.end_sample,"SWD","CH_IO",ScanaStudio.get_channel_color(ch_io),ScanaStudio.get_channel_color(ch_io));
var start_request = dec_item_new_v2 (ch_io, swd_simple)[0];
dec_item_new_v2 (ch_io, swd_simple);
ScanaStudio.dec_item_add_content("Start");
ScanaStudio.dec_item_add_content("S");
display_sample(swd_simple);
ScanaStudio.dec_item_end();
packet_view_add_packet_v2(ch_io,swd_simple,"Start","Request","Wrap");
trs_io.value = -1;
//Read APnDP bit
parity_array = [];
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
item_display = parity_pre_calcul(swd_simple.unsigned_words, nbits, parity_array);
dec_item_new_v2(ch_io,swd_simple);
if (swd_value == 1)
{
packet_string = "AP (Access Port Register)";
APnDP = "AP";
ScanaStudio.dec_item_add_content("APnDP : AP (Access Port Register)");
ScanaStudio.dec_item_add_content("APnDP : AP");
ScanaStudio.dec_item_add_content("AP");
}
else
{
packet_string = "DP (Debug Port Register)";
APnDP = "DP";
ScanaStudio.dec_item_add_content("APnDP : DP (Debug Port Register)");
ScanaStudio.dec_item_add_content("APnDP : DP");
ScanaStudio.dec_item_add_content("DP");
}
display_sample(swd_simple);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"APnDP",packet_string,"Data");
//Read RnW bit
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
item_display = parity_pre_calcul(swd_simple.unsigned_words, nbits, parity_array);
dec_item_new_v2(ch_io,swd_simple);
if (swd_value == 1)
{
packet_string = "Read";
RnW = "Read";
ScanaStudio.dec_item_add_content("RnW : Read");
ScanaStudio.dec_item_add_content("Read");
}
else
{
packet_string = "Write";
RnW = "Write";
ScanaStudio.dec_item_add_content("RnW : Write");
ScanaStudio.dec_item_add_content("Write");
}
display_sample(swd_simple);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"RnW",packet_string,"Data");
//Read A[2:3] bit
nbits = 2;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
Adress = swd_value * 4;
get_register_name(RnW, Adress, APnDP, DP_BANK_SEL);
item_display = parity_pre_calcul(swd_simple.unsigned_words, nbits, parity_array);
dec_item_new_v2(ch_io,swd_simple);
display_register_name();
ScanaStudio.dec_item_add_content("Adress field : 0x" + Adress);
ScanaStudio.dec_item_add_content("0x" + pad(Adress.toString(16),1));
display_sample(swd_simple);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"A[2:3]",packet_string,"Data");
//Read parity bit
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
parity_calculated = get_parity(parity_array);
item_display = parity_pre_calcul(swd_simple.unsigned_words, nbits, parity_array);
dec_item_new_v2(ch_io,swd_simple);
if (swd_value == parity_calculated)
{
packet_string = "OK";
types = "Check";
ScanaStudio.dec_item_add_content("Parity bit OK");
ScanaStudio.dec_item_add_content("OK");
}
else
{
packet_string = "NOT OK";
types = "Error";
if ((Protocol_version == SWD_PROTOCOL_VERSION_2) || DPIDR_exit_error_state == false || (Protocol_State == "Error")) // If we're in these case you instantly go Lockout State and search a line reset to go Reset State
{
Protocol_State = "Lockout";
}
else if (Protocol_State == "Transfer" || Protocol_State == "Reset")
{
Protocol_State = "Error";
}
ScanaStudio.dec_item_add_content("Parity bit NOT OK");
ScanaStudio.dec_item_add_content("NOT OK");
ScanaStudio.dec_item_emphasize_error();
}
display_sample_0_1(swd_simple, item_display);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"Parity bit",packet_string,types);
//Read Stop bit
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
dec_item_new_v2(ch_io,swd_simple);
if (swd_value == 0)
{
packet_string = "Stop";
types = "Wrap";
ScanaStudio.dec_item_add_content("Stop");
ScanaStudio.dec_item_add_content("S");
}
else
{
packet_string = "Stop bit value should be LOW";
types = "Error";
if ((Protocol_version == SWD_PROTOCOL_VERSION_2) || DPIDR_exit_error_state == false || (Protocol_State == "Error"))
{
Protocol_State = "Lockout";
}
else if (Protocol_State == "Transfer" || Protocol_State == "Reset")
{
Protocol_State = "Error";
}
ScanaStudio.dec_item_add_content("Stop");
ScanaStudio.dec_item_add_content("S");
ScanaStudio.dec_item_emphasize_error();
}
display_sample(swd_simple);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"Stop",packet_string,types);
//Read Park bit
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
var protocol_error_start = dec_item_new_v2(ch_io,swd_simple)[1]; // protocol_error start take the value of the sample of the end of this item
if (swd_value == 1)
{
packet_string = "Park bit";
types = "Wrap";
ScanaStudio.dec_item_add_content("Park bit");
ScanaStudio.dec_item_add_content("P");
}
else
{
packet_string = "Park bit value should be HIGH";
types = "Error";
if ((Protocol_version == SWD_PROTOCOL_VERSION_2) || DPIDR_exit_error_state == false || (Protocol_State == "Error"))
{
Protocol_State = "Lockout";
}
else if (Protocol_State == "Transfer" || Protocol_State == "Reset")
{
Protocol_State = "Error";
}
ScanaStudio.dec_item_add_content("Park bit should be HIGH");
ScanaStudio.dec_item_add_content("P");
ScanaStudio.dec_item_emphasize_error();
}
display_sample(swd_simple);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"Park bit",packet_string,types);
var end_request = protocol_error_start;
// Display the driver
ScanaStudio.dec_item_new(ch_clk,start_request + 1,end_request - 1);
ScanaStudio.dec_item_add_content("Driver : Host");
ScanaStudio.dec_item_end();
while (trs_io.sample_index < swd_simple.end_sample)
{
if (!ScanaStudio.trs_is_not_last(ch_io))
{
break;
}
trs_io = ScanaStudio.trs_get_next(ch_io);
}
if (Protocol_State == "Reset" && Register_name == "DPIDR")
{
Protocol_State = "Transfer";
state_machine = ENUM_STATE_ACK;
}
else if (Protocol_State == "Reset" && Register_name == "TARGETSEL")
{
state_machine = ENUM_STATE_ACK;
}
else if (Protocol_State == "Reset" && Protocol_version == SWD_PROTOCOL_VERSION_2)
// If the SW-DP implements SWD protocol version 2, it must enter the lockout state after a single protocol error
// immediately after a line reset. However, if the first packet request detected by the target following line reset is valid
// it can then revert to entering the lockout state after an IMPLEMENTATION DEFINED number of protocol errors.
{
Protocol_State = "Transfer";
state_machine = ENUM_STATE_ACK;
}
else if (Protocol_State == "Error")
{
state_machine = ENUM_STATE_REQUEST;
}
else if (Protocol_State == "Error" && Register_name == "DPIDR" && DPIDR_exit_error_state == true)
{
Protocol_State = "Transfer";
state_machine = ENUM_STATE_ACK;
}
else if (Protocol_State == "Lockout")
{
state_machine = ENUM_STATE_RESET;
}
break;
} //end if the last access was the TARGETSEL Register
else // if the last access wasn't the TARGETSEL Register
{
if (trs_io.value == 1) //(rising edge)
{
// Check if there is a line reset
nbits = 50;
reset_sequence = sync_decode_v2(io_channel,trs_io.sample_index,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
reset = true;
for (i=0; i<50; i++)
{
if(reset_sequence.signed_words[i] == 0)
{
reset = false;
}
}
if (reset == true)
{
ScanaStudio.dec_item_new(ch_io,reset_sequence.start_sample + 1,reset_sequence.end_sample - 1);
ScanaStudio.dec_item_add_content("Reset Sequence, go back to normal state");
ScanaStudio.dec_item_end();
// Packet view
ScanaStudio.packet_view_add_packet(true,ch_io,reset_sequence.start_sample,reset_sequence.end_sample,"RESET SEQUENCE","50 clocks with SWDIOTMS HIGH",ScanaStudio.PacketColors.Preamble.Title,ScanaStudio.PacketColors.Preamble.Content);
// Idle cycle
trs_io = ScanaStudio.trs_get_next(ch_io); // falling edge because the last one was a rising
var start_idle_cycles = trs_io.sample_index;
trs_io = ScanaStudio.trs_get_next(ch_io); // rising edge because the last one was a falling
ScanaStudio.dec_item_new(ch_io,start_idle_cycles + 1,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Idle Cycles");
ScanaStudio.dec_item_end();
// Display the driver on the clk line
var test = ScanaStudio.dec_item_new(ch_io,reset_sequence.start_sample,trs_io.sample_index - 1);
ScanaStudio.dec_item_add_content("Driver : Host");
ScanaStudio.dec_item_end();
ScanaStudio.packet_view_add_packet(false,ch_io,start_idle_cycles,trs_io.sample_index - 1,"RESET SEQUENCE","IDLE CYCLES",ScanaStudio.PacketColors.Misc.Title,ScanaStudio.PacketColors.Misc.Content);
Protocol_State = "Reset";
DP_BANK_SEL = 0;
Turnaround_period = 1;
}
// Start of the Request
parity_array = [];
nbits = 1;
swd_simple = sync_decode_v2(io_channel,trs_io.sample_index,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
if (swd_value == 0)
{
dec_item_new_v2(ch_io, swd_simple);
ScanaStudio.dec_item_add_content("Start");
ScanaStudio.dec_item_add_content("S");
ScanaStudio.dec_item_add_sample_point(swd_simple.start_sample,"P")
ScanaStudio.dec_item_emphasize_error();
state_machine = ENUM_STATE_REQUEST;
trs_io.value = -1;
item_display = [];
parity_array = [];
ScanaStudio.dec_item_end();
//Packet View
ScanaStudio.packet_view_add_packet(true,ch_io,swd_simple.start_sample,swd_simple.end_sample,"SWD","CH_IO",ScanaStudio.get_channel_color(ch_io),ScanaStudio.get_channel_color(ch_io));
break;
}
//Packet View
ScanaStudio.packet_view_add_packet(true,ch_io,swd_simple.start_sample,swd_simple.end_sample,"SWD","CH_IO",ScanaStudio.get_channel_color(ch_io),ScanaStudio.get_channel_color(ch_io));
var start_request = dec_item_new_v2 (ch_io, swd_simple)[0];
dec_item_new_v2 (ch_io, swd_simple);
ScanaStudio.dec_item_add_content("Start");
ScanaStudio.dec_item_add_content("S");
display_sample(swd_simple);
ScanaStudio.dec_item_end();
packet_view_add_packet_v2(ch_io,swd_simple,"Start","Request","Wrap");
trs_io.value = -1;
//Read APnDP bit
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
item_display = parity_pre_calcul(swd_simple.unsigned_words, nbits, parity_array);
dec_item_new_v2(ch_io,swd_simple);
if (swd_value == 1)
{
packet_string = "AP (Access Port Register)";
APnDP = "AP";
ScanaStudio.dec_item_add_content("APnDP : AP (Access Port Register)");
ScanaStudio.dec_item_add_content("APnDP : AP");
ScanaStudio.dec_item_add_content("AP");
}
else
{
packet_string = "DP (Debug Port Register)";
APnDP = "DP";
ScanaStudio.dec_item_add_content("APnDP : DP (Debug Port Register)");
ScanaStudio.dec_item_add_content("APnDP : DP");
ScanaStudio.dec_item_add_content("DP");
}
display_sample(swd_simple);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"APnDP",packet_string,"Data");
//Read RnW bit
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
item_display = parity_pre_calcul(swd_simple.unsigned_words, nbits, parity_array);
dec_item_new_v2(ch_io,swd_simple);
if (swd_value == 1)
{
packet_string = "Read";
RnW = "Read";
ScanaStudio.dec_item_add_content("RnW : Read");
ScanaStudio.dec_item_add_content("Read");
}
else
{
packet_string = "Write";
RnW = "Write";
ScanaStudio.dec_item_add_content("RnW : Write");
ScanaStudio.dec_item_add_content("Write");
}
display_sample(swd_simple);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"RnW",packet_string,"Data");
//Read A[2:3] bit
nbits = 2;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
Adress = swd_value * 4;
get_register_name(RnW, Adress, APnDP, DP_BANK_SEL);
item_display = parity_pre_calcul(swd_simple.unsigned_words, nbits, parity_array);
dec_item_new_v2(ch_io,swd_simple);
display_register_name();
ScanaStudio.dec_item_add_content("Adress field : 0x" + Adress);
ScanaStudio.dec_item_add_content("0x" + pad(Adress.toString(16),1));
display_sample(swd_simple);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"A[2:3]",packet_string,"Data");
//Read parity bit
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
parity_calculated = get_parity(parity_array);
item_display = parity_pre_calcul(swd_simple.unsigned_words, nbits, parity_array);
dec_item_new_v2(ch_io,swd_simple);
if (swd_value == parity_calculated)
{
packet_string = "OK";
types = "Check";
ScanaStudio.dec_item_add_content("Parity bit OK");
ScanaStudio.dec_item_add_content("OK");
}
else
{
packet_string = "NOT OK";
types = "Error";
if ((Protocol_version == SWD_PROTOCOL_VERSION_2) || DPIDR_exit_error_state == false || (Protocol_State == "Error"))
// If we're in these case you instantly go Lockout State and search a line reset to go Reset State
{
Protocol_State = "Lockout";
}
else if (Protocol_State == "Transfer" || Protocol_State == "Reset")
{
Protocol_State = "Error";
}
ScanaStudio.dec_item_add_content("Parity bit NOT OK");
ScanaStudio.dec_item_add_content("NOT OK");
ScanaStudio.dec_item_emphasize_error();
}
display_sample_0_1(swd_simple, item_display);
ScanaStudio.dec_item_end();
//Packet View
packet_view_add_packet_v2(ch_io,swd_simple,"Parity bit",packet_string,types);
//Read Stop bit
nbits = 1;
swd_simple = sync_decode_v2(io_channel,swd_simple.end_sample +1,nbits);
if (last_trans == true) //if sync_decode overpassed the last sample
{
go_to_last_trans_io();
break;
}
swd_value = Number(swd_simple.unsigned_words);
dec_item_new_v2(ch_io,swd_simple);
if (swd_value == 0)
{
packet_string = "Stop";
types = "Wrap";
ScanaStudio.dec_item_add_content("Stop");
ScanaStudio.dec_item_add_content("S");
}
else
{
packet_string = "Stop bit value should be LOW";
types = "Error";
if ((Protocol_version == SWD_PROTOCOL_VERSION_2) || DPIDR_exit_error_state == false || (Protocol_State == "Error"))
{
Protocol_State = "Lockout";
}
else if (Protocol_State == "Transfer" || Protocol_State == "Reset")
{
Protocol_State = "Error";
}
ScanaStudio.dec_item_add_content("Stop");
ScanaStudio.dec_item_add_content("S");
ScanaStudio.dec_item_emphasize_error();
}