2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2015 Benjamin Larsson <benjamin@southpole.se>
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.
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.
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
21 import sigrokdecode as srd
23 class SamplerateError(Exception):
26 class Decoder(srd.Decoder):
30 longname = 'RFID EM4100'
31 desc = 'EM4100 100-150kHz RFID protocol.'
36 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
39 {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-high',
40 'values': ('active-low', 'active-high')},
41 {'id': 'datarate' , 'desc': 'Data rate', 'default': 64,
42 'values': (64, 32, 16)},
43 # {'id': 'coding', 'desc': 'Bit coding', 'default': 'biphase',
44 # 'values': ('biphase', 'manchester', 'psk')},
45 {'id': 'coilfreq', 'desc': 'Coil frequency', 'default': 125000},
50 ('version-customer', 'Version/customer'),
52 ('rowparity-ok', 'Row parity OK'),
53 ('rowparity-err', 'Row parity error'),
54 ('colparity-ok', 'Column parity OK'),
55 ('colparity-err', 'Column parity error'),
56 ('stopbit', 'Stop bit'),
60 ('bits', 'Bits', (0,)),
61 ('fields', 'Fields', (1, 2, 3, 4, 5, 6, 7, 8)),
62 ('tags', 'Tags', (9,)),
66 self.samplerate = None
68 self.last_samplenum = None
69 self.lastlast_samplenum = None
72 self.halfbit_limit = 0
85 self.data_col_parity = [0, 0, 0, 0, 0, 0]
86 self.col_parity = [0, 0, 0, 0, 0, 0]
88 self.all_row_parity_ok = True
89 self.col_parity_pos = []
91 def metadata(self, key, value):
92 if key == srd.SRD_CONF_SAMPLERATE:
93 self.samplerate = value
94 self.bit_width = (self.samplerate / self.options['coilfreq']) * self.options['datarate']
95 self.halfbit_limit = self.bit_width/2 + self.bit_width/4
96 self.polarity = 0 if self.options['polarity'] == 'active-low' else 1
99 self.out_ann = self.register(srd.OUTPUT_ANN)
101 def putbit(self, bit, ss, es):
102 self.put(ss, es, self.out_ann, [0, [str(bit)]])
103 if self.state == 'HEADER':
105 if self.first_one > 0:
107 if self.first_one == 9:
108 self.put(self.ss_first, es, self.out_ann,
109 [1, ['Header', 'Head', 'He', 'H']])
111 self.state = 'PAYLOAD'
113 if self.first_one == 0:
121 if self.state == 'PAYLOAD':
122 self.payload_cnt += 1
123 if self.data_bits == 0:
128 if self.data_bits == 5:
129 s = 'Version/customer' if self.payload_cnt <= 10 else 'Data'
130 c = 2 if self.payload_cnt <= 10 else 3
131 self.put(self.ss_data, ss, self.out_ann,
132 [c, [s + ': %X' % self.data, '%X' % self.data]])
133 s = 'OK' if self.data_parity == bit else 'ERROR'
134 c = 4 if s == 'OK' else 5
136 self.all_row_parity_ok = False
137 self.put(ss, es, self.out_ann,
138 [c, ['Row parity: ' + s, 'RP: ' + s, 'RP', 'R']])
139 self.tag = (self.tag << 4) | self.data
141 if self.payload_cnt == 50:
142 self.state = 'TRAILER'
145 self.data_parity ^= bit
146 self.data_col_parity[self.data_bits] ^= bit
147 self.data = (self.data << 1) | bit
150 if self.state == 'TRAILER':
151 self.payload_cnt += 1
152 if self.data_bits == 0:
157 self.col_parity[self.data_bits] = bit
158 self.col_parity_pos.append([ss, es])
160 if self.data_bits == 5:
161 self.put(ss, es, self.out_ann, [8, ['Stop bit', 'SB', 'S']])
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']])
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:
175 self.put(self.ss_first, es, self.out_ann,
176 [9, ['Tag: %010X' % self.tag, 'Tag', 'T']])
181 if self.payload_cnt == 5:
182 self.state = 'HEADER'
184 self.data_col_parity = [0, 0, 0, 0, 0, 0]
185 self.col_parity = [0, 0, 0, 0, 0, 0]
186 self.col_parity_pos = []
187 self.all_row_parity_ok = True
189 def manchester_decode(self, samplenum, pl, pp, pin):
190 bit = self.oldpin ^ self.polarity
191 if pl > self.halfbit_limit:
192 es = int(samplenum - pl/2)
193 if self.oldpl > self.halfbit_limit:
194 ss = int(self.oldsamplenum - self.oldpl/2)
196 ss = int(self.oldsamplenum - self.oldpl)
197 self.putbit(bit, ss, es)
198 self.last_bit_pos = int(samplenum - pl/2)
201 if self.oldpl > self.halfbit_limit:
202 ss = int(self.oldsamplenum - self.oldpl/2)
203 self.putbit(bit, ss, es)
204 self.last_bit_pos = int(samplenum)
206 if self.last_bit_pos <= self.oldsamplenum - self.oldpl:
207 ss = int(self.oldsamplenum - self.oldpl)
208 self.putbit(bit, ss, es)
209 self.last_bit_pos = int(samplenum)
211 def decode(self, ss, es, data):
212 if not self.samplerate:
213 raise SamplerateError('Cannot decode without samplerate.')
214 for (samplenum, (pin,)) in data:
215 # Ignore identical samples early on (for performance reasons).
216 if self.oldpin == pin:
219 if self.oldpin is None:
221 self.last_samplenum = samplenum
222 self.lastlast_samplenum = samplenum
223 self.last_edge = samplenum
226 self.oldsamplenum = 0
227 self.last_bit_pos = 0
230 if self.oldpin != pin:
231 pl = samplenum - self.oldsamplenum
233 self.manchester_decode(samplenum, pl, pp, pin)
236 self.oldsamplenum = samplenum