]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/spi/pd.py
spi: Convert to PD API version 3
[libsigrokdecode.git] / decoders / spi / pd.py
index 3040ed296380c027bc90c5c0044746077d4ec266..1252a6a07cb7f7728872d23e512249970cd4ff58 100644 (file)
@@ -15,8 +15,7 @@
 ## GNU General Public License for more details.
 ##
 ## You should have received a copy of the GNU General Public License
-## along with this program; if not, write to the Free Software
-## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+## along with this program; if not, see <http://www.gnu.org/licenses/>.
 ##
 
 import sigrokdecode as srd
@@ -78,7 +77,7 @@ class ChannelError(Exception):
     pass
 
 class Decoder(srd.Decoder):
-    api_version = 2
+    api_version = 3
     id = 'spi'
     name = 'SPI'
     longname = 'Serial Peripheral Interface'
@@ -138,9 +137,7 @@ class Decoder(srd.Decoder):
         self.ss_transfer = -1
         self.cs_was_deasserted = False
         self.oldcs = None
-        self.oldpins = None
         self.have_cs = self.have_miso = self.have_mosi = None
-        self.no_cs_notification = False
 
     def metadata(self, key, value):
         if key == srd.SRD_CONF_SAMPLERATE:
@@ -149,9 +146,10 @@ class Decoder(srd.Decoder):
     def start(self):
         self.out_python = self.register(srd.OUTPUT_PYTHON)
         self.out_ann = self.register(srd.OUTPUT_ANN)
-        self.out_bin = self.register(srd.OUTPUT_BINARY)
+        self.out_binary = self.register(srd.OUTPUT_BINARY)
         self.out_bitrate = self.register(srd.OUTPUT_META,
                 meta=(int, 'Bitrate', 'Bitrate during transfers'))
+        self.bw = (self.options['wordsize'] + 7) // 8
 
     def putw(self, data):
         self.put(self.ss_block, self.samplenum, self.out_ann, data)
@@ -165,10 +163,12 @@ class Decoder(srd.Decoder):
 
         if self.have_miso:
             ss, es = self.misobits[-1][1], self.misobits[0][2]
-            self.put(ss, es, self.out_bin, (0, bytes([so])))
+            bdata = so.to_bytes(self.bw, byteorder='big')
+            self.put(ss, es, self.out_binary, [0, bdata])
         if self.have_mosi:
             ss, es = self.mosibits[-1][1], self.mosibits[0][2]
-            self.put(ss, es, self.out_bin, (1, bytes([si])))
+            bdata = si.to_bytes(self.bw, byteorder='big')
+            self.put(ss, es, self.out_binary, [1, bdata])
 
         self.put(ss, es, self.out_python, ['BITS', si_bits, so_bits])
         self.put(ss, es, self.out_python, ['DATA', si, so])
@@ -305,27 +305,31 @@ class Decoder(srd.Decoder):
         # Found the correct clock edge, now get the SPI bit(s).
         self.handle_bit(miso, mosi, clk, cs)
 
-    def decode(self, ss, es, data):
+    def decode(self):
         if not self.samplerate:
             raise SamplerateError('Cannot decode without samplerate.')
-        # Either MISO or MOSI can be omitted (but not both). CS# is optional.
-        for (self.samplenum, pins) in data:
 
+        # Either MISO or MOSI can be omitted (but not both). CS# is optional.
+        self.have_miso = self.has_channel(1)
+        self.have_mosi = self.has_channel(2)
+        self.have_cs = self.has_channel(3)
+        if not self.have_miso and not self.have_mosi:
+            raise ChannelError('Either MISO or MOSI (or both) pins required.')
+
+        # Tell stacked decoders that we don't have a CS# signal.
+        if not self.have_cs:
+            self.put(0, 0, self.out_python, ['CS-CHANGE', None, None])
+
+        # "Pixel compatibility" with the v2 implementation. Grab and
+        # process the very first sample before checking for edges. The
+        # previous implementation did this by seeding old values with None,
+        # which led to an immediate "change" in comparison.
+        pins = self.wait({})
+        (clk, miso, mosi, cs) = pins
+        self.find_clk_edge(miso, mosi, clk, cs)
+
+        while True:
             # Ignore identical samples early on (for performance reasons).
-            if self.oldpins == pins:
-                continue
-            self.oldpins, (clk, miso, mosi, cs) = pins, pins
-            self.have_miso = (miso in (0, 1))
-            self.have_mosi = (mosi in (0, 1))
-            self.have_cs = (cs in (0, 1))
-
-            # Either MISO or MOSI (but not both) can be omitted.
-            if not (self.have_miso or self.have_mosi):
-                raise ChannelError('Either MISO or MOSI (or both) pins required.')
-
-            # Tell stacked decoders that we don't have a CS# signal.
-            if not self.no_cs_notification and not self.have_cs:
-                self.put(0, 0, self.out_python, ['CS-CHANGE', None, None])
-                self.no_cs_notification = True
-
+            pins = self.wait([{0: 'e'}, {1: 'e'}, {2: 'e'}, {3: 'e'}])
+            (clk, miso, mosi, cs) = pins
             self.find_clk_edge(miso, mosi, clk, cs)