- def decode(self, ss, es, data):
- if self.samplerate is None:
- raise Exception("Cannot decode without samplerate.")
- # Either MISO or MOSI can be omitted (but not both). CS# is optional.
- for (self.samplenum, pins) in data:
-
- # 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 Exception('Either MISO or MOSI (or both) pins required.')
-
- # State machine.
- if self.state == 'IDLE':
- self.find_clk_edge(miso, mosi, clk, cs)
- else:
- raise Exception('Invalid state: %s' % self.state)
-
+ def decode(self):
+ # The CLK input is mandatory. Other signals are (individually)
+ # optional. Yet either MISO or MOSI (or both) must be provided.
+ # Tell stacked decoders when we don't have a CS# signal.
+ if not self.has_channel(0):
+ raise ChannelError('Either MISO or MOSI (or both) pins required.')
+ self.have_miso = self.has_channel(1)
+ self.have_mosi = self.has_channel(2)
+ if not self.have_miso and not self.have_mosi:
+ raise ChannelError('Either MISO or MOSI (or both) pins required.')
+ self.have_cs = self.has_channel(3)
+ if not self.have_cs:
+ self.put(0, 0, self.out_python, ['CS-CHANGE', None, None])
+
+ # We want all CLK changes. We want all CS changes if CS is used.
+ # Map 'have_cs' from boolean to an integer index. This simplifies
+ # evaluation in other locations.
+ wait_cond = [{0: 'e'}]
+ if self.have_cs:
+ self.have_cs = len(wait_cond)
+ wait_cond.append({3: 'e'})
+
+ # "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.
+ (clk, miso, mosi, cs) = self.wait({})
+ self.find_clk_edge(miso, mosi, clk, cs, True)
+
+ while True:
+ (clk, miso, mosi, cs) = self.wait(wait_cond)
+ self.find_clk_edge(miso, mosi, clk, cs, False)