2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2013-2016 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, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
29 - 'ITEM', [<item>, <itembitsize>]
30 - 'WORD', [<word>, <wordbitsize>, <worditemcount>]
33 - A single item (a number). It can be of arbitrary size. The max. number
34 of bits in this item is specified in <itembitsize>.
37 - The size of an item (in bits). For a 4-bit parallel bus this is 4,
38 for a 16-bit parallel bus this is 16, and so on.
41 - A single word (a number). It can be of arbitrary size. The max. number
42 of bits in this word is specified in <wordbitsize>. The (exact) number
43 of items in this word is specified in <worditemcount>.
46 - The size of a word (in bits). For a 2-item word with 8-bit items
47 <wordbitsize> is 16, for a 3-item word with 4-bit items <wordbitsize>
51 - The size of a word (in number of items). For a 4-item word (no matter
52 how many bits each item consists of) <worditemcount> is 4, for a 7-item
53 word <worditemcount> is 7, and so on.
56 def channel_list(num_channels):
57 l = [{'id': 'clk', 'name': 'CLK', 'desc': 'Clock line'}]
58 for i in range(num_channels):
59 d = {'id': 'd%d' % i, 'name': 'D%d' % i, 'desc': 'Data line %d' % i}
63 class ChannelError(Exception):
68 class Decoder(srd.Decoder):
72 longname = 'Parallel sync bus'
73 desc = 'Generic parallel synchronous bus.'
76 outputs = ['parallel']
77 optional_channels = channel_list(NUM_CHANNELS)
79 {'id': 'clock_edge', 'desc': 'Clock edge to sample on',
80 'default': 'rising', 'values': ('rising', 'falling')},
81 {'id': 'wordsize', 'desc': 'Data wordsize', 'default': 1},
82 {'id': 'endianness', 'desc': 'Data endianness',
83 'default': 'little', 'values': ('little', 'big')},
93 self.saved_item = None
94 self.ss_item = self.es_item = None
99 self.out_python = self.register(srd.OUTPUT_PYTHON)
100 self.out_ann = self.register(srd.OUTPUT_ANN)
102 def putpb(self, data):
103 self.put(self.ss_item, self.es_item, self.out_python, data)
105 def putb(self, data):
106 self.put(self.ss_item, self.es_item, self.out_ann, data)
108 def putpw(self, data):
109 self.put(self.ss_word, self.es_word, self.out_python, data)
111 def putw(self, data):
112 self.put(self.ss_word, self.es_word, self.out_ann, data)
114 def handle_bits(self, datapins):
115 # If this is the first item in a word, save its sample number.
116 if self.itemcount == 0:
117 self.ss_word = self.samplenum
119 # Get the bits for this item.
120 item, used_pins = 0, datapins.count(1) + datapins.count(0)
121 for i in range(used_pins):
122 item |= datapins[i] << i
124 self.items.append(item)
128 # Save the start sample and item for later (no output yet).
129 self.ss_item = self.samplenum
131 self.saved_item = item
133 # Output the saved item (from the last CLK edge to the current).
134 self.es_item = self.samplenum
135 self.putpb(['ITEM', self.saved_item])
136 self.putb([0, ['%X' % self.saved_item]])
137 self.ss_item = self.samplenum
138 self.saved_item = item
140 endian, ws = self.options['endianness'], self.options['wordsize']
142 # Get as many items as the configured wordsize says.
143 if self.itemcount < ws:
146 # Output annotations/python for a word (a collection of items).
149 if endian == 'little':
150 word |= self.items[i] << ((ws - 1 - i) * used_pins)
151 elif endian == 'big':
152 word |= self.items[i] << (i * used_pins)
154 self.es_word = self.samplenum
155 # self.putpw(['WORD', word])
156 # self.putw([1, ['%X' % word]])
157 self.ss_word = self.samplenum
159 self.itemcount, self.items = 0, []
162 for i in range(len(self.optional_channels)):
163 if self.has_channel(i):
164 self.num_channels += 1
166 if self.num_channels == 0:
167 raise ChannelError('At least one channel has to be supplied.')
169 if not self.has_channel(0):
170 # CLK was not supplied, sample on ANY edge of ANY of the pins
171 # (but only of those pins that were actually supplied).
173 for i in range(1, len(self.optional_channels)):
174 if self.has_channel(i):
175 conds.append({i: 'e'})
177 self.handle_bits(self.wait(conds)[1:])
179 # Sample on the rising or falling CLK edge (depends on config).
181 pins = self.wait({0: self.options['clock_edge'][0]})
182 self.handle_bits(pins[1:])