]> sigrok.org Git - libsigrokdecode.git/blame - decoders/miller/pd.py
Miller encoding PD
[libsigrokdecode.git] / decoders / miller / pd.py
CommitLineData
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
28import sigrokdecode as srd
29
30def roundto(x, k=1.0):
31 return round(x / k) * k
32
33class Decoder(srd.Decoder):
34 api_version = 3
35 id = 'miller'
36 name = 'Miller'
37 longname = 'Miller decoder for NFC'
38 desc = 'Decodes (modified) Miller encoding as used in NFC communication.'
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))
54
55 def __init__(self):
56 self.samplerate = None
57
58 def metadata(self, key, value):
59 if key == srd.SRD_CONF_SAMPLERATE:
60 self.samplerate = value
61
62 def start(self):
63 self.out_ann = self.register(srd.OUTPUT_ANN)
64 self.out_binary = self.register(srd.OUTPUT_BINARY)
65
66 def decode_bits(self):
67 timeunit = self.samplerate / self.options['baudrate']
68 edgetype = self.options['edge'][0]
69
70 self.wait({0: edgetype}) # first symbol, beginning of unit
71 prevedge = self.samplenum
72
73 # start of message: '0'
74 prevbit = 0
75 yield (0, prevedge, prevedge + timeunit)
76 expectedstart = self.samplenum + timeunit
77
78 # end of message: '0' followed by one idle symbol
79
80 while True:
81 self.wait([{0: edgetype}, {'skip': int(3 * timeunit)}])
82 got_timeout = self.matched[1]
83 sampledelta = (self.samplenum - prevedge)
84 prevedge = self.samplenum
85 timedelta = roundto(sampledelta / timeunit, 0.5)
86
87 # a mark stands for a 1 bit
88 # a mark has an edge in the middle
89
90 # a space stands for a 0 bit
91 # a space either has an edge at the beginning or no edge at all
92 # after a mark, a space is edge-less
93 # after a space, a space has an edge
94
95 # we get 1.0, 1.5, 2.0 times between edges
96
97 # end of transmission is always a space, either edged or edge-less
98
99 if prevbit == 0: # space -> ???
100 if timedelta == 1.0: # 1.0 units -> space
101 yield (0, self.samplenum, self.samplenum + timeunit)
102 prevbit = 0
103 expectedstart = self.samplenum + timeunit
104 elif timedelta == 1.5: # 1.5 units -> mark
105 yield (1, expectedstart, self.samplenum + 0.5*timeunit)
106 prevbit = 1
107 expectedstart = self.samplenum + timeunit*0.5
108 elif timedelta >= 2.0:
109 # idle symbol (end of message)
110 yield None
111 else:
112 # assert timedelta >= 2.0
113 yield (False, self.samplenum - sampledelta, self.samplenum)
114 break
115 else: # mark -> ???
116 if timedelta <= 0.5:
117 yield (False, self.samplenum - sampledelta, self.samplenum)
118 break
119 if timedelta == 1.0: # 1.0 units -> mark again (1.5 from start)
120 yield (1, expectedstart, self.samplenum + 0.5*timeunit)
121 prevbit = 1
122 expectedstart = self.samplenum + 0.5*timeunit
123 elif timedelta == 1.5: # 1.5 units -> space (no pulse) and space (pulse)
124 yield (0, expectedstart, self.samplenum)
125 yield (0, self.samplenum, self.samplenum + timeunit)
126 prevbit = 0
127 expectedstart = self.samplenum + timeunit
128 elif timedelta == 2.0: # 2.0 units -> space (no pulse) and mark (pulse)
129 yield (0, expectedstart, expectedstart + timeunit)
130 yield (1, self.samplenum - 0.5*timeunit, self.samplenum + 0.5*timeunit)
131 prevbit = 1
132 expectedstart = self.samplenum + timeunit*0.5
133 else: # longer -> space and end of message
134 yield (0, expectedstart, expectedstart + timeunit)
135 yield None
136 break
137
138 def decode_run(self):
139 numbits = 0
140 bitvalue = 0
141 bitstring = ''
142 stringstart = None
143 stringend = None
144
145 for bit in self.decode_bits():
146 if bit is None:
147 break
148
149 (value, ss, es) = bit
150
151 if value is False:
152 self.put(int(ss), int(es), self.out_ann, [1, ['ERROR']])
153 else:
154 self.put(int(ss), int(es), self.out_ann, [0, ['{}'.format(value)]])
155
156 if value is False:
157 numbits = 0
158 break
159
160 if stringstart is None:
161 stringstart = ss
162
163 stringend = es
164
165 bitvalue |= value << numbits
166 numbits += 1
167
168 bitstring += '{}'.format(value)
169 if numbits % 4 == 0:
170 bitstring += ' '
171
172 if not numbits:
173 return
174
175 self.put(int(stringstart), int(stringend), self.out_ann, [1, ['{}'.format(bitstring)]])
176
177 numbytes = numbits // 8 + (numbits % 8 > 0)
178 bytestring = bitvalue.to_bytes(numbytes, 'little')
179 self.put(int(stringstart), int(stringend), self.out_binary, [1, bytestring])
180
181 def decode(self):
182 while True:
183 self.decode_run()