X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Favr_isp%2Fpd.py;h=26246623b4510bfd0179f2428bb029cc1246b389;hp=be6c1903db3ce196d9b520fb4338ee4c00f28bf0;hb=3b0013416fcfb17e8bc6cfd6850e79971f239fe0;hpb=3bd76451f01dface8df58828a2bbc242d3391db9 diff --git a/decoders/avr_isp/pd.py b/decoders/avr_isp/pd.py index be6c190..2624662 100644 --- a/decoders/avr_isp/pd.py +++ b/decoders/avr_isp/pd.py @@ -1,7 +1,7 @@ ## -## This file is part of the sigrok project. +## This file is part of the libsigrokdecode project. ## -## Copyright (C) 2012 Uwe Hermann +## Copyright (C) 2012-2014 Uwe Hermann ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -18,32 +18,8 @@ ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ## -# AVR ISP protocol decoder - import sigrokdecode as srd - -# Device code addresses: -# 0x00: vendor code, 0x01: part family + flash size, 0x02: part number - -# Vendor code -vendor_code = { - 0x1e: 'Atmel', - 0x00: 'Device locked', -} - -# (Part family + flash size, part number) -part = { - (0x90, 0x01): 'AT90S1200', - (0x91, 0x01): 'AT90S2313', - (0x92, 0x01): 'AT90S4414', - (0x92, 0x05): 'ATmega48', # 4kB flash - (0x93, 0x01): 'AT90S8515', - (0x93, 0x0a): 'ATmega88', # 8kB flash - (0x94, 0x06): 'ATmega168', # 16kB flash - (0xff, 0xff): 'Device code erased, or target missing', - (0x01, 0x02): 'Device locked', - # TODO: Lots more entries. -} +from .parts import * VENDOR_CODE_ATMEL = 0x1e @@ -51,7 +27,7 @@ class Decoder(srd.Decoder): api_version = 1 id = 'avr_isp' name = 'AVR ISP' - longname = 'AVR in-system programming' + longname = 'AVR In-System Programming' desc = 'Protocol for in-system programming Atmel AVR MCUs.' license = 'gplv2+' inputs = ['spi', 'logic'] @@ -62,9 +38,21 @@ class Decoder(srd.Decoder): ] options = {} annotations = [ - ['Text', 'Human-readable text'], - ['Warnings', 'Human-readable warnings'], + ['pe', 'Programming enable'], + ['rsb0', 'Read signature byte 0'], + ['rsb1', 'Read signature byte 1'], + ['rsb2', 'Read signature byte 2'], + ['ce', 'Chip erase'], + ['rfb', 'Read fuse bits'], + ['rhfb', 'Read high fuse bits'], + ['refb', 'Read extended fuse bits'], + ['warnings', 'Warnings'], ] + annotation_rows = ( + ('bits', 'Bits', ()), + ('commands', 'Commands', tuple(range(7 + 1))), + ('warnings', 'Warnings', (8,)), + ) def __init__(self, **kwargs): self.state = 'IDLE' @@ -72,12 +60,9 @@ class Decoder(srd.Decoder): self.cmd_ss, self.cmd_es = 0, 0 self.xx, self.yy, self.zz, self.mm = 0, 0, 0, 0 - def start(self, metadata): - # self.out_proto = self.add(srd.OUTPUT_PROTO, 'avr_isp') - self.out_ann = self.add(srd.OUTPUT_ANN, 'avr_isp') - - def report(self): - pass + def start(self): + # self.out_python = self.register(srd.OUTPUT_PYTHON) + self.out_ann = self.register(srd.OUTPUT_ANN) def putx(self, data): self.put(self.cmd_ss, self.cmd_es, self.out_ann, data) @@ -89,13 +74,13 @@ class Decoder(srd.Decoder): # Sanity check on reply. if ret[1:4] != [0xac, 0x53, cmd[2]]: - self.putx([1, ['Warning: Unexpected bytes in reply!']]) + self.putx([8, ['Warning: Unexpected bytes in reply!']]) def handle_cmd_read_signature_byte_0x00(self, cmd, ret): # Signature byte 0x00: vendor code. self.vendor_code = ret[3] v = vendor_code[self.vendor_code] - self.putx([0, ['Vendor code: 0x%02x (%s)' % (ret[3], v)]]) + self.putx([1, ['Vendor code: 0x%02x (%s)' % (ret[3], v)]]) # Store for later. self.xx = cmd[1] # Same as ret[2]. @@ -104,60 +89,69 @@ class Decoder(srd.Decoder): # Sanity check on reply. if ret[1] != 0x30 or ret[2] != cmd[1]: - self.putx([1, ['Warning: Unexpected bytes in reply!']]) + self.putx([8, ['Warning: Unexpected bytes in reply!']]) # Sanity check for the vendor code. if self.vendor_code != VENDOR_CODE_ATMEL: - self.putx([1, ['Warning: Vendor code was not 0x1e (Atmel)!']]) + self.putx([8, ['Warning: Vendor code was not 0x1e (Atmel)!']]) def handle_cmd_read_signature_byte_0x01(self, cmd, ret): # Signature byte 0x01: part family and memory size. self.part_fam_flash_size = ret[3] - self.putx([0, ['Part family / memory size: 0x%02x' % ret[3]]]) + self.putx([2, ['Part family / memory size: 0x%02x' % ret[3]]]) # Store for later. self.mm = cmd[3] # Sanity check on reply. if ret[1] != 0x30 or ret[2] != cmd[1] or ret[0] != self.yy: - self.putx([1, ['Warning: Unexpected bytes in reply!']]) + self.putx([8, ['Warning: Unexpected bytes in reply!']]) def handle_cmd_read_signature_byte_0x02(self, cmd, ret): # Signature byte 0x02: part number. self.part_number = ret[3] - self.putx([0, ['Part number: 0x%02x' % ret[3]]]) + self.putx([3, ['Part number: 0x%02x' % ret[3]]]) # TODO: Fix range. p = part[(self.part_fam_flash_size, self.part_number)] - self.putx([0, ['Device: Atmel %s' % p]]) + self.putx([3, ['Device: Atmel %s' % p]]) # Sanity check on reply. if ret[1] != 0x30 or ret[2] != self.xx or ret[0] != self.mm: - self.putx([1, ['Warning: Unexpected bytes in reply!']]) + self.putx([8, ['Warning: Unexpected bytes in reply!']]) self.xx, self.yy, self.zz, self.mm = 0, 0, 0, 0 def handle_cmd_chip_erase(self, cmd, ret): - # TODO - self.putx([0, ['Chip erase']]) + # Chip erase (erases both flash an EEPROM). + # Upon successful chip erase, the lock bits will also be erased. + # The only way to end a Chip Erase cycle is to release RESET#. + self.putx([4, ['Chip erase']]) + + # TODO: Check/handle RESET#. + + # Sanity check on reply. + bit = (ret[2] & (1 << 7)) >> 7 + if ret[1] != 0xac or bit != 1 or ret[3] != cmd[2]: + self.putx([8, ['Warning: Unexpected bytes in reply!']]) def handle_cmd_read_fuse_bits(self, cmd, ret): # Read fuse bits. - self.putx([0, ['Read fuse bits: 0x%02x' % ret[3]]]) + self.putx([5, ['Read fuse bits: 0x%02x' % ret[3]]]) # TODO: Decode fuse bits. # TODO: Sanity check on reply. def handle_cmd_read_fuse_high_bits(self, cmd, ret): # Read fuse high bits. - self.putx([0, ['Read fuse high bits: 0x%02x' % ret[3]]]) + self.putx([6, ['Read fuse high bits: 0x%02x' % ret[3]]]) # TODO: Decode fuse bits. # TODO: Sanity check on reply. def handle_cmd_read_extended_fuse_bits(self, cmd, ret): # Read extended fuse bits. - self.putx([0, ['Read extended fuse bits: 0x%02x' % ret[3]]]) + self.putx([7, ['Read extended fuse bits: 0x%02x' % ret[3]]]) # TODO: Decode fuse bits. # TODO: Sanity check on reply. @@ -187,21 +181,25 @@ class Decoder(srd.Decoder): def decode(self, ss, es, data): ptype, mosi, miso = data - if ptype != 'DATA': + # For now, only use DATA and BITS packets. + if ptype not in ('DATA', 'BITS'): return - # self.put(0, 0, self.out_ann, - # [0, ['MOSI: 0x%02x, MISO: 0x%02x' % (mosi, miso)]]) + # Store the individual bit values and ss/es numbers. The next packet + # is guaranteed to be a 'DATA' packet belonging to this 'BITS' one. + if ptype == 'BITS': + self.miso_bits, self.mosi_bits = miso, mosi + return self.ss, self.es = ss, es + if len(self.mosi_bytes) == 0: + self.cmd_ss = ss + # Append new bytes. self.mosi_bytes.append(mosi) self.miso_bytes.append(miso) - if len(self.mosi_bytes) == 0: - self.cmd_ss = ss - # All commands consist of 4 bytes. if len(self.mosi_bytes) < 4: return