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