2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 import sigrokdecode as srd
30 - 'ITEM', [<item>, <itembitsize>]
31 - 'WORD', [<word>, <wordbitsize>, <worditemcount>]
34 - A single item (a number). It can be of arbitrary size. The max. number
35 of bits in this item is specified in <itembitsize>.
38 - The size of an item (in bits). For a 4-bit parallel bus this is 4,
39 for a 16-bit parallel bus this is 16, and so on.
42 - A single word (a number). It can be of arbitrary size. The max. number
43 of bits in this word is specified in <wordbitsize>. The (exact) number
44 of items in this word is specified in <worditemcount>.
47 - The size of a word (in bits). For a 2-item word with 8-bit items
48 <wordbitsize> is 16, for a 3-item word with 4-bit items <wordbitsize>
52 - The size of a word (in number of items). For a 4-item word (no matter
53 how many bits each item consists of) <worditemcount> is 4, for a 7-item
54 word <worditemcount> is 7, and so on.
57 def channel_list(num_channels):
58 l = [{'id': 'clk', 'name': 'CLK', 'desc': 'Clock line'}]
59 for i in range(num_channels):
60 d = {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data line %d' % i}
64 class ChannelError(Exception):
67 class Decoder(srd.Decoder):
71 longname = 'Parallel sync bus'
72 desc = 'Generic parallel synchronous bus.'
75 outputs = ['parallel']
76 optional_channels = channel_list(8)
78 {'id': 'clock_edge', 'desc': 'Clock edge to sample on',
79 'default': 'rising', 'values': ('rising', 'falling')},
80 {'id': 'wordsize', 'desc': 'Data wordsize', 'default': 1},
81 {'id': 'endianness', 'desc': 'Data endianness',
82 'default': 'little', 'values': ('little', 'big')},
93 self.saved_item = None
96 self.ss_item = self.es_item = None
100 self.out_python = self.register(srd.OUTPUT_PYTHON)
101 self.out_ann = self.register(srd.OUTPUT_ANN)
103 def putpb(self, data):
104 self.put(self.ss_item, self.es_item, self.out_python, data)
106 def putb(self, data):
107 self.put(self.ss_item, self.es_item, self.out_ann, data)
109 def putpw(self, data):
110 self.put(self.ss_word, self.es_word, self.out_python, data)
112 def putw(self, data):
113 self.put(self.ss_word, self.es_word, self.out_ann, data)
115 def handle_bits(self, datapins):
116 # If this is the first item in a word, save its sample number.
117 if self.itemcount == 0:
118 self.ss_word = self.samplenum
120 # Get the bits for this item.
121 item, used_pins = 0, datapins.count(b'\x01') + datapins.count(b'\x00')
122 for i in range(used_pins):
123 item |= datapins[i] << i
125 self.items.append(item)
129 # Save the start sample and item for later (no output yet).
130 self.ss_item = self.samplenum
132 self.saved_item = item
134 # Output the saved item (from the last CLK edge to the current).
135 self.es_item = self.samplenum
136 self.putpb(['ITEM', self.saved_item])
137 self.putb([0, ['%X' % self.saved_item]])
138 self.ss_item = self.samplenum
139 self.saved_item = item
141 endian, ws = self.options['endianness'], self.options['wordsize']
143 # Get as many items as the configured wordsize says.
144 if self.itemcount < ws:
147 # Output annotations/python for a word (a collection of items).
150 if endian == 'little':
151 word |= self.items[i] << ((ws - 1 - i) * used_pins)
152 elif endian == 'big':
153 word |= self.items[i] << (i * used_pins)
155 self.es_word = self.samplenum
156 # self.putpw(['WORD', word])
157 # self.putw([1, ['%X' % word]])
158 self.ss_word = self.samplenum
160 self.itemcount, self.items = 0, []
162 def find_clk_edge(self, clk, datapins):
163 # Ignore sample if the clock pin hasn't changed.
164 if clk == self.oldclk:
168 # Sample data on rising/falling clock edge (depends on config).
169 c = self.options['clock_edge']
170 if c == 'rising' and clk == 0: # Sample on rising clock edge.
172 elif c == 'falling' and clk == 1: # Sample on falling clock edge.
175 # Found the correct clock edge, now get the bits.
176 self.handle_bits(datapins)
178 def decode(self, ss, es, data):
179 for (self.samplenum, pins) in data:
181 # Ignore identical samples early on (for performance reasons).
182 if self.oldpins == pins:
186 if sum(1 for p in pins if p in (0, 1)) == 0:
187 raise ChannelError('At least one channel has to be supplied.')
189 if pins[0] not in (0, 1):
190 self.handle_bits(pins[1:])
192 self.find_clk_edge(pins[0], pins[1:])