]> sigrok.org Git - libsigrokdecode.git/blob - decoders/avr_isp/pd.py
srd: Initial decoder for AVR ISP protocol.
[libsigrokdecode.git] / decoders / avr_isp / pd.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 # AVR ISP protocol decoder
22
23 import sigrokdecode as srd
24
25 # Device code addresses:
26 # 0x00: vendor code, 0x01: part family + flash size, 0x02: part number
27
28 # Vendor code
29 vendor_code = {
30     0x1e: 'Atmel',
31     0x00: 'Device locked',
32 }
33
34 # (Part family + flash size, part number)
35 part = {
36     (0x90, 0x01): 'AT90S1200',
37     (0x91, 0x01): 'AT90S2313',
38     (0x92, 0x01): 'AT90S4414',
39     (0x92, 0x05): 'ATmega48', # 4kB flash
40     (0x93, 0x01): 'AT90S8515',
41     (0x93, 0x0a): 'ATmega88', # 8kB flash
42     (0x94, 0x06): 'ATmega168', # 16kB flash
43     (0xff, 0xff): 'Device code erased, or target missing',
44     (0x01, 0x02): 'Device locked',
45     # TODO: Lots more entries.
46 }
47
48 VENDOR_CODE_ATMEL = 0x1e
49
50 class Decoder(srd.Decoder):
51     api_version = 1
52     id = 'avr_isp'
53     name = 'AVR ISP'
54     longname = 'AVR in-system programming'
55     desc = 'Protocol for in-system programming Atmel AVR MCUs.'
56     license = 'gplv2+'
57     inputs = ['spi', 'logic']
58     outputs = ['avr_isp']
59     probes = []
60     optional_probes = [
61         {'id': 'reset', 'name': 'RESET#', 'desc': 'Target AVR MCU reset'},
62     ]
63     options = {}
64     annotations = [
65         ['Text', 'Human-readable text'],
66         ['Warnings', 'Human-readable warnings'],
67     ]
68
69     def __init__(self, **kwargs):
70         self.state = 'IDLE'
71         self.mosi_bytes, self.miso_bytes = [], []
72         self.cmd_ss, self.cmd_es = 0, 0
73         self.xx, self.yy, self.zz, self.mm = 0, 0, 0, 0
74
75     def start(self, metadata):
76         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'avr_isp')
77         self.out_ann = self.add(srd.OUTPUT_ANN, 'avr_isp')
78
79     def report(self):
80         pass
81
82     def putx(self, data):
83         self.put(self.cmd_ss, self.cmd_es, self.out_ann, data)
84
85     def handle_cmd_programming_enable(self, cmd, ret):
86         # Programming enable.
87         # Note: The chip doesn't send any ACK for 'Programming enable'.
88         self.putx([0, ['Programming enable']])
89
90         # Sanity check on reply.
91         if ret[1:4] != [0xac, 0x53, cmd[2]]:
92             self.putx([1, ['Warning: Unexpected bytes in reply!']])
93
94     def handle_cmd_read_signature_byte_0x00(self, cmd, ret):
95         # Signature byte 0x00: vendor code.
96         self.vendor_code = ret[3]
97         v = vendor_code[self.vendor_code]
98         self.putx([0, ['Vendor code: 0x%02x (%s)' % (ret[3], v)]])
99
100         # Store for later.
101         self.xx = cmd[1] # Same as ret[2].
102         self.yy = cmd[3]
103         self.zz = ret[0]
104
105         # Sanity check on reply.
106         if ret[1] != 0x30 or ret[2] != cmd[1]:
107             self.putx([1, ['Warning: Unexpected bytes in reply!']])
108
109         # Sanity check for the vendor code.
110         if self.vendor_code != VENDOR_CODE_ATMEL:
111             self.putx([1, ['Warning: Vendor code was not 0x1e (Atmel)!']])
112
113     def handle_cmd_read_signature_byte_0x01(self, cmd, ret):
114         # Signature byte 0x01: part family and memory size.
115         self.part_fam_flash_size = ret[3]
116         self.putx([0, ['Part family / memory size: 0x%02x' % ret[3]]])
117
118         # Store for later.
119         self.mm = cmd[3]
120
121         # Sanity check on reply.
122         if ret[1] != 0x30 or ret[2] != cmd[1] or ret[0] != self.yy:
123             self.putx([1, ['Warning: Unexpected bytes in reply!']])
124
125     def handle_cmd_read_signature_byte_0x02(self, cmd, ret):
126         # Signature byte 0x02: part number.
127         self.part_number = ret[3]
128         self.putx([0, ['Part number: 0x%02x' % ret[3]]])
129
130         # TODO: Fix range.
131         p = part[(self.part_fam_flash_size, self.part_number)]
132         self.putx([0, ['Device: Atmel %s' % p]])
133
134         # Sanity check on reply.
135         if ret[1] != 0x30 or ret[2] != self.xx or ret[0] != self.mm:
136             self.putx([1, ['Warning: Unexpected bytes in reply!']])
137
138         self.xx, self.yy, self.zz, self.mm = 0, 0, 0, 0
139
140     def handle_cmd_chip_erase(self, cmd, ret):
141         # TODO
142         self.putx([0, ['Chip erase']])
143
144     def handle_cmd_read_fuse_bits(self, cmd, ret):
145         # Read fuse bits.
146         self.putx([0, ['Read fuse bits: 0x%02x' % ret[3]]])
147
148         # TODO: Decode fuse bits.
149         # TODO: Sanity check on reply.
150
151     def handle_cmd_read_fuse_high_bits(self, cmd, ret):
152         # Read fuse high bits.
153         self.putx([0, ['Read fuse high bits: 0x%02x' % ret[3]]])
154
155         # TODO: Decode fuse bits.
156         # TODO: Sanity check on reply.
157
158     def handle_cmd_read_extended_fuse_bits(self, cmd, ret):
159         # Read extended fuse bits.
160         self.putx([0, ['Read extended fuse bits: 0x%02x' % ret[3]]])
161
162         # TODO: Decode fuse bits.
163         # TODO: Sanity check on reply.
164
165     def handle_command(self, cmd, ret):
166         if cmd[:2] == [0xac, 0x53]:
167             self.handle_cmd_programming_enable(cmd, ret)
168         elif cmd[0] == 0xac and (cmd[1] & (1 << 7)) == (1 << 7):
169             self.handle_cmd_chip_erase(cmd, ret)
170         elif cmd[:3] == [0x50, 0x00, 0x00]:
171             self.handle_cmd_read_fuse_bits(cmd, ret)
172         elif cmd[:3] == [0x58, 0x08, 0x00]:
173             self.handle_cmd_read_fuse_high_bits(cmd, ret)
174         elif cmd[:3] == [0x50, 0x08, 0x00]:
175             self.handle_cmd_read_extended_fuse_bits(cmd, ret)
176         elif cmd[0] == 0x30 and cmd[2] == 0x00:
177             self.handle_cmd_read_signature_byte_0x00(cmd, ret)
178         elif cmd[0] == 0x30 and cmd[2] == 0x01:
179             self.handle_cmd_read_signature_byte_0x01(cmd, ret)
180         elif cmd[0] == 0x30 and cmd[2] == 0x02:
181             self.handle_cmd_read_signature_byte_0x02(cmd, ret)
182         else:
183             c = '%02x %02x %02x %02x' % tuple(cmd)
184             r = '%02x %02x %02x %02x' % tuple(ret)
185             self.putx([0, ['Unknown command: %s (reply: %s)!' % (c, r)]])
186
187     def decode(self, ss, es, data):
188         ptype, mosi, miso = data
189
190         if ptype != 'DATA':
191             return
192
193         # self.put(0, 0, self.out_ann,
194         #          [0, ['MOSI: 0x%02x, MISO: 0x%02x' % (mosi, miso)]])
195
196         self.ss, self.es = ss, es
197
198         # Append new bytes.
199         self.mosi_bytes.append(mosi)
200         self.miso_bytes.append(miso)
201
202         if len(self.mosi_bytes) == 0:
203             self.cmd_ss = ss
204
205         # All commands consist of 4 bytes.
206         if len(self.mosi_bytes) < 4:
207             return
208
209         self.cmd_es = es
210
211         self.handle_command(self.mosi_bytes, self.miso_bytes)
212
213         self.mosi_bytes = []
214         self.miso_bytes = []
215