Skip to content

Better support for using the IRQ line in I2C - #82

Merged
siddacious merged 2 commits into
adafruit:masterfrom
Udinic:nonblock_irq
Nov 23, 2020
Merged

Better support for using the IRQ line in I2C#82
siddacious merged 2 commits into
adafruit:masterfrom
Udinic:nonblock_irq

Conversation

@Udinic

@Udinic Udinic commented Sep 24, 2020

Copy link
Copy Markdown

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.

@siddacious siddacious left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Udinic It looks like the clang-format check is failing; please take a look at the info in the README to get it passing. Thanks :)

https://github.com/adafruit/Adafruit-PN532#formatting-and-clang-format

@Udinic
Udinic requested a review from siddacious November 1, 2020 20:57
@Udinic

Udinic commented Nov 1, 2020

Copy link
Copy Markdown
Author

Rebased + formatted the code using clang-format

@siddacious

Copy link
Copy Markdown
Contributor

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

@siddacious
siddacious merged commit bee37dd into adafruit:master Nov 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants