forked from tueddy/PN5180-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PN5180.cpp
694 lines (599 loc) · 22.3 KB
/
PN5180.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// NAME: PN5180.cpp
//
// DESC: Implementation of PN5180 class.
//
// Copyright (c) 2018 by Andreas Trappmann. All rights reserved.
//
// This file is part of the PN5180 library for the Arduino environment.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
//#define DEBUG 1
#include <Arduino.h>
#include "PN5180.h"
#include "Debug.h"
// PN5180 1-Byte Direct Commands
// see 11.4.3.3 Host Interface Command List
#define PN5180_WRITE_REGISTER (0x00)
#define PN5180_WRITE_REGISTER_OR_MASK (0x01)
#define PN5180_WRITE_REGISTER_AND_MASK (0x02)
#define PN5180_READ_REGISTER (0x04)
#define PN5180_WRITE_EEPROM (0x06)
#define PN5180_READ_EEPROM (0x07)
#define PN5180_SEND_DATA (0x09)
#define PN5180_READ_DATA (0x0A)
#define PN5180_SWITCH_MODE (0x0B)
#define PN5180_LOAD_RF_CONFIG (0x11)
#define PN5180_RF_ON (0x16)
#define PN5180_RF_OFF (0x17)
uint8_t PN5180::readBuffer[508];
PN5180::PN5180(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, SPIClass &spi) {
PN5180_NSS = SSpin;
PN5180_BUSY = BUSYpin;
PN5180_RST = RSTpin;
PN5180_SPI = spi;
/*
* 11.4.1 Physical Host Interface
* The interface of the PN5180 to a host microcontroller is based on a SPI interface,
* extended by signal line BUSY. The maximum SPI speed is 7 Mbps and fixed to CPOL
* = 0 and CPHA = 0.
*/
// Settings for PN5180: 7Mbps, MSB first, SPI_MODE0 (CPOL=0, CPHA=0)
PN5180_SPI_SETTINGS = SPISettings(7000000, MSBFIRST, SPI_MODE0);
}
void PN5180::begin() {
pinMode(PN5180_NSS, OUTPUT);
pinMode(PN5180_BUSY, INPUT);
pinMode(PN5180_RST, OUTPUT);
digitalWrite(PN5180_NSS, HIGH); // disable
digitalWrite(PN5180_RST, HIGH); // no reset
PN5180_SPI.begin();
PN5180DEBUG(F("SPI pinout: "));
PN5180DEBUG(F("SS=")); PN5180DEBUG(SS);
PN5180DEBUG(F(", MOSI=")); PN5180DEBUG(MOSI);
PN5180DEBUG(F(", MISO=")); PN5180DEBUG(MISO);
PN5180DEBUG(F(", SCK=")); PN5180DEBUG(SCK);
PN5180DEBUG("\n");
}
void PN5180::end() {
digitalWrite(PN5180_NSS, HIGH); // disable
PN5180_SPI.end();
}
/*
* WRITE_REGISTER - 0x00
* This command is used to write a 32-bit value (little endian) to a configuration register.
* The address of the register must exist. If the condition is not fulfilled, an exception is
* raised.
*/
bool PN5180::writeRegister(uint8_t reg, uint32_t value) {
uint8_t *p = (uint8_t*)&value;
#ifdef DEBUG
PN5180DEBUG(F("Write Register 0x"));
PN5180DEBUG(formatHex(reg));
PN5180DEBUG(F(", value (LSB first)=0x"));
for (int i=0; i<4; i++) {
PN5180DEBUG(formatHex(p[i]));
}
PN5180DEBUG("\n");
#endif
/*
For all 4 byte command parameter transfers (e.g. register values), the payload
parameters passed follow the little endian approach (Least Significant Byte first).
*/
uint8_t buf[6] = { PN5180_WRITE_REGISTER, reg, p[0], p[1], p[2], p[3] };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(buf, 6);
PN5180_SPI.endTransaction();
return true;
}
/*
* WRITE_REGISTER_OR_MASK - 0x01
* This command modifies the content of a register using a logical OR operation. The
* content of the register is read and a logical OR operation is performed with the provided
* mask. The modified content is written back to the register.
* The address of the register must exist. If the condition is not fulfilled, an exception is
* raised.
*/
bool PN5180::writeRegisterWithOrMask(uint8_t reg, uint32_t mask) {
uint8_t *p = (uint8_t*)&mask;
#ifdef DEBUG
PN5180DEBUG(F("Write Register 0x"));
PN5180DEBUG(formatHex(reg));
PN5180DEBUG(F(" with OR mask (LSB first)=0x"));
for (int i=0; i<4; i++) {
PN5180DEBUG(formatHex(p[i]));
}
PN5180DEBUG("\n");
#endif
uint8_t buf[6] = { PN5180_WRITE_REGISTER_OR_MASK, reg, p[0], p[1], p[2], p[3] };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(buf, 6);
PN5180_SPI.endTransaction();
return true;
}
/*
* WRITE _REGISTER_AND_MASK - 0x02
* This command modifies the content of a register using a logical AND operation. The
* content of the register is read and a logical AND operation is performed with the provided
* mask. The modified content is written back to the register.
* The address of the register must exist. If the condition is not fulfilled, an exception is
* raised.
*/
bool PN5180::writeRegisterWithAndMask(uint8_t reg, uint32_t mask) {
uint8_t *p = (uint8_t*)&mask;
#ifdef DEBUG
PN5180DEBUG(F("Write Register 0x"));
PN5180DEBUG(formatHex(reg));
PN5180DEBUG(F(" with AND mask (LSB first)=0x"));
for (int i=0; i<4; i++) {
PN5180DEBUG(formatHex(p[i]));
}
PN5180DEBUG("\n");
#endif
uint8_t buf[6] = { PN5180_WRITE_REGISTER_AND_MASK, reg, p[0], p[1], p[2], p[3] };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(buf, 6);
PN5180_SPI.endTransaction();
return true;
}
/*
* READ_REGISTER - 0x04
* This command is used to read the content of a configuration register. The content of the
* register is returned in the 4 byte response.
* The address of the register must exist. If the condition is not fulfilled, an exception is
* raised.
*/
bool PN5180::readRegister(uint8_t reg, uint32_t *value) {
PN5180DEBUG(F("Reading register 0x"));
PN5180DEBUG(formatHex(reg));
PN5180DEBUG(F("...\n"));
uint8_t cmd[2] = { PN5180_READ_REGISTER, reg };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(cmd, 2, (uint8_t*)value, 4);
PN5180_SPI.endTransaction();
PN5180DEBUG(F("Register value=0x"));
PN5180DEBUG(formatHex(*value));
PN5180DEBUG("\n");
return true;
}
/*
* WRITE_EEPROM - 0x06
*/
bool PN5180::writeEEprom(uint8_t addr, uint8_t *buffer, uint8_t len) {
uint8_t cmd[len + 2];
cmd[0] = PN5180_WRITE_EEPROM;
cmd[1] = addr;
for (int i = 0; i < len; i++) cmd[2 + i] = buffer[i];
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(cmd, len + 2);
PN5180_SPI.endTransaction();
return true;
}
/*
* READ_EEPROM - 0x07
* This command is used to read data from EEPROM memory area. The field 'Address'
* indicates the start address of the read operation. The field Length indicates the number
* of bytes to read. The response contains the data read from EEPROM (content of the
* EEPROM); The data is read in sequentially increasing order starting with the given
* address.
* EEPROM Address must be in the range from 0 to 254, inclusive. Read operation must
* not go beyond EEPROM address 254. If the condition is not fulfilled, an exception is
* raised.
*/
bool PN5180::readEEprom(uint8_t addr, uint8_t *buffer, int len) {
if ((addr > 254) || ((addr+len) > 254)) {
PN5180DEBUG(F("ERROR: Reading beyond addr 254!\n"));
return false;
}
PN5180DEBUG(F("Reading EEPROM at 0x"));
PN5180DEBUG(formatHex(addr));
PN5180DEBUG(F(", size="));
PN5180DEBUG(len);
PN5180DEBUG(F("...\n"));
uint8_t cmd[3] = { PN5180_READ_EEPROM, addr, uint8_t(len) };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(cmd, 3, buffer, len);
PN5180_SPI.endTransaction();
#ifdef DEBUG
PN5180DEBUG(F("EEPROM values: "));
for (int i=0; i<len; i++) {
PN5180DEBUG(formatHex(buffer[i]));
PN5180DEBUG(" ");
}
PN5180DEBUG("\n");
#endif
return true;
}
/*
* SEND_DATA - 0x09
* This command writes data to the RF transmission buffer and starts the RF transmission.
* The parameter ‘Number of valid bits in last Byte’ indicates the exact number of bits to be
* transmitted for the last byte (for non-byte aligned frames).
* Precondition: Host shall configure the Transceiver by setting the register
* SYSTEM_CONFIG.COMMAND to 0x3 before using the SEND_DATA command, as
* the command SEND_DATA is only writing data to the transmission buffer and starts the
* transmission but does not perform any configuration.
* The size of ‘Tx Data’ field must be in the range from 0 to 260, inclusive (the 0 byte length
* allows a symbol only transmission when the TX_DATA_ENABLE is cleared).‘Number of
* valid bits in last Byte’ field must be in the range from 0 to 7. The command must not be
* called during an ongoing RF transmission. Transceiver must be in ‘WaitTransmit’ state
* with ‘Transceive’ command set. If the condition is not fulfilled, an exception is raised.
*/
bool PN5180::sendData(uint8_t *data, int len, uint8_t validBits) {
if (len > 260) {
PN5180DEBUG(F("ERROR: sendData with more than 260 bytes is not supported!\n"));
return false;
}
#ifdef DEBUG
PN5180DEBUG(F("Send data (len="));
PN5180DEBUG(len);
PN5180DEBUG(F("):"));
for (int i=0; i<len; i++) {
PN5180DEBUG(" ");
PN5180DEBUG(formatHex(data[i]));
}
PN5180DEBUG("\n");
#endif
uint8_t buffer[len+2];
buffer[0] = PN5180_SEND_DATA;
buffer[1] = validBits; // number of valid bits of last byte are transmitted (0 = all bits are transmitted)
for (int i=0; i<len; i++) {
buffer[2+i] = data[i];
}
writeRegisterWithAndMask(SYSTEM_CONFIG, 0xfffffff8); // Idle/StopCom Command
writeRegisterWithOrMask(SYSTEM_CONFIG, 0x00000003); // Transceive Command
/*
* Transceive command; initiates a transceive cycle.
* Note: Depending on the value of the Initiator bit, a
* transmission is started or the receiver is enabled
* Note: The transceive command does not finish
* automatically. It stays in the transceive cycle until
* stopped via the IDLE/StopCom command
*/
PN5180TransceiveStat transceiveState = getTransceiveState();
if (PN5180_TS_WaitTransmit != transceiveState) {
PN5180DEBUG(F("*** ERROR: Transceiver not in state WaitTransmit!?\n"));
return false;
}
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
bool success = transceiveCommand(buffer, len+2);
PN5180_SPI.endTransaction();
return success;
}
/*
* READ_DATA - 0x0A
* This command reads data from the RF reception buffer, after a successful reception.
* The RX_STATUS register contains the information to verify if the reception had been
* successful. The data is available within the response of the command. The host controls
* the number of bytes to be read via the SPI interface.
* The RF data had been successfully received. In case the instruction is executed without
* preceding an RF data reception, no exception is raised but the data read back from the
* reception buffer is invalid. If the condition is not fulfilled, an exception is raised.
*/
uint8_t * PN5180::readData(int len) {
if (len > 508) {
Serial.println(F("*** FATAL: Reading more than 508 bytes is not supported!"));
return 0L;
}
PN5180DEBUG(F("Reading Data (len="));
PN5180DEBUG(len);
PN5180DEBUG(F(")...\n"));
uint8_t cmd[2] = { PN5180_READ_DATA, 0x00 };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(cmd, 2, readBuffer, len);
PN5180_SPI.endTransaction();
#ifdef DEBUG
PN5180DEBUG(F("Data read: "));
for (int i=0; i<len; i++) {
PN5180DEBUG(formatHex(readBuffer[i]));
PN5180DEBUG(" ");
}
PN5180DEBUG("\n");
#endif
return readBuffer;
}
bool PN5180::readData(uint8_t len, uint8_t *buffer) {
if (len > 508) {
return false;
}
uint8_t cmd[2] = { PN5180_READ_DATA, 0x00 };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
bool success = transceiveCommand(cmd, 2, buffer, len);
PN5180_SPI.endTransaction();
return success;
}
/* prepare LPCD registers */
bool PN5180::prepareLPCD() {
//=======================================LPCD CONFIG================================================================================
PN5180DEBUG(F("----------------------------------"));
PN5180DEBUG(F("prepare LPCD..."));
uint8_t data[255];
uint8_t response[256];
//1. Set Fieldon time LPCD_FIELD_ON_TIME (0x36)
uint8_t fieldOn = 0xF0;//0x## -> ##(base 10) x 8μs + 62 μs
data[0] = fieldOn;
writeEEprom(0x36, data, 1);
readEEprom(0x36, response, 1);
fieldOn = response[0];
PN5180DEBUG("LPCD-fieldOn time: ");
PN5180DEBUG(formatHex(fieldOn));
//2. Set threshold level AGC_LPCD_THRESHOLD @ EEPROM 0x37
uint8_t threshold = 0x03;
data[0] = threshold;
writeEEprom(0x37, data, 1);
readEEprom(0x37, response, 1);
threshold = response[0];
PN5180DEBUG("LPCD-threshold: ");
PN5180DEBUG(formatHex(threshold));
//3. Select LPCD mode LPCD_REFVAL_GPO_CONTROL (0x38)
uint8_t lpcdMode = 0x01; // 1 = LPCD SELF CALIBRATION
// 0 = LPCD AUTO CALIBRATION (this mode does not work, should look more into it, no reason why it shouldn't work)
data[0] = lpcdMode;
writeEEprom(0x38, data, 1);
readEEprom(0x38, response, 1);
lpcdMode = response[0];
PN5180DEBUG("lpcdMode: ");
PN5180DEBUG(formatHex(lpcdMode));
// LPCD_GPO_TOGGLE_BEFORE_FIELD_ON (0x39)
uint8_t beforeFieldOn = 0xF0;
data[0] = beforeFieldOn;
writeEEprom(0x39, data, 1);
readEEprom(0x39, response, 1);
beforeFieldOn = response[0];
PN5180DEBUG("beforeFieldOn: ");
PN5180DEBUG(formatHex(beforeFieldOn));
// LPCD_GPO_TOGGLE_AFTER_FIELD_ON (0x3A)
uint8_t afterFieldOn = 0xF0;
data[0] = afterFieldOn;
writeEEprom(0x3A, data, 1);
readEEprom(0x3A, response, 1);
afterFieldOn = response[0];
PN5180DEBUG("afterFieldOn: ");
PN5180DEBUG(formatHex(afterFieldOn));
delay(100);
return true;
}
/* switch the mode to LPCD (low power card detection)
* Parameter 'wakeupCounterInMs' must be in the range from 0x0 - 0xA82
* max. wake-up time is 2960 ms.
*/
bool PN5180::switchToLPCD(uint16_t wakeupCounterInMs) {
// clear all IRQ flags
clearIRQStatus(0xffffffff);
// enable only LPCD and general error IRQ
writeRegister(IRQ_ENABLE, LPCD_IRQ_STAT | GENERAL_ERROR_IRQ_STAT);
// switch mode to LPCD
uint8_t cmd[4] = { PN5180_SWITCH_MODE, 0x01, (uint8_t)(wakeupCounterInMs & 0xFF), (uint8_t)((wakeupCounterInMs >> 8U) & 0xFF) };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
bool success = transceiveCommand(cmd, sizeof(cmd));
PN5180_SPI.endTransaction();
return success;
}
/*
* LOAD_RF_CONFIG - 0x11
* Parameter 'Transmitter Configuration' must be in the range from 0x0 - 0x1C, inclusive. If
* the transmitter parameter is 0xFF, transmitter configuration is not changed.
* Field 'Receiver Configuration' must be in the range from 0x80 - 0x9C, inclusive. If the
* receiver parameter is 0xFF, the receiver configuration is not changed. If the condition is
* not fulfilled, an exception is raised.
* The transmitter and receiver configuration shall always be configured for the same
* transmission/reception speed. No error is returned in case this condition is not taken into
* account.
*
* Transmitter: RF Protocol Speed Receiver: RF Protocol Speed
* configuration (kbit/s) configuration (kbit/s)
* byte (hex) byte (hex)
* ----------------------------------------------------------------------------------------------
* ->0D ISO 15693 ASK100 26 8D ISO 15693 26
* 0E ISO 15693 ASK10 26 8E ISO 15693 53
*/
bool PN5180::loadRFConfig(uint8_t txConf, uint8_t rxConf) {
PN5180DEBUG(F("Load RF-Config: txConf="));
PN5180DEBUG(formatHex(txConf));
PN5180DEBUG(F(", rxConf="));
PN5180DEBUG(formatHex(rxConf));
PN5180DEBUG("\n");
uint8_t cmd[3] = { PN5180_LOAD_RF_CONFIG, txConf, rxConf };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(cmd, 3);
PN5180_SPI.endTransaction();
return true;
}
/*
* RF_ON - 0x16
* This command is used to switch on the internal RF field. If enabled the TX_RFON_IRQ is
* set after the field is switched on.
*/
bool PN5180::setRF_on() {
PN5180DEBUG(F("Set RF ON\n"));
uint8_t cmd[2] = { PN5180_RF_ON, 0x00 };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(cmd, 2);
PN5180_SPI.endTransaction();
while (0 == (TX_RFON_IRQ_STAT & getIRQStatus())); // wait for RF field to set up
clearIRQStatus(TX_RFON_IRQ_STAT);
return true;
}
/*
* RF_OFF - 0x17
* This command is used to switch off the internal RF field. If enabled, the TX_RFOFF_IRQ
* is set after the field is switched off.
*/
bool PN5180::setRF_off() {
PN5180DEBUG(F("Set RF OFF\n"));
uint8_t cmd[2] { PN5180_RF_OFF, 0x00 };
PN5180_SPI.beginTransaction(PN5180_SPI_SETTINGS);
transceiveCommand(cmd, 2);
PN5180_SPI.endTransaction();
while (0 == (TX_RFOFF_IRQ_STAT & getIRQStatus())); // wait for RF field to shut down
clearIRQStatus(TX_RFOFF_IRQ_STAT);
return true;
}
//---------------------------------------------------------------------------------------------
/*
11.4.3.1 A Host Interface Command consists of either 1 or 2 SPI frames depending whether the
host wants to write or read data from the PN5180. An SPI Frame consists of multiple
bytes.
All commands are packed into one SPI Frame. An SPI Frame consists of multiple bytes.
No NSS toggles allowed during sending of an SPI frame.
For all 4 byte command parameter transfers (e.g. register values), the payload
parameters passed follow the little endian approach (Least Significant Byte first).
Direct Instructions are built of a command code (1 Byte) and the instruction parameters
(max. 260 bytes). The actual payload size depends on the instruction used.
Responses to direct instructions contain only a payload field (no header).
All instructions are bound to conditions. If at least one of the conditions is not fulfilled, an exception is
raised. In case of an exception, the IRQ line of PN5180 is asserted and corresponding interrupt
status register contain information on the exception.
*/
/*
* A Host Interface Command consists of either 1 or 2 SPI frames depending whether the
* host wants to write or read data from the PN5180. An SPI Frame consists of multiple
* bytes.
* All commands are packed into one SPI Frame. An SPI Frame consists of multiple bytes.
* No NSS toggles allowed during sending of an SPI frame.
* For all 4 byte command parameter transfers (e.g. register values), the payload
* parameters passed follow the little endian approach (Least Significant Byte first).
* The BUSY line is used to indicate that the system is BUSY and cannot receive any data
* from a host. Recommendation for the BUSY line handling by the host:
* 1. Assert NSS to Low
* 2. Perform Data Exchange
* 3. Wait until BUSY is high
* 4. Deassert NSS
* 5. Wait until BUSY is low
* If there is a parameter error, the IRQ is set to ACTIVE and a GENERAL_ERROR_IRQ is set.
*/
bool PN5180::transceiveCommand(uint8_t *sendBuffer, size_t sendBufferLen, uint8_t *recvBuffer, size_t recvBufferLen) {
#ifdef DEBUG
PN5180DEBUG(F("Sending SPI frame: '"));
for (uint8_t i=0; i<sendBufferLen; i++) {
if (i>0) PN5180DEBUG(" ");
PN5180DEBUG(formatHex(sendBuffer[i]));
}
PN5180DEBUG("'\n");
#endif
// 0.
unsigned long startedWaiting = millis();
while (LOW != digitalRead(PN5180_BUSY)) {
if (millis() - startedWaiting > commandTimeout) return false;
}; // wait until busy is low
// 1.
digitalWrite(PN5180_NSS, LOW); delay(2);
// 2.
for (uint8_t i=0; i<sendBufferLen; i++) {
PN5180_SPI.transfer(sendBuffer[i]);
}
// 3.
startedWaiting = millis();
while (HIGH != digitalRead(PN5180_BUSY)) {
if (millis() - startedWaiting > commandTimeout) return false;
}; // wait until busy is high
// 4.
digitalWrite(PN5180_NSS, HIGH); delay(1);
// 5.
startedWaiting = millis();
while (LOW != digitalRead(PN5180_BUSY)) {
if (millis() - startedWaiting > commandTimeout) return false;
}; // wait until busy is low
// check, if write-only
//
if ((0 == recvBuffer) || (0 == recvBufferLen)) return true;
PN5180DEBUG(F("Receiving SPI frame...\n"));
// 1.
digitalWrite(PN5180_NSS, LOW); delay(2);
// 2.
for (uint8_t i=0; i<recvBufferLen; i++) {
recvBuffer[i] = PN5180_SPI.transfer(0xff);
}
// 3.
startedWaiting = millis();
while (HIGH != digitalRead(PN5180_BUSY)) {
if (millis() - startedWaiting > commandTimeout) return false;
}; // wait until busy is high
// 4.
digitalWrite(PN5180_NSS, HIGH); delay(1);
// 5.
startedWaiting = millis();
while (LOW != digitalRead(PN5180_BUSY)) {
if (millis() - startedWaiting > commandTimeout) return false;
}; // wait until busy is low
#ifdef DEBUG
PN5180DEBUG(F("Received: "));
for (uint8_t i=0; i<recvBufferLen; i++) {
if (i > 0) PN5180DEBUG(" ");
PN5180DEBUG(formatHex(recvBuffer[i]));
}
PN5180DEBUG("'\n");
#endif
return true;
}
/*
* Reset NFC device
*/
void PN5180::reset() {
digitalWrite(PN5180_RST, LOW); // at least 10us required
delay(10);
digitalWrite(PN5180_RST, HIGH); // 2ms to ramp up required
delay(10);
while (0 == (IDLE_IRQ_STAT & getIRQStatus())); // wait for system to start up
clearIRQStatus(0xffffffff); // clear all flags
}
/**
* @name getInterrupt
* @desc read interrupt status register and clear interrupt status
*/
uint32_t PN5180::getIRQStatus() {
PN5180DEBUG(F("Read IRQ-Status register...\n"));
uint32_t irqStatus;
readRegister(IRQ_STATUS, &irqStatus);
PN5180DEBUG(F("IRQ-Status=0x"));
PN5180DEBUG(formatHex(irqStatus));
PN5180DEBUG("\n");
return irqStatus;
}
bool PN5180::clearIRQStatus(uint32_t irqMask) {
PN5180DEBUG(F("Clear IRQ-Status with mask=x"));
PN5180DEBUG(formatHex(irqMask));
PN5180DEBUG("\n");
return writeRegister(IRQ_CLEAR, irqMask);
}
/*
* Get TRANSCEIVE_STATE from RF_STATUS register
*/
#ifdef DEBUG
extern void showIRQStatus(uint32_t);
#endif
PN5180TransceiveStat PN5180::getTransceiveState() {
PN5180DEBUG(F("Get Transceive state...\n"));
uint32_t rfStatus;
if (!readRegister(RF_STATUS, &rfStatus)) {
#ifdef DEBUG
showIRQStatus(getIRQStatus());
#endif
PN5180DEBUG(F("ERROR reading RF_STATUS register.\n"));
return PN5180TransceiveStat(0);
}
/*
* TRANSCEIVE_STATEs:
* 0 - idle
* 1 - wait transmit
* 2 - transmitting
* 3 - wait receive
* 4 - wait for data
* 5 - receiving
* 6 - loopback
* 7 - reserved
*/
uint8_t state = ((rfStatus >> 24) & 0x07);
PN5180DEBUG(F("TRANSCEIVE_STATE=0x"));
PN5180DEBUG(formatHex(state));
PN5180DEBUG("\n");
return PN5180TransceiveStat(state);
}