2014/10/03

Connect PN532 NFC to Raspberry Pi

1. Enable SPI on Raspberry Pi


PN532 NFC提供3種介面, SPI, I2C, 與UART
先選SPI做測試,

在RPi上面, SPI與I2C預設是關的, 我們要先打開它,

$ sudo nano /etc/modprobe.d/raspi-blacklist.conf


會看到裡面有一些黑名單

blacklist spi-bcm2708
blacklist i2c-bcm2708
blacklist snd-soc-pcm512x
blacklist snd-soc-wm8804


我們要用到spi, 所以把它mark掉

#blacklist spi-bcm2708

存檔離開, 然後重開機讓設定生效


重開機之後, 來看看SPI driver有沒有起來

$ ls /dev/spidev*
/dev/spidev0.0  /dev/spidev0.1

會看到有兩個 SPI driver, 前面的數字是第幾個SPI peripheral,
兩個數字都是1表示 RPi 只有一個SPI port
第二個數字是 Chip Selectt 使用pin CS0 & CS1


2. Introduction to SPI


Serial Peripheral Interface (SPI), 跟 I2C很像
是同步序列資料協定(synchronous serial data link)

常用的接角定義:
  • SCLK—Serial Clock(自master輸出), 有時也叫SCK
  • MOSI/SIMO—Master Output, Slave Input(自master輸出)
  • MISO/SOMI—Master Input, Slave Output(自slave輸出)
  • SS—Slave Select(active low;自master輸出), 在RPi上叫SPI_CE0_N, SPI_CE1_N, 在PN532上叫SSEL

在使用上, 1個master可以對應多個slave
可能的接法如下
如果device扮演的是Master, 那麼它的MOSI就等於是它的Output, MISO就是它的Input
如果device扮演的是Slave, 那麼它的MOSI就等於是它的Input, MISO就是它的Output

3. Connect PN532 to RPi via SPi


底下是 RPi 的接腳配置


其中跟 SPI 相關的有:
SPI_MOSI
SPI_MISO
SPI_SCLK
SPI_CE0_N
SPI_CE1_N

我們這樣接:

  • PN532 的 3.3V 接到 RPi 的3V3 (可接1或17的位置)
  • PN532 的 GND 接到 RPi 的接地 (可接 6, 9, 14, 20, 25, 30, 34, 39, 我是接39)
  • PN532 的 SCK 接到 RPi 的 SPI_SCLK (23的位置)
  • PN532 的 MISO 接到 RPi 的 SPI_MISO (21的位置)
  • PN532 的 MOSI 接到 RPi 的 SPI_MOSI (19的位置)
  • PN532 的 SSEL 接到 RPi 的SPI_CE0_N


4. Install libnfc in RPi


接好之後, 有很多種方式可以與PN532溝通, 用libnfc是其中一種方便的方式

首先先更安裝有dependency的library
sudo apt-get update
$ sudo apt-get install libusb-dev libpcsclite-dev

然後抓libnfc source code, 可以上它們的網站來找
https://code.google.com/p/libnfc/

其中stable version在寫這篇文章的時候在這裡
https://bintray.com/nfc-tools/sources/libnfc

抓好之後, 將它解壓縮

$ tar -xf libnfc-1.7.1.tar.bz2

然後compile & install
$ cd libnfc-1.7.1
$ ./configure --prefix=/usr --sysconfdir=/etc
$ make
$ sudo make install

修改它的config
$ cd /etc
$ sudo mkdir nfc
$ sudo nano /etc/nfc/libnfc.conf

把底下copy paste
# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually a device
# configuration using file or environment variable
allow_autoscan = true

# Allow intrusive auto-detection (default: false)
# Warning: intrusive auto-detection can seriously disturb other devices
# This option is not recommended, user should prefer to add manually his device.
allow_intrusive_scan = false

# Set log level (default: error)
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# Note: if you compiled with --enable-debug option, the default log level is "debug"
log_level = 1

# Manually set default device (no default)
# To set a default device, you must set both name and connstring for your device
# Note: if autoscan is enabled, default device will be the first device available in device list.
device.name = "Itead_PN532_SPI"
device.connstring = "pn532_spi:/dev/spidev0.0:500000"

設定完成, 來試試看有沒有問題, 拿一張悠遊卡放在PN532旁邊
$ nfc-list
$ nfc-list
nfc-list uses libnfc 1.7.1
NFC device: pn532_spi:/dev/spidev0.0 opened
1 ISO14443A passive target(s) found:
ISO/IEC 14443A (106 kbps) target:
    ATQA (SENS_RES): 00  04
       UID (NFCID1): 2b  6a  20  87
      SAK (SEL_RES): 08

會動之後, 就可以玩其它功能了





沒有留言:

張貼留言