2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 def __init__(self, data):
24 def probe(self, probe):
25 s = ord(self.data[probe / 8]) & (1 << (probe % 8))
26 return True if s else False
28 def sampleiter(data, unitsize):
29 for i in range(0, len(data), unitsize):
30 yield(Sample(data[i:i+unitsize]))
35 longname = '...longname...'
36 longdesc = '...longdesc...'
37 author = 'Gareth McMullin'
38 email = 'gareth@blacksphere.co.nz'
42 # Probe names with a set of defaults
43 probes = {'sdata':0, 'sck':1}
46 def __init__(self, unitsize, **kwargs):
47 # Metadata comes in here, we don't care for now
49 self.unitsize = unitsize
51 self.probes = Decoder.probes.copy()
55 self.bytesreceived = 0
58 return "SPI: %d bytes received" % self.bytesreceived
60 def decode(self, data):
61 # We should accept a list of samples and iterate...
62 for sample in sampleiter(data["data"], self.unitsize):
64 sck = sample.probe(self.probes["sck"])
65 # Sample SDATA on rising SCK
66 if sck == self.oldsck:
72 # If this is first bit, save timestamp
74 self.time = data["time"]
75 # Receive bit into our shift register
76 sdata = sample.probe(self.probes["sdata"])
78 self.rxdata |= 1 << (7 - self.rxcount)
80 # Continue to receive if not a byte yet
83 # Received a byte, pass up to sigrok
84 outdata = {"time":self.time,
85 "duration":data["time"] + data["duration"] - self.time,
87 "display":("%02X" % self.rxdata),
94 # Keep stats for summary
95 self.bytesreceived += 1
97 if __name__ == "__main__":
98 data = open("spi_dump.bin").read()
100 # dummy class to keep Decoder happy for test
106 dec = Decoder(driver='ols', unitsize=1, starttime=0)
107 dec.decode({"time":0, "duration":len(data), "data":data, "type":"logic"})
114 # sigrok-cli -d 0:samplerate=1000000:rle=on --time=1s -p 1,2 -a spidec