Skip to content

Commit

Permalink
feat: remove duplicated wait for discovery notification
Browse files Browse the repository at this point in the history
  • Loading branch information
DeimosHall committed Aug 31, 2023
1 parent fbc884c commit bdf460a
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 215 deletions.
172 changes: 89 additions & 83 deletions examples/ISO14443-3A_read_block/ISO14443-3A_read_block.ino
Original file line number Diff line number Diff line change
@@ -1,135 +1,141 @@
/**
* Example to read a ISO14443-3A(Tag Type 2 - T2T) block 5 and show its information
* Authors:
* Example to read a ISO14443-3A(Tag Type 2 - T2T) block 5 and show its information
* Authors:
* Salvador Mendoza - @Netxing - salmg.net
* For Electronic Cats - electroniccats.com
*
*
* November 2020
*
* This code is beerware; if you see me (or any other collaborator
* member) at the local, and you've found our code helpful,
*
* This code is beerware; if you see me (or any other collaborator
* member) at the local, and you've found our code helpful,
* please buy us a round!
* Distributed as-is; no warranty is given.
*/

#include "Electroniccats_PN7150.h"
#define PN7150_IRQ (15)
#define PN7150_VEN (14)
#define PN7150_ADDR (0x28)

#define BLK_NB_ISO14443_3A (5) //Block to be read it
#include "Electroniccats_PN7150.h"
#define PN7150_IRQ (15)
#define PN7150_VEN (14)
#define PN7150_ADDR (0x28)

#define BLK_NB_ISO14443_3A (5) // Block to be read it

Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28
RfIntf_t RfInterface; //Intarface to save data for multiple tags
Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR); // creates a global NFC device interface object, attached to pins 7 (IRQ) and 8 (VEN) and using the default I2C address 0x28
RfIntf_t RfInterface; // Intarface to save data for multiple tags

uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation
uint8_t mode = 1; // modes: 1 = Reader/ Writer, 2 = Emulation

