-
Notifications
You must be signed in to change notification settings - Fork 1
/
CR95HF.cpp
564 lines (436 loc) · 16.3 KB
/
CR95HF.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
MIT License
Copyright (c) 2020 Pavel Slama
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <CR95HF.h>
CR95HF::CR95HF(PinName tx, PinName rx, int baud):
_serial(tx, rx, baud) {
_serial.attach(
callback(this, &CR95HF::rxCb),
SerialBase::RxIrq
);
}
CR95HF::~CR95HF() {};
cr95hf_error_t CR95HF::init() {
tr_info("Init");
cr95hf_error_t res = CR95HF_ERROR_OK;
_tx_buf[0] = IDN;
_tx_buf[1] = 0;
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
// IDN device type check
if (_rx_buf[CR95HF_HEADER_LEN] != 'N' ||
_rx_buf[CR95HF_HEADER_LEN + 1] != 'F' ||
_rx_buf[CR95HF_HEADER_LEN + 2] != 'C') {
tr_error("Invalid device type");
return CR95HF_ERROR_UNSUPPORTED;
}
return res;
}
cr95hf_error_t CR95HF::setProtocol(protocol_t protocol) {
cr95hf_error_t res = CR95HF_ERROR_OK;
_protocol = protocol;
_tx_buf[0] = PROTOCOL_SELECT;
_tx_buf[1] = 1; // payload_len
_tx_buf[2] = protocol;
switch (protocol) {
case FIELD_OFF:
tr_info("Turning off");
_tx_buf[3] = 0x00;
_tx_buf[1] += 1; // payload_len
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
break;
case ISO14443A:
tr_info("Setting protocol to ISO14443A");
// select protocol, datasheet table 12
_tx_buf[3] = 0x00;
_tx_buf[1] += 1; // payload_len
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
// set synchronization level
_tx_buf[0] = WRITE_REGISTER;
_tx_buf[1] = 4; // payload_len
_tx_buf[2] = CR95HF_TIMERW_VALUE;
_tx_buf[3] = 0x00;
_tx_buf[4] = 0x58; // TimerW - value between 0x50-0x60
_tx_buf[5] = CR95HF_TIMERW_CONFIRMATION;
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
// modulation and gain
_tx_buf[0] = WRITE_REGISTER;
_tx_buf[1] = 4; // payload_len
_tx_buf[2] = CR95HF_ANALOG_CONFIGURATION_ADDR;
_tx_buf[3] = 0x01;
_tx_buf[4] = 0x01;
_tx_buf[5] = 0xD7; // modulation & gain - value 0xD0, 0xD1, 0xD3, 0xD7 or 0xDF
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
break;
default:
tr_info("Unsupported protocol");
res = CR95HF_ERROR_UNSUPPORTED;
break;
}
return res;
}
// TODO
cr95hf_error_t CR95HF::calibration() {
tr_info("Performing calibration");
cr95hf_error_t res = CR95HF_ERROR_OK;
_tx_buf[0] = IDLE; // cmd
_tx_buf[1] = 0x0E; // payload_len
_tx_buf[2] = 0x03; // WU control
_tx_buf[3] = 0xA1; // Enter control - tag detector calibration
_tx_buf[4] = 0x00;
_tx_buf[5] = 0xF8; // WU control - tag detector calibration
_tx_buf[6] = 0x01;
_tx_buf[7] = 0x18; // Leave control
_tx_buf[8] = 0x00;
_tx_buf[9] = 0x20; // WU period
_tx_buf[10] = 0x60; // Osc start
_tx_buf[11] = 0x60; // DAC start
_tx_buf[12] = 0x00; // DAC data
_tx_buf[13] = 0x00;
_tx_buf[14] = 0x3F; // Swing count
_tx_buf[15] = 0x01; // Max sleep
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
_tx_buf[12] = 0x00; // DAC data
_tx_buf[13] = 0xFC;
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
_tx_buf[12] = 0x00; // DAC data
_tx_buf[13] = 0x7C;
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
_tx_buf[12] = 0x00; // DAC data
_tx_buf[13] = 0x3C;
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
_tx_buf[12] = 0x00; // DAC data
_tx_buf[13] = 0x5C;
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
_tx_buf[12] = 0x00; // DAC data
_tx_buf[13] = 0x6C;
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
_tx_buf[12] = 0x00; // DAC data
_tx_buf[13] = 0x74;
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
_tx_buf[12] = 0x00; // DAC data
_tx_buf[13] = 0x70;
res = send();
if (res != CR95HF_ERROR_OK) {
tr_error("Invalid response");
return res;
}
return res;
}
bool CR95HF::isTagInRange() {
tr_debug("Searching to tag");
switch (_protocol) {
case ISO14443A: {
// REQA
_tx_buf[0] = SEND_RECV;
_tx_buf[1] = 2; // payload_len
_tx_buf[2] = 0x26;
_tx_buf[3] = 0x07;
if (send() != CR95HF_ERROR_FRAME_RECV_OK) {
return false;
}
// _rx_buf holds ATQA
if (_rx_buf[CR95HF_HEADER_LEN] & 0b00100000) { // RFU
tr_error("RFU must be zero");
return false;
}
_uid_offset = 0; // reset offset
_uid_len = _rx_buf[CR95HF_HEADER_LEN] >> 6;
if (_uid_len == 0b00) {
_uid_len = MIFARE_UID_SINGLE_SIZE;
} else if (_uid_len == 0b01) {
_uid_len = MIFARE_UID_DOUBLE_SIZE;
} else if (_uid_len == 0b10) {
_uid_len = MIFARE_UID_TRIPLE_SIZE;
} else {
tr_error("Invalid UID len");
_uid_len = 0;
return false;
}
tr_info("TAG in range, len: %u", _uid_len);
return true;
}
default:
break;
}
return false;
}
uint8_t CR95HF::getTagUID(uint8_t *uid, uint8_t *tag_type) {
tr_info("Getting tag UID");
switch (_protocol) {
case ISO14443A: {
uint8_t sak = MIFARE_SAK_UID_NOT_COMPLETE;
// Anticollision loop
for (auto i = 0; i < 3; i++) {
if (sak == MIFARE_SAK_UID_NOT_COMPLETE) {
if (!anticol(_cl_level[i])) {
return 0;
}
sak = select(_cl_level[i]);
if (sak == 0xFF) {
return 0;
}
}
}
if (uid) {
memcpy(uid, _uid, _uid_offset);
}
if (tag_type) {
*tag_type = sak;
}
tr_info("Tag UID: %s", tr_array(_uid, _uid_offset));
return _uid_offset;
}
break;
default:
break;
}
return 0;
}
bool CR95HF::anticol(uint8_t level) {
tr_debug("Anticol request");
_tx_buf[0] = SEND_RECV;
_tx_buf[1] = 3; // payload_len
_tx_buf[2] = level;
_tx_buf[3] = 0x20;
_tx_buf[4] = 0x08;
if (send() != CR95HF_ERROR_FRAME_RECV_OK) {
tr_error("Invalid response");
return false;
}
uint8_t collision = (_rx_buf[_rx_buf[1] - 1] & 0x80);
uint8_t len_origin = _rx_buf[1];
uint8_t byte_collision_index = _rx_buf[_rx_buf[1]];
uint8_t bit_collision_index = _rx_buf[_rx_buf[1] + 1];
uint8_t new_byte_collision_index = 0;
uint8_t new_bit_collision_index = 0;
uint8_t tag[4] = {0};
tr_debug("Collision: %u", collision);
if (bit_collision_index == 0x08) {
tr_error("Previous collision missed");
return false;
}
while (collision == 0x80) {
collision = 0x00;
_tx_buf[CR95HF_HEADER_LEN + 1] = 0x20 + ((byte_collision_index) << 4) + (bit_collision_index + 1);
if (byte_collision_index == 0) {
_tx_buf[CR95HF_HEADER_LEN + 2] = _rx_buf[2] &
((uint8_t)(~(0xFF << (bit_collision_index + 1)))); // ISO said it's better to put collision bit to value 1
_tx_buf[CR95HF_HEADER_LEN + 3] = (bit_collision_index + 1) | 0x40; // add split frame bit
tag[0] = _tx_buf[CR95HF_HEADER_LEN + 2];
} else if (byte_collision_index == 1) {
_tx_buf[CR95HF_HEADER_LEN + 2] = _rx_buf[2];
_tx_buf[CR95HF_HEADER_LEN + 3] = _rx_buf[3] &
((uint8_t)(~(0xFF << (bit_collision_index + 1)))); // ISO said it's better to put collision bit to value 1
_tx_buf[CR95HF_HEADER_LEN + 4] = (bit_collision_index + 1) | 0x40; // add split frame bit
tag[0] = _tx_buf[CR95HF_HEADER_LEN + 2];
tag[1] = _tx_buf[CR95HF_HEADER_LEN + 3];
} else if (byte_collision_index == 2) {
_tx_buf[CR95HF_HEADER_LEN + 2] = _rx_buf[2];
_tx_buf[CR95HF_HEADER_LEN + 3] = _rx_buf[3];
_tx_buf[CR95HF_HEADER_LEN + 4] = _rx_buf[4] &
((uint8_t)(~(0xFF << (bit_collision_index + 1)))); // ISO said it's better to put collision bit to value 1
_tx_buf[CR95HF_HEADER_LEN + 5] = (bit_collision_index + 1) | 0x40; // add split frame bit
tag[0] = _tx_buf[CR95HF_HEADER_LEN + 2];
tag[1] = _tx_buf[CR95HF_HEADER_LEN + 3];
tag[2] = _tx_buf[CR95HF_HEADER_LEN + 4];
} else if (byte_collision_index == 3) {
_tx_buf[CR95HF_HEADER_LEN + 2] = _rx_buf[2];
_tx_buf[CR95HF_HEADER_LEN + 3] = _rx_buf[3];
_tx_buf[CR95HF_HEADER_LEN + 4] = _rx_buf[4];
_tx_buf[CR95HF_HEADER_LEN + 5] = _rx_buf[5] &
((uint8_t)(~(0xFF << (bit_collision_index + 1)))); // ISO said it's better to put collision bit to value 1
_tx_buf[CR95HF_HEADER_LEN + 6] = (bit_collision_index + 1) | 0x40; // add split frame bit
tag[0] = _tx_buf[CR95HF_HEADER_LEN + 2];
tag[1] = _tx_buf[CR95HF_HEADER_LEN + 3];
tag[2] = _tx_buf[CR95HF_HEADER_LEN + 4];
tag[3] = _tx_buf[CR95HF_HEADER_LEN + 5];
} else {
tr_error("Invalid byte collision");
return false;
}
_tx_buf[1] = 3 + byte_collision_index + 1; // payload_len
if (send() != CR95HF_ERROR_FRAME_RECV_OK) {
tr_error("Invalid response");
return false;
}
// check if there is another collision to take into account
collision = (_rx_buf[_rx_buf[1] - 1]) & 0x80;
if (collision == 0x80) {
new_byte_collision_index = _rx_buf[_rx_buf[1]];
new_bit_collision_index = _rx_buf[_rx_buf[1] + 1];
}
// we can check that non-alignement is the one expected
uint8_t remaining_bit = 8 - (0x0F & (_rx_buf[CR95HF_HEADER_LEN + (_rx_buf[1] - 2) - 1]));
if (remaining_bit == bit_collision_index + 1) {
// recreate the good UID
if (byte_collision_index == 0) {
tag[0] = ((~(0xFF << (bit_collision_index + 1))) & _tx_buf[CR95HF_HEADER_LEN + 2]) | _rx_buf[2] ;
tag[1] = _rx_buf[3];
tag[2] = _rx_buf[4];
tag[3] = _rx_buf[5];
} else if (byte_collision_index == 1) {
tag[1] = ((~(0xFF << (bit_collision_index + 1))) & _tx_buf[CR95HF_HEADER_LEN + 3]) | _rx_buf[2] ;
tag[2] = _rx_buf[3];
tag[3] = _rx_buf[4];
} else if (byte_collision_index == 2) {
tag[2] = ((~(0xFF << (bit_collision_index + 1))) & _tx_buf[CR95HF_HEADER_LEN + 4]) | _rx_buf[2] ;
tag[3] = _rx_buf[3];
} else if (byte_collision_index == 3) {
tag[3] = ((~(0xFF << (bit_collision_index + 1))) & _tx_buf[CR95HF_HEADER_LEN + 5]) | _rx_buf[2] ;
} else {
tr_error("Invalid byte collision");
return false;
}
} else {
tr_error("Invalid remaining bit");
return false;
}
// prepare the buffer expected by the caller
_rx_buf[0] = 0x80;
_rx_buf[1] = len_origin;
_rx_buf[2] = tag[0];
_rx_buf[3] = tag[1];
_rx_buf[4] = tag[2];
_rx_buf[5] = tag[3];
_rx_buf[6] = tag[0] ^ tag[1] ^ tag[2] ^ tag[3];
// if collision was detected restart anticol
if (collision == 0x80) {
if (byte_collision_index != new_byte_collision_index) {
byte_collision_index += new_byte_collision_index;
bit_collision_index = new_bit_collision_index;
} else {
byte_collision_index += new_byte_collision_index;
bit_collision_index += (new_bit_collision_index + 1);
}
}
}
if ((_uid_len == MIFARE_UID_SINGLE_SIZE && level == MIFARE_CL_1) ||
(_uid_len == MIFARE_UID_DOUBLE_SIZE && level == MIFARE_CL_2) ||
(_uid_len == MIFARE_UID_TRIPLE_SIZE && level == MIFARE_CL_3)) {
memcpy(_uid + _uid_offset, _rx_buf + CR95HF_HEADER_LEN, 4);
_uid_offset += 4;
} else { // UID_PART
memcpy(_uid + _uid_offset, _rx_buf + CR95HF_HEADER_LEN + 1, 3);
_uid_offset += 3;
}
tr_debug("UID[%u] so far: %s", _uid_offset, tr_array(_uid, _uid_offset));
return true;
}
uint8_t CR95HF::select(uint8_t level) {
tr_debug("Select request");
_tx_buf[0] = SEND_RECV;
_tx_buf[1] = 8; // payload_len
_tx_buf[2] = level;
_tx_buf[3] = 0x70;
memcpy(_tx_buf + 4, _rx_buf + 2, 4); // copy UID or CT+UID
_tx_buf[8] = _rx_buf[_rx_buf[1] - 2]; // copy BCC
_tx_buf[9] = 0x20 | 0x8; // append CRC + 8 bits in first byte
if (send() != CR95HF_ERROR_FRAME_RECV_OK) {
tr_error("Invalid response");
return 0xFF;
}
tr_debug("sak: %u", _rx_buf[2]);
return _rx_buf[2];
}
cr95hf_error_t CR95HF::send() {
uint8_t len = _tx_buf[1] + CR95HF_HEADER_LEN;
tr_debug("Sending[%u]: %s", len, tr_array(static_cast<const uint8_t *>(_tx_buf), len));
// zero RX buffer
memset(_rx_buf, 0, sizeof(_rx_buf));
_rx_buf_size = 0;
_serial.write(_tx_buf, len);
uint32_t wait = _flags.wait_all(CR95HF_FLAGS_RX_DONE, MBED_CONF_CR95HF_TIMEOUT);
if (wait != CR95HF_FLAGS_RX_DONE) {
tr_error("RX timeout");
return CR95HF_ERROR_TIMEOUT;
}
tr_debug("Response[%u]: %s", _rx_buf_size, tr_array(_rx_buf, _rx_buf_size));
return _rx_buf[0];
}
void CR95HF::rxCb() {
uint8_t character;
while (_serial.readable()) {
ssize_t len = _serial.read(&character, 1);
if (len == 1) {
_rx_buf[_rx_buf_size] = character;
_rx_buf_size++;
if (_rx_buf_size == 2) { // got length byte
if (_rx_buf[1] == 0) { // zero len
_flags.set(CR95HF_FLAGS_RX_DONE);
} else {
_data_len = _rx_buf[1] + CR95HF_HEADER_LEN;
}
} else if (_rx_buf_size > CR95HF_HEADER_LEN && _rx_buf_size == _data_len) {
_flags.set(CR95HF_FLAGS_RX_DONE);
}
// prevent overflow
if (_rx_buf_size >= sizeof(_rx_buf)) {
_rx_buf_size = 0;
}
}
}
}