]> sigrok.org Git - libsigrokdecode.git/blame - decoders/spi.py
srd: Add initial DCF77 protocol decoder.
[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
677d597b 21import sigrokdecode as srd
67e847fd 22
677d597b 23class Decoder(srd.Decoder):
67e847fd 24 id = 'spi'
2b7d0e2b 25 name = 'SPI'
2b7d0e2b 26 longname = 'Serial Peripheral Interface (SPI) bus'
9a12a6e7 27 desc = '...desc...'
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 38 options = {}
9a12a6e7 39 annotations = []
6eb87578 40
3643fc3f 41 def __init__(self):
c66baa8c 42 self.oldsck = 1
6eb87578
GM
43 self.rxcount = 0
44 self.rxdata = 0
45 self.bytesreceived = 0
46
3643fc3f 47 def start(self, metadata):
56202222
UH
48 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'spi')
49 self.out_ann = self.add(srd.OUTPUT_ANN, 'spi')
3643fc3f 50
6eb87578 51 def report(self):
e100d51e 52 return 'SPI: %d bytes received' % self.bytesreceived
6eb87578 53
2b9837d9 54 def decode(self, ss, es, data):
6b5b91d2
UH
55 # HACK! At the moment the number of probes is not handled correctly.
56 # E.g. if an input file (-i foo.sr) has more than two probes enabled.
57 for (samplenum, (sdata, sck, x, y, z, a)) in data:
6eb87578 58
6eb87578
GM
59 # Sample SDATA on rising SCK
60 if sck == self.oldsck:
61 continue
62 self.oldsck = sck
ad2dc0de
UH
63 if not sck:
64 continue
6eb87578
GM
65
66 # If this is first bit, save timestamp
67 if self.rxcount == 0:
2b9837d9 68 self.time = ss # FIXME
6eb87578 69 # Receive bit into our shift register
6eb87578
GM
70 if sdata:
71 self.rxdata |= 1 << (7 - self.rxcount)
72 self.rxcount += 1
73 # Continue to receive if not a byte yet
74 if self.rxcount != 8:
75 continue
76 # Received a byte, pass up to sigrok
2b9837d9
UH
77 outdata = {'time':self.time, # FIXME
78 'duration':ss + es - self.time, # FIXME
e100d51e
UH
79 'data':self.rxdata,
80 'display':('%02X' % self.rxdata),
81 'type':'spi',
6eb87578 82 }
c9b24fc3
UH
83 # self.put(0, 0, self.out_proto, out_proto)
84 self.put(0, 0, self.out_ann, outdata)
6eb87578
GM
85 # Reset decoder state
86 self.rxdata = 0
87 self.rxcount = 0
88 # Keep stats for summary
89 self.bytesreceived += 1
ad2dc0de 90