PN532 v3 with Raspberry Pi 3

Asked by Lukas Vana (fabian) on 2016-09-16

Hello,

I'm trying to connect PN532 version 3 to my Raspberry Pi 3. I connected it to UART pins: https://dl.dropboxusercontent.com/u/15411424/2016-09-16%2011.24.43.jpg

In /dev there is ttyAMA0 device, but it don't work, so I tried /dev/ttyS0, whish works with nfc-list tool. In my /etc/nfc/libnfc.conf is:
device.connstring = "pn532_uart:/dev/ttyS0"
and I can get:
root@raspberrypi:~# nfc-list
nfc-list uses libnfc 1.7.1
NFC device: pn532_uart:/dev/ttyS0 opened

When I'm trying tagtool.py on first try I always get:

root@raspberrypi:~/nfcpy/trunk/examples# python tagtool.py --device tty:S0:pn532
[nfc.clf] searching for reader on path tty:S0:pn532
[nfc.clf.pn532] input/output error while waiting for ack
[main] no contactless reader available

Second try:

root@raspberrypi:~/nfcpy/trunk/examples# python tagtool.py --device tty:S0:pn532
[nfc.clf] searching for reader on path tty:S0:pn532
Traceback (most recent call last):
  File "tagtool.py", line 581, in <module>
    TagTool().run()
  File "/root/nfcpy/trunk/examples/cli.py", line 461, in run
    while self.run_once() and self.options.loop:
  File "/root/nfcpy/trunk/examples/cli.py", line 388, in run_once
    clf = nfc.ContactlessFrontend(path)
  File "/root/nfcpy/trunk/nfc/clf/__init__.py", line 71, in __init__
    if path and not self.open(path):
  File "/root/nfcpy/trunk/nfc/clf/__init__.py", line 145, in open
    self.device = device.connect(path)
  File "/root/nfcpy/trunk/nfc/clf/device.py", line 104, in connect
    device = driver.init(tty)
  File "/root/nfcpy/trunk/nfc/clf/pn532.py", line 395, in init
    if not transport.read(timeout=100) == set_baudrate_rsp:
  File "/root/nfcpy/trunk/nfc/clf/transport.py", line 138, in read
    LEN = frame[3]
IndexError: bytearray index out of range

Third and next tries:

root@raspberrypi:~/nfcpy/trunk/examples# python tagtool.py --device tty:S0:pn532
[nfc.clf] searching for reader on path tty:S0:pn532
[main] no contactless reader available

Now when I try nfc-list again it throws error, too:

root@raspberrypi:~/nfcpy/trunk/examples# nfc-list
nfc-list uses libnfc 1.7.1
error libnfc.driver.pn532_uart pn53x_check_communication error
nfc-list: ERROR: Unable to open NFC device: pn532_uart:/dev/ttyS0

When I reconnect PN532 I can reproduce it again exactly the same.

I have latest version of nfcpy 0.11.1:
root@raspberrypi:~# pip list|grep nfc
nfcpy (0.11.1)

I have found interesting info about RPI3 tty changes: http://raspberrypi.stackexchange.com/questions/45507/pn532-in-raspberry-pi-3-is-not-working "The raspberry pi 3 has changed things around a bit: ttyAMA0 now refers to the serial port that is connected to the bluetooth. The old serial port is now called ttyS0. So if you have an RPI3, everywhere you see "ttyAMA0" below, you should read "ttyS0"."

Have you some idea about my errors? Thanks!

Question information

Language:
English Edit question
Status:
Solved
For:
nfcpy Edit question
Assignee:
No assignee Edit question
Solved by:
Lukas Vana (fabian)
Solved:
2016-09-17
Last query:
2016-09-17
Last reply:
2016-09-16

This is unfortunately true, there is a problem with the UART on GPIO 14/15 of an RPi 3. I've learned that the change from ttyAMA0 to ttyS0 is not just the device node but it's quite different hardware behind. The ttyAMA0 is a full hardware UART that can can go much faster than 115.2 kbps and therefore nfcpy increases the baudrate to 921.6 kbps. But ttyS0 is much less capable 'mini uart' that seems unable to reliably work at higher baudrates (there's a lot of related info out there, one good overview is http://spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3 and one of the discussions I found is https://github.com/raspberrypi/firmware/issues/553).

The lates version on https://github.com/nfcpy/nfcpy master branch will not increase the serial speed if running with ttyS0 on RPi. That works well but of course also a bit slower. To get faster speed either use an FTDI friend and connect through USB or you could also map the hardware uart on ttyAMA0 back to the GPIO pins and assign the 'min uart' to Bluetooth (see the first link above for how that works).

BTW. where have you got that PN532 board. It's a nice form factor, looks better than Adafruit's breakout board.

Thanks for the reply and info!

I also tried to connect PN532 through "CP2102 USB to UART Bridge Controller". Again nfc-list with pn532_uart:/dev/ttyUSB0:115200 was able to read cards, bud nfcpy not.

I'll try to map ttyAMA0 back according to the instructions.

PN532 is from Czech eshop: http://robotstore.cz/obchod/arduino/mini-rfid-nfc-pn532-v3-0-cteni-zapis-cipu-karet-spi-i2c-arduino-modul/, but it's available also on DX: http://www.dx.com/p/pn532-nfc-near-field-communication-rfid-v3-module-red-blue-435089

Sorry, there is another RPi issue around USB Serial adapters. That was fixed on the master branch just a couple of days ago, so you'd need to grab the source from there (or wait for the next releaseif you want to stick to PyPI) A summary of the issue is at https://github.com/nfcpy/nfcpy/wiki/USB-TTL-serial-adapter-on-Raspberry-Pi.

Thanks for the info about the PN532 board.

I swapped ttyS0 and ttyAMA0 and nfcpy works ok:

root@raspberrypi:~/nfcpy/trunk/examples# python tagtool.py --device tty:AMA0
[nfc.clf] searching for reader on path tty:AMA0
[nfc.clf] using PN532v1.6 at /dev/ttyAMA0
** waiting for a tag **
Type2Tag ID=07502889

Thank you your help!