]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/sle44xx/pd.py
sle44xx: rephrase annotation text construction
[libsigrokdecode.git] / decoders / sle44xx / pd.py
index e897b13b7b8be1d2901cf79edc9c733cf42e1a80..02bea428b694eec7f992fd5ad70cfb2ce9a1d004 100644 (file)
 
 import sigrokdecode as srd
 
-'''
-OUTPUT_PYTHON format:
+class Pin:
+    RST, CLK, IO, = range(3)
 
-Packet:
-[<ptype>, <pdata>]
+class Ann:
+    BIT, ATR, CMD, DATA, RESET, = range(5)
 
-<ptype>:
- - 'RESET'  (Reset/Abort condition)
- - 'ATR'    (ATR data from card)
- - 'CMD'    (Command from reader)
- - 'DATA'   (Data from card)
+class Bin:
+    SEND_DATA, = range(1)
 
-<pdata> is the data to/from the card
-For 'RESET' <pdata> is None.
-'''
-
-# CMD: [annotation-type-index, long annotation, short annotation]
+# CMD: [annotation class index, annotation texts for zoom levels]
 proto = {
-    'RESET':           [0, 'Reset',         'R'],
-    'ATR':             [1, 'ATR',           'ATR'],
-    'CMD':             [2, 'Command',       'C'],
-    'DATA':            [3, 'Data',          'D'],
+    'BIT':   [Ann.BIT,   '{bit}',],
+    'ATR':   [Ann.ATR,   'Answer To Reset: {data:02x}', 'ATR: {data:02x}', '{data:02x}',],
+    'CMD':   [Ann.CMD,   'Command: {data:02x}', 'Cmd: {data:02x}', '{data:02x}',],
+    'DATA':  [Ann.DATA,  'Data: {data:02x}', '{data:02x}',],
+    'RESET': [Ann.RESET, 'Reset', 'R',],
 }
 
+def lookup_proto_ann_txt(cmd, variables):
+    ann = proto.get(cmd, None)
+    if ann is None:
+        return None, []
+    cls, texts = ann[0], ann[1:]
+    texts = [t.format(**variables) for t in texts]
+    return cls, texts
+
 class Decoder(srd.Decoder):
     api_version = 3
     id = 'sle44xx'
     name = 'SLE 44xx'
-    longname = 'SLE44xx protocol'
+    longname = 'SLE44xx memory card'
     desc = 'SLE 4418/28/32/42 memory card serial protocol'
     license = 'gplv2+'
     inputs = ['logic']
-    outputs = ['sle44xx']
+    outputs = []
     tags = ['Memory']
     channels = (
         {'id': 'rst', 'name': 'RST', 'desc': 'Reset line'},
@@ -59,16 +61,16 @@ class Decoder(srd.Decoder):
         {'id': 'io', 'name': 'I/O', 'desc': 'I/O data line'},
     )
     annotations = (
-        ('reset', 'Reset'),
+        ('bit', 'Bit'),
         ('atr', 'ATR'),
         ('cmd', 'Command'),
         ('data', 'Data exchange'),
-        ('bit', 'Bit'),
+        ('reset', 'Reset'),
     )
     annotation_rows = (
-        ('bits', 'Bits', (4,)),
-        ('fields', 'Fields', (1, 2, 3)),
-        ('interrupts', 'Interrupts', (0,)),
+        ('bits', 'Bits', (Ann.BIT,)),
+        ('fields', 'Fields', (Ann.ATR, Ann.CMD, Ann.DATA)),
+        ('interrupts', 'Interrupts', (Ann.RESET,)),
     )
     binary = (
         ('send-data', 'Send data'),
@@ -89,24 +91,20 @@ class Decoder(srd.Decoder):
             self.samplerate = value
 
     def start(self):
-        self.out_python = self.register(srd.OUTPUT_PYTHON)
         self.out_ann = self.register(srd.OUTPUT_ANN)
         self.out_binary = self.register(srd.OUTPUT_BINARY)
 
     def putx(self, data):
         self.put(self.ss, self.es, self.out_ann, data)
 
-    def putp(self, data):
-        self.put(self.ss, self.es, self.out_python, data)
-
     def putb(self, data):
         self.put(self.ss, self.es, self.out_binary, data)
 
     def handle_reset(self, pins):
         self.ss, self.es = self.samplenum, self.samplenum
-        cmd = 'RESET' # No need to set the global self.cmd as this command is atomic
-        self.putp([cmd, None])
-        self.putx([proto[cmd][0], proto[cmd][1:]])
+        self.cmd = 'RESET'
+        cls, texts = lookup_proto_ann_txt(self.cmd, {})
+        self.putx([cls, texts])
         self.bitcount = self.databyte = 0
         self.bits = []
         self.cmd = 'ATR' # Next data bytes will be ATR
@@ -147,13 +145,14 @@ class Decoder(srd.Decoder):
 
         self.ss, self.es = self.ss_byte, self.samplenum + self.bitwidth
 
-        self.putb([0, bytes([self.databyte])])
+        self.putb([Bin.SEND_DATA, bytes([self.databyte])])
 
-        for bit in self.bits:
-            self.put(bit[1], bit[2], self.out_ann, [4, ['%d' % bit[0]]])
+        for bit_val, bit_ss, bit_es in self.bits:
+            cls, texts = lookup_proto_ann_txt('BIT', {'bit': bit_val})
+            self.put(bit_ss, bit_es, self.out_ann, [cls, texts])
 
-        self.putx([proto[self.cmd][0], ['%s: %02X' % (proto[self.cmd][1], self.databyte),
-                   '%s: %02X' % (proto[self.cmd][2], self.databyte), '%02X' % self.databyte]])
+        cls, texts = lookup_proto_ann_txt(self.cmd, {'data': self.databyte})
+        self.putx([cls, texts])
 
         # Done with this packet.
         self.bitcount = self.databyte = 0
@@ -161,12 +160,24 @@ class Decoder(srd.Decoder):
 
     def decode(self):
         while True:
-            pins = self.wait([{0: 'r'}, {0: 'l', 1: 'r'}, {1: 'h', 2: 'f'}, {1: 'h', 2: 'r'}])
-            if self.matched[0]: # RESET condition (R): RST = rising
+            # Signal conditions tracked by the protocol decoder:
+            # - RESET condition (R): RST = rising
+            # - Incoming data (D): RST = low, CLK = rising.
+            # - Command mode START: CLK = high, I/O = falling.
+            # - Command mode STOP: CLK = high, I/O = rising.
+            (COND_RESET, COND_DATA, COND_CMD_START, COND_CMD_STOP,) = range(4)
+            conditions = [
+                {Pin.RST: 'r'},
+                {Pin.RST: 'l', Pin.CLK: 'r'},
+                {Pin.CLK: 'h', Pin.IO: 'f'},
+                {Pin.CLK: 'h', Pin.IO: 'r'},
+            ]
+            pins = self.wait(conditions)
+            if self.matched[COND_RESET]:
                 self.handle_reset(pins)
-            elif self.matched[1]: # Incoming data (D): RST = low, CLK = rising.
+            elif self.matched[COND_DATA]:
                 self.handle_data(pins)
-            elif self.matched[2]: # Command mode START: CLK = high, I/O = falling.
+            elif self.matched[COND_CMD_START]:
                 self.handle_command(pins)
-            elif self.matched[3]: # Command mode STOP: CLK = high, I/O = rising.
+            elif self.matched[COND_CMD_STOP]:
                 self.handle_command(pins)