]>
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 | |
3b24e378 | 21 | class Sample(): |
6eb87578 GM |
22 | def __init__(self, data): |
23 | self.data = data | |
24 | def probe(self, probe): | |
25 | s = ord(self.data[probe / 8]) & (1 << (probe % 8)) | |
26 | return True if s else False | |
3b24e378 KS |
27 | |
28 | def sampleiter(data, unitsize): | |
6eb87578 GM |
29 | for i in range(0, len(data), unitsize): |
30 | yield(Sample(data[i:i+unitsize])) | |
3b24e378 KS |
31 | |
32 | class Decoder(): | |
6eb87578 GM |
33 | name = 'SPI Decoder' |
34 | desc = '...desc...' | |
35 | longname = '...longname...' | |
36 | longdesc = '...longdesc...' | |
37 | author = 'Gareth McMullin' | |
38 | email = 'gareth@blacksphere.co.nz' | |
39 | license = 'gplv2+' | |
40 | inputs = ['logic'] | |
41 | outputs = ['spi'] | |
42 | # Probe names with a set of defaults | |
43 | probes = {'sdata':0, 'sck':1} | |
44 | options = {} | |
45 | ||
3643fc3f | 46 | def __init__(self): |
f97f531c | 47 | self.probes = Decoder.probes.copy() |
6eb87578 GM |
48 | self.oldsck = True |
49 | self.rxcount = 0 | |
50 | self.rxdata = 0 | |
51 | self.bytesreceived = 0 | |
52 | ||
3643fc3f | 53 | def start(self, metadata): |
e100d51e | 54 | self.unitsize = metadata['unitsize'] |
3643fc3f | 55 | |
6eb87578 | 56 | def report(self): |
e100d51e | 57 | return 'SPI: %d bytes received' % self.bytesreceived |
6eb87578 GM |
58 | |
59 | def decode(self, data): | |
60 | # We should accept a list of samples and iterate... | |
e100d51e | 61 | for sample in sampleiter(data['data'], self.unitsize): |
6eb87578 | 62 | |
e100d51e | 63 | sck = sample.probe(self.probes['sck']) |
6eb87578 GM |
64 | # Sample SDATA on rising SCK |
65 | if sck == self.oldsck: | |
66 | continue | |
67 | self.oldsck = sck | |
ad2dc0de UH |
68 | if not sck: |
69 | continue | |
6eb87578 GM |
70 | |
71 | # If this is first bit, save timestamp | |
72 | if self.rxcount == 0: | |
e100d51e | 73 | self.time = data['time'] |
6eb87578 | 74 | # Receive bit into our shift register |
e100d51e | 75 | sdata = sample.probe(self.probes['sdata']) |
6eb87578 GM |
76 | if sdata: |
77 | self.rxdata |= 1 << (7 - self.rxcount) | |
78 | self.rxcount += 1 | |
79 | # Continue to receive if not a byte yet | |
80 | if self.rxcount != 8: | |
81 | continue | |
82 | # Received a byte, pass up to sigrok | |
e100d51e UH |
83 | outdata = {'time':self.time, |
84 | 'duration':data['time'] + data['duration'] - self.time, | |
85 | 'data':self.rxdata, | |
86 | 'display':('%02X' % self.rxdata), | |
87 | 'type':'spi', | |
6eb87578 | 88 | } |
6eb87578 GM |
89 | sigrok.put(outdata) |
90 | # Reset decoder state | |
91 | self.rxdata = 0 | |
92 | self.rxcount = 0 | |
93 | # Keep stats for summary | |
94 | self.bytesreceived += 1 | |
ad2dc0de | 95 | |
e100d51e UH |
96 | if __name__ == '__main__': |
97 | data = open('spi_dump.bin').read() | |
3b24e378 | 98 | |
6eb87578 GM |
99 | # dummy class to keep Decoder happy for test |
100 | class Sigrok(): | |
101 | def put(self, data): | |
102 | print "\t", data | |
103 | sigrok = Sigrok() | |
3b24e378 | 104 | |
6eb87578 | 105 | dec = Decoder(driver='ols', unitsize=1, starttime=0) |
e100d51e | 106 | dec.decode({'time':0, 'duration':len(data), 'data':data, 'type':'logic'}) |
3b24e378 | 107 | |
6eb87578 | 108 | print dec.summary() |
3b24e378 | 109 | else: |
6eb87578 | 110 | import sigrok |
3b24e378 KS |
111 | |
112 | #Tested with: | |
113 | # sigrok-cli -d 0:samplerate=1000000:rle=on --time=1s -p 1,2 -a spidec | |
114 |