]> sigrok.org Git - libsigrokdecode.git/blame - decoders/counter/pd.py
counter: Consider edge counter offset also in word processing
[libsigrokdecode.git] / decoders / counter / pd.py
CommitLineData
6ca7aa50
SB
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2018 Stefan Brüns <stefan.bruens@rwth-aachen.de>
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, see <http://www.gnu.org/licenses/>.
18##
19
20import sigrokdecode as srd
21
f113f6c9
UH
22PIN_DATA, PIN_RESET = range(2)
23ROW_EDGE, ROW_WORD, ROW_RESET = range(3)
f2120b9e 24
6ca7aa50
SB
25class Decoder(srd.Decoder):
26 api_version = 3
27 id = 'counter'
28 name = 'Counter'
29 longname = 'Edge counter'
30 desc = 'Count number of edges.'
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = []
34 channels = (
35 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
36 )
37 optional_channels = (
38 {'id': 'reset', 'name': 'Reset', 'desc': 'Reset line'},
39 )
40 annotations = (
41 ('edge_count', 'Edge count'),
42 ('word_count', 'Word count'),
42dcd8ef 43 ('word_reset', 'Word reset'),
6ca7aa50
SB
44 )
45 annotation_rows = (
f2120b9e
GS
46 ('edge_counts', 'Edges', (ROW_EDGE,)),
47 ('word_counts', 'Words', (ROW_WORD,)),
48 ('word_resets', 'Word resets', (ROW_RESET,)),
6ca7aa50
SB
49 )
50 options = (
e8b53818
GS
51 {'id': 'data_edge', 'desc': 'Edges to count (data)', 'default': 'any',
52 'values': ('any', 'rising', 'falling')},
6ca7aa50 53 {'id': 'divider', 'desc': 'Count divider (word width)', 'default': 0},
e8b53818
GS
54 {'id': 'reset_edge', 'desc': 'Edge which clears counters (reset)',
55 'default': 'falling', 'values': ('rising', 'falling')},
5dd2a4c0
SA
56 {'id': 'edge_off', 'desc': 'Edge counter value after start/reset', 'default': 0},
57 {'id': 'word_off', 'desc': 'Word counter value after start/reset', 'default': 0},
547b79f4 58 {'id': 'dead_cycles', 'desc': 'Ignore this many edges after reset', 'default': 0},
d52bd4f2
SA
59 {'id': 'start_with_reset', 'desc': 'Assume decode starts with reset',
60 'default': 'no', 'values': ('no', 'yes')},
6ca7aa50
SB
61 )
62
63 def __init__(self):
64 self.reset()
65
66 def reset(self):
deabbb70 67 pass
6ca7aa50
SB
68
69 def metadata(self, key, value):
70 if key == srd.SRD_CONF_SAMPLERATE:
71 self.samplerate = value
72
73 def start(self):
74 self.out_ann = self.register(srd.OUTPUT_ANN)
6ca7aa50 75
647d5286
GS
76 def putc(self, cls, ss, annlist):
77 self.put(ss, self.samplenum, self.out_ann, [cls, annlist])
6ca7aa50
SB
78
79 def decode(self):
d11290fe
GS
80 opt_edge_map = {'rising': 'r', 'falling': 'f', 'any': 'e'}
81
821a2141
GS
82 data_edge = self.options['data_edge']
83 divider = self.options['divider']
84 if divider < 0:
85 divider = 0
86 reset_edge = self.options['reset_edge']
deabbb70 87
821a2141
GS
88 condition = [{PIN_DATA: opt_edge_map[data_edge]}]
89 have_reset = self.has_channel(PIN_RESET)
90 if have_reset:
f2120b9e 91 cond_reset = len(condition)
821a2141 92 condition.append({PIN_RESET: opt_edge_map[reset_edge]})
6ca7aa50 93
e663ab18 94 edge_count = int(self.options['edge_off'])
17869208 95 edge_start = None
e663ab18 96 word_count = int(self.options['word_off'])
17869208 97 word_start = None
d52bd4f2
SA
98
99 if self.options['start_with_reset'] == 'yes':
100 dead_count = int(self.options['dead_cycles'])
101 else:
102 dead_count = 0
103
6ca7aa50
SB
104 while True:
105 self.wait(condition)
647d5286 106 now = self.samplenum
f2120b9e 107
821a2141 108 if have_reset and self.matched[cond_reset]:
5dd2a4c0 109 edge_count = int(self.options['edge_off'])
17869208 110 edge_start = now
5dd2a4c0 111 word_count = int(self.options['word_off'])
17869208 112 word_start = now
647d5286 113 self.putc(ROW_RESET, now, ['Word reset', 'Reset', 'Rst', 'R'])
547b79f4
GS
114 dead_count = int(self.options['dead_cycles'])
115 continue
116
117 if dead_count:
118 dead_count -= 1
119 edge_start = now
120 word_start = now
6ca7aa50
SB
121 continue
122
17869208
GS
123 # Implementation note: In the absence of a RESET condition
124 # before the first data edge, any arbitrary choice of where
125 # to start the annotation is valid. One may choose to emit a
126 # narrow annotation (where ss=es), or assume that the cycle
127 # which corresponds to the counter value started at sample
128 # number 0. We decided to go with the latter here, to avoid
129 # narrow annotations (see bug #1210). None of this matters in
130 # the presence of a RESET condition in the input stream.
131 if edge_start is None:
132 edge_start = 0
133 if word_start is None:
134 word_start = 0
135
821a2141 136 edge_count += 1
17869208
GS
137 self.putc(ROW_EDGE, edge_start, ["{:d}".format(edge_count)])
138 edge_start = now
6ca7aa50 139
66cff5a6
SA
140 word_edge_count = edge_count - int(self.options['edge_off'])
141 if divider and (word_edge_count % divider) == 0:
821a2141 142 word_count += 1
17869208
GS
143 self.putc(ROW_WORD, word_start, ["{:d}".format(word_count)])
144 word_start = now