adafruit / Adafruit-PN532

Arduino library for SPI and I2C access to the PN532 RFID/Near Field Communication chip
Other
422 stars 266 forks source link

Better support for using the IRQ line in I2C #82

Closed Udinic closed 3 years ago

Udinic commented 4 years ago

Using the IRQ line we can save the host controller from being blocked waiting for new cards. When a new card is detected, the PN532 will drive the IRQ to be low (It's usually high, see the PN532 user manual), which will let the host controller know it can try to read the detected card. This frees the host controller to do other things while waiting for new cards to be detected.

To implement this, I added a method for starting the card detection mode (startPassiveTargetIDDetection()) and another method for reading the detected card (readDetectedPassiveTargetID()). We need the PN532 to be connected using I2C and have the IRQ line connected to one of the pins in the host controller. The flow is like this:

  1. Host controller calls startPassiveTargetIDDetection()
  2. Assuming the IRQ is connected to pin D2, when this pin is low then we know there's a card detected by the reader.
  3. The host controller calls readDetectedPassiveTargetID() to read the card information.
  4. Host controller can call startPassiveTargetIDDetection() again to start listening again for new cards.

I added readMifareClassicIrq.ino as a working example for this method. There's more documentation in the example, including a wiring diagram.

This code was tested with both Arduino Mega2560 and ESP32. The changes I made are adding a new functionality and aren't affecting any existing functionality. Everything is backward compatible. The new feature is only relevant in I2C mode, SPI or UART modes aren't affected.

I added this code because there's no clear example how to use the IRQ line, not in the official tutorial nor in any of the relevant discussion forums. I hope this helps others like me who struggled trying to get this behavior to work.

Udinic commented 3 years ago

Rebased + formatted the code using clang-format

siddacious commented 3 years ago

I'm going to go ahead and merge since the doxygen failures are for pre-existing code