2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
5 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 import sigrokdecode as srd
28 # Clock polarity options
29 CPOL_0 = 0 # Clock is low when inactive
30 CPOL_1 = 1 # Clock is high when inactive
33 CPHA_0 = 0 # Data is valid on the rising clock edge
34 CPHA_1 = 1 # Data is valid on the falling clock edge
43 class Decoder(srd.Decoder):
46 longname = 'Serial Peripheral Interface (SPI) bus'
48 longdesc = '...longdesc...'
49 author = 'Gareth McMullin'
50 email = 'gareth@blacksphere.co.nz'
55 {'id': 'mosi', 'name': 'MOSI',
56 'desc': 'SPI MOSI line (Master out, slave in)'},
57 {'id': 'miso', 'name': 'MISO',
58 'desc': 'SPI MISO line (Master in, slave out)'},
59 {'id': 'sck', 'name': 'CLK', 'desc': 'SPI clock line'},
60 {'id': 'cs', 'name': 'CS#', 'desc': 'SPI CS (chip select) line'},
63 'cs_active_low': ['CS# active low', ACTIVE_LOW],
64 'clock_polarity': ['Clock polarity', CPOL_0],
65 'clock_phase': ['Clock phase', CPHA_0],
66 'bit_order': ['Bit order within the SPI data', MSB_FIRST],
67 'word_size': ['Word size of SPI data', 8], # 1-64?
70 ['Hex', 'SPI data bytes in hex format'],
78 self.bytesreceived = 0
81 # Set protocol decoder option defaults.
82 self.cs_active_low = Decoder.options['cs_active_low'][1]
83 self.clock_polarity = Decoder.options['clock_polarity'][1]
84 self.clock_phase = Decoder.options['clock_phase'][1]
85 self.bit_order = Decoder.options['bit_order'][1]
86 self.word_size = Decoder.options['word_size'][1]
88 def start(self, metadata):
89 self.out_proto = self.add(srd.OUTPUT_PROTO, 'spi')
90 self.out_ann = self.add(srd.OUTPUT_ANN, 'spi')
93 return 'SPI: %d bytes received' % self.bytesreceived
95 def decode(self, ss, es, data):
96 # HACK! At the moment the number of probes is not handled correctly.
97 # E.g. if an input file (-i foo.sr) has more than two probes enabled.
98 # for (samplenum, (mosi, sck, x, y, z, a)) in data:
99 # for (samplenum, (cs, miso, sck, mosi, wp, hold)) in data:
100 for (samplenum, (cs, miso, sck, mosi, wp, hold)) in data:
102 self.samplenum += 1 # FIXME
104 # Sample data on rising SCK edges.
105 if sck == self.oldsck:
111 # If this is the first bit, save its sample number.
112 if self.bitcount == 0:
113 self.start_sample = samplenum
115 # Receive MOSI bit into our shift register.
116 if self.bit_order == MSB_FIRST:
117 self.mosidata |= mosi << (self.wordsize - 1 - self.bitcount)
119 self.mosidata |= mosi << self.bitcount
121 # Receive MISO bit into our shift register.
122 if self.bit_order == MSB_FIRST:
123 self.misodata |= miso << (self.wordsize - 1 - self.bitcount)
125 self.misodata |= miso << self.bitcount
129 # Continue to receive if not a byte yet.
130 if self.bitcount != self.wordsize:
133 self.put(self.start_sample, self.samplenum, self.out_proto,
134 ['data', self.mosidata, self.misodata])
135 self.put(self.start_sample, self.samplenum, self.out_ann,
136 [ANN_HEX, ['MOSI: 0x%02x, MISO: 0x%02x' % (self.mosidata,
139 # Reset decoder state.
144 # Keep stats for summary.
145 self.bytesreceived += 1