-
Notifications
You must be signed in to change notification settings - Fork 0
/
pk_debugger_iface.py
1302 lines (1042 loc) · 49.3 KB
/
pk_debugger_iface.py
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
class target():
def __init__(self, dev_id, flash_size, page_size, ram_start, ram_size, dev_name):
self.dev_id = dev_id
self.flash_size = flash_size
self.page_size = page_size
self.ram_start = ram_start
self.ram_size = ram_size
self.dev_name = dev_name
target_lut = {
# DeviceID[0:2] | Flash | Page | RAM Off | RAM Len | Target Name
target (0x1E9120, 2048, 64, 0x3F80, 128, "ATtiny214"),
target (0x1E9121, 2048, 64, 0x3F80, 128, "ATtiny212"),
target (0x1E9122, 2048, 64, 0x3F80, 128, "ATtiny204"),
target (0x1E9123, 2048, 64, 0x3F80, 128, "ATtiny202"),
target (0x1E9220, 4096, 64, 0x3F00, 256, "ATtiny417"),
target (0x1E9221, 4096, 64, 0x3F00, 256, "ATtiny416"),
target (0x1E9222, 4096, 64, 0x3F00, 256, "ATtiny414"),
target (0x1E9223, 4096, 64, 0x3F00, 256, "ATtiny412"),
target (0x1E9225, 4096, 64, 0x3F00, 256, "ATtiny406"),
target (0x1E9226, 4096, 64, 0x3F00, 256, "ATtiny404"),
target (0x1E9227, 4096, 64, 0x3F00, 256, "ATtiny402"),
target (0x1E922A, 4096, 64, 0x3E00, 512, "ATtiny427"),
target (0x1E922B, 4096, 64, 0x3E00, 512, "ATtiny426"),
target (0x1E922C, 4096, 64, 0x3E00, 512, "ATtiny424"),
target (0x1E9320, 8192, 64, 0x3E00, 512, "ATtiny817"),
target (0x1E9321, 8192, 64, 0x3E00, 512, "ATtiny816"),
target (0x1E9322, 8192, 64, 0x3E00, 512, "ATtiny814"),
target (0x1E9323, 8192, 64, 0x3E00, 512, "ATtiny807"),
target (0x1E9324, 8192, 64, 0x3E00, 512, "ATtiny806"),
target (0x1E9325, 8192, 64, 0x3E00, 512, "ATtiny804"),
target (0x1E9326, 8192, 64, 0x3C00, 1024, "ATmega808"),
target (0x1E9327, 8192, 64, 0x3C00, 1024, "ATtiny827"),
target (0x1E9328, 8192, 64, 0x3C00, 1024, "ATtiny826"),
target (0x1E9329, 8192, 64, 0x3C00, 1024, "ATtiny824"),
target (0x1E932A, 8192, 64, 0x3C00, 1024, "ATmega809"),
target (0x1E932C, 8192, 64, 0x7800, 2048, "AVR8EA28"),
target (0x1E9420, 16384, 64, 0x3800, 2048, "ATtiny1617"),
target (0x1E9421, 16384, 64, 0x3800, 2048, "ATtiny1616"),
target (0x1E9422, 16384, 64, 0x3800, 2048, "ATtiny1614"),
target (0x1E9423, 16384, 64, 0x3C00, 1024, "ATtiny1607"),
target (0x1E9424, 16384, 64, 0x3C00, 1024, "ATtiny1606"),
target (0x1E9425, 16384, 64, 0x3C00, 1024, "ATtiny1604"),
target (0x1E9426, 16384, 64, 0x3800, 2048, "ATmega1609"),
target (0x1E9427, 16384, 64, 0x3800, 2048, "ATmega1608"),
target (0x1E9428, 16384, 64, 0x3800, 2048, "ATtiny1627"),
target (0x1E9429, 16384, 64, 0x3800, 2048, "ATtiny1626"),
target (0x1E942A, 16384, 64, 0x3800, 2048, "ATtiny1624"),
target (0x1E9431, 16384, 512, 0x7800, 2048, "AVR16DD32"),
target (0x1E9432, 16384, 512, 0x7800, 2048, "AVR16DD28"),
target (0x1E9433, 16384, 512, 0x7800, 2048, "AVR16DD20"),
target (0x1E9434, 16384, 512, 0x7800, 2048, "AVR16DD14"),
target (0x1E9435, 16384, 64, 0x7800, 2048, "AVR16EA48"),
target (0x1E9436, 16384, 64, 0x7800, 2048, "AVR16EA32"),
target (0x1E9449, 16384, 64, 0x7800, 2048, "AVR16EB14"),
target (0x1E9521, 32768, 128, 0x3800, 2048, "ATtiny3216"),
target (0x1E9526, 32768, 128, 0x3400, 3072, "ATtiny3227"),
target (0x1E9527, 32768, 128, 0x3400, 3072, "ATtiny3226"),
target (0x1E9528, 32768, 128, 0x3400, 3072, "ATtiny3224"),
target (0x1E9530, 32768, 128, 0x3000, 4096, "ATmega3208"),
target (0x1E9531, 32768, 128, 0x3000, 4096, "ATmega3209"),
target (0x1E9532, 32768, 512, 0x7000, 4096, "AVR32DA48"),
target (0x1E9533, 32768, 512, 0x7000, 4096, "AVR32DA32"),
target (0x1E9534, 32768, 512, 0x7000, 4096, "AVR32DA28"),
target (0x1E9535, 32768, 512, 0x7000, 4096, "AVR32DB48"),
target (0x1E9536, 32768, 512, 0x7000, 4096, "AVR32DB32"),
target (0x1E9537, 32768, 512, 0x7000, 4096, "AVR32DB28"),
target (0x1E9538, 32768, 512, 0x7000, 4096, "AVR32DD32"),
target (0x1E9539, 32768, 512, 0x7000, 4096, "AVR32DD28"),
target (0x1E953A, 32768, 512, 0x7000, 4096, "AVR32DD20"),
target (0x1E953B, 32768, 512, 0x7000, 4096, "AVR32DD14"),
target (0x1E953C, 32768, 64, 0x7000, 4096, "AVR32EA48"),
target (0x1E953D, 32768, 64, 0x7000, 4096, "AVR32EA32"),
target (0x1E953E, 32768, 64, 0x7000, 4096, "AVR32EA28"),
target (0x1E9612, 65536, 512, 0x6000, 8192, "AVR64DA64"),
target (0x1E9613, 65536, 512, 0x6000, 8192, "AVR64DA48"),
target (0x1E9614, 65536, 512, 0x6000, 8192, "AVR64DA32"),
target (0x1E9615, 65536, 512, 0x6000, 8192, "AVR64DA28"),
target (0x1E9616, 65536, 512, 0x6000, 8192, "AVR64DB64"),
target (0x1E9617, 65536, 512, 0x6000, 8192, "AVR64DB48"),
target (0x1E9618, 65536, 512, 0x6000, 8192, "AVR64DB32"),
target (0x1E9619, 65536, 512, 0x6000, 8192, "AVR64DB28"),
target (0x1E961A, 65536, 512, 0x6000, 8192, "AVR64DD32"),
target (0x1E961B, 65536, 512, 0x6000, 8192, "AVR64DD28"),
target (0x1E961C, 65536, 512, 0x6000, 8192, "AVR64DD20"),
target (0x1E961D, 65536, 512, 0x6000, 8192, "AVR64DD14"),
target (0x1E961E, 65536, 128, 0x6800, 6144, "AVR64EA48"),
target (0x1E961F, 65536, 128, 0x6800, 6144, "AVR64EA32"),
target (0x1E9620, 65536, 128, 0x6800, 6144, "AVR64EA28"),
target (0x1E9650, 49152, 128, 0x2800, 6144, "ATmega4808"),
target (0x1E9651, 49152, 128, 0x2800, 6144, "ATmega4809"),
target (0x1E9707, 131072, 512, 0x4000, 16384, "AVR128DA64"),
target (0x1E9708, 131072, 512, 0x4000, 16384, "AVR128DA48"),
target (0x1E9709, 131072, 512, 0x4000, 16384, "AVR128DA32"),
target (0x1E970A, 131072, 512, 0x4000, 16384, "AVR128DA28"),
target (0x1E970B, 131072, 512, 0x4000, 16384, "AVR128DB64"),
target (0x1E970C, 131072, 512, 0x4000, 16384, "AVR128DB48"),
target (0x1E970D, 131072, 512, 0x4000, 16384, "AVR128DB32"),
target (0x1E970E, 131072, 512, 0x4000, 16384, "AVR128DB28"),
}
register_lut = [
# Name, Index, bitsize, type, gdb_group, gdb_type, gdb_num, gdb_feature
[ "r0", 0, 8, "lower", "general", "uint8", None, None],
[ "r1", 1, 8, "lower", "general", "uint8", None, None],
[ "r2", 2, 8, "lower", "general", "uint8", None, None],
[ "r3", 3, 8, "lower", "general", "uint8", None, None],
[ "r4", 4, 8, "lower", "general", "uint8", None, None],
[ "r5", 5, 8, "lower", "general", "uint8", None, None],
[ "r6", 6, 8, "lower", "general", "uint8", None, None],
[ "r7", 7, 8, "lower", "general", "uint8", None, None],
[ "r8", 8, 8, "lower", "general", "uint8", None, None],
[ "r9", 9, 8, "lower", "general", "uint8", None, None],
["r10", 10, 8, "lower", "general", "uint8", None, None],
["r11", 11, 8, "lower", "general", "uint8", None, None],
["r12", 12, 8, "lower", "general", "uint8", None, None],
["r13", 13, 8, "lower", "general", "uint8", None, None],
["r14", 14, 8, "lower", "general", "uint8", None, None],
["r15", 15, 8, "lower", "general", "uint8", None, None],
["r16", 16, 8, "higher", "general", "uint8", None, None],
["r17", 17, 8, "higher", "general", "uint8", None, None],
["r18", 18, 8, "higher", "general", "uint8", None, None],
["r19", 19, 8, "higher", "general", "uint8", None, None],
["r20", 20, 8, "higher", "general", "uint8", None, None],
["r21", 21, 8, "higher", "general", "uint8", None, None],
["r22", 22, 8, "higher", "general", "uint8", None, None],
["r23", 23, 8, "higher", "general", "uint8", None, None],
["r24", 24, 8, "higher", "general", "uint8", None, None],
["r25", 25, 8, "higher", "general", "uint8", None, None],
["r26", 26, 8, "higher", "general", "uint8", None, None],
["r27", 27, 8, "higher", "general", "uint8", None, None],
["r28", 28, 8, "higher", "general", "uint8", None, None],
["r29", 29, 8, "higher", "general", "uint8", None, None],
["r30", 30, 8, "higher", "general", "uint8", None, None],
["r31", 31, 8, "higher", "general", "uint8", None, None],
# CPU Extra Registers
["SREG", 32, 8, "higher", "general", "uint8", None, None],
[ "SP", 33, 16, "higher", "general","data_ptr", None, None],
["PC2", 34, 32, "higher", "general","code_ptr", None, None],
# Pseudo Reg? Not quite sure what avr-gdb needs that one for
# [ "pc", 35, 32, "higher", "general", "uint8", None, None],
# I wish I could add these, but GDB does not allow that.
# Pseudo Regs for easier readability
# [ "rX", 32, 16, "pointer", "system", "data_ptr", None, None],
# [ "rY", 33, 16, "pointer", "system", "data_ptr", None, None],
# [ "rZ", 34, 16, "pointer", "system", "data_ptr", None, None],
]
import threading
import libusb_package
import usb.util, usb.core
from time import sleep
from xml.etree import ElementTree as ET
from enum import Enum
import scripts_dict as scr_dict
class UnknownPart(Exception):
# Raised when a part name can not be found in the target_lut
pass
class UnknownScript(Exception):
# Raised when there is no correspnding script set to talk to the target
pass
class ErrorXML(Exception):
# Raised when we failed to generte an XML File
pass
class NoDebugger(Exception):
# Raised when no debugger was found in the USB devices
pass
class CommunicationFailure(Exception):
# Raised whenever we don't get a response from a debugger
pass
def get_uint32_from_buf(buf:bytearray, pos:int, endian = "little"):
retval = 0
if (endian == "little"):
retval |= (buf[pos + 0] & 0xFF) << 0
retval |= (buf[pos + 1] & 0xFF) << 8
retval |= (buf[pos + 2] & 0xFF) << 16
retval |= (buf[pos + 3] & 0xFF) << 24
elif (endian == "big"):
retval = (buf[pos + 0] & 0xFF) << 24
retval |= (buf[pos + 1] & 0xFF) << 16
retval |= (buf[pos + 2] & 0xFF) << 8
retval |= (buf[pos + 3] & 0xFF) << 0
return retval
def uint32_to_buf(num:int):
buffer = bytearray(4)
buffer[0] = (num >> 0) & 0xFF
buffer[1] = (num >> 8) & 0xFF
buffer[2] = (num >> 16) & 0xFF
buffer[3] = (num >> 24) & 0xFF
return buffer
probeMode = Enum("probeMode", ["OFF", "PROG", "DEBUG", "TO_Write", "TO_Read"])
targetState = Enum("targetState", ["OFF", "HALT", "RUN", "ERASED"])
class pk_debugger(threading.Thread):
script_cmd_type = 0x0100
script_upload_type = 0x80000102
script_download_type = 0x0C0000101
def __init__(self, part:str, voltage:int, baud:int):
if ((voltage in range (1800, 5500)) and (voltage != 0) and (voltage != None)):
self._voltage = voltage
else:
self._voltage = 0
if((baud != None) and (baud in range(10, 900))):
self._updi_speed = baud
else:
self._updi_speed = 200
self.part = part.upper()
self._devID = 0x00
self._revision = 0x00
self._target:target = None
# scripts
self.scripts = dict()
self.sib = ""
self.nvm_version = ""
# USB
self._vid = 0x04D8
self._pid = (0x9036, 0x9012)
self._icd = None
self._apVersion = ""
self._serialNum = ""
# EP Numbers, same for PK4 and PK5
self._cmd_read_ep = 0x81 # 129 == EP IN 1
self._cmd_write_ep = 0x02 # 2 == EP OUT 2
self._data_read_ep = 0x83 # 131 == EP IN 3
self._data_write_ep = 0x04 # 4 == EP OUT 4
# ADC Readings (in mV/mC/mA)
self.internalVdd = 0
self.TargetVdd = 0
self.TargetVpp = 0
self.InternalVpp = 0
#self.VddSense = 0 # Defaults to 0
#self.Temp = 0 # Temp defaults to 25°C
self.VddCurrentSense = 0
self.VddVoltageSense = 0
# memory map: [Name:str, from:int, to:int]
self._target_xml = ""
self._memory_map_xml = ""
self._target_regs = bytearray(32)
self._target_sp = 0
self.target_pc = 0
self._targetState = targetState.OFF
self._target_attached = False
self._target_halt_status = 0x00
self.target_flash_erased = 0
self._new_program = bytearray(0x1FFFF) # 128k parts
self._program_tail = 0
self._probeMode = probeMode.OFF
self._probePowered = False # True if Voltage supplied by debugger
#EOF
def init_debugger(self):
if self.find_icd() == None:
raise NoDebugger()
if self._set_cpu(self.part) != 0:
raise UnknownPart()
if self._set_dev_info(self.part) != 0:
raise UnknownScript()
if self._create_target_xml() != 0:
raise ErrorXML()
if self._create_mem_map_xml() != 0:
raise ErrorXML()
def check_connection(self):
if self.get_Firmware_Info() != 0:
raise CommunicationFailure
def _set_dev_info(self, part_name:str):
part_name = part_name.upper()
for dev in target_lut:
if (part_name == dev.dev_name):
self._target = dev
return 0
return -1
def _create_target_xml(self):
if (self._target_xml != ""): # create once only
return -1
if (self._target is None):
return -1
xml_header = b"""<?xml version="1.0"?><!DOCTYPE target SYSTEM "gdb-target.dtd">"""
tree_root = ET.Element("target")
avr_arch = "avr:103"
if self._target.flash_size == 65536:
avr_arch = "avr:102"
if self._target.flash_size == 131072:
avr_arch = "avr:104"
tree_arch = ET.SubElement(tree_root, "architecture").text = avr_arch
tree_comp = ET.SubElement(tree_root, "compatible").text = "avr"
tree_feat = ET.SubElement(tree_root, "feature", name="org.gnu.avr8.profile") # Placeholder profile name
for reg in register_lut:
ET.SubElement(tree_feat, "reg", name=reg[0], bitsize=str(reg[2]), regnum=str(reg[1]), type=reg[5])
print("INFO: Generated target.xml")
self._target_xml = xml_header + ET.tostring(tree_root)
return 0
#EOF
def _create_mem_map_xml(self):
if (self._memory_map_xml != ""): # Generate only once
return -1
if (self._target == None): # we need to know our target first
return -1
xml_header = b"""<?xml version="1.0"?><!DOCTYPE memory-map PUBLIC "+//IDN gnu.org//DTD GDB Memory Map V1.0//EN" "http://sourceware.org/gdb/gdb-memory-map.dtd">"""
tree_root = ET.Element("memory-map")
ram_start_hex = "80{0:04x}".format(self._target.ram_start) # GDB expects RAM to start at 0x80 0000
ram_length_hex = "{0:x}".format(self._target.ram_size)
ET.SubElement(tree_root, "memory", type="ram", start=ram_start_hex, length=ram_length_hex)
flash_length_hex = "{0:x}".format(self._target.flash_size) # Flash starts at 0x00, the opposite compared to AVR debugging
flash_map = ET.SubElement(tree_root, "memory", type="flash", start="00", length=flash_length_hex)
blocksize = ET.SubElement(flash_map, "property", name="blocksize").text = "{0:x}".format(self._target.page_size)
print("INFO: Generated memory_map.xml")
self._memory_map_xml = xml_header + ET.tostring(tree_root)
return 0
#EOF
def _set_cpu(self, cpu:str):
cpu_str = cpu.upper()
if cpu_str in scr_dict.scripts:
self.scripts = scr_dict.scripts[cpu_str]
print("Scripts for {0} found.".format(cpu_str))
return 0
else:
print("Scripts for {0} not found. Allowed names are:".format(cpu_str))
for x in scr_dict.scripts:
print(x)
return -1
def find_icd(self):
if self._icd is not None:
return self._icd
devices = list(libusb_package.find(find_all=1))
#print(f'Found {len(devices)} USB-Devices in the system')
icd = None
for pid in self._pid:
icd = libusb_package.find(idVendor=self._vid, idProduct=pid)
if icd is not None:
self._icd = icd
return icd
#print("No Debugger found by VID/PID")
return None
#EOF
def software_reset(self):
self.run_Script(self.script_cmd_type, bytearray(0), [0xE7], 0)
self.read_response("Software Reset")
def recover_script(self):
recover_cmd = 0x0107
transfer = self.create_msg_header(recover_cmd, 16, 0x00)
self._icd.write(self._cmd_write_ep, transfer)
self.read_response("recover script")
def set_PTG_mode(self, enabled:int):
ptg_mode_array = [0x5E, bool(enabled), 0x00, 0x00, 0x00]
response_length = 4
self.run_Script(self.script_upload_type, bytearray(0), ptg_mode_array, response_length)
self.read_response("Set PTG Mode")
self.read_script_upload(response_length, "Set PTG Mode")
self.send_script_done("Set PTG Mode")
def get_Firmware_Info(self):
self._icd.write(self._cmd_write_ep, [0xE1])
ret = self._icd.read(self._cmd_read_ep, 1024, 25)
if (ret[0] != 0xE1):
print('ERROR: Wrong Device Response On "Get Firmware Info". Repose: \n' + ret)
return -1
self._apVersion = "apVer: {0:02X}.{1:02X}.{2:02X}, ".format(ret[3],ret[4],ret[5])
self._serialNum = "serialNumber: "
for x in range(32, 50):
self._serialNum += chr(ret[x])
return 0
#EOF
def set_live_connect(self, connect:int):
if (self._probeMode == probeMode.OFF):
set_live_conn_script = [0x39, bool(connect)]
self.run_Script(self.script_cmd_type, bytearray(0), set_live_conn_script, 0)
self.read_response("Set Live Connect")
#EOF
def set_Voltage (self, mVoltTarget:int): # 0x40
if (mVoltTarget == 0):
self.run_Script(self.script_cmd_type, bytearray(0), [0x44], 0)
if (0x00 == self.read_response("Shutdown Power")[0]):
self._probeMode == probeMode.OFF
self._targetState == targetState.OFF
else:
if (self._probeMode == probeMode.OFF):
setVoltageCmd = [0x40]
setVoltageCmd += uint32_to_buf(mVoltTarget) # (uint32_t) Vdd
setVoltageCmd += uint32_to_buf(mVoltTarget) # (uint32_t) Vpp operation
setVoltageCmd += uint32_to_buf(mVoltTarget) # (uint32_t) Vpp_op
setVoltageCmd += [0x42, 0x43]
self.run_Script(self.script_cmd_type, bytearray(0), setVoltageCmd, 0)
self.read_response("Enable Power System")
else:
print("ERROR: Power System can be only enabled while detached")
#EOF
def select_power_source(self, internal:int): # 0x46
power_source = [0x46, bool(internal), 0x00, 0x00, 0x00]
self.run_Script(self.script_cmd_type, bytearray(0), power_source, 0)
self.read_response("Select Power Source")
#EOF
def get_Voltages(self): # 0x47
getVoltagesCmd = [0x47]
self.run_Script(self.script_cmd_type, bytearray(0), getVoltagesCmd, 0)
ret = self.read_response("Read voltages")[1]
self.internalVdd = get_uint32_from_buf(ret, 0)
self.TargetVdd = get_uint32_from_buf(ret, 4)
self.TargetVpp = get_uint32_from_buf(ret, 8)
self.InternalVpp = get_uint32_from_buf(ret, 12) # probably HV Reset Voltage source
#VddSense = get_uint32_from_buf(ret, 16) # Returned 0
#Temp = get_uint32_from_buf(ret, 20) # Temp defaults to 25°C
self.VddCurrentSense = get_uint32_from_buf(ret, 24)
self.VddVoltageSense = get_uint32_from_buf(ret, 28)
print("internalVdd: " + str(self.internalVdd) + "mV")
print("TargetVdd: " + str(self.TargetVdd) + "mV")
print("TargetVpp: " + str(self.TargetVpp) + "mV")
print("InternalVpp: " + str(self.InternalVpp) + "mV")
#print("VddSense: " + str(VddSense) + "mV")
#print("Temp: " + str(Temp) + "mC")
print("VddCurrentSense: " + str(self.VddCurrentSense) + "mA")
print("VddVoltageSense: " + str(self.VddVoltageSense) + "mV")
#EOF
def set_LED_Brightness(self, brightness:int):
LedBrightnessCmd = [0xCF, (0xFF & (brightness-1))]
self.run_Script(self.script_cmd_type, bytearray(0), LedBrightnessCmd, 0)
self.read_response("set_LED_Brightness")
#EOF
def set_UPDI_Speed(self, speed:int):
print("Setting UPDI speed to {0}kHz".format(speed))
self.run_Script(self.script_cmd_type, uint32_to_buf(speed), self.scripts["SetSpeed"], 0)
self.read_response("Set UPDI Speed")
#EOF
def enter_Prog_Mode(self):
if (self._probeMode == probeMode.DEBUG):
self.exit_Debug_Mode()
if (self._probeMode != probeMode.PROG):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["EnterProgMode"], 0)
ret = self.read_response("Enter Programming Mode")[0]
if (ret == 0x00):
self._probeMode = probeMode.PROG
print("INFO: Entered PROG mode")
else:
print("ERROR: Can't enter PROG mode, Target mode not OFF")
#EOF
def exit_Prog_Mode(self):
if (self._probeMode == probeMode.PROG):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["ExitProgMode"], 0)
ret = self.read_response("Exit Program Mode")[0]
if (ret == 0x00):
self._probeMode = probeMode.OFF
print("INFO: Left PROG mode")
else:
print("ERROR: Can't leave PROG mode, Target not in PROG Mode")
#EOF
def enter_Debug_Mode(self):
if (self._probeMode == probeMode.PROG):
self.exit_Prog_Mode()
if (self._probeMode != probeMode.DEBUG):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["EnterDebugMode"], 0)
ret = self.read_response("Enter Debug Mode")[0]
if (ret == 0x00):
self._probeMode = probeMode.DEBUG
#self._target_halt_status = 0xAAAAAAAA
print("INFO: Entered DEBUG mode")
else:
print("ERROR: Can't enter DEBUG mode, Target mode not OFF")
#EOF
def exit_Debug_Mode(self):
if (self._probeMode == probeMode.DEBUG):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["ExitDebugMode"], 0)
ret = self.read_response("Exit Debug Mode")[0]
if (ret == 0x00):
self._probeMode = probeMode.OFF
print("INFO: Left DEBUG mode")
else:
print("ERROR: Can't leave DEBUG mode, Target not in DEBUG Mode")
#EOF
def halt_Target(self):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["Halt"], 0)
ret = self.read_response("Halt Target")[0]
if (ret == 0x00):
self._targetState = targetState.HALT
return True
else:
return False
#EOF
def resume_Target(self):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["Run"], 0)
ret = self.read_response("Resume Target")[0]
if (ret == 0x00):
self._targetState = targetState.RUN
return True
else:
return False
#EOF
def get_halt_status(self): # Returns True if halted
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["GetHaltStatus"], 0)
ret = self.read_response("Halt Status")[1]
new_hs = get_uint32_from_buf(ret, 0)
if (self._target_halt_status != new_hs):
print("Halt Status returned: {0:04x}".format(new_hs))
self._target_halt_status = new_hs
if new_hs == 0xAAAAAAAA:
return True
else:
return False
#EOF
def reset_Target(self): # Will halt automatically
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["DebugReset"], 0)
return self.read_response("Reset Target")[0] # 0x00 is good
def reset_halt_Target(self):
self.halt_Target()
return self.reset_Target()
def hold_in_reset(self):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["HoldInReset"], 0)
return self.read_response("hold in reset")[0] # 0x00 is good
def release_from_reset(self):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["ReleaseFromReset"], 0)
return self.read_response("release reset")[0] # 0x00 is good
def get_runtime_data(self):
runtime = [0x1E, 0x00]
self.run_Script(self.script_upload_type, bytearray(0), runtime, 0)
self.read_response("runtime data")
self.read_script_upload(0, "runtime data")
self.send_script_done("runtime data")
def set_hw_breakpoint(self, num:int, addr:int):
param = uint32_to_buf(num)
param += uint32_to_buf(addr)
self.run_Script(self.script_cmd_type, param, self.scripts["SetHWBP"], 0)
ret = self.read_response("Set HW breakpoint")[0]
if ret != 0x00:
print("ERROR: Failed setting HW breakpoint {0} at addr {1:04X}".format(num, addr))
return False
return True
#EOF
def clear_hw_breakpoint(self, num:int):
param = uint32_to_buf(num)
self.run_Script(self.script_cmd_type, param, self.scripts["ClearHWBP"], 0)
ret = self.read_response("Clear HW breakpoint")[0]
if ret != 0x00:
print("ERROR: Failed clearing HW breakpoint {0}".format(num))
return False
return True
#EOF
def step_target(self):
self.run_Script(self.script_cmd_type, [], self.scripts["SingleStep"], 0)
ret = self.read_response("Single Step")[0]
if ret != 0x00:
print("ERROR: Failed Stepping to next instruction")
return False
return True
def get_PC(self):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["GetPC"], 0)
ret = self.read_response("get_PC")[1]
self.target_pc = get_uint32_from_buf(ret, 0)
#print("Current PC: {0:06X}".format(self.target_pc))
return ret, self.target_pc # get byte (LE) representation, and integer
#EOF
def set_PC(self, value):
param = bytearray(4)
if (isinstance(value, int)):
param = uint32_to_buf(value)
elif (isinstance(value, bytearray)):
param = value
else:
print("ERROR, Wrong Data Type in Set PC")
return
param_len = len(param)
if param_len != 4:
print("ERROR: Wrong bytearray length passed: {0}".format(param_len))
return
if self._target == None:
print("ERROR: Target must be known to Write PC to it")
return
flash_size = self._target.flash_size
flash_mask = (flash_size >> 9) - 1 # Max PC is half the Flash size
param[3] = 0x00 # make sure to have valid inputs only
param[2] = 0x00
if (param[1] > flash_mask):
print("ERROR: PC bigger then Flash Size")
return
self.run_Script(self.script_cmd_type, param, self.scripts["SetPC"], 0)
ret = self.read_response("set_PC")[0]
if ret == 0x00:
return True
else:
return False
#EOF
def get_SP_SREG(self):
return self.read_target_mem8(0x3D, 3)
#EOF
def get_target_ID(self, force:bool = False):
if (self._devID == 0x00) or (force is True):
self.run_Script(self.script_cmd_type, bytearray(0), self.scripts["GetDeviceID"], 0)
ret = self.read_response("Get Target ID")[1]
revision = ret[3]
devID = ret[0] << 16 | ret[1] << 8 | ret[2]
self._devID = devID
self._revision = revision
print("Device ID: {0:06X}".format(devID))
#EOF
def check_BIST(self):
key = b"BIST Tested"
self.get_Status_From_String(key, "check BIST")
key = b"BIST Results"
self.get_Status_From_String(key, "check BIST")
def get_Target_Id(self):
self.enter_Prog_Mode()
#self.check_BIST()
self.get_target_ID()
self.exit_Prog_Mode()
def get_ocd_regs(self):
data = self.read_target_mem8(0xF80, 12)
hwbpa = get_uint32_from_buf(data, 0)
hwbpb = get_uint32_from_buf(data, 4)
print("HWBP: 0x{0:04X} | 0x{1:04X}, 0x{2:02X}, 0x{3:02X}, 0x{4:02X}, 0x{5:02X}, "\
.format(hwbpa, hwbpb, data[8], data[9], data[10], data[11]))
def get_probe_status(self):
get_key = [
0x9B, 0x00, 0x07, # load into reg 0x00 the int 0x07 (ASI_KEY_STATUS)
0x1E, 0x0E, 0x00, # load CS reg
0x9F, # copy data to return
0x9B, 0x00, 0x0B, # load into reg 0x00 the int 0x0B (ASI_SYS_STATUS)
0x1E, 0x0E, 0x00, # load CS reg
0x9F, # copy data to return
]
message = self.create_msg_header(self.script_cmd_type, 24+len(get_key), 0)
message += self.create_script_header(0, len(get_key))
message += bytearray(get_key)
try:
self._icd.write(self._cmd_write_ep, message, 1000)
except usb.core.USBTimeoutError:
return probeMode.TO_Write
ret_val = probeMode.OFF
try:
ret = self._icd.read(self._cmd_read_ep, 1024, 1000)
ret_status = get_uint32_from_buf(ret, 16)
if (ret_status == 0x00):
data_len = ret[20]
if (data_len == 0x02):
if ret[24] == 0x02:
ret_val = probeMode.DEBUG
elif ret[25] == 0x08:
ret_val = probeMode.PROG
ret_val = probeMode.OFF
except usb.core.USBTimeoutError:
ret_val = probeMode.TO_Read
try:
self.recover_script()
except:
pass
return ret_val
def create_msg_header(self, type:int, message_len:int, transfer_len:int):
header = uint32_to_buf(type)
header += uint32_to_buf(0) # Always 0
header += uint32_to_buf(message_len)
header += uint32_to_buf(transfer_len)
return header
def create_script_header(self, arg_len:int, script_len:int):
scr_header = uint32_to_buf(arg_len)
scr_header += uint32_to_buf(script_len)
return scr_header
def run_Script(self, type:int, scr_params:bytearray, script:bytearray, transfer_len:int):
if (self._icd == None):
raise NoDebugger
script_len = len(script)
param_len = len(scr_params)
header_len = 16 + 8
preamble_len = header_len + param_len
message_len = preamble_len + script_len
transfer = bytearray(message_len)
transfer[ 0:16] = self.create_msg_header(type, message_len, transfer_len)
transfer[16:24] = self.create_script_header(param_len, script_len)
for x in range (0, param_len):
transfer[header_len + x] = scr_params[x]
for x in range (0, script_len):
transfer[preamble_len + x] = script[x]
try:
self._icd.write(self._cmd_write_ep, transfer)
except:
print("Script write failed, trying to reset")
self.recover_script()
#EOF
def read_response(self, func:str, timeout:int = 1000):
try:
ret = self._icd.read(self._cmd_read_ep, 1024, timeout)
response_status = get_uint32_from_buf(ret, 0)
if (response_status != 0x0D):
raise ValueError("Wrong Device Response On '" + func + "'. Repose: {0}".format(ret))
except:
print("Read response failed, trying to reset")
self.recover_script()
return
ret_len = get_uint32_from_buf(ret, 8)
ret_status = get_uint32_from_buf(ret, 16)
data_len = get_uint32_from_buf(ret, 20)
if (ret_len == 0x10):
return [0x00, []]
elif (ret_len < 24):
pass
else:
data = bytearray(data_len)
if (data_len > 0):
data = ret[24:24+data_len]
if (ret_status == 0x0104):
print("WARNING: Write to Register Failed. Chip must be halted first")
elif (ret_status != 0x00):
print("WARNING: Status is not 0: {0:04x}".format(ret_status))
return [ret_status, data]
#EOF
def read_script_upload(self, expected_len:int, func:str):
if (self._icd == None):
raise NoDebugger
ret = self._icd.read(self._data_read_ep, expected_len, 1000)
return ret
#EOF
def write_script_download(self, data:bytearray, func:str):
if (self._icd == None):
raise NoDebugger
self._icd.write(self._data_write_ep, data)
def send_script_done(self, func:str):
if (self._icd == None):
raise NoDebugger
script_done_type = 0x0103 # == 259
transfer = self.create_msg_header(script_done_type, 16, 0)
self._icd.write(self._cmd_write_ep, transfer)
self.read_response(func)
#EOF
def get_Status_From_String(self, key:bytes, func:str):
if (self._icd == None):
raise NoDebugger
type = 0x0105
if (key[-1:] != 0x00):
key += b"\x00" # Add String NULL terminator if there is none
message_len = 16 + len(key) + 1
transfer = self.create_msg_header(type, message_len, 0)
transfer += key
self._icd.write(self._cmd_write_ep, transfer)
ret = self._icd.read(self._cmd_read_ep, 1024, 250)
response_status = get_uint32_from_buf(ret, 0)
if (response_status != 0x0D):
print("ERROR: bad Response")
status_len = get_uint32_from_buf(ret, 8)
status = ret[16:status_len] # No Terminating NULL
return bytes(status)
#EOF
def get_memory_map_xml(self):
return self._memory_map_xml
#EOF
def get_target_xml(self):
return self._target_xml
#EOF
def conv_byte_to_hex(self, byte:int):
byte = byte & 0xFF
first = (byte // 16) + 0x30
if first > 0x39:
first += 0x07
sec = (byte & 0x0F) + 0x30
if sec > 0x39:
sec += 0x07
return chr(first)+chr(sec)
def get_target_registers(self):
#read_len = end - start + 1
#if start not in range(0, 32) or end not in range(0, 32):
# raise ValueError ("Register Numbers out of range. Start: {0}, End: {1}".format(start, end))
#if read_len not in range(1, 33):
# raise ValueError ("Register Range out of Bounds. Start: {0}, End: {1}".format(start, end))
cpu_regs = self.read_target_mem8(0, 32) # limit to all CPU regs for now
sreg_sp = self.read_target_mem8(0x3D, 3)
cpu_pc = self.get_PC()[1] * 2
ret_val = ""
#for x in range (0, start):
# ret_val += "xx" # Unread registers
for x in range (0, 32):
ret_val += self.conv_byte_to_hex(cpu_regs[x])
#for x in range (read_len, 32):
# ret_val += "xx" # Unread registers
# X, Y and Z (RIP)
#if (start <= 26 and end >= 27):
#ret_val += self.conv_regval_to_hex(regs[26]) + self.conv_regval_to_hex(regs[27])
#else:
# ret_val += "xxxx"
#if (start <= 28 and end >= 29):
#ret_val += self.conv_regval_to_hex(regs[28]) + self.conv_regval_to_hex(regs[29])
#else:
# ret_val += "xxxx"
#if (start <= 30 and end >= 31):
#ret_val += self.conv_regval_to_hex(regs[30]) + self.conv_regval_to_hex(regs[31])
#else:
# ret_val += "xxxx"
# SREG, SP, PC2
ret_val += self.conv_byte_to_hex(sreg_sp[2])
ret_val += self.conv_byte_to_hex(sreg_sp[0]) + self.conv_byte_to_hex(sreg_sp[1])
ret_val += self.conv_byte_to_hex((cpu_pc >> 0) & 0xFF)
ret_val += self.conv_byte_to_hex((cpu_pc >> 8) & 0xFF)
ret_val += self.conv_byte_to_hex((cpu_pc >> 16) & 0xFF)
ret_val += self.conv_byte_to_hex((cpu_pc >> 24) & 0xFF)
return ret_val.encode("ascii")
def set_target_registers(self, data:bytes):
data_len = len(data)
expected_len = 32 + 1 + 2 + 4
if (data_len < expected_len):
print("ERROR, received registers are too short")
return
if (data_len > expected_len):
print("ERROR, received registers are too long")
return
addr = 4000 # == 0x0FA0. Undocumented Memory area between SYSCFG and NVMCTRL. 0xF80 seems to be OCD module address
self.write_target_mem8(addr, data[0:32]) # CPU Regs, all in one go
self.write_target_mem8(0x3F, data[32:33]) # SREG
self.write_target_mem8(0x3D, data[33:35]) # SP
self.set_PC(data[35:39]) # PC
def get_target_register(self, reg:int):
if reg in range(0, 32):