adafruit / Adafruit-PN532

Arduino library for SPI and I2C access to the PN532 RFID/Near Field Communication chip
Other
421 stars 265 forks source link

add constructor accepting Adafruit_SPIDevice #97

Closed fabianoriccardi closed 1 year ago

fabianoriccardi commented 2 years ago

I have added a constructor to personalize the frequency of hardware SPI. The issue #80 is still open, so this pull request may help.

To initialize the PN532 class you will have to type:

Adafruit_PN532 nfc(Adafruit_SPIDevice(PN532_SS, 100000, SPI_BITORDER_LSBFIRST));
caternuson commented 2 years ago

Sorry. Just now seeing this.

@fabianoriccardi This can be done by changing the existing constructor:

  Adafruit_PN532(uint8_t ss);  // Hardware SPI

to:

  Adafruit_PN532(uint8_t ss, SPIClass *theSPI = &SPI);  // Hardware SPI

and then change the existing code to pass that in:

Adafruit_PN532::Adafruit_PN532(uint8_t ss, SPIClass *theSPI) {
  spi_dev =
      new Adafruit_SPIDevice(ss, 1000000, SPI_BITORDER_LSBFIRST, SPI_MODE0, theSPI);
}

That won't break anything in terms of backwards compatibility. It also does not require creating the SPIDevice instance in user code like your example above.

fabianoriccardi commented 2 years ago

I don't fully know the codebase, so forgive me if I'm wrong. With the following line:

new Adafruit_SPIDevice(ss, 1000000, SPI_BITORDER_LSBFIRST, SPI_MODE0, theSPI);

you are setting the frequency to 1MHz, which is the problem with ESP32.

caternuson commented 2 years ago

@ladyada This is a generic issue - How to expose the other parameters of SPIDevice? I don't think we want to do this by having the SPIDevice instance created in user code?

caternuson commented 1 year ago

Closing. Ability to specify SPI bus added with #112. The frequency used is confirmed working with ESP32.

If there is still a need to specify an alternate frequency, please open a new issue with information about what is not working with current library code.