]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/spiflash/pd.py
spiflash: Add FIDELIX FM25Q32 metadata.
[libsigrokdecode.git] / decoders / spiflash / pd.py
index 8a0b0e323be1bddd86fd3caba8e53041d4249632..d33ac0ce298bcb5f7157eb3ef0cccd5a69990971 100644 (file)
@@ -84,12 +84,23 @@ class Decoder(srd.Decoder):
     options = (
         {'id': 'chip', 'desc': 'Chip', 'default': tuple(chips.keys())[0],
             'values': tuple(chips.keys())},
+        {'id': 'format', 'desc': 'Data format', 'default': 'hex',
+            'values': ('hex', 'ascii')},
     )
 
     def __init__(self):
+        self.device_id = -1
         self.on_end_transaction = None
         self.end_current_transaction()
 
+        # Build dict mapping command keys to handler functions. Each
+        # command in 'cmds' (defined in lists.py) has a matching
+        # handler self.handle_<shortname>.
+        def get_handler(cmd):
+            s = 'handle_%s' % cmds[cmd][0].lower().replace('/', '_')
+            return getattr(self, s)
+        self.cmd_handlers = dict((cmd, get_handler(cmd)) for cmd in cmds.keys())
+
     def end_current_transaction(self):
         if self.on_end_transaction is not None: # Callback for CS# transition.
             self.on_end_transaction()
@@ -102,6 +113,7 @@ class Decoder(srd.Decoder):
     def start(self):
         self.out_ann = self.register(srd.OUTPUT_ANN)
         self.chip = chips[self.options['chip']]
+        self.vendor = self.options['chip'].split('_')[0]
 
     def putx(self, data):
         # Simplification, most annotations span exactly one SPI byte/packet.
@@ -110,6 +122,10 @@ class Decoder(srd.Decoder):
     def putb(self, data):
         self.put(self.ss_block, self.es_block, self.out_ann, data)
 
+    def vendor_device(self):
+        dev = device_name[self.vendor].get(self.device_id, 'Unknown')
+        return '%s %s' % (self.chip['vendor'], dev)
+
     def handle_wren(self, mosi, miso):
         self.putx([0, ['Command: %s' % cmds[self.state][1]]])
         self.state = None
@@ -135,9 +151,8 @@ class Decoder(srd.Decoder):
             self.putx([2, ['Device ID: 0x%02x' % miso]])
 
         if self.cmdstate == 4:
-            # TODO: Check self.device_id is valid & exists in device_names.
             # TODO: Same device ID? Check!
-            d = 'Device: Macronix %s' % device_name[self.device_id]
+            d = 'Device: %s' % self.vendor_device()
             self.put(self.ss_block, self.es, self.out_ann, [0, [d]])
             self.state = None
         else:
@@ -292,7 +307,20 @@ class Decoder(srd.Decoder):
         pass # TODO
 
     def handle_rdp_res(self, mosi, miso):
-        pass # TODO
+        if self.cmdstate == 1:
+            # Byte 1: Master sends command ID.
+            self.ss_block = self.ss
+            self.putx([16, ['Command: %s' % cmds[self.state][1]]])
+        elif self.cmdstate in (2, 3, 4):
+            # Bytes 2/3/4: Master sends three dummy bytes.
+            self.putx([24, ['Dummy byte: %02x' % mosi]])
+        elif self.cmdstate == 5:
+            # Byte 5: Slave sends device ID.
+            self.device_id = miso
+            self.putx([24, ['Device: %s' % self.vendor_device()]])
+            self.state = None
+
+        self.cmdstate += 1
 
     def handle_rems(self, mosi, miso):
         if self.cmdstate == 1:
@@ -323,7 +351,8 @@ class Decoder(srd.Decoder):
 
         if self.cmdstate == 6:
             id = self.ids[1] if self.manufacturer_id_first else self.ids[0]
-            self.putx([24, ['Device: Macronix %s' % device_name[id]]])
+            self.device_id = id
+            self.putx([24, ['Device: %s' % self.vendor_device()]])
             self.state = None
         else:
             self.cmdstate += 1
@@ -353,7 +382,10 @@ class Decoder(srd.Decoder):
         # Print accumulated block of data
         # (called on CS# de-assert via self.on_end_transaction callback).
         self.es_block = self.es # Ends on the CS# de-assert sample.
-        s = ' '.join([('%02x' % b) for b in self.data])
+        if self.options['format'] == 'hex':
+            s = ' '.join([('%02x' % b) for b in self.data])
+        else:
+            s = ''.join(map(chr, self.data))
         self.putb([25, ['%s %d bytes: %s' % (label, len(self.data), s)]])
 
     def decode(self, ss, es, data):
@@ -373,10 +405,8 @@ class Decoder(srd.Decoder):
             self.cmdstate = 1
 
         # Handle commands.
-        if self.state in cmds:
-            s = 'handle_%s' % cmds[self.state][0].lower().replace('/', '_')
-            handle_reg = getattr(self, s)
-            handle_reg(mosi, miso)
-        else:
+        try:
+            self.cmd_handlers[self.state](mosi, miso)
+        except KeyError:
             self.putx([24, ['Unknown command: 0x%02x' % mosi]])
             self.state = None