-
-
Save tuniii/d234578971733ec0824c137d29fa40af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define RFID_PN5180_STATE_INIT 0u | |
#define RFID_PN5180_NFC14443_STATE_RESET 1u | |
#define RFID_PN5180_NFC14443_STATE_SETUPRF 2u | |
#define RFID_PN5180_NFC14443_STATE_READCARD 3u | |
#define RFID_PN5180_NFC15693_STATE_RESET 4u | |
#define RFID_PN5180_NFC15693_STATE_SETUPRF 5u | |
#define RFID_PN5180_NFC15693_STATE_DISABLEPRIVACYMODE 6u | |
#define RFID_PN5180_NFC15693_STATE_GETINVENTORY 7u | |
// Instructs RFID-scanner to scan for new RFID-tags using PN5180 | |
void rfidScanner(void *parameter) { | |
static PN5180ISO14443 nfc14443(RFID_CS, RFID_BUSY, RFID_RST); | |
static PN5180ISO15693 nfc15693(RFID_CS, RFID_BUSY, RFID_RST); | |
static uint8_t stateMachine = RFID_PN5180_STATE_INIT; | |
uint32_t stateMachineStart = millis(); | |
uint32_t stateMachineRuntime; | |
byte cardId[cardIdSize], lastCardId[cardIdSize]; | |
char *cardIdString; | |
uint8_t uid[10]; | |
if(RFID_PN5180_STATE_INIT == stateMachine) { | |
nfc14443.begin(); | |
nfc14443.reset(); | |
// show PN5180 reader version | |
uint8_t firmwareVersion[2]; | |
nfc14443.readEEprom(FIRMWARE_VERSION, firmwareVersion, sizeof(firmwareVersion)); | |
Serial.print(F("Firmware version=")); | |
Serial.print(firmwareVersion[1]); | |
Serial.print("."); | |
Serial.println(firmwareVersion[0]); | |
// activate RF field | |
delay(4); | |
loggerNl(serialDebug, (char *) FPSTR(rfidScannerReady), LOGLEVEL_DEBUG); | |
} | |
// 1. check for an ISO-14443 card | |
else if(RFID_PN5180_NFC14443_STATE_RESET == stateMachine) { | |
nfc14443.reset(); | |
} | |
else if(RFID_PN5180_NFC14443_STATE_SETUPRF == stateMachine) { | |
nfc14443.setupRF(); | |
} | |
else if(RFID_PN5180_NFC14443_STATE_READCARD == stateMachine) { | |
if (nfc14443.readCardSerial(uid) >= 4u) { | |
if (psramInit()) { | |
cardIdString = (char *) ps_malloc(cardIdSize*3 +1); | |
} else { | |
cardIdString = (char *) malloc(cardIdSize*3 +1); | |
} | |
if (cardIdString == NULL) { | |
logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR); | |
#ifdef NEOPIXEL_ENABLE | |
showLedError = true; | |
#endif | |
return; | |
} | |
for (uint8_t i=0; i<cardIdSize; i++) | |
cardId[i] = uid[i]; | |
// check for different card id | |
if ( memcmp( (const void *)cardId, (const void *)lastCardId, sizeof(cardId)) == 0) | |
return; | |
memcpy(lastCardId, cardId, sizeof(cardId)); | |
uint8_t n = 0; | |
logger(serialDebug, (char *) FPSTR(rfidTagDetected), LOGLEVEL_NOTICE); | |
for (uint8_t i=0; i<cardIdSize; i++) { | |
snprintf(logBuf, serialLoglength, "%02x", cardId[i]); | |
logger(serialDebug, logBuf, LOGLEVEL_NOTICE); | |
n += snprintf (&cardIdString[n], sizeof(cardIdString) / sizeof(cardIdString[0]), "%03d", cardId[i]); | |
if (i<(cardIdSize-1)) { | |
logger(serialDebug, "-", LOGLEVEL_NOTICE); | |
} else { | |
logger(serialDebug, "\n", LOGLEVEL_NOTICE); | |
} | |
} | |
xQueueSend(rfidCardQueue, &cardIdString, 0); | |
} | |
} | |
// 2. check for an ISO-15693 card | |
else if(RFID_PN5180_NFC15693_STATE_RESET == stateMachine) { | |
nfc15693.reset(); | |
} | |
else if(RFID_PN5180_NFC15693_STATE_SETUPRF == stateMachine) { | |
nfc15693.setupRF(); | |
} | |
else if(RFID_PN5180_NFC15693_STATE_DISABLEPRIVACYMODE == stateMachine) { | |
// check for ICODE-SLIX2 password protected tag | |
// put your privacy password here, e.g.: | |
// https://de.ifixit.com/Antworten/Ansehen/513422/nfc+Chips+f%C3%BCr+tonies+kaufen | |
uint8_t password[] = {0x01, 0x02, 0x03, 0x04}; | |
ISO15693ErrorCode myrc = nfc15693.disablePrivacyMode(password); | |
if (ISO15693_EC_OK == myrc) { | |
Serial.println(F("disabling privacy-mode successful")); | |
} | |
} | |
else if(RFID_PN5180_NFC15693_STATE_GETINVENTORY == stateMachine) { | |
// try to read ISO15693 inventory | |
ISO15693ErrorCode rc = nfc15693.getInventory(uid); | |
if (rc == ISO15693_EC_OK) { | |
if (psramInit()) { | |
cardIdString = (char *) ps_malloc(cardIdSize*3 +1); | |
} else { | |
cardIdString = (char *) malloc(cardIdSize*3 +1); | |
} | |
if (cardIdString == NULL) { | |
logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR); | |
#ifdef NEOPIXEL_ENABLE | |
showLedError = true; | |
#endif | |
return; | |
} | |
for (uint8_t i=0; i<cardIdSize; i++) | |
cardId[i] = uid[i]; | |
// check for different card id | |
if ( memcmp( (const void *)cardId, (const void *)lastCardId, sizeof(cardId)) == 0) | |
return; | |
memcpy(lastCardId, cardId, sizeof(cardId)); | |
uint8_t n = 0; | |
logger(serialDebug, (char *) FPSTR(rfid15693TagDetected), LOGLEVEL_NOTICE); | |
for (uint8_t i=0; i<cardIdSize; i++) { | |
snprintf(logBuf, serialLoglength, "%02x", cardId[i]); | |
logger(serialDebug, logBuf, LOGLEVEL_NOTICE); | |
n += snprintf (&cardIdString[n], sizeof(cardIdString) / sizeof(cardIdString[0]), "%03d", cardId[i]); | |
if (i<(cardIdSize-1)) { | |
logger(serialDebug, "-", LOGLEVEL_NOTICE); | |
} else { | |
logger(serialDebug, "\n", LOGLEVEL_NOTICE); | |
} | |
} | |
xQueueSend(rfidCardQueue, &cardIdString, 0); | |
} | |
} | |
stateMachineRuntime = millis() - stateMachineStart; | |
Serial.printf("rfidScanner: state %d, time = %d\n", stateMachine, stateMachineRuntime); | |
stateMachine++; | |
if(stateMachine > RFID_PN5180_NFC15693_STATE_GETINVENTORY) | |
{ | |
stateMachine = RFID_PN5180_NFC14443_STATE_RESET; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rfidScanner: state 1, time = 12
rfidScanner: state 2, time = 16
rfidScanner: state 3, time = 96
rfidScanner: state 4, time = 12
rfidScanner: state 5, time = 22
rfidScanner: state 6, time = 31
rfidScanner: state 7, time = 31
rfidScanner: state 1, time = 12
rfidScanner: state 2, time = 16
rfidScanner: state 3, time = 123
rfidScanner: state 4, time = 12
rfidScanner: state 5, time = 22
rfidScanner: state 6, time = 31
rfidScanner: state 7, time = 31