WMT-GmbH / pn532

no_std implementation of the Pn532 protocol using embedded_hal traits
Apache License 2.0
20 stars 19 forks source link

TimeoutAck error when using library with rppal #13

Closed kelada closed 1 year ago

kelada commented 1 year ago

Hi there. I'm interested in using this library to replace an existing application I've written using https://github.com/adafruit/Adafruit_CircuitPython_PN532 python library. I am new to embedded rust so it's very likely I'm doing something wrong.

Running the example below I always get TimeoutAck errors, but I'm not sure why. Looking at the Adafruit library I've understood that I need to enable the msb-spi flag in addition to matching the clock frequency (source)

However, I don't seem to have had much luck with this. Any guidance would be greatly appreciated.

Hardware:

Software

Cargo.toml

...
[dependencies]
pn532 = { version = "0.2.2", features = ["msb-spi"] }
rppal = { version = "0.14.1", features = ["hal"] }

main.rs

use pn532::{spi::SPIInterface, IntoDuration, Pn532, Request};
use rppal::gpio::Gpio;
use rppal::hal::Timer;
use rppal::spi::{Bus, Mode, SlaveSelect, Spi};
use std::error::Error;

/// RPi 3B
// SPI: SPI0 (GPIO 10, 9, 11)
// CS:  GPIO 8
fn main() -> Result<(), Box<dyn Error>> {
    let timer = Timer::new();

    let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 1_000_000, Mode::Mode0)?;
    let cs = Gpio::new()?.get(8)?.into_output();
    let interface = SPIInterface { spi, cs };

    let mut pn532: Pn532<_, _, 32> = Pn532::new(interface, timer);
    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]);
        }
    } else {
        println!("Found: nothing");
    }

    Ok(())
}
dimpolo commented 1 year ago

Hi. Thanks for taking the time to submit this issue. There is currently a bug SPI implementation in 0.2.2 which is fixed by #7.

I haven't gotten around publishing a new version, sorry about that.

Could you check if using this fixes your problem?

pn532 = { git = "https://github.com/WMT-GmbH/pn532", features = ["msb-spi"] }
dimpolo commented 1 year ago

You'll probably also want to include this line before inlisting the target (I should update the readme)

pn532.process(
    &Request::sam_configuration(SAMMode::Normal, false),
    0,
    50.ms(),
)?;
kelada commented 1 year ago

Thanks for the quick response.

Unfortunately it throws an TimeoutAck error at this new line.

dimpolo commented 1 year ago

Unfortunately it throws an TimeoutAck error at this new line.

That is with the updated git dependency?

kelada commented 1 year ago

Unfortunately it throws an TimeoutAck error at this new line.

That is with the updated git dependency?

Yeah, I still get the same error.

[dependencies]
pn532 = { git = "https://github.com/WMT-GmbH/pn532", branch = "master", features = ["msb-spi"] }
rppal = { version = "0.14.1", features = ["hal"] }
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: TimeoutAck', src/main.rs:25:10

Note: I'm using unwrap() rather than ? to get it to panic if there is an error.

dimpolo commented 1 year ago

Hm strange. At this point I can't see why the code woudn't work.

Could you compare the signals with the adafruit implementation using a scope or logic analyzer?

I'll try and reproduce the issue on my end but I won't have access to the hardware until in two weeks.

kelada commented 1 year ago

Could you compare the signals with the adafruit implementation using a scope or logic analyzer?

I don't have either on hand today, i'll see if I can get ahold of something this week.

In the meantime, do you think it's worth me testing I2C to see that that works?

dimpolo commented 1 year ago

Sure thing! Be sure to cross check with the adafruit library to rule out hardware issues :)

kelada commented 1 year ago

In regard to SPI, changing the CS pin from 8 to 5 on the RPi seems to have resolved the consistant TimeoutAck error when calling sam_configuration(...). I'm satisfied that this is not an issue with the library. Thanks for you help.

dimpolo commented 1 year ago

Thanks for checking back in. Glad you found a solution :)