From 47b270aa965ea7e6a96957eaacd46bb2320046f7 Mon Sep 17 00:00:00 2001 From: deimos Date: Mon, 7 Aug 2023 15:46:43 -0600 Subject: [PATCH] refactor: card mode receive --- .../DetectingReaders/DetectingReaders.ino | 2 +- examples/NDEFSend/NDEFSend.ino | 2 +- src/Electroniccats_PN7150.cpp | 183 +++++++++--------- src/Electroniccats_PN7150.h | 9 +- 4 files changed, 103 insertions(+), 93 deletions(-) diff --git a/examples/DetectingReaders/DetectingReaders.ino b/examples/DetectingReaders/DetectingReaders.ino index 7f7d889..f4f076c 100644 --- a/examples/DetectingReaders/DetectingReaders.ino +++ b/examples/DetectingReaders/DetectingReaders.ino @@ -48,7 +48,7 @@ void setup(){ } void loop(){ - if(nfc.CardModeReceive(Cmd, &CmdSize) == 0) { //Data in buffer? + if(nfc.cardModeReceive(Cmd, &CmdSize) == 0) { //Data in buffer? if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { //Expect at least two bytes switch (Cmd[1]) { case 0xA4: //Something tries to select a file, meaning that it is a reader diff --git a/examples/NDEFSend/NDEFSend.ino b/examples/NDEFSend/NDEFSend.ino index e88bfcc..a48d11d 100644 --- a/examples/NDEFSend/NDEFSend.ino +++ b/examples/NDEFSend/NDEFSend.ino @@ -81,7 +81,7 @@ void loop() { void checkReaders() { Serial.print("."); - if (nfc.CardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? + if (nfc.cardModeReceive(Cmd, &CmdSize) == 0) { // Data in buffer? if ((CmdSize >= 2) && (Cmd[0] == 0x00)) { // Expect at least two bytes if (Cmd[1] == 0xA4) { Serial.println("\nReader detected!"); diff --git a/src/Electroniccats_PN7150.cpp b/src/Electroniccats_PN7150.cpp index 6f93604..e1c2faf 100644 --- a/src/Electroniccats_PN7150.cpp +++ b/src/Electroniccats_PN7150.cpp @@ -69,6 +69,28 @@ uint8_t Electroniccats_PN7150::begin() { return SUCCESS; } +bool Electroniccats_PN7150::isTimeOut() const { + return ((millis() - timeOutStartTime) >= timeOut); +} + +void Electroniccats_PN7150::setTimeOut(unsigned long theTimeOut) { + timeOutStartTime = millis(); + timeOut = theTimeOut; +} + +bool Electroniccats_PN7150::getMessage(uint16_t timeout) { // check for message using timeout, 5 milisec as default + setTimeOut(timeout); + rxMessageLength = 0; + while (!isTimeOut()) { + rxMessageLength = readData(rxBuffer); + if (rxMessageLength) + break; + else if (timeout == 1337) + setTimeOut(timeout); + } + return rxMessageLength; +} + bool Electroniccats_PN7150::hasMessage() const { return (HIGH == digitalRead(_IRQpin)); // PN7150 indicates it has data by driving IRQ signal HIGH } @@ -131,96 +153,11 @@ uint32_t Electroniccats_PN7150::readData(uint8_t rxBuffer[]) const { return bytesReceived; } -bool Electroniccats_PN7150::isTimeOut() const { - return ((millis() - timeOutStartTime) >= timeOut); -} - -void Electroniccats_PN7150::setTimeOut(unsigned long theTimeOut) { - timeOutStartTime = millis(); - timeOut = theTimeOut; -} - -bool Electroniccats_PN7150::getMessage(uint16_t timeout) { // check for message using timeout, 5 milisec as default - setTimeOut(timeout); - rxMessageLength = 0; - while (!isTimeOut()) { - rxMessageLength = readData(rxBuffer); - if (rxMessageLength) - break; - else if (timeout == 1337) - setTimeOut(timeout); - } - return rxMessageLength; -} - -uint8_t Electroniccats_PN7150::wakeupNCI() { // the device has to wake up using a core reset - uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x01}; - uint16_t NbBytes = 0; - - // Reset RF settings restauration flag - (void)writeData(NCICoreReset, 4); - getMessage(15); - NbBytes = rxMessageLength; - if ((NbBytes == 0) || (rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00)) { - return ERROR; - } - getMessage(); - NbBytes = rxMessageLength; - if (NbBytes != 0) { - // NCI_PRINT_BUF("NCI << ", Answer, NbBytes); - // Is CORE_GENERIC_ERROR_NTF ? - if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)) { - /* Is PN7150B0HN/C11004 Anti-tearing recovery procedure triggered ? */ - // if ((rxBuffer[3] == 0xE6)) gRfSettingsRestored_flag = true; - } else { - return ERROR; - } - } - return SUCCESS; -} - -uint8_t Electroniccats_PN7150::connectNCI() { - uint8_t i = 2; - uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; - - // Open connection to NXPNCI - begin(); - // Loop until NXPNCI answers - while (wakeupNCI() != SUCCESS) { - if (i-- == 0) - return ERROR; - delay(500); - } - - (void)writeData(NCICoreInit, sizeof(NCICoreInit)); - getMessage(); - if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) - return ERROR; - - // Retrieve NXP-NCI NFC Controller generation - if (rxBuffer[17 + rxBuffer[8]] == 0x08) - gNfcController_generation = 1; - else if (rxBuffer[17 + rxBuffer[8]] == 0x10) - gNfcController_generation = 2; - - // Retrieve NXP-NCI NFC Controller FW version - gNfcController_fw_version[0] = rxBuffer[17 + rxBuffer[8]]; // 0xROM_CODE_V - gNfcController_fw_version[1] = rxBuffer[18 + rxBuffer[8]]; // 0xFW_MAJOR_NO - gNfcController_fw_version[2] = rxBuffer[19 + rxBuffer[8]]; // 0xFW_MINOR_NO -#ifdef DEBUG - Serial.println("0xROM_CODE_V: " + String(gNfcController_fw_version[0], HEX)); - Serial.println("FW_MAJOR_NO: " + String(gNfcController_fw_version[1], HEX)); - Serial.println("0xFW_MINOR_NO: " + String(gNfcController_fw_version[2], HEX)); - Serial.println("gNfcController_generation: " + String(gNfcController_generation, HEX)); -#endif - - return SUCCESS; -} - int Electroniccats_PN7150::getFirmwareVersion() { return ((gNfcController_fw_version[0] & 0xFF) << 16) | ((gNfcController_fw_version[1] & 0xFF) << 8) | (gNfcController_fw_version[2] & 0xFF); } +// Deprecated, use getFirmwareVersion() instead int Electroniccats_PN7150::GetFwVersion() { return getFirmwareVersion(); } @@ -325,6 +262,7 @@ uint8_t Electroniccats_PN7150::configMode(uint8_t modeSE) { return SUCCESS; } +// Deprecated, use configMode() instead uint8_t Electroniccats_PN7150::ConfigMode(uint8_t modeSE) { return Electroniccats_PN7150::configMode(modeSE); } @@ -355,10 +293,75 @@ uint8_t Electroniccats_PN7150::startDiscovery(uint8_t modeSE) { return SUCCESS; } +// Deprecated, use startDiscovery() instead uint8_t Electroniccats_PN7150::StartDiscovery(uint8_t modeSE) { return Electroniccats_PN7150::startDiscovery(modeSE); } +uint8_t Electroniccats_PN7150::connectNCI() { + uint8_t i = 2; + uint8_t NCICoreInit[] = {0x20, 0x01, 0x00}; + + // Open connection to NXPNCI + begin(); + // Loop until NXPNCI answers + while (wakeupNCI() != SUCCESS) { + if (i-- == 0) + return ERROR; + delay(500); + } + + (void)writeData(NCICoreInit, sizeof(NCICoreInit)); + getMessage(); + if ((rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x01) || (rxBuffer[3] != 0x00)) + return ERROR; + + // Retrieve NXP-NCI NFC Controller generation + if (rxBuffer[17 + rxBuffer[8]] == 0x08) + gNfcController_generation = 1; + else if (rxBuffer[17 + rxBuffer[8]] == 0x10) + gNfcController_generation = 2; + + // Retrieve NXP-NCI NFC Controller FW version + gNfcController_fw_version[0] = rxBuffer[17 + rxBuffer[8]]; // 0xROM_CODE_V + gNfcController_fw_version[1] = rxBuffer[18 + rxBuffer[8]]; // 0xFW_MAJOR_NO + gNfcController_fw_version[2] = rxBuffer[19 + rxBuffer[8]]; // 0xFW_MINOR_NO +#ifdef DEBUG + Serial.println("0xROM_CODE_V: " + String(gNfcController_fw_version[0], HEX)); + Serial.println("FW_MAJOR_NO: " + String(gNfcController_fw_version[1], HEX)); + Serial.println("0xFW_MINOR_NO: " + String(gNfcController_fw_version[2], HEX)); + Serial.println("gNfcController_generation: " + String(gNfcController_generation, HEX)); +#endif + + return SUCCESS; +} + +uint8_t Electroniccats_PN7150::wakeupNCI() { // the device has to wake up using a core reset + uint8_t NCICoreReset[] = {0x20, 0x00, 0x01, 0x01}; + uint16_t NbBytes = 0; + + // Reset RF settings restauration flag + (void)writeData(NCICoreReset, 4); + getMessage(15); + NbBytes = rxMessageLength; + if ((NbBytes == 0) || (rxBuffer[0] != 0x40) || (rxBuffer[1] != 0x00)) { + return ERROR; + } + getMessage(); + NbBytes = rxMessageLength; + if (NbBytes != 0) { + // NCI_PRINT_BUF("NCI << ", Answer, NbBytes); + // Is CORE_GENERIC_ERROR_NTF ? + if ((rxBuffer[0] == 0x60) && (rxBuffer[1] == 0x07)) { + /* Is PN7150B0HN/C11004 Anti-tearing recovery procedure triggered ? */ + // if ((rxBuffer[3] == 0xE6)) gRfSettingsRestored_flag = true; + } else { + return ERROR; + } + } + return SUCCESS; +} + void Electroniccats_PN7150::ProcessCardMode(RfIntf_t RfIntf) { uint8_t Answer[MAX_NCI_FRAME_SIZE]; @@ -428,11 +431,12 @@ bool Electroniccats_PN7150::cardModeSend(unsigned char *pData, unsigned char Dat return status; } +// Deprecated, use cardModeSend() instead bool Electroniccats_PN7150::CardModeSend(unsigned char *pData, unsigned char DataSize) { return Electroniccats_PN7150::cardModeSend(pData, DataSize); } -bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) { +bool Electroniccats_PN7150::cardModeReceive(unsigned char *pData, unsigned char *pDataSize) { #ifdef DEBUG2 Serial.println("[DEBUG] cardModeReceive exec"); #endif @@ -460,6 +464,11 @@ bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char return status; } +// Deprecated, use cardModeReceive() instead +bool Electroniccats_PN7150::CardModeReceive(unsigned char *pData, unsigned char *pDataSize) { + return Electroniccats_PN7150::cardModeReceive(pData, pDataSize); +} + void Electroniccats_PN7150::FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf) { uint8_t i, temp; diff --git a/src/Electroniccats_PN7150.h b/src/Electroniccats_PN7150.h index 2d8812d..3707ad1 100644 --- a/src/Electroniccats_PN7150.h +++ b/src/Electroniccats_PN7150.h @@ -253,12 +253,12 @@ class Electroniccats_PN7150 { public: Electroniccats_PN7150(uint8_t IRQpin, uint8_t VENpin, uint8_t I2Caddress, TwoWire *wire = &Wire); - int getFirmwareVersion(); - int GetFwVersion(); // Deprecated, use getFirmwareVersion() instead uint8_t begin(void); + bool hasMessage() const; uint8_t writeData(uint8_t data[], uint32_t dataLength) const; // write data from DeviceHost to PN7150. Returns success (0) or Fail (> 0) uint32_t readData(uint8_t data[]) const; // read data from PN7150, returns the amount of bytes read - bool hasMessage() const; + int getFirmwareVersion(); + int GetFwVersion(); // Deprecated, use getFirmwareVersion() instead uint8_t configMode(uint8_t modeSE); uint8_t ConfigMode(uint8_t modeSE); // Deprecated, use configMode() instead uint8_t startDiscovery(uint8_t modeSE); @@ -267,7 +267,8 @@ class Electroniccats_PN7150 { uint8_t wakeupNCI(); bool cardModeSend(unsigned char *pData, unsigned char DataSize); bool CardModeSend(unsigned char *pData, unsigned char DataSize); // Deprecated, use cardModeSend() instead - bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); + bool cardModeReceive(unsigned char *pData, unsigned char *pDataSize); + bool CardModeReceive(unsigned char *pData, unsigned char *pDataSize); // Deprecated, use cardModeReceive() instead bool WaitForDiscoveryNotification(RfIntf_t *pRfIntf, uint8_t tout = 0); void FillInterfaceInfo(RfIntf_t *pRfIntf, uint8_t *pBuf); bool ReaderTagCmd(unsigned char *pCommand, unsigned char CommandSize, unsigned char *pAnswer, unsigned char *pAnswerSize);