diff options
author | Stefan Brüns <stefan.bruens@rwth-aachen.de> | 2018-01-02 02:17:07 +0100 |
---|---|---|
committer | Uwe Hermann <uwe@hermann-uwe.de> | 2018-01-03 17:17:47 +0100 |
commit | 6ca7aa50829904240ea07bcb7594c838ec467b98 (patch) | |
tree | 1cff96da1c5b688b083a61e3d5b470a5dedb5b2c /decoders | |
parent | fcdd8c687dfce24cbb3569d0d02b0e34df0535d6 (diff) | |
download | libsigrokdecode-6ca7aa50829904240ea07bcb7594c838ec467b98.tar.gz libsigrokdecode-6ca7aa50829904240ea07bcb7594c838ec467b98.zip |
Add a trivial "counter" decoder
This decoder just counts the number of falling and/or rising edges. This
is especially useful for diagnosing protocols with a clock signal or a
fixed number of transitions per bit, e.g. pulse length coded.
It also provides a divider, which can be used to e.g. count the number
of words in I²C or SPI transfers.
Diffstat (limited to 'decoders')
-rw-r--r-- | decoders/counter/__init__.py | 28 | ||||
-rw-r--r-- | decoders/counter/pd.py | 95 |
2 files changed, 123 insertions, 0 deletions
diff --git a/decoders/counter/__init__.py b/decoders/counter/__init__.py new file mode 100644 index 0000000..e731311 --- /dev/null +++ b/decoders/counter/__init__.py @@ -0,0 +1,28 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2018 Stefan Brüns <stefan.bruens@rwth-aachen.de> +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see <http://www.gnu.org/licenses/>. +## + +''' +This PD is a simple counter. + +It can count rising and/or falling edges, provides an optional reset +signal. It can also divide the count to e.g. count the numger of +fixed length words (where a word corresponds to e.g. 9 clock edges). +''' + +from .pd import Decoder diff --git a/decoders/counter/pd.py b/decoders/counter/pd.py new file mode 100644 index 0000000..e32dabd --- /dev/null +++ b/decoders/counter/pd.py @@ -0,0 +1,95 @@ +## +## This file is part of the libsigrokdecode project. +## +## Copyright (C) 2018 Stefan Brüns <stefan.bruens@rwth-aachen.de> +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see <http://www.gnu.org/licenses/>. +## + +import sigrokdecode as srd + +class Decoder(srd.Decoder): + api_version = 3 + id = 'counter' + name = 'Counter' + longname = 'Edge counter' + desc = 'Count number of edges.' + license = 'gplv2+' + inputs = ['logic'] + outputs = [] + channels = ( + {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, + ) + optional_channels = ( + {'id': 'reset', 'name': 'Reset', 'desc': 'Reset line'}, + ) + annotations = ( + ('edge_count', 'Edge count'), + ('word_count', 'Word count'), + ) + annotation_rows = ( + ('edge_counts', 'Edges', (0,)), + ('word_counts', 'Words', (1,)), + ) + options = ( + {'id': 'edge', 'desc': 'Edges to check', 'default': 'any', 'values': ('any', 'rising', 'falling')}, + {'id': 'divider', 'desc': 'Count divider (word width)', 'default': 0}, + ) + + def __init__(self): + self.reset() + + def reset(self): + self.edge_count = 0 + self.word_count = 0 + self.have_reset = None + + def metadata(self, key, value): + if key == srd.SRD_CONF_SAMPLERATE: + self.samplerate = value + + def start(self): + self.out_ann = self.register(srd.OUTPUT_ANN) + self.edge = self.options['edge'] + self.divider = self.options['divider'] + if self.divider < 0: + self.divider = 0 + + def put_count(self, ann_class, count): + self.put(self.samplenum, self.samplenum, self.out_ann, + [ann_class, [str(count)]]) + + def decode(self): + condition = [{'rising': {0: 'r'}, + 'falling': {0: 'f'}, + 'any': {0: 'e'},}[self.edge]] + + if self.has_channel(1): + self.have_reset = True + condition.append({1: 'f'}) + + while True: + self.wait(condition) + if self.have_reset and self.matched[1]: + self.edge_count = 0 + self.word_count = 0 + self.put_count(1, 'R') + continue + + self.edge_count += 1 + + self.put_count(0, self.edge_count) + if self.divider > 0 and (self.edge_count % self.divider) == 0: + self.word_count += 1 + self.put_count(1, self.word_count) |