10 releases

0.5.0 Dec 17, 2024
0.4.0 Mar 12, 2024
0.3.3 Mar 5, 2024
0.3.2 Jul 17, 2023
0.1.0 Sep 2, 2019

#157 in Embedded development

Download history 14/week @ 2024-09-23 9/week @ 2024-10-14 4/week @ 2024-11-18 23/week @ 2024-12-09 139/week @ 2024-12-16

162 downloads per month

MIT/Apache

62KB
1K SLoC

pn532

github Crates.io docs.rs

no_std implementation of the Pn532 protocol using embedded_hal traits.

Since communication with the Pn532 can be rather slow at times, communication can be split into multiple parts, a timeout can be provided or an async runtime can be used.

The Pn532 supports different serial links. The Interface trait abstracts over these different links.

Interface can be manually implemented or one the four provided interface structs can be used:

  • spi::SPIInterface
  • spi::SPIInterfaceWithIrq
  • i2c::I2CInterface
  • i2c::I2CInterfaceWithIrq

Troubleshooting

General

  • check you're using Request::sam_configuration to initialize the PN532

I2C

  • check wiring
  • check you're using external pull-up resistors

SPI

  • check wiring
  • check you set the SPI mode to Mode0 (CPOL = 0, CPHA = 0)
  • check you set the SPI to LSB mode (oftentimes this is not the default) or enable the msb-spi feature

SPI example

use pn532::{requests::SAMMode, spi::SPIInterface, Pn532, Request};
use pn532::IntoDuration; // trait for `ms()`, your HAL might have its own

// spi is a struct implementing embedded_hal::spi::SpiDevice
// timer is a struct implementing pn532::CountDown

let mut pn532: Pn532<_, _, 32> = Pn532::new(SPIInterface { spi }, timer);
if let Err(e) = pn532.process(&Request::sam_configuration(SAMMode::Normal, false), 0, 50.ms()){
    println!("Could not initialize PN532: {e:?}")
}
if let Ok(uid) = pn532.process(&Request::INLIST_ONE_ISO_A_TARGET, 7, 1000.ms()){
    let result = pn532.process(&Request::ntag_read(10), 17, 50.ms()).unwrap();
    if result[0] == 0x00 {
        println!("page 10: {:?}", &result[1..5]);
    }
}

msb-spi feature

If you want to use either spi::SPIInterface or spi::SPIInterfaceWithIrq and your peripheral cannot be set to lsb mode you need to enable the msb-spi feature of this crate.

std feature

Enable the std feature to use serialport::SerialPortInterface. Only works for targets supported by the serialport crate.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~67–720KB
~11K SLoC