]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/uart/pd.py
uart: Allow either RX or TX to be optional.
[libsigrokdecode.git] / decoders / uart / pd.py
index 6ecc8cf3fffce22bc4b1702c603f49007b8bf5f3..18507cf7597924516342f0aceafcb2963af2c892 100644 (file)
@@ -1,7 +1,7 @@
 ##
 ## This file is part of the libsigrokdecode project.
 ##
-## Copyright (C) 2011-2013 Uwe Hermann <uwe@hermann-uwe.de>
+## Copyright (C) 2011-2014 Uwe Hermann <uwe@hermann-uwe.de>
 ##
 ## 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
 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 ##
 
-# UART protocol decoder
-
 import sigrokdecode as srd
 
 '''
-Protocol output format:
+OUTPUT_PYTHON format:
 
 UART packet:
 [<packet-type>, <rxtx>, <packet-data>]
@@ -79,13 +77,13 @@ class Decoder(srd.Decoder):
     license = 'gplv2+'
     inputs = ['logic']
     outputs = ['uart']
-    probes = [
+    probes = []
+    optional_probes = [
         # Allow specifying only one of the signals, e.g. if only one data
         # direction exists (or is relevant).
         {'id': 'rx', 'name': 'RX', 'desc': 'UART receive line'},
         {'id': 'tx', 'name': 'TX', 'desc': 'UART transmit line'},
     ]
-    optional_probes = []
     options = {
         'baudrate': ['Baud rate', 115200],
         'num_data_bits': ['Data bits', 8], # Valid: 5-9.
@@ -97,13 +95,18 @@ class Decoder(srd.Decoder):
         # TODO: Options to invert the signal(s).
     }
     annotations = [
-        ['RX data', 'UART RX data'],
-        ['TX data', 'UART TX data'],
-        ['Start bits', 'UART start bits'],
-        ['Parity bits', 'UART parity bits'],
-        ['Stop bits', 'UART stop bits'],
-        ['Warnings', 'Warnings'],
+        ['rx-data', 'UART RX data'],
+        ['tx-data', 'UART TX data'],
+        ['start-bits', 'UART start bits'],
+        ['parity-bits', 'UART parity bits'],
+        ['stop-bits', 'UART stop bits'],
+        ['warnings', 'Warnings'],
     ]
+    binary = (
+        ('rx', 'RX dump'),
+        ('tx', 'TX dump'),
+        ('rxtx', 'RX/TX dump'),
+    )
 
     def putx(self, rxtx, data):
         s, halfbit = self.startsample[rxtx], int(self.bit_width / 2)
@@ -115,9 +118,14 @@ class Decoder(srd.Decoder):
 
     def putp(self, data):
         s, halfbit = self.samplenum, int(self.bit_width / 2)
-        self.put(s - halfbit, s + halfbit, self.out_proto, data)
+        self.put(s - halfbit, s + halfbit, self.out_python, data)
+
+    def putbin(self, rxtx, data):
+        s, halfbit = self.startsample[rxtx], int(self.bit_width / 2)
+        self.put(s - halfbit, self.samplenum + halfbit, self.out_bin, data)
 
     def __init__(self, **kwargs):
+        self.samplerate = None
         self.samplenum = 0
         self.frame_start = [-1, -1]
         self.startbit = [-1, -1]
@@ -130,17 +138,16 @@ class Decoder(srd.Decoder):
         self.oldbit = [1, 1]
         self.oldpins = [1, 1]
 
-    def start(self, metadata):
-        self.samplerate = metadata['samplerate']
-        self.out_proto = self.add(srd.OUTPUT_PROTO, 'uart')
-        self.out_ann = self.add(srd.OUTPUT_ANN, 'uart')
-
-        # The width of one UART bit in number of samples.
-        self.bit_width = \
-            float(self.samplerate) / float(self.options['baudrate'])
+    def start(self):
+        self.out_python = self.register(srd.OUTPUT_PYTHON)
+        self.out_bin = self.register(srd.OUTPUT_BINARY)
+        self.out_ann = self.register(srd.OUTPUT_ANN)
 
-    def report(self):
-        pass
+    def metadata(self, key, value):
+        if key == srd.SRD_CONF_SAMPLERATE:
+            self.samplerate = value;
+            # The width of one UART bit in number of samples.
+            self.bit_width = float(self.samplerate) / float(self.options['baudrate'])
 
     # Return true if we reached the middle of the desired bit, false otherwise.
     def reached_bit(self, rxtx, bitnum):
@@ -223,7 +230,8 @@ class Decoder(srd.Decoder):
 
         b, f = self.databyte[rxtx], self.options['format']
         if f == 'ascii':
-            self.putx(rxtx, [rxtx, [chr(b)]])
+            c = chr(b) if b in range(30, 126 + 1) else '[%02X]' % b
+            self.putx(rxtx, [rxtx, [c]])
         elif f == 'dec':
             self.putx(rxtx, [rxtx, [str(b)]])
         elif f == 'hex':
@@ -235,6 +243,9 @@ class Decoder(srd.Decoder):
         else:
             raise Exception('Invalid data format option: %s' % f)
 
+        self.putbin(rxtx, (rxtx, bytes([b])))
+        self.putbin(rxtx, (2, bytes([b])))
+
     def get_parity_bit(self, rxtx, signal):
         # If no parity is used/configured, skip to the next state immediately.
         if self.options['parity_type'] == 'none':
@@ -280,7 +291,8 @@ class Decoder(srd.Decoder):
         self.putg([4, ['Stop bit', 'Stop', 'T']])
 
     def decode(self, ss, es, data):
-        # TODO: Either RX or TX could be omitted (optional probe).
+        if self.samplerate is None:
+            raise Exception("Cannot decode without samplerate.")
         for (self.samplenum, pins) in data:
 
             # Note: Ignoring identical samples here for performance reasons
@@ -289,8 +301,17 @@ class Decoder(srd.Decoder):
             #     continue
             self.oldpins, (rx, tx) = pins, pins
 
+            # Either RX or TX (but not both) can be omitted.
+            has_pin = [rx in (0, 1), tx in (0, 1)]
+            if has_pin == [False, False]:
+                raise Exception('Either TX or RX (or both) pins required.')
+
             # State machine.
             for rxtx in (RX, TX):
+                # Don't try to handle RX (or TX) if not supplied.
+                if not has_pin[rxtx]:
+                    continue
+
                 signal = rx if (rxtx == RX) else tx
 
                 if self.state[rxtx] == 'WAIT FOR START BIT':