]> sigrok.org Git - libsigrokdecode.git/blob - decoders/spi.py
srd: SPI: s/sdata/mosi/.
[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 sigrokdecode as srd
22
23 class Decoder(srd.Decoder):
24     id = 'spi'
25     name = 'SPI'
26     longname = 'Serial Peripheral Interface (SPI) bus'
27     desc = '...desc...'
28     longdesc = '...longdesc...'
29     author = 'Gareth McMullin'
30     email = 'gareth@blacksphere.co.nz'
31     license = 'gplv2+'
32     inputs = ['logic']
33     outputs = ['spi']
34     probes = [
35         {'id': 'mosi', 'name': 'MOSI',
36          'desc': 'SPI MOSI line (Master out, slave in)'},
37         {'id': 'sck', 'name': 'CLK', 'desc': 'SPI clock line'},
38     ]
39     options = {}
40     annotations = [
41         ['TODO', 'TODO'],
42     ]
43
44     def __init__(self):
45         self.oldsck = 1
46         self.bitcount = 0
47         self.mosidata = 0
48         self.bytesreceived = 0
49
50     def start(self, metadata):
51         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'spi')
52         self.out_ann = self.add(srd.OUTPUT_ANN, 'spi')
53
54     def report(self):
55         return 'SPI: %d bytes received' % self.bytesreceived
56
57     def decode(self, ss, es, data):
58         # HACK! At the moment the number of probes is not handled correctly.
59         # E.g. if an input file (-i foo.sr) has more than two probes enabled.
60         # for (samplenum, (mosi, sck, x, y, z, a)) in data:
61         # for (samplenum, (cs, miso, sck, mosi, wp, hold)) in data:
62         for (samplenum, (cs, miso, sck, mosi, wp, hold)) in data:
63
64             # Sample data on rising SCK edges.
65             if sck == self.oldsck:
66                 continue
67             self.oldsck = sck
68             if sck == 0:
69                 continue
70
71             # If this is the first bit, save timestamp.
72             if self.bitcount == 0:
73                 self.time = samplenum
74
75             # Receive bit into our shift register.
76             if mosi == 1:
77                 self.mosidata |= 1 << (7 - self.bitcount)
78
79             self.bitcount += 1
80
81             # Continue to receive if not a byte yet.
82             if self.bitcount != 8:
83                 continue
84
85             # self.put(0, 0, self.out_proto, out_proto) # TODO
86             self.put(0, 0, self.out_ann, [0, ['0x%02x' % self.mosidata]])
87
88             # Reset decoder state.
89             self.mosidata = 0
90             self.bitcount = 0
91
92             # Keep stats for summary.
93             self.bytesreceived += 1
94