]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/spiflash/pd.py
spiflash: Handle CS# transitions, allow variable-length transfers
[libsigrokdecode.git] / decoders / spiflash / pd.py
index 6b46ad11423703ba7c2b6e15007e7e6e4e87818f..056b0a0358dee65d46a70e6a1d00d5d9090f6762 100644 (file)
 ##
 
 import sigrokdecode as srd
-
-# Dict which maps command IDs to their names and descriptions.
-cmds = {
-    0x06: ('WREN', 'Write enable'),
-    0x04: ('WRDI', 'Write disable'),
-    0x9f: ('RDID', 'Read identification'),
-    0x05: ('RDSR', 'Read status register'),
-    0x01: ('WRSR', 'Write status register'),
-    0x03: ('READ', 'Read data'),
-    0x0b: ('FAST/READ', 'Fast read data'),
-    0xbb: ('2READ', '2x I/O read'),
-    0x20: ('SE', 'Sector erase'),
-    0xd8: ('BE', 'Block erase'),
-    0x60: ('CE', 'Chip erase'),
-    0xc7: ('CE2', 'Chip erase'), # Alternative command ID
-    0x02: ('PP', 'Page program'),
-    0xad: ('CP', 'Continuously program mode'),
-    0xb9: ('DP', 'Deep power down'),
-    0xab: ('RDP/RES', 'Release from deep powerdown / Read electronic ID'),
-    0x90: ('REMS', 'Read electronic manufacturer & device ID'),
-    0xef: ('REMS2', 'Read ID for 2x I/O mode'),
-    0xb1: ('ENSO', 'Enter secured OTP'),
-    0xc1: ('EXSO', 'Exit secured OTP'),
-    0x2b: ('RDSCUR', 'Read security register'),
-    0x2f: ('WRSCUR', 'Write security register'),
-    0x70: ('ESRY', 'Enable SO to output RY/BY#'),
-    0x80: ('DSRY', 'Disable SO to output RY/BY#'),
-}
-
-device_name = {
-    0x14: 'MX25L1605D',
-    0x15: 'MX25L3205D',
-    0x16: 'MX25L6405D',
-}
+from .lists import *
 
 def cmd_annotation_classes():
     return tuple([tuple([cmd[0].lower(), cmd[1]]) for cmd in cmds.values()])
@@ -89,7 +56,7 @@ class Decoder(srd.Decoder):
     longname = 'SPI flash chips'
     desc = 'xx25 series SPI (NOR) flash chip protocol.'
     license = 'gplv2+'
-    inputs = ['logic']
+    inputs = ['spi']
     outputs = ['spiflash']
     annotations = cmd_annotation_classes() + (
         ('bits', 'Bits'),
@@ -101,8 +68,19 @@ class Decoder(srd.Decoder):
         ('commands', 'Commands', tuple(range(23 + 1))),
         ('warnings', 'Warnings', (26,)),
     )
+    options = (
+        {'id': 'chip', 'desc': 'Chip', 'default': tuple(chips.keys())[0],
+            'values': tuple(chips.keys())},
+    )
+
+    def __init__(self):
+        self.on_end_transaction = None
+        self.end_current_transaction()
 
-    def __init__(self, **kwargs):
+    def end_current_transaction(self):
+        if self.on_end_transaction is not None: # Callback for CS# transition.
+            self.on_end_transaction()
+            self.on_end_transaction = None
         self.state = None
         self.cmdstate = 1
         self.addr = 0
@@ -110,13 +88,14 @@ class Decoder(srd.Decoder):
 
     def start(self):
         self.out_ann = self.register(srd.OUTPUT_ANN)
+        self.chip = chips[self.options['chip']]
 
     def putx(self, data):
         # Simplification, most annotations span exactly one SPI byte/packet.
         self.put(self.ss, self.es, self.out_ann, data)
 
     def putb(self, data):
-        self.put(self.block_ss, self.block_es, self.out_ann, data)
+        self.put(self.ss_block, self.es_block, self.out_ann, data)
 
     def handle_wren(self, mosi, miso):
         self.putx([0, ['Command: %s' % cmds[self.state][1]]])
@@ -161,13 +140,8 @@ class Decoder(srd.Decoder):
             self.putx([3, ['Command: %s' % cmds[self.state][1]]])
         elif self.cmdstate >= 2:
             # Bytes 2-x: Slave sends status register as long as master clocks.
-            if self.cmdstate <= 3: # TODO: While CS# asserted.
-                self.putx([24, ['Status register: 0x%02x' % miso]])
-                self.putx([25, [decode_status_reg(miso)]])
-
-            if self.cmdstate == 3: # TODO: If CS# got de-asserted.
-                self.state = None
-                return
+            self.putx([24, ['Status register: 0x%02x' % miso]])
+            self.putx([25, [decode_status_reg(miso)]])
 
         self.cmdstate += 1
 
