]>
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'] | |
41 | outputs = ['miller'] | |
42 | channels = ( | |
43 | {'id': 'data', 'name': 'Data', 'desc': 'Data signal'}, | |
44 | ) | |
45 | options = ( | |
46 | {'id': 'baudrate', 'desc': 'Baud rate', 'default': 106000}, | |
47 | {'id': 'edge', 'desc': 'Edge', 'default': 'falling', 'values': ('rising', 'falling', 'either')}, | |
48 | ) | |
49 | annotations = ( | |
50 | ('bit', 'Bit'), | |
51 | ('bitstring', 'Bitstring'), | |
52 | ) | |
53 | annotation_rows = tuple((u, v, (i,)) for i, (u, v) in enumerate(annotations)) | |
31673692 UH |
54 | binary = ( |
55 | ('raw', 'Raw binary'), | |
56 | ) | |
4e7f2d70 CR |
57 | |
58 | def __init__(self): | |
d1de22c0 GS |
59 | self.reset() |
60 | ||
61 | def reset(self): | |
4e7f2d70 CR |
62 | self.samplerate = None |
63 | ||
64 | def metadata(self, key, value): | |
65 | if key == srd.SRD_CONF_SAMPLERATE: | |
66 | self.samplerate = value | |
67 | ||
68 | def start(self): | |
69 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
70 | self.out_binary = self.register(srd.OUTPUT_BINARY) | |
71 | ||
72 | def decode_bits(self): | |
73 | timeunit = self.samplerate / self.options['baudrate'] | |
74 | edgetype = self.options['edge'][0] | |
75 | ||
76 | self.wait({0: edgetype}) # first symbol, beginning of unit | |
77 | prevedge = self.samplenum | |
78 | ||
79 | # start of message: '0' | |
80 | prevbit = 0 | |
81 | yield (0, prevedge, prevedge + timeunit) | |
82 | expectedstart = self.samplenum + timeunit | |
83 | ||
84 | # end of message: '0' followed by one idle symbol | |
85 | ||
86 | while True: | |
87 | self.wait([{0: edgetype}, {'skip': int(3 * timeunit)}]) | |
88 | got_timeout = self.matched[1] | |
89 | sampledelta = (self.samplenum - prevedge) | |
90 | prevedge = self.samplenum | |
91 | timedelta = roundto(sampledelta / timeunit, 0.5) | |
92 | ||
93 | # a mark stands for a 1 bit | |
94 | # a mark has an edge in the middle | |
95 | ||
96 | # a space stands for a 0 bit | |
97 | # a space either has an edge at the beginning or no edge at all | |
98 | # after a mark, a space is edge-less | |
99 | # after a space, a space has an edge | |
100 | ||
101 | # we get 1.0, 1.5, 2.0 times between edges | |
102 | ||
103 | # end of transmission is always a space, either edged or edge-less | |
104 | ||
105 | if prevbit == 0: # space -> ??? | |
106 | if timedelta == 1.0: # 1.0 units -> space | |
107 | yield (0, self.samplenum, self.samplenum + timeunit) | |
108 | prevbit = 0 | |
109 | expectedstart = self.samplenum + timeunit | |
110 | elif timedelta == 1.5: # 1.5 units -> mark | |
111 | yield (1, expectedstart, self.samplenum + 0.5*timeunit) | |
112 | prevbit = 1 | |
113 | expectedstart = self.samplenum + timeunit*0.5 | |
114 | elif timedelta >= 2.0: | |
115 | # idle symbol (end of message) | |
116 | yield None | |
117 | else: | |
118 | # assert timedelta >= 2.0 | |
119 | yield (False, self.samplenum - sampledelta, self.samplenum) | |
120 | break | |
121 | else: # mark -> ??? | |
122 | if timedelta <= 0.5: | |
123 | yield (False, self.samplenum - sampledelta, self.samplenum) | |
124 | break | |
125 | if timedelta == 1.0: # 1.0 units -> mark again (1.5 from start) | |
126 | yield (1, expectedstart, self.samplenum + 0.5*timeunit) | |
127 | prevbit = 1 | |
128 | expectedstart = self.samplenum + 0.5*timeunit | |
129 | elif timedelta == 1.5: # 1.5 units -> space (no pulse) and space (pulse) | |
130 | yield (0, expectedstart, self.samplenum) | |
131 | yield (0, self.samplenum, self.samplenum + timeunit) | |
132 | prevbit = 0 | |
133 | expectedstart = self.samplenum + timeunit | |
134 | elif timedelta == 2.0: # 2.0 units -> space (no pulse) and mark (pulse) | |
135 | yield (0, expectedstart, expectedstart + timeunit) | |
136 | yield (1, self.samplenum - 0.5*timeunit, self.samplenum + 0.5*timeunit) | |
137 | prevbit = 1 | |
138 | expectedstart = self.samplenum + timeunit*0.5 | |
139 | else: # longer -> space and end of message | |
140 | yield (0, expectedstart, expectedstart + timeunit) | |
141 | yield None | |
142 | break | |
143 | ||
144 | def decode_run(self): | |
145 | numbits = 0 | |
146 | bitvalue = 0 | |
147 | bitstring = '' | |
148 | stringstart = None | |
149 | stringend = None | |
150 | ||
151 | for bit in self.decode_bits(): | |
152 | if bit is None: | |
153 | break | |
154 | ||
155 | (value, ss, es) = bit | |
156 | ||
157 | if value is False: | |
158 | self.put(int(ss), int(es), self.out_ann, [1, ['ERROR']]) | |
159 | else: | |
160 | self.put(int(ss), int(es), self.out_ann, [0, ['{}'.format(value)]]) | |
161 | ||
162 | if value is False: | |
163 | numbits = 0 | |
164 | break | |
165 | ||
166 | if stringstart is None: | |
167 | stringstart = ss | |
168 | ||
169 | stringend = es | |
170 | ||
171 | bitvalue |= value << numbits | |
172 | numbits += 1 | |
173 | ||
174 | bitstring += '{}'.format(value) | |
175 | if numbits % 4 == 0: | |
176 | bitstring += ' ' | |
177 | ||
178 | if not numbits: | |
179 | return | |
180 | ||
181 | self.put(int(stringstart), int(stringend), self.out_ann, [1, ['{}'.format(bitstring)]]) | |
182 | ||
183 | numbytes = numbits // 8 + (numbits % 8 > 0) | |
184 | bytestring = bitvalue.to_bytes(numbytes, 'little') | |
31673692 | 185 | self.put(int(stringstart), int(stringend), self.out_binary, [0, bytestring]) |
4e7f2d70 CR |
186 | |
187 | def decode(self): | |
188 | while True: | |
189 | self.decode_run() |