]>
Commit | Line | Data |
---|---|---|
4e7f2d70 CR |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2017 Christoph Rackwitz <christoph.rackwitz@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 | # http://www.gorferay.com/type-a-communications-interface/ | |
21 | # https://resources.infosecinstitute.com/introduction-rfid-security/ | |
22 | # https://www.radio-electronics.com/info/wireless/nfc/near-field-communications-modulation-rf-signal-interface.php | |
23 | # https://www.researchgate.net/figure/Modified-Miller-Code_fig16_283498836 | |
24 | ||
25 | # Miller: either edge | |
26 | # modified Miller: falling edge | |
27 | ||
28 | import sigrokdecode as srd | |
29 | ||
30 | def roundto(x, k=1.0): | |
31 | return round(x / k) * k | |
32 | ||
33 | class Decoder(srd.Decoder): | |
34 | api_version = 3 | |
35 | id = 'miller' | |
36 | name = 'Miller' | |
1f5685a6 UH |
37 | longname = 'Miller encoding' |
38 | desc = 'Miller encoding protocol.' | |
4e7f2d70 CR |
39 | license = 'gplv2+' |
40 | inputs = ['logic'] | |
6cbba91f | 41 | outputs = [] |
d6d8a8a4 | 42 | tags = ['Encoding'] |
4e7f2d70 CR |
43 | channels = ( |
44 | {'id': 'data', 'name': 'Data', 'desc': 'Data signal'}, | |
45 | ) | |
46 | options = ( | |
47 | {'id': 'baudrate', 'desc': 'Baud rate', 'default': 106000}, | |
48 | {'id': 'edge', 'desc': 'Edge', 'default': 'falling', 'values': ('rising', 'falling', 'either')}, | |
49 | ) | |
50 | annotations = ( | |
51 | ('bit', 'Bit'), | |
52 | ('bitstring', 'Bitstring'), | |
53 | ) | |
54 | annotation_rows = tuple((u, v, (i,)) for i, (u, v) in enumerate(annotations)) | |
31673692 UH |
55 | binary = ( |
56 | ('raw', 'Raw binary'), | |
57 | ) | |
4e7f2d70 CR |
58 | |
59 | def __init__(self): | |
d1de22c0 GS |
60 | self.reset() |
61 | ||
62 | def reset(self): | |
4e7f2d70 CR |
63 | self.samplerate = None |
64 | ||
65 | def metadata(self, key, value): | |
66 | if key == srd.SRD_CONF_SAMPLERATE: | |
67 | self.samplerate = value | |
68 | ||
69 | def start(self): | |
70 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
71 | self.out_binary = self.register(srd.OUTPUT_BINARY) | |
72 | ||
73 | def decode_bits(self): | |
74 | timeunit = self.samplerate / self.options['baudrate'] | |
75 | edgetype = self.options['edge'][0] | |
76 | ||
77 | self.wait({0: edgetype}) # first symbol, beginning of unit | |
78 | prevedge = self.samplenum | |
79 | ||
80 | # start of message: '0' | |
81 | prevbit = 0 | |
82 | yield (0, prevedge, prevedge + timeunit) | |
83 | expectedstart = self.samplenum + timeunit | |
84 | ||
85 | # end of message: '0' followed by one idle symbol | |
86 | ||
87 | while True: | |
88 | self.wait([{0: edgetype}, {'skip': int(3 * timeunit)}]) | |
89 | got_timeout = self.matched[1] | |
90 | sampledelta = (self.samplenum - prevedge) | |
91 | prevedge = self.samplenum | |
92 | timedelta = roundto(sampledelta / timeunit, 0.5) | |
93 | ||
94 | # a mark stands for a 1 bit | |
95 | # a mark has an edge in the middle | |
96 | ||
97 | # a space stands for a 0 bit | |
98 | # a space either has an edge at the beginning or no edge at all | |
99 | # after a mark, a space is edge-less | |
100 | # after a space, a space has an edge | |
101 | ||
102 | # we get 1.0, 1.5, 2.0 times between edges | |
103 | ||
104 | # end of transmission is always a space, either edged or edge-less | |
105 | ||
106 | if prevbit == 0: # space -> ??? | |
107 | if timedelta == 1.0: # 1.0 units -> space | |
108 | yield (0, self.samplenum, self.samplenum + timeunit) | |
109 | prevbit = 0 | |
110 | expectedstart = self.samplenum + timeunit | |
111 | elif timedelta == 1.5: # 1.5 units -> mark | |
112 | yield (1, expectedstart, self.samplenum + 0.5*timeunit) | |
113 | prevbit = 1 | |
114 | expectedstart = self.samplenum + timeunit*0.5 | |
115 | elif timedelta >= 2.0: | |
116 | # idle symbol (end of message) | |
117 | yield None | |
118 | else: | |
119 | # assert timedelta >= 2.0 | |
120 | yield (False, self.samplenum - sampledelta, self.samplenum) | |
121 | break | |
122 | else: # mark -> ??? | |
123 | if timedelta <= 0.5: | |
124 | yield (False, self.samplenum - sampledelta, self.samplenum) | |
125 | break | |
126 | if timedelta == 1.0: # 1.0 units -> mark again (1.5 from start) | |
127 | yield (1, expectedstart, self.samplenum + 0.5*timeunit) | |
128 | prevbit = 1 | |
129 | expectedstart = self.samplenum + 0.5*timeunit | |
130 | elif timedelta == 1.5: # 1.5 units -> space (no pulse) and space (pulse) | |
131 | yield (0, expectedstart, self.samplenum) | |
132 | yield (0, self.samplenum, self.samplenum + timeunit) | |
133 | prevbit = 0 | |
134 | expectedstart = self.samplenum + timeunit | |
135 | elif timedelta == 2.0: # 2.0 units -> space (no pulse) and mark (pulse) | |
136 | yield (0, expectedstart, expectedstart + timeunit) | |
137 | yield (1, self.samplenum - 0.5*timeunit, self.samplenum + 0.5*timeunit) | |
138 | prevbit = 1 | |
139 | expectedstart = self.samplenum + timeunit*0.5 | |
140 | else: # longer -> space and end of message | |
141 | yield (0, expectedstart, expectedstart + timeunit) | |
142 | yield None | |
143 | break | |
144 | ||
145 | def decode_run(self): | |
146 | numbits = 0 | |
147 | bitvalue = 0 | |
148 | bitstring = '' | |
149 | stringstart = None | |
150 | stringend = None | |
151 | ||
152 | for bit in self.decode_bits(): | |
153 | if bit is None: | |
154 | break | |
155 | ||
156 | (value, ss, es) = bit | |
157 | ||
158 | if value is False: | |
159 | self.put(int(ss), int(es), self.out_ann, [1, ['ERROR']]) | |
160 | else: | |
161 | self.put(int(ss), int(es), self.out_ann, [0, ['{}'.format(value)]]) | |
162 | ||
163 | if value is False: | |
164 | numbits = 0 | |
165 | break | |
166 | ||
167 | if stringstart is None: | |
168 | stringstart = ss | |
169 | ||
170 | stringend = es | |
171 | ||
172 | bitvalue |= value << numbits | |
173 | numbits += 1 | |
174 | ||
175 | bitstring += '{}'.format(value) | |
176 | if numbits % 4 == 0: | |
177 | bitstring += ' ' | |
178 | ||
179 | if not numbits: | |
180 | return | |
181 | ||
182 | self.put(int(stringstart), int(stringend), self.out_ann, [1, ['{}'.format(bitstring)]]) | |
183 | ||
184 | numbytes = numbits // 8 + (numbits % 8 > 0) | |
185 | bytestring = bitvalue.to_bytes(numbytes, 'little') | |
31673692 | 186 | self.put(int(stringstart), int(stringend), self.out_binary, [0, bytestring]) |
4e7f2d70 CR |
187 | |
188 | def decode(self): | |
189 | while True: | |
190 | self.decode_run() |