Skip to main content
added syntax highlight
Source Link
Bence Kaulics
  • 7.8k
  • 8
  • 42
  • 90
/* Made these as static global variables */
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)



// Reads cards, beep and long light flash for good card read, short light flash for no card read, lights mostly on for NFC chip not talking right
void TestNFC_ReadTag (void) {
    bool success;
    static flag = 0;

    /************************************ Everytime Try Reading a Card ***********************************/
    // Check for a good card read
    success = PN532_readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 1000);
    
    if (success) {

        if(flag == 0){
           testPN532WritePage();
           flag = 1;
        }
        else{
           testPN532ReadPage();
           flag = 0;
     }

        LIGHT_ON();
        EventTimerCreate(200,1,TestingLightsOff);
        for (int nfcBeep = 0; nfcBeep < 300; nfcBeep++) {
            DACB.CH0DATA = 0x700;
            _delay_us(200);
            DACB.CH0DATA = 0x900;
            _delay_us(200);
        }
        DACB.CH0DATA = 0x800;
        _delay_ms(500);         // Reduced from 1000ms to 500ms to avoid WDT reset - Vinay
    }
    // If Invalid card read then short light flash no beep
    else {
        LIGHT_ON();
        _delay_ms(10);
        LIGHT_OFF();
    }
}

void TestNFCInterrupt_ReadTag (void){

    /* Read the tag and write/read to NTAG213 user memory */
    TestNFC_ReadTag();

    /* Needs to restart the scan to detect the NTAG passively */
    PN532_StartScan();
}
/* Made these as static global variables */
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)



// Reads cards, beep and long light flash for good card read, short light flash for no card read, lights mostly on for NFC chip not talking right
void TestNFC_ReadTag (void) {
    bool success;
    static flag = 0;

    /************************************ Everytime Try Reading a Card ***********************************/
    // Check for a good card read
    success = PN532_readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 1000);
    
    if (success) {

        if(flag == 0){
           testPN532WritePage();
           flag = 1;
        }
        else{
           testPN532ReadPage();
           flag = 0;
     }

        LIGHT_ON();
        EventTimerCreate(200,1,TestingLightsOff);
        for (int nfcBeep = 0; nfcBeep < 300; nfcBeep++) {
            DACB.CH0DATA = 0x700;
            _delay_us(200);
            DACB.CH0DATA = 0x900;
            _delay_us(200);
        }
        DACB.CH0DATA = 0x800;
        _delay_ms(500);         // Reduced from 1000ms to 500ms to avoid WDT reset - Vinay
    }
    // If Invalid card read then short light flash no beep
    else {
        LIGHT_ON();
        _delay_ms(10);
        LIGHT_OFF();
    }
}

void TestNFCInterrupt_ReadTag (void){

    /* Read the tag and write/read to NTAG213 user memory */
    TestNFC_ReadTag();

    /* Needs to restart the scan to detect the NTAG passively */
    PN532_StartScan();
}
/* Made these as static global variables */
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)



// Reads cards, beep and long light flash for good card read, short light flash for no card read, lights mostly on for NFC chip not talking right
void TestNFC_ReadTag (void) {
    bool success;
    static flag = 0;

    /************************************ Everytime Try Reading a Card ***********************************/
    // Check for a good card read
    success = PN532_readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 1000);
    
    if (success) {

        if(flag == 0){
           testPN532WritePage();
           flag = 1;
        }
        else{
           testPN532ReadPage();
           flag = 0;
     }

        LIGHT_ON();
        EventTimerCreate(200,1,TestingLightsOff);
        for (int nfcBeep = 0; nfcBeep < 300; nfcBeep++) {
            DACB.CH0DATA = 0x700;
            _delay_us(200);
            DACB.CH0DATA = 0x900;
            _delay_us(200);
        }
        DACB.CH0DATA = 0x800;
        _delay_ms(500);         // Reduced from 1000ms to 500ms to avoid WDT reset - Vinay
    }
    // If Invalid card read then short light flash no beep
    else {
        LIGHT_ON();
        _delay_ms(10);
        LIGHT_OFF();
    }
}

void TestNFCInterrupt_ReadTag (void){

    /* Read the tag and write/read to NTAG213 user memory */
    TestNFC_ReadTag();

    /* Needs to restart the scan to detect the NTAG passively */
    PN532_StartScan();
}
/* Made these as static global variables */
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)