@@ -190,19 +164,10 @@ class Decoder(srd.Decoder):
                 self.addr = 0
         elif self.cmdstate >= 5:
             # Bytes 5-x: Master reads data bytes (until CS# de-asserted).
-            # TODO: For now we hardcode 256 bytes per READ command.
-            if self.cmdstate <= 256 + 4: # TODO: While CS# asserted.
-                self.data.append(miso)
-                # self.putx([0, ['New read byte: 0x%02x' % miso]])
-
-            if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted.
-                # s = ', '.join(map(hex, self.data))
-                s = ''.join(map(chr, self.data))
-                self.putx([24, ['Read data']])
-                self.putx([25, ['Read data: %s' % s]])
-                self.data = []
-                self.state = None
-                return
+            if self.cmdstate == 5:
+                self.ss_block = self.ss
+                self.on_end_transaction = lambda: self.output_data_block('Read')
+            self.data.append(miso)
 
         self.cmdstate += 1
 
@@ -216,27 +181,19 @@ class Decoder(srd.Decoder):
             # Bytes 2/3/4: Master sends read address (24bits, MSB-first).
             self.putx([24, ['AD%d: 0x%02x' % (self.cmdstate - 1, mosi)]])
             if self.cmdstate == 2:
-                self.block_ss = self.ss
+                self.ss_block = self.ss
             self.addr |= (mosi << ((4 - self.cmdstate) * 8))
         elif self.cmdstate == 5:
             self.putx([24, ['Dummy byte: 0x%02x' % mosi]])
-            self.block_es = self.es
+            self.es_block = self.es
             self.putb([5, ['Read address: 0x%06x' % self.addr]])
             self.addr = 0
         elif self.cmdstate >= 6:
             # Bytes 6-x: Master reads data bytes (until CS# de-asserted).
-            # TODO: For now we hardcode 32 bytes per FAST READ command.
             if self.cmdstate == 6:
-                self.block_ss = self.ss
-            if self.cmdstate <= 32 + 5: # TODO: While CS# asserted.
-                self.data.append(miso)
-            if self.cmdstate == 32 + 5: # TODO: If CS# got de-asserted.
-                self.block_es = self.es
-                s = ' '.join([hex(b)[2:] for b in self.data])
-                self.putb([25, ['Read data: %s' % s]])
-                self.data = []
-                self.state = None
-                return
+                self.ss_block = self.ss
+                self.on_end_transaction = lambda: self.output_block("Read")
+            self.data.append(miso)
 
         self.cmdstate += 1
 
@@ -294,19 +251,10 @@ class Decoder(srd.Decoder):
                 self.addr = 0
         elif self.cmdstate >= 5:
             # Bytes 5-x: Master sends data bytes (until CS# de-asserted).
-            # TODO: For now we hardcode 256 bytes per page / PP command.
-            if self.cmdstate <= 256 + 4: # TODO: While CS# asserted.
-                self.data.append(mosi)
-                # self.putx([0, ['New data byte: 0x%02x' % mosi]])
-
-            if self.cmdstate == 256 + 4: # TODO: If CS# got de-asserted.
-                # s = ', '.join(map(hex, self.data))
-                s = ''.join(map(chr, self.data))
-                self.putx([24, ['Page data']])
-                self.putx([25, ['Page data: %s' % s]])
-                self.data = []
-                self.state = None
-                return
+            if self.cmdstate == 5:
+                self.ss_block = self.ss
+                self.on_end_transaction = lambda: self.output_data_block('Page data')
+            self.data.append(mosi)
 
         self.cmdstate += 1
 
@@ -374,24 +322,24 @@ class Decoder(srd.Decoder):
     def handle_dsry(self, mosi, miso):
         pass # TODO
 
-    def decode(self, ss, es, data):
+    def output_data_block(self, label):
+        # 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])
+        self.putb([25, ['%s %d bytes: %s' % (label, len(self.data), s)]])
 
+    def decode(self, ss, es, data):
         ptype, mosi, miso = data
 
-        # if ptype == 'DATA':
-        #     self.putx([0, ['MOSI: 0x%02x, MISO: 0x%02x' % (mosi, miso)]])
+        self.ss, self.es = ss, es
 
-        # if ptype == 'CS-CHANGE':
-        #     if mosi == 1 and miso == 0:
-        #         self.putx([0, ['Asserting CS#']])
-        #     elif mosi == 0 and miso == 1:
-        #         self.putx([0, ['De-asserting CS#']])
+        if ptype == 'CS-CHANGE':
+            self.end_current_transaction()
 
         if ptype != 'DATA':
             return
 
-        self.ss, self.es = ss, es
-
         # If we encountered a known chip command, enter the resp. state.
         if self.state is None:
             self.state = mosi