]>
Commit | Line | Data |
---|---|---|
6eb87578 GM |
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 | ## | |
ad2dc0de | 20 | |
67e847fd GM |
21 | import sigrok |
22 | ||
3b24e378 | 23 | class Sample(): |
6eb87578 GM |
24 | def __init__(self, data): |
25 | self.data = data | |
26 | def probe(self, probe): | |
fb53ee5e | 27 | s = self.data[int(probe / 8)] & (1 << (probe % 8)) |
6eb87578 | 28 | return True if s else False |
3b24e378 KS |
29 | |
30 | def sampleiter(data, unitsize): | |
6eb87578 GM |
31 | for i in range(0, len(data), unitsize): |
32 | yield(Sample(data[i:i+unitsize])) | |
3b24e378 | 33 | |
1c8ac5bf | 34 | class Decoder(sigrok.Decoder): |
67e847fd | 35 | id = 'spi' |
2b7d0e2b | 36 | name = 'SPI' |
6eb87578 | 37 | desc = '...desc...' |
2b7d0e2b | 38 | longname = 'Serial Peripheral Interface (SPI) bus' |
6eb87578 GM |
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 | ||
3643fc3f | 49 | def __init__(self): |
f97f531c | 50 | self.probes = Decoder.probes.copy() |
6eb87578 GM |
51 | self.oldsck = True |
52 | self.rxcount = 0 | |
53 | self.rxdata = 0 | |
54 | self.bytesreceived = 0 | |
e5080882 BV |
55 | self.output_protocol = None |
56 | self.output_annotation = None | |
6eb87578 | 57 | |
3643fc3f | 58 | def start(self, metadata): |
e100d51e | 59 | self.unitsize = metadata['unitsize'] |
ffe880e7 | 60 | # self.output_protocol = self.output_new(2) |
e5080882 | 61 | self.output_annotation = self.output_new(1) |
3643fc3f | 62 | |
6eb87578 | 63 | def report(self): |
e100d51e | 64 | return 'SPI: %d bytes received' % self.bytesreceived |
6eb87578 | 65 | |
ffe880e7 | 66 | def decode(self, timeoffset, duration, data): |
6eb87578 | 67 | # We should accept a list of samples and iterate... |
ffe880e7 | 68 | for sample in sampleiter(data, self.unitsize): |
6eb87578 | 69 | |
e100d51e | 70 | sck = sample.probe(self.probes['sck']) |
6eb87578 GM |
71 | # Sample SDATA on rising SCK |
72 | if sck == self.oldsck: | |
73 | continue | |
74 | self.oldsck = sck | |
ad2dc0de UH |
75 | if not sck: |
76 | continue | |
6eb87578 GM |
77 | |
78 | # If this is first bit, save timestamp | |
79 | if self.rxcount == 0: | |
ffe880e7 | 80 | self.time = timeoffset # FIXME |
6eb87578 | 81 | # Receive bit into our shift register |
e100d51e | 82 | sdata = sample.probe(self.probes['sdata']) |
6eb87578 GM |
83 | if sdata: |
84 | self.rxdata |= 1 << (7 - self.rxcount) | |
85 | self.rxcount += 1 | |
86 | # Continue to receive if not a byte yet | |
87 | if self.rxcount != 8: | |
88 | continue | |
89 | # Received a byte, pass up to sigrok | |
e100d51e | 90 | outdata = {'time':self.time, |
ffe880e7 | 91 | 'duration':timeoffset + duration - self.time, |
e100d51e UH |
92 | 'data':self.rxdata, |
93 | 'display':('%02X' % self.rxdata), | |
94 | 'type':'spi', | |
6eb87578 | 95 | } |
ffe880e7 UH |
96 | # self.put(self.output_protocol, 0, 0, out_proto) |
97 | self.put(self.output_annotation, 0, 0, outdata) | |
6eb87578 GM |
98 | # Reset decoder state |
99 | self.rxdata = 0 | |
100 | self.rxcount = 0 | |
101 | # Keep stats for summary | |
102 | self.bytesreceived += 1 | |
ad2dc0de | 103 |