X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fspi%2Fspi.py;h=bbf0f3705af62f9762794d3e19cf064ccd84f793;hp=57f55624ffe4c5ed5d27de921c6ada1d6ffc74d0;hb=94bbdb9a4146ec5eaa56411706dc92de3a92f2d8;hpb=8a7ce2a3f77bae0a4a6aa08751bdd9a963168192 diff --git a/decoders/spi/spi.py b/decoders/spi/spi.py index 57f5562..bbf0f37 100644 --- a/decoders/spi/spi.py +++ b/decoders/spi/spi.py @@ -21,23 +21,9 @@ import sigrokdecode as srd -# Chip-select options -ACTIVE_LOW = 0 -ACTIVE_HIGH = 1 - -# Clock polarity options -CPOL_0 = 0 # Clock is low when inactive -CPOL_1 = 1 # Clock is high when inactive - -# Clock phase options -CPHA_0 = 0 # Data is valid on the leading clock edge -CPHA_1 = 1 # Data is valid on the trailing clock edge - -# Bit order options -MSB_FIRST = 0 -LSB_FIRST = 1 - # Key: (CPOL, CPHA). Value: SPI mode. +# Clock polarity (CPOL) = 0/1: Clock is low/high when inactive. +# Clock phase (CPHA) = 0/1: Data is valid on the leading/trailing clock edge. spi_mode = { (0, 0): 0, # Mode 0 (0, 1): 1, # Mode 1 @@ -59,18 +45,19 @@ class Decoder(srd.Decoder): inputs = ['logic'] outputs = ['spi'] probes = [ - {'id': 'mosi', 'name': 'MOSI', - 'desc': 'SPI MOSI line (Master out, slave in)'}, {'id': 'miso', 'name': 'MISO', 'desc': 'SPI MISO line (Master in, slave out)'}, + {'id': 'mosi', 'name': 'MOSI', + 'desc': 'SPI MOSI line (Master out, slave in)'}, {'id': 'sck', 'name': 'CLK', 'desc': 'SPI clock line'}, {'id': 'cs', 'name': 'CS#', 'desc': 'SPI CS (chip select) line'}, ] + optional_probes = [] # TODO options = { - 'cs_polarity': ['CS# polarity', ACTIVE_LOW], - 'cpol': ['Clock polarity', CPOL_0], - 'cpha': ['Clock phase', CPHA_0], - 'bitorder': ['Bit order within the SPI data', MSB_FIRST], + 'cs_polarity': ['CS# polarity', 'active-low'], + 'cpol': ['Clock polarity', 0], + 'cpha': ['Clock phase', 0], + 'bitorder': ['Bit order within the SPI data', 'msb-first'], 'wordsize': ['Word size of SPI data', 8], # 1-64? } annotations = [ @@ -94,11 +81,8 @@ class Decoder(srd.Decoder): return 'SPI: %d bytes received' % self.bytesreceived def decode(self, ss, es, data): - # HACK! At the moment the number of probes is not handled correctly. - # E.g. if an input file (-i foo.sr) has more than two probes enabled. - # for (samplenum, (mosi, sck, x, y, z, a)) in data: - # for (samplenum, (cs, miso, sck, mosi, wp, hold)) in data: - for (samplenum, (cs, miso, sck, mosi, wp, hold)) in data: + # TODO: Either MISO or MOSI could be optional. CS# is optional. + for (samplenum, (miso, mosi, sck, cs)) in data: self.samplenum += 1 # FIXME @@ -122,27 +106,29 @@ class Decoder(srd.Decoder): # If this is the first bit, save its sample number. if self.bitcount == 0: self.start_sample = samplenum - active_low = (self.options['cs_polarity'] == ACTIVE_LOW) + active_low = (self.options['cs_polarity'] == 'active-low') deasserted = cs if active_low else not cs if deasserted: self.cs_was_deasserted_during_data_word = 1 + ws = self.options['wordsize'] + # Receive MOSI bit into our shift register. - if self.options['bitorder'] == MSB_FIRST: - self.mosidata |= mosi << (self.options['wordsize'] - 1 - self.bitcount) + if self.options['bitorder'] == 'msb-first': + self.mosidata |= mosi << (ws - 1 - self.bitcount) else: self.mosidata |= mosi << self.bitcount # Receive MISO bit into our shift register. - if self.options['bitorder'] == MSB_FIRST: - self.misodata |= miso << (self.options['wordsize'] - 1 - self.bitcount) + if self.options['bitorder'] == 'msb-first': + self.misodata |= miso << (ws - 1 - self.bitcount) else: self.misodata |= miso << self.bitcount self.bitcount += 1 - # Continue to receive if not a byte yet. - if self.bitcount != self.options['wordsize']: + # Continue to receive if not enough bits were received, yet. + if self.bitcount != ws: continue self.put(self.start_sample, self.samplenum, self.out_proto,