AnsweredAssumed Answered

SECTOR_SELECT command with PN532

Question asked by Nesli Köse on Oct 16, 2018
Latest reply on Oct 28, 2018 by Kan_Li

Hello,

 

I am trying to read the session registers in an NXP NTAG I2C 1k with a PN532 using an Arduino board to eventually communicate in Pass-through mode. I downloaded an NFC library for Arduino that includes most of the functions like "Read Page" or "Write Page". For example, WritePage command looks like this:

 

uint8_t PN532::mifareultralight_WritePage (uint8_t page, uint8_t *buffer)
{
/* Prepare the first command */
pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
pn532_packetbuffer[1] = 1; /* Card number */
pn532_packetbuffer[2] = MIFARE_CMD_WRITE_ULTRALIGHT; /* Mifare UL Write cmd = 0xA2 */
pn532_packetbuffer[3] = page; /* page Number (0..63) */
memcpy (pn532_packetbuffer + 4, buffer, 4); /* Data Payload */

/* Send the command */
if (HAL(writeCommand)(pn532_packetbuffer, 8)) {
return 0;
}

/* Read the response packet */
return (0 < HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)));
}

 

So I added a SectorSelect command based on the WritePage function. I used PN532_COMMAND_INCOMMUNICATETHRU instead of INDATAEXCHANGE, and the function expects a timeout after the second command instead of an ACK response:

 

uint8_t PN532::SectorSelect (uint8_t sectorNumber)
{
/* Prepare the command */
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU; //(0x42)
pn532_packetbuffer[1] = 1; /* Card number */
pn532_packetbuffer[2] = PN532_CMD_SECTOR_SELECT; 
pn532_packetbuffer[3] = 0xFF;

/* Send the command */
if (HAL(writeCommand)(pn532_packetbuffer, 4)) {
Serial.print("\n1st Sector Select Command Failed");
return 0;
}

/* Read the response packet */ 
HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));

/* Prepare the 2nd command */ 
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU; //(0x42)
pn532_packetbuffer[1] = 1; /* Card number */
pn532_packetbuffer[2] = sectorNumber; 
pn532_packetbuffer[3] = 0x00; 
pn532_packetbuffer[4] = 0x00; 
pn532_packetbuffer[5] = 0x00;

/* Send the 2nd command */
if (HAL(writeCommand)(pn532_packetbuffer, 6) > -2 ){
Serial.print("\n2nd Sector Select Command Failed");
return 0;
}

/* Read the response packet */
HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));

Serial.print("\n2nd Sector Select Command Successful");
// Return OK signal
return 1;
}

I have a few questions regarding this piece of code I wrote. 

1) The timeout duration defined in this library is 10 ms, however the datasheet says that 1 ms of no response is enough for a Passive ACK. So at the beginning, I only waited for 1 ms and did not get any response so the function returned the OK signal. However, when I try sending a ReadPage command after a sector select command I got an error with the read function even though it works normally before a sector select.

 

2) After that I tried waiting for 10 ms for the Passive ACK signal, but weird enough instead of getting no response or a NACK response, I eventually get an ACK response after the second sector_select command as well, so the function gives an error, and a ReadPage command does not work afterwards either.

 

Any advice or comment would be much appreciated.

 

 

*** The library I originally used and changed is at : https://github.com/Seeed-Studio/PN532

Attachments

Outcomes