]> sigrok.org Git - libsigrokdecode.git/blob - decoders/spi.py
srd: PDs now explicitly register with sigrok module.
[libsigrokdecode.git] / decoders / spi.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
5 ##
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.
10 ##
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.
15 ##
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
19 ##
20
21 import sigrok
22
23 class Sample():
24     def __init__(self, data):
25         self.data = data
26     def probe(self, probe):
27         s = ord(self.data[probe / 8]) & (1 << (probe % 8))
28         return True if s else False
29
30 def sampleiter(data, unitsize):
31     for i in range(0, len(data), unitsize):
32         yield(Sample(data[i:i+unitsize]))
33
34 class Decoder():
35     id = 'spi'
36     name = 'SPI Decoder'
37     desc = '...desc...'
38     longname = '...longname...'
39     longdesc = '...longdesc...'
40     author = 'Gareth McMullin'
41     email = 'gareth@blacksphere.co.nz'
42     license = 'gplv2+'
43     inputs = ['logic']
44     outputs = ['spi']
45     # Probe names with a set of defaults
46     probes = {'sdata':0, 'sck':1}
47     options = {}
48
49     def __init__(self):
50         self.probes = Decoder.probes.copy()
51         self.oldsck = True
52         self.rxcount = 0
53         self.rxdata = 0
54         self.bytesreceived = 0
55
56     def start(self, metadata):
57         self.unitsize = metadata['unitsize']
58
59     def report(self):
60         return 'SPI: %d bytes received' % self.bytesreceived
61
62     def decode(self, data):
63         # We should accept a list of samples and iterate...
64         for sample in sampleiter(data['data'], self.unitsize):
65
66             sck = sample.probe(self.probes['sck'])
67             # Sample SDATA on rising SCK
68             if sck == self.oldsck:
69                 continue
70             self.oldsck = sck
71             if not sck:
72                 continue
73
74             # If this is first bit, save timestamp
75             if self.rxcount == 0:
76                 self.time = data['time']
77             # Receive bit into our shift register
78             sdata = sample.probe(self.probes['sdata'])
79             if sdata:
80                 self.rxdata |= 1 << (7 - self.rxcount)
81             self.rxcount += 1
82             # Continue to receive if not a byte yet
83             if self.rxcount != 8:
84                 continue
85             # Received a byte, pass up to sigrok
86             outdata = {'time':self.time,
87                 'duration':data['time'] + data['duration'] - self.time,
88                 'data':self.rxdata,
89                 'display':('%02X' % self.rxdata),
90                 'type':'spi',
91             }
92             sigrok.put(outdata)
93             # Reset decoder state
94             self.rxdata = 0
95             self.rxcount = 0
96             # Keep stats for summary
97             self.bytesreceived += 1
98
99 sigrok.register(Decoder)
100