]> sigrok.org Git - libsigrokdecode.git/blame - decoders/spi.py
srd: SPI: Handle both directions, output proto data.
[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>
d6bace96 5## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
6eb87578
GM
6##
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation; either version 2 of the License, or
10## (at your option) any later version.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with this program; if not, write to the Free Software
19## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20##
ad2dc0de 21
677d597b 22import sigrokdecode as srd
67e847fd 23
d6bace96
UH
24# Annotation formats
25ANN_HEX = 0
26
677d597b 27class Decoder(srd.Decoder):
67e847fd 28 id = 'spi'
2b7d0e2b 29 name = 'SPI'
2b7d0e2b 30 longname = 'Serial Peripheral Interface (SPI) bus'
9a12a6e7 31 desc = '...desc...'
6eb87578
GM
32 longdesc = '...longdesc...'
33 author = 'Gareth McMullin'
34 email = 'gareth@blacksphere.co.nz'
35 license = 'gplv2+'
36 inputs = ['logic']
37 outputs = ['spi']
6b5b91d2 38 probes = [
de9cee24
UH
39 {'id': 'mosi', 'name': 'MOSI',
40 'desc': 'SPI MOSI line (Master out, slave in)'},
4e570fa9
UH
41 {'id': 'miso', 'name': 'MISO',
42 'desc': 'SPI MISO line (Master in, slave out)'},
6b5b91d2 43 {'id': 'sck', 'name': 'CLK', 'desc': 'SPI clock line'},
4e570fa9 44 {'id': 'cs', 'name': 'CS#', 'desc': 'SPI CS (chip select) line'},
6b5b91d2 45 ]
6eb87578 46 options = {}
b1bb5eed 47 annotations = [
d6bace96 48 ['Hex', 'SPI data bytes in hex format'],
b1bb5eed 49 ]
6eb87578 50
3643fc3f 51 def __init__(self):
c66baa8c 52 self.oldsck = 1
a10bfc48 53 self.bitcount = 0
4917bb31 54 self.mosidata = 0
d6bace96 55 self.misodata = 0
6eb87578 56 self.bytesreceived = 0
d6bace96 57 self.samplenum = -1
6eb87578 58
3643fc3f 59 def start(self, metadata):
d6bace96 60 self.out_proto = self.add(srd.OUTPUT_PROTO, 'spi')
56202222 61 self.out_ann = self.add(srd.OUTPUT_ANN, 'spi')
3643fc3f 62
6eb87578 63 def report(self):
e100d51e 64 return 'SPI: %d bytes received' % self.bytesreceived
6eb87578 65
2b9837d9 66 def decode(self, ss, es, data):
6b5b91d2
UH
67 # HACK! At the moment the number of probes is not handled correctly.
68 # E.g. if an input file (-i foo.sr) has more than two probes enabled.
de9cee24 69 # for (samplenum, (mosi, sck, x, y, z, a)) in data:
b1bb5eed 70 # for (samplenum, (cs, miso, sck, mosi, wp, hold)) in data:
de9cee24 71 for (samplenum, (cs, miso, sck, mosi, wp, hold)) in data:
6eb87578 72
d6bace96
UH
73 self.samplenum += 1 # FIXME
74
de9cee24 75 # Sample data on rising SCK edges.
6eb87578
GM
76 if sck == self.oldsck:
77 continue
78 self.oldsck = sck
b1bb5eed 79 if sck == 0:
ad2dc0de 80 continue
6eb87578 81
d6bace96 82 # If this is the first bit, save its sample number.
a10bfc48 83 if self.bitcount == 0:
d6bace96 84 self.start_sample = samplenum
b1bb5eed
UH
85
86 # Receive bit into our shift register.
de9cee24 87 if mosi == 1:
4917bb31 88 self.mosidata |= 1 << (7 - self.bitcount)
d6bace96
UH
89 if miso == 1:
90 self.misodata |= 1 << (7 - self.bitcount)
b1bb5eed 91
a10bfc48 92 self.bitcount += 1
b1bb5eed
UH
93
94 # Continue to receive if not a byte yet.
a10bfc48 95 if self.bitcount != 8:
6eb87578 96 continue
b1bb5eed 97
d6bace96
UH
98 self.put(self.start_sample, self.samplenum, self.out_proto,
99 ['data', self.mosidata, self.misodata])
100 self.put(self.start_sample, self.samplenum, self.out_ann,
101 [ANN_HEX, ['MOSI: 0x%02x, MISO: 0x%02x' % (self.mosidata,
102 self.misodata)]])
b1bb5eed
UH
103
104 # Reset decoder state.
4917bb31 105 self.mosidata = 0
d6bace96 106 self.misodata = 0
a10bfc48 107 self.bitcount = 0
b1bb5eed
UH
108
109 # Keep stats for summary.
6eb87578 110 self.bytesreceived += 1
ad2dc0de 111