]> sigrok.org Git - libsigrokdecode.git/blame - decoders/em4100/pd.py
em4100: Eliminate some unnecessary int()s.
[libsigrokdecode.git] / decoders / em4100 / pd.py
CommitLineData
8f265907
BL
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2015 Benjamin Larsson <benjamin@southpole.se>
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 data 2 of the License, or
9## (at your option) any later data.
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, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
21import sigrokdecode as srd
22
23class SamplerateError(Exception):
24 pass
25
26class Decoder(srd.Decoder):
27 api_version = 2
28 id = 'em4100'
29 name = 'EM4100'
30 longname = 'RFID EM4100'
31 desc = 'EM4100 100-150kHz RFID protocol.'
32 license = 'gplv2+'
33 inputs = ['logic']
34 outputs = ['em4100']
35 channels = (
36 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
37 )
38 options = (
39 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
40 'values': ('active-low', 'active-high')},
1a782fad
UH
41 {'id': 'datarate' , 'desc': 'Data rate', 'default': 64,
42 'values': (64, 32, 16)},
8f265907
BL
43# {'id': 'coding', 'desc': 'Bit coding', 'default': 'biphase',
44# 'values': ('biphase', 'manchester', 'psk')},
1a782fad 45 {'id': 'coilfreq', 'desc': 'Coil frequency', 'default': 125000},
8f265907
BL
46 )
47 annotations = (
428e40cb
UH
48 ('bit', 'Bit'),
49 ('header', 'Header'),
50 ('version-customer', 'Version/customer'),
51 ('data', 'Data'),
52 ('rowparity-ok', 'Row parity OK'),
53 ('rowparity-err', 'Row parity error'),
54 ('colparity-ok', 'Column parity OK'),
55 ('colparity-err', 'Column parity error'),
8f265907 56 ('stopbit', 'Stop bit'),
428e40cb 57 ('tag', 'Tag'),
8f265907
BL
58 )
59 annotation_rows = (
60 ('bits', 'Bits', (0,)),
428e40cb
UH
61 ('fields', 'Fields', (1, 2, 3, 4, 5, 6, 7, 8)),
62 ('tags', 'Tags', (9,)),
8f265907
BL
63 )
64
65 def __init__(self, **kwargs):
66 self.samplerate = None
67 self.oldpin = None
68 self.last_samplenum = None
69 self.lastlast_samplenum = None
70 self.last_edge = 0
71 self.bit_width = 0
72 self.halfbit_limit = 0
73 self.oldpp = 0
74 self.oldpl = 0
75 self.oldsamplenum = 0
76 self.last_bit_pos = 0
77 self.first_start = 0
78 self.first_one = 0
79 self.state = 'HEADER'
80 self.data = 0
81 self.data_bits = 0
82 self.data_start = 0
83 self.data_parity = 0
84 self.payload_cnt = 0
85 self.data_col_parity = [0, 0, 0, 0, 0, 0]
86 self.col_parity = [0, 0, 0, 0, 0, 0]
428e40cb
UH
87 self.tag = 0
88 self.all_row_parity_ok = True
89 self.col_parity_pos = []
8f265907
BL
90
91 def metadata(self, key, value):
92 if key == srd.SRD_CONF_SAMPLERATE:
93 self.samplerate = value
1a782fad 94 self.bit_width = (self.samplerate / self.options['coilfreq']) * self.options['datarate']
8f265907
BL
95 self.halfbit_limit = self.bit_width/2 + self.bit_width/4
96 self.polarity = 0 if self.options['polarity'] == 'active-low' else 1
97
98 def start(self):
99 self.out_ann = self.register(srd.OUTPUT_ANN)
100
101 def add_bit(self, bit, bit_start, bit_stop):
102 if self.state == 'HEADER':
103 if bit == 1:
104 if self.first_one > 0:
105 self.first_one += 1
106 if self.first_one == 9:
1a782fad 107 self.put(self.first_start, bit_stop, self.out_ann,
8f265907
BL
108 [1, ['Header', 'Head', 'He', 'H']])
109 self.first_one = 0
110 self.state = 'PAYLOAD'
111 return
112 if self.first_one == 0:
113 self.first_one = 1
114 self.first_start = bit_start
115
116 if bit == 0:
117 self.first_one = 0
118 return
119
120 if self.state == 'PAYLOAD':
121 self.payload_cnt += 1
122 if self.data_bits == 0:
123 self.data_start = bit_start
124 self.data = 0
125 self.data_parity = 0
126 self.data_bits += 1
127 if self.data_bits == 5:
428e40cb
UH
128 s = 'Version/customer' if self.payload_cnt <= 10 else 'Data'
129 c = 2 if self.payload_cnt <= 10 else 3
1a782fad 130 self.put(self.data_start, bit_start, self.out_ann,
428e40cb
UH
131 [c, [s + ': %X' % self.data, '%X' % self.data]])
132 s = 'OK' if self.data_parity == bit else 'ERROR'
133 c = 4 if s == 'OK' else 5
134 if s == 'ERROR':
135 self.all_row_parity_ok = False
1a782fad 136 self.put(bit_start, bit_stop, self.out_ann,
428e40cb
UH
137 [c, ['Row parity: ' + s, 'RP: ' + s, 'RP', 'R']])
138 self.tag = (self.tag << 4) | self.data
8f265907
BL
139 self.data_bits = 0
140 if self.payload_cnt == 50:
141 self.state = 'TRAILER'
142 self.payload_cnt = 0
143
144 self.data_parity ^= bit
145 self.data_col_parity[self.data_bits] ^= bit
146 self.data = (self.data << 1) | bit
147 return
148
149 if self.state == 'TRAILER':
150 self.payload_cnt += 1
151 if self.data_bits == 0:
152 self.data_start = bit_start
153 self.data = 0
154 self.data_parity = 0
155 self.data_bits += 1
156 self.col_parity[self.data_bits] = bit
1a782fad 157 self.col_parity_pos.append([bit_start, bit_stop])
8f265907
BL
158
159 if self.data_bits == 5:
1a782fad 160 self.put(bit_start, bit_stop, self.out_ann,
428e40cb
UH
161 [8, ['Stop bit', 'SB', 'S']])
162
163 for i in range(1, 5):
164 s = 'OK' if self.data_col_parity[i] == \
165 self.col_parity[i] else 'ERROR'
166 c = 6 if s == 'OK' else 7
167 self.put(self.col_parity_pos[i - 1][0],
168 self.col_parity_pos[i - 1][1], self.out_ann,
169 [c, ['Column parity %d: %s' % (i, s),
170 'CP%d: %s' % (i, s), 'CP%d' % i, 'C']])
171
172 # Emit an annotation for valid-looking tags.
173 all_col_parity_ok = (self.data_col_parity[1:5] == self.col_parity[1:5])
174 if all_col_parity_ok and self.all_row_parity_ok:
1a782fad 175 self.put(self.first_start, bit_stop, self.out_ann,
428e40cb 176 [9, ['Tag: %010X' % self.tag, 'Tag', 'T']])
8f265907 177
428e40cb 178 self.tag = 0
8f265907 179 self.data_bits = 0
428e40cb 180
8f265907
BL
181 if self.payload_cnt == 5:
182 self.state = 'HEADER'
183 self.payload_cnt = 0
184 self.data_col_parity = [0, 0, 0, 0, 0, 0]
185 self.col_parity = [0, 0, 0, 0, 0, 0]
428e40cb
UH
186 self.col_parity_pos = []
187 self.all_row_parity_ok = True
8f265907
BL
188
189 def putbit(self, bit, bit_start, bit_stop):
1a782fad 190 self.put(bit_start, bit_stop, self.out_ann, [0, [str(bit)]])
8f265907
BL
191 self.add_bit(bit, bit_start, bit_stop)
192
193 def manchester_decode(self, samplenum, pl, pp, pin):
194 bit_start = 0
195 bit_stop = 0
196 bit = self.oldpin ^ self.polarity
197 if pl > self.halfbit_limit:
198 samples = samplenum - self.oldsamplenum
199 t = samples / self.samplerate
200
201 if self.oldpl > self.halfbit_limit:
202 bit_start = int(self.oldsamplenum - self.oldpl/2)
203 bit_stop = int(samplenum - pl/2)
204 self.putbit(bit, bit_start, bit_stop)
205 if self.oldpl <= self.halfbit_limit:
206 bit_start = int(self.oldsamplenum - self.oldpl)
207 bit_stop = int(samplenum - pl/2)
208 self.putbit(bit, bit_start, bit_stop)
209 self.last_bit_pos = int(samplenum - pl/2)
210
211 if pl < self.halfbit_limit:
212 samples = samplenum - self.oldsamplenum
213 t = samples / self.samplerate
214
215 if self.oldpl > self.halfbit_limit:
1a782fad 216 bit_start = int(self.oldsamplenum - self.oldpl/2)
8f265907
BL
217 bit_stop = int(samplenum)
218 self.putbit(bit, bit_start, bit_stop)
219 self.last_bit_pos = int(samplenum)
220 if self.oldpl <= self.halfbit_limit:
221 if self.last_bit_pos <= self.oldsamplenum - self.oldpl:
1a782fad 222 bit_start = int(self.oldsamplenum - self.oldpl)
8f265907
BL
223 bit_stop = int(samplenum)
224 self.putbit(bit, bit_start, bit_stop)
225 self.last_bit_pos = int(samplenum)
226
227 def decode(self, ss, es, data):
228 if not self.samplerate:
229 raise SamplerateError('Cannot decode without samplerate.')
230 for (samplenum, (pin,)) in data:
231 # Ignore identical samples early on (for performance reasons).
232 if self.oldpin == pin:
233 continue
234
235 if self.oldpin is None:
236 self.oldpin = pin
237 self.last_samplenum = samplenum
238 self.lastlast_samplenum = samplenum
239 self.last_edge = samplenum
240 self.oldpl = 0
241 self.oldpp = 0
242 self.oldsamplenum = 0
243 self.last_bit_pos = 0
244 continue
245
246 if self.oldpin != pin:
247 pl = samplenum - self.oldsamplenum
248 pp = pin
249
250 self.manchester_decode(samplenum, pl, pp, pin)
251
252 self.oldpl = pl
253 self.oldpp = pp
254 self.oldsamplenum = samplenum
255 self.oldpin = pin