jtagarm7 obligatory stupid bug fixes, doh!
[goodfet] / client / GoodFETJTAG.py
1 #!/usr/bin/env python
2 # GoodFET Basic JTAG Client
3
4 import sys, binascii, struct
5
6 # Standard verbs
7 READ  = 0x00
8 WRITE = 0x01
9 PEEK  = 0x02
10 POKE  = 0x03
11 SETUP = 0x10
12 START = 0x20
13 STOP  = 0x21
14 CALL  = 0x30
15 EXEC  = 0x31
16 NOK   = 0x7E
17 OK    = 0x7F
18
19 # JTAG commands
20 JTAG_RESET_TAP              = 0x82
21 JTAG_RESET_TARGET           = 0x83
22 JTAG_DETECT_IR_WIDTH        = 0x84
23 JTAG_DETECT_CHAIN_LENGTH    = 0x85
24 JTAG_GET_DEVICE_ID          = 0x86
25
26 from GoodFET import GoodFET
27 from intelhex import IntelHex
28
29 class GoodFETJTAG(GoodFET):
30
31     """A GoodFET variant for basic JTAG'ing."""
32
33     JTAGAPP=0x10;
34     APP=JTAGAPP;
35  
36     def _check_return(self, verb, length=0):
37         if (self.app == self.APP) and \
38            (self.verb == verb) and \
39            (len(self.data) == length):
40             print "OK"
41             return True
42         print "Failed!"
43         return False
44
45     def setup(self):
46         """Move the FET into the JTAG configuration."""
47         sys.stdout.write("Initializing JTAG...")
48         self.writecmd(self.APP, SETUP)
49         self._check_return(SETUP)
50
51     def reset_tap(self):
52         sys.stdout.write("Resetting TAP...")
53         self.writecmd(self.APP, JTAG_RESET_TAP)
54         self._check_return(JTAG_RESET_TAP)
55
56     def reset_target(self):
57         sys.stdout.write("Resseting target device...")
58         self.writecmd(self.APP, JTAG_RESET_TARGET)
59         self._check_return(JTAG_RESET_TARGET)
60
61     def detect_ir_width(self):
62         sys.stdout.write("Detecting IR width...")
63         self.writecmd(self.APP, JTAG_DETECT_IR_WIDTH)
64         self._check_return(JTAG_DETECT_IR_WIDTH, 2)
65         width = struct.unpack("!H", self.data)[0]
66         return width
67
68     def detect_chain_length(self):
69         sys.stdout.write("Detecting chain length...")
70         self.writecmd(self.APP, JTAG_DETECT_CHAIN_LENGTH)
71         self._check_return(JTAG_DETECT_CHAIN_LENGTH, 2)
72         length = struct.unpack("!H", self.data)[0]
73         return length
74
75     def get_device_id(self, chip):
76         sys.stdout.write("Getting ID for device %d..." % chip)
77         self.writecmd(self.APP, JTAG_GET_DEVICE_ID, 2, struct.pack("!H", chip))
78         self._check_return(JTAG_GET_DEVICE_ID, 4)
79         id = struct.unpack("!L", self.data)[0]
80         return id
81
82