2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 # AVR ISP protocol decoder
23 import sigrokdecode as srd
25 # Device code addresses:
26 # 0x00: vendor code, 0x01: part family + flash size, 0x02: part number
31 0x00: 'Device locked',
34 # (Part family + flash size, part number)
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.
48 VENDOR_CODE_ATMEL = 0x1e
50 class Decoder(srd.Decoder):
54 longname = 'AVR in-system programming'
55 desc = 'Protocol for in-system programming Atmel AVR MCUs.'
57 inputs = ['spi', 'logic']
61 {'id': 'reset', 'name': 'RESET#', 'desc': 'Target AVR MCU reset'},
65 ['Text', 'Human-readable text'],
66 ['Warnings', 'Human-readable warnings'],
69 def __init__(self, **kwargs):
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
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')
83 self.put(self.cmd_ss, self.cmd_es, self.out_ann, data)
85 def handle_cmd_programming_enable(self, cmd, ret):
87 # Note: The chip doesn't send any ACK for 'Programming enable'.
88 self.putx([0, ['Programming enable']])
90 # Sanity check on reply.
91 if ret[1:4] != [0xac, 0x53, cmd[2]]:
92 self.putx([1, ['Warning: Unexpected bytes in reply!']])
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)]])
101 self.xx = cmd[1] # Same as ret[2].
105 # Sanity check on reply.
106 if ret[1] != 0x30 or ret[2] != cmd[1]:
107 self.putx([1, ['Warning: Unexpected bytes in reply!']])
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)!']])
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]]])
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!']])
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]]])
131 p = part[(self.part_fam_flash_size, self.part_number)]
132 self.putx([0, ['Device: Atmel %s' % p]])
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!']])
138 self.xx, self.yy, self.zz, self.mm = 0, 0, 0, 0
140 def handle_cmd_chip_erase(self, cmd, ret):
142 self.putx([0, ['Chip erase']])
144 def handle_cmd_read_fuse_bits(self, cmd, ret):
146 self.putx([0, ['Read fuse bits: 0x%02x' % ret[3]]])
148 # TODO: Decode fuse bits.
149 # TODO: Sanity check on reply.
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]]])
155 # TODO: Decode fuse bits.
156 # TODO: Sanity check on reply.
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]]])
162 # TODO: Decode fuse bits.
163 # TODO: Sanity check on reply.
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)
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)]])
187 def decode(self, ss, es, data):
188 ptype, mosi, miso = data
193 # self.put(0, 0, self.out_ann,
194 # [0, ['MOSI: 0x%02x, MISO: 0x%02x' % (mosi, miso)]])
196 self.ss, self.es = ss, es
199 self.mosi_bytes.append(mosi)
200 self.miso_bytes.append(miso)
202 if len(self.mosi_bytes) == 0:
205 # All commands consist of 4 bytes.
206 if len(self.mosi_bytes) < 4:
211 self.handle_command(self.mosi_bytes, self.miso_bytes)