]> sigrok.org Git - libsigrokdecode.git/blame - decoders/spi.py
srd: Add Panasonic PAN1321 decoder (on top of UART).
[libsigrokdecode.git] / decoders / spi.py
CommitLineData
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
6b5b91d2 21import sigrokdecode
67e847fd 22
6b5b91d2 23class Decoder(sigrokdecode.Decoder):
67e847fd 24 id = 'spi'
2b7d0e2b 25 name = 'SPI'
6eb87578 26 desc = '...desc...'
2b7d0e2b 27 longname = 'Serial Peripheral Interface (SPI) bus'
6eb87578
GM
28 longdesc = '...longdesc...'
29 author = 'Gareth McMullin'
30 email = 'gareth@blacksphere.co.nz'
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = ['spi']
6b5b91d2
UH
34 probes = [
35 {'id': 'sdata', 'name': 'DATA', 'desc': 'SPI data line (MISO or MOSI)'},
36 {'id': 'sck', 'name': 'CLK', 'desc': 'SPI clock line'},
37 ]
6eb87578
GM
38 options = {}
39
3643fc3f 40 def __init__(self):
c66baa8c 41 self.oldsck = 1
6eb87578
GM
42 self.rxcount = 0
43 self.rxdata = 0
44 self.bytesreceived = 0
c9b24fc3
UH
45 self.out_proto = None
46 self.out_ann = None
6eb87578 47
3643fc3f 48 def start(self, metadata):
7cf79452
UH
49 # self.out_proto = self.add(sigrokdecode.SRD_OUTPUT_PROTOCOL, 'spi')
50 self.out_ann = self.add(sigrokdecode.SRD_OUTPUT_ANNOTATION, 'spi')
3643fc3f 51
6eb87578 52 def report(self):
e100d51e 53 return 'SPI: %d bytes received' % self.bytesreceived
6eb87578 54
ffe880e7 55 def decode(self, timeoffset, duration, data):
6b5b91d2
UH
56 # HACK! At the moment the number of probes is not handled correctly.
57 # E.g. if an input file (-i foo.sr) has more than two probes enabled.
58 for (samplenum, (sdata, sck, x, y, z, a)) in data:
6eb87578 59
6eb87578
GM
60 # Sample SDATA on rising SCK
61 if sck == self.oldsck:
62 continue
63 self.oldsck = sck
ad2dc0de
UH
64 if not sck:
65 continue
6eb87578
GM
66
67 # If this is first bit, save timestamp
68 if self.rxcount == 0:
ffe880e7 69 self.time = timeoffset # FIXME
6eb87578 70 # Receive bit into our shift register
6eb87578
GM
71 if sdata:
72 self.rxdata |= 1 << (7 - self.rxcount)
73 self.rxcount += 1
74 # Continue to receive if not a byte yet
75 if self.rxcount != 8:
76 continue
77 # Received a byte, pass up to sigrok
e100d51e 78 outdata = {'time':self.time,
ffe880e7 79 'duration':timeoffset + duration - self.time,
e100d51e
UH
80 'data':self.rxdata,
81 'display':('%02X' % self.rxdata),
82 'type':'spi',
6eb87578 83 }
c9b24fc3
UH
84 # self.put(0, 0, self.out_proto, out_proto)
85 self.put(0, 0, self.out_ann, outdata)
6eb87578
GM
86 # Reset decoder state
87 self.rxdata = 0
88 self.rxcount = 0
89 # Keep stats for summary
90 self.bytesreceived += 1
ad2dc0de 91