Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect pn532 i2c timing #367

Merged
merged 3 commits into from
Dec 5, 2016
Merged

Respect pn532 i2c timing #367

merged 3 commits into from
Dec 5, 2016

Conversation

oliv3r
Copy link
Contributor

@oliv3r oliv3r commented Oct 21, 2016

The pn532 states in its datasheet that the i2c stop and start conditions require atleast 1.3 ms between them. Currently, there is a nanosleep of 90 ms which seems arbitrarily chosen (nothing in the comments/git log from it). A delay however is always needed, as otherwise we write to fast for sure.

Additionally, this delay is not always added everywhere, so we still sometimes send data way to fast for the pn532. This patch creates wrappers around i2c_read and i2c_write to ensure proper timing of read and write commands.

Additionally we clarify the preamble/start condition a bit, as there was a bug in the text. No code changes where done in that regard.

Note, I have tested it on one board on one machine and not yet endlessly. So I'd like to first properly review this first before merging :)

edit: I cleaned some more on the preamble bit, and added a retry mechamism to the pn532_send() routine.

The pn532 documentation differs slightly from the included ascii art
documentation on how a packet looks like. The preamble was omitted
however the postamble is mentioned. This patch adds the Preamble to the
ascii frame documentation.

The code later refers incorrectly to the start byte as the preamble.
This variable was renamed to more descriptively state that it is the
preambe and the start bytes.

Signed-off-by: Olliver Schinagl <[email protected]>
The pn532 user manual states that after a i2c stop condition and before a i2c
start condition there should be a delay of minimally 1.3 milliseconds.
This is probably a limitation of the i2c peripheral or the firmware. In
any case, each i2c_read and i2c_write creates the packets which are
complemented with start/stop markers. It is thus required to take care
of timing in these two functions.

We solve this by wrapping the lower i2c_read and i2c_write functions for
the pn532, as this requirement is not for all chips.

Currently, we keep time using local variable, and thus the code is not
thread-safe. With libnfc being single threaded and only one instances of
libnfc can open a bus anyway, this is not yet a problem.

Signed-off-by: Olliver Schinagl <[email protected]>
Currently, we very occasionally can EXNIO errors from pn532_i2c_write() ->
i2c_write() -> write(). This may happen about once every 30 seconds.
Based from the kernel sources, EXNIO happens if the chip no longer
responds to its own address.

To make sure we do not loose any sent packets, we retry to send
PN532_SEND_RETRIES number of times. Since we miss 1 every 30 or so
seconds, doing 1 retry should be fine.

This might be considered a hack as the failure may be some other timing
related issue.

Signed-off-by: Olliver Schinagl <[email protected]>
@neomilium neomilium merged commit b2a9cce into nfc-tools:master Dec 5, 2016
@neomilium
Copy link
Member

Nice work !

@oliv3r
Copy link
Contributor Author

oliv3r commented Dec 5, 2016

Thanks, and I forgot this thread, but the timing is wrong. The datasheet says 1.3 ms and even though due to delays we are at 1.4-ish, this is still to fast for the I2C block inside the chip. We are now trying to find the actual timings, but NXP said they are not sure what is going on here. We are now at 4ms and my guess is we will find the sweet spot at 2.5ms.

@oliv3r oliv3r deleted the respect_pn532_i2c_timing branch December 5, 2016 22:51
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.

None yet

2 participants