void ResetMode(){ //Reset the configuration mode after each reading
void ResetMode() { // Reset the configuration mode after each reading
Serial.println("Re-initializing...");
nfc.configMode(mode);
nfc.ConfigMode(mode);
nfc.StartDiscovery(mode);
}

void PrintBuf(const byte * data, const uint32_t numBytes){ //Print hex data buffer in format
void PrintBuf(const byte* data, const uint32_t numBytes) { // Print hex data buffer in format
uint32_t szPos;
for (szPos=0; szPos < numBytes; szPos++){
for (szPos = 0; szPos < numBytes; szPos++) {
Serial.print(F("0x"));
// Append leading 0 for small values
if (data[szPos] <= 0xF)
Serial.print(F("0"));
Serial.print(data[szPos]&0xff, HEX);
Serial.print(data[szPos] & 0xff, HEX);
if ((numBytes > 1) && (szPos != numBytes - 1))
Serial.print(F(" "));
}
Serial.println();
}

void PCD_ISO14443_3A_scenario (void){
bool status;
unsigned char Resp[256];
unsigned char RespSize;
/* Read block */
unsigned char ReadBlock[] = {0x30, BLK_NB_ISO14443_3A};
status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize);
if((status == NFC_ERROR) || (Resp[RespSize-1] != 0x00)){
Serial.print("Error reading block: ");
Serial.print(ReadBlock[1],HEX);
Serial.print(" with error: ");
Serial.print(Resp[RespSize-1],HEX);
return;
}
Serial.print("------------------------Block ");
Serial.print(BLK_NB_ISO14443_3A, HEX);
Serial.println("-------------------------");
PrintBuf(Resp, 4);
void PCD_ISO14443_3A_scenario(void) {
bool status;
unsigned char Resp[256];
unsigned char RespSize;
/* Read block */
unsigned char ReadBlock[] = {0x30, BLK_NB_ISO14443_3A};

status = nfc.readerTagCmd(ReadBlock, sizeof(ReadBlock), Resp, &RespSize);
if ((status == NFC_ERROR) || (Resp[RespSize - 1] != 0x00)) {
Serial.print("Error reading block: ");
Serial.print(ReadBlock[1], HEX);
Serial.print(" with error: ");
Serial.print(Resp[RespSize - 1], HEX);
return;
}
Serial.print("------------------------Block ");
Serial.print(BLK_NB_ISO14443_3A, HEX);
Serial.println("-------------------------");
PrintBuf(Resp, 4);
}

void setup(){
void setup() {
Serial.begin(9600);
while(!Serial);
while (!Serial)
;
Serial.println("Read ISO14443-3A(T2T) data block 5 with PN7150");
Serial.println("Initializing...");
if (nfc.connectNCI()) { //Wake up the board

Serial.println("Initializing...");
if (nfc.connectNCI()) { // Wake up the board
Serial.println("Error while setting up the mode, check connections!");
while (1);
while (1)
;
}

if (nfc.configureSettings()) {
Serial.println("The Configure Settings failed!");
while (1);
while (1)
;
}
if(nfc.configMode(mode)){ //Set up the configuration mode

if (nfc.ConfigMode(mode)) { // Set up the configuration mode
Serial.println("The Configure Mode failed!!");
while (1);
while (1)
;
}
nfc.StartDiscovery(mode); //NCI Discovery mode
nfc.StartDiscovery(mode); // NCI Discovery mode
Serial.println("Waiting for an ISO14443-3A Card ...");
}

void loop(){
if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards
switch(RfInterface.Protocol) {
void loop() {
if (!nfc.WaitForDiscoveryNotification(&RfInterface)) { // Waiting to detect cards
switch (RfInterface.Protocol) {
case PROT_T2T:
Serial.println(" - Found ISO14443-3A(T2T) card");
switch(RfInterface.ModeTech) { //Indetify card technology
case (MODE_POLL | TECH_PASSIVE_NFCA):
char tmp[16];
Serial.print("\tSENS_RES = ");
sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[0]);
Serial.print(tmp); Serial.print(" ");
sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SensRes[1]);
Serial.print(tmp); Serial.println(" ");

Serial.print("\tNFCID = ");
PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen);

if(RfInterface.Info.NFC_APP.SelResLen != 0) {
Serial.print("\tSEL_RES = ");
sprintf(tmp, "0x%.2X",RfInterface.Info.NFC_APP.SelRes[0]);
Serial.print(tmp); Serial.println(" ");
}
switch (RfInterface.ModeTech) { // Indetify card technology
case (MODE_POLL | TECH_PASSIVE_NFCA):
char tmp[16];
Serial.print("\tSENS_RES = ");
sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[0]);
Serial.print(tmp);
Serial.print(" ");
sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SensRes[1]);
Serial.print(tmp);
Serial.println(" ");

Serial.print("\tNFCID = ");
PrintBuf(RfInterface.Info.NFC_APP.NfcId, RfInterface.Info.NFC_APP.NfcIdLen);

if (RfInterface.Info.NFC_APP.SelResLen != 0) {
Serial.print("\tSEL_RES = ");
sprintf(tmp, "0x%.2X", RfInterface.Info.NFC_APP.SelRes[0]);
Serial.print(tmp);
Serial.println(" ");
}
break;
}
PCD_ISO14443_3A_scenario();
break;
}
PCD_ISO14443_3A_scenario();
break;

default:
Serial.println(" - Found a card, but it is not ISO14443-3A(T2T)!");
break;
Serial.println(" - Found a card, but it is not ISO14443-3A(T2T)!");
break;
}
//* It can detect multiple cards at the same time if they use the same protocol
if(RfInterface.MoreTags) {
nfc.readerActivateNext(&RfInterface);

//* It can detect multiple cards at the same time if they use the same protocol
if (RfInterface.MoreTags) {
nfc.readerActivateNext(&RfInterface);
}
//* Wait for card removal
//* Wait for card removal
nfc.processReaderMode(RfInterface, PRESENCE_CHECK);
Serial.println("CARD REMOVED!");

nfc.stopDiscovery();
nfc.StartDiscovery(mode);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/ISO14443-3A_write_block/ISO14443-3A_write_block.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ uint8_t mode = 1; // modes: 1 =

void ResetMode(){ //Reset the configuration mode after each reading
Serial.println("Re-initializing...");
nfc.configMode(mode);
nfc.ConfigMode(mode);
nfc.StartDiscovery(mode);
}

Expand Down Expand Up @@ -98,7 +98,7 @@ void setup(){
while (1);
}

if(nfc.configMode(mode)){ //Set up the configuration mode
if(nfc.ConfigMode(mode)){ //Set up the configuration mode
Serial.println("The Configure Mode failed!!");
while (1);
}
Expand All @@ -107,7 +107,7 @@ void setup(){
}

void loop(){
if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards
if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards
switch(RfInterface.Protocol) {
case PROT_T2T:
Serial.println(" - Found ISO14443-3A(T2T) card");
Expand Down
6 changes: 3 additions & 3 deletions examples/ISO15693_read_block/ISO15693_read_block.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ uint8_t mode = 1; // modes: 1 =

void ResetMode(){ //Reset the configuration mode after each reading
Serial.println("Re-initializing...");
nfc.configMode(mode);
nfc.ConfigMode(mode);
nfc.StartDiscovery(mode);
}

Expand Down Expand Up @@ -83,7 +83,7 @@ void setup(){
while (1);
}

if(nfc.configMode(mode)){ //Set up the configuration mode
if(nfc.ConfigMode(mode)){ //Set up the configuration mode
Serial.println("The Configure Mode is failed!!");
while (1);
}
Expand All @@ -92,7 +92,7 @@ void setup(){
}

void loop(){
if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards
if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards
switch(RfInterface.Protocol) {
case PROT_ISO15693:
Serial.println(" - Found ISO15693 card");
Expand Down
6 changes: 3 additions & 3 deletions examples/ISO15693_write_block/ISO15693_write_block.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void setup(){
while (1);
}

if(nfc.configMode(mode)){ //Set up the configuration mode
if(nfc.ConfigMode(mode)){ //Set up the configuration mode
Serial.println("The Configure Mode is failed!!");
while (1);
}
Expand All @@ -51,7 +51,7 @@ void setup(){

void ResetMode(){ //Reset the configuration mode after each reading
Serial.println("Re-initializing...");
nfc.configMode(mode);
nfc.ConfigMode(mode);
nfc.StartDiscovery(mode);
}

Expand Down Expand Up @@ -105,7 +105,7 @@ void PCD_ISO15693_scenario (void){
}

void loop(){
if(!nfc.waitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards
if(!nfc.WaitForDiscoveryNotification(&RfInterface)){ // Waiting to detect cards
switch(RfInterface.Protocol) {
case PROT_ISO15693:
Serial.println(" - Found ISO15693 card");
Expand Down
Loading

0 comments on commit bdf460a

Please sign in to comment.