]>
Commit | Line | Data |
---|---|---|
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 | desc = '...desc...' | |
27 | longname = 'Serial Peripheral Interface (SPI) bus' | |
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': 'sdata', 'name': 'DATA', 'desc': 'SPI data line (MISO or MOSI)'}, | |
36 | {'id': 'sck', 'name': 'CLK', 'desc': 'SPI clock line'}, | |
37 | ] | |
38 | options = {} | |
39 | ||
40 | def __init__(self): | |
41 | self.oldsck = 1 | |
42 | self.rxcount = 0 | |
43 | self.rxdata = 0 | |
44 | self.bytesreceived = 0 | |
45 | ||
46 | def start(self, metadata): | |
47 | # self.out_proto = self.add(srd.OUTPUT_PROTO, 'spi') | |
48 | self.out_ann = self.add(srd.OUTPUT_ANN, 'spi') | |
49 | ||
50 | def report(self): | |
51 | return 'SPI: %d bytes received' % self.bytesreceived | |
52 | ||
53 | def decode(self, timeoffset, duration, data): | |
54 | # HACK! At the moment the number of probes is not handled correctly. | |
55 | # E.g. if an input file (-i foo.sr) has more than two probes enabled. | |
56 | for (samplenum, (sdata, sck, x, y, z, a)) in data: | |
57 | ||
58 | # Sample SDATA on rising SCK | |
59 | if sck == self.oldsck: | |
60 | continue | |
61 | self.oldsck = sck | |
62 | if not sck: | |
63 | continue | |
64 | ||
65 | # If this is first bit, save timestamp | |
66 | if self.rxcount == 0: | |
67 | self.time = timeoffset # FIXME | |
68 | # Receive bit into our shift register | |
69 | if sdata: | |
70 | self.rxdata |= 1 << (7 - self.rxcount) | |
71 | self.rxcount += 1 | |
72 | # Continue to receive if not a byte yet | |
73 | if self.rxcount != 8: | |
74 | continue | |
75 | # Received a byte, pass up to sigrok | |
76 | outdata = {'time':self.time, | |
77 | 'duration':timeoffset + duration - self.time, | |
78 | 'data':self.rxdata, | |
79 | 'display':('%02X' % self.rxdata), | |
80 | 'type':'spi', | |
81 | } | |
82 | # self.put(0, 0, self.out_proto, out_proto) | |
83 | self.put(0, 0, self.out_ann, outdata) | |
84 | # Reset decoder state | |
85 | self.rxdata = 0 | |
86 | self.rxcount = 0 | |
87 | # Keep stats for summary | |
88 | self.bytesreceived += 1 | |
89 |