Skip to content

Commit

Permalink
refactor: card mode receive
Browse files Browse the repository at this point in the history
  • Loading branch information
DeimosHall committed Aug 7, 2023
1 parent 7fdfb74 commit 47b270a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 93 deletions.
2 changes: 1 addition & 1 deletion examples/DetectingReaders/DetectingReaders.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/NDEFSend/NDEFSend.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
183 changes: 96 additions & 87 deletions src/Electroniccats_PN7150.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
9 changes: 5 additions & 4 deletions src/Electroniccats_PN7150.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 47b270a

Please sign in to comment.