PN532: SPI card emulation is not working
I have the elechouse v3 module (or probably a clone, since I don’t know anymore when I purchased it and can’t find out if it is a clone or not).
After setting up everything like in the example for the emulate_tag_ndef.h, I have this loop:
// the loop function runs over and over again until power down or reset
void loop()
{
Serial.println("Begin emulation.");
if (!nfc.emulate(10000))timeout 10 seconds
Serial.println("timed out");
else
Serial.println("mobile device found!");
if (nfc.writeOccured()) {
Serial.println("\nWrite occured !");
uint8_t *tag_buf;
uint16_t length;
nfc.getContent(&tag_buf, &length);
NdefMessage msg = NdefMessage(tag_buf, length);
msg.print();
}
delay(1000);
}
It should emulate an NFC tag with the seeeds studio tag. Unfortunately, it doesn’t send the url and gives the wrong data to the phone.

Enabling the debug on the library, this is what I get once the phone will answer:
Begin emulation. write: 8C 05 04 00 12 34 56 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 read: 8D 08 E0 80 write: 86 read: 87 13 status is not ok tgGetData failed! write: 52 00 read: 53 00 mobile device found!
Shouldn’t it be able to emulate a mifare? I just don’t know if it is something with the library or if it is something with the module.
Edit:
- 8C is the command for tgInitAsTarget
- 05 means PICC only (0x04) + Passive only (0x01)
- 04 00 is SENS_RES (why is it not 08 00 like on the pn532 examples of NXP?)
- 12 34 56 is the Uid
- 20 is SEL_RES (Mifare DESFire)
- The others are NFCId1/2/3t
The answer seems to be ok for this command. But for the 0x86 command (GetData()) I get always the error “The data format does not match to the specification.”. I don’t know what it can be or how to debug it… Seems to be more a phone error…
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 18 (2 by maintainers)
Hi guys @Dermiste @Adrianotiger. I managed to parse horrendous fix together, based on Adrianos explanation (forgive me, I have 10h Arduino experience).
emulatetag.cpp, emulate function (whole function, but only changes in the command array and in the runloop retry stuff)
PN532.cpp, tgGetData function
Some other sidenotes:
It still take a few attempts to get it to recognize the tag. Need to have the phone in the right angle, very close etc… at least for my chip.
I have Arduino MKR 1010 WiFi, and at first I got errors because of
_spi->setDataModeso this is my PN532_SPI.cpp begin function (use newbeginTransactionmethod)Arduino <-> Chip
RESET <-> RSTO MOSI 8 <-> MOSI SCK 9 <-> SCK MISO 10 <-> MISO SDA 11 <-> SS GND <-> GND 5V <-> VCC
use PIN 11 as the SS pin:
so in the example, swap the argument SS -> PN532_SPI pn532spi(SPI, 11);
After a lot of googling and changes in the library, I found some errors and found some info about it. As said from a lot of users, the NXP documentation isn’t really good and so a lot of info are missing. But the library structure is good and works. Just some info about it:
Starting with this tgInitAsTarget:
On the last commit you changed it and now it is wrong. The PN532 is emulating a FeliCa card and you have to set it up correctly. So, you have to write it in this way:
Some more info on https://www.nxp.com/docs/en/nxp/application-notes/AN133910.pdf page 51 (how to fill TgInitAsTarget parameters?). I am not sure about it, but after setting it correctly, my phone begun to read the NDEF Tags.
This is not all. An important point is the first tgGetData call. You probably get this one:
if you have a cloned shield or if the NFC-shield is not correctly positioned on your smartphone… Just continue to reading or polling will help you to get a right answer.
Once you get another answer, this one could be:
This is a good answer! The smartphone replied with “target has been released by its initiator”. It means that the message was arrived to destination and the smartphone want to read more (https://stackoverflow.com/questions/24777810/the-pn532-configured-as-target-has-been-released-by-its-initiator). So, if you get this 0x29 answer, just init the PN532 again and the smartphone will read the Tag correctly. You have to modify the
pn532.cpp - tgGetData()to parse the 0x29 command and you have to modify theemulatetag.cpp - emulate()to callif (pn532.tgInitAsTarget(command, sizeof(command), 1000) == 1) continue;if the result of the status was 0x29 and it is the first command read inside the loop.I am still playing with this module, so maybe this is not the best solution. But it works now! If you want I can create a fork and commit my changes… But as said, it is probably not the perfect solution… Just a working solution.
Edit: All messages between phone and PN532 are APDU messages, from the ISO7816. If you want parse the messages, you can begin with this wiki: https://en.wikipedia.org/wiki/Smart_card_application_protocol_data_unit
Hi @Adrianotiger, I guess it’s my turn to step on this issue and try to make things work. It’s been a while but do you possibly have code pieces to share regarding this?
I can’t remember 😛 I think, I begun to make multiple reads. So if I got an incorrect answer, I just made the request again. The module works also better with a high voltage. They write from 2.7 to 5.5V. With 4.5V my module begun to give strange answers… I think you should try a good power supply with 1A and 5V. To have a stable module.