// Reads cards, beep and long light flash for good card read, short light flash for no card read, lights mostly on for NFC chip not talking right
void TestNFC_ReadTag (void) {
    bool success;
    static flag = 0;

    /************************************ Everytime Try Reading a Card ***********************************/
    // Check for a good card read
    success = PN532_readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 1000);
    
    if (success) {

        if(flag == 0){
           testPN532WritePage();
           flag = 1;
        }
        else{
           testPN532ReadPage();
           flag = 0;
     }

        LIGHT_ON();
        EventTimerCreate(200,1,TestingLightsOff);
        for (int nfcBeep = 0; nfcBeep < 300; nfcBeep++) {
            DACB.CH0DATA = 0x700;
            _delay_us(200);
            DACB.CH0DATA = 0x900;
            _delay_us(200);
        }
        DACB.CH0DATA = 0x800;
        _delay_ms(500);         // Reduced from 1000ms to 500ms to avoid WDT reset - Vinay
    }
    // If Invalid card read then short light flash no beep
    else {
        LIGHT_ON();
        _delay_ms(10);
        LIGHT_OFF();
    }
}

void TestNFCInterrupt_ReadTag (void){

    /* Read the tag and write/read to NTAG213 user memory */
    TestNFC_ReadTag();

    /* Needs to restart the scan to detect the NTAG passively */
    PN532_StartScan();
}
deleted 4 characters in body
Source Link

After a while of debugging and going the through the code, I got to know that the problem was happening due to the Audio beeps on detection/reading a new card within the void TestNFC_ReadTag (void) function which was causing some intermediate delay between the time the UUID was read and the subsequent call to the read/write page API. So to write to or read from the NTAGs user memory, it is always necessary to issue the INDATAEXCHANGE command and invoke the read/write page immediately after the tag is read. So, I made some slight changes to the void TestNFC_ReadTag (void) function to meet this requirement and it works. The below is the changes I made:

After a while of debugging and going the through the code, I got to know that the problem was happening due to the Audio beeps on detection/reading a new card within the void TestNFC_ReadTag (void) function which was causing some intermediate delay between the time the UUID was read and the subsequent call to the read/write page API. So to write to or read from the NTAGs user memory, it is always necessary to issue the INDATAEXCHANGE command and invoke the read/write page immediately after the tag is read. So, I made some slight changes to the void TestNFC_ReadTag (void) function to meet this requirement and it works. The below is the changes I made:

After a while of debugging and going through the code, I got to know that the problem was happening due to the Audio beeps on detection/reading a new card within the void TestNFC_ReadTag (void) function which was causing some intermediate delay between the time the UUID was read and the subsequent call to the read/write page API. So to write to or read from the NTAGs user memory, it is always necessary to issue the INDATAEXCHANGE command and invoke the read/write page immediately after the tag is read. So, I made some slight changes to the void TestNFC_ReadTag (void) function to meet this requirement and it works. The below is the changes I made:

Source Link

After a while of debugging and going the through the code, I got to know that the problem was happening due to the Audio beeps on detection/reading a new card within the void TestNFC_ReadTag (void) function which was causing some intermediate delay between the time the UUID was read and the subsequent call to the read/write page API. So to write to or read from the NTAGs user memory, it is always necessary to issue the INDATAEXCHANGE command and invoke the read/write page immediately after the tag is read. So, I made some slight changes to the void TestNFC_ReadTag (void) function to meet this requirement and it works. The below is the changes I made:

/* Made these as static global variables */
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)



// Reads cards, beep and long light flash for good card read, short light flash for no card read, lights mostly on for NFC chip not talking right
void TestNFC_ReadTag (void) {
    bool success;
    static flag = 0;

    /************************************ Everytime Try Reading a Card ***********************************/
    // Check for a good card read
    success = PN532_readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 1000);
    
    if (success) {

        if(flag == 0){
           testPN532WritePage();
           flag = 1;
        }
        else{
           testPN532ReadPage();
           flag = 0;
     }

        LIGHT_ON();
        EventTimerCreate(200,1,TestingLightsOff);
        for (int nfcBeep = 0; nfcBeep < 300; nfcBeep++) {
            DACB.CH0DATA = 0x700;
            _delay_us(200);
            DACB.CH0DATA = 0x900;
            _delay_us(200);
        }
        DACB.CH0DATA = 0x800;
        _delay_ms(500);         // Reduced from 1000ms to 500ms to avoid WDT reset - Vinay
    }
    // If Invalid card read then short light flash no beep
    else {
        LIGHT_ON();
        _delay_ms(10);
        LIGHT_OFF();
    }
}

void TestNFCInterrupt_ReadTag (void){

    /* Read the tag and write/read to NTAG213 user memory */
    TestNFC_ReadTag();

    /* Needs to restart the scan to detect the NTAG passively */
    PN532_StartScan();
}

I hope this helps!