diff --git a/include/NfcSpiBus.h b/include/NfcSpiBus.h index b3ffcc9..57bcd49 100644 --- a/include/NfcSpiBus.h +++ b/include/NfcSpiBus.h @@ -20,3 +20,7 @@ #define PN5180_SPI_BUS HSPI // SPI2 on classic ESP32 #endif #endif + +class SPIClass; +// Dedicated NFC bus, or the TFT's Arduino SPI handle on shared-bus boards. +SPIClass& nfcSpiBus(); diff --git a/lib/PN5180/PN5180.cpp b/lib/PN5180/PN5180.cpp index 0959188..6da2e91 100644 --- a/lib/PN5180/PN5180.cpp +++ b/lib/PN5180/PN5180.cpp @@ -37,6 +37,10 @@ static SPIClass pn5180_spi(PN5180_SPI_BUS); #endif +SPIClass& nfcSpiBus() { + return pn5180_spi; +} + class PN5180SPITransaction { public: PN5180SPITransaction(SPIClass& spi, const SPISettings& settings) @@ -93,13 +97,16 @@ PN5180::PN5180(uint8_t SSpin, uint8_t BUSYpin, uint8_t RSTpin, PN5180_SPI_SETTINGS = SPISettings(PN5180_SPI_HZ, MSBFIRST, SPI_MODE0); } -void PN5180::begin() { - pinMode(PN5180_NSS, OUTPUT); +void PN5180::beginControlPins() { pinMode(PN5180_BUSY, INPUT); pinMode(PN5180_RST, OUTPUT); + digitalWrite(PN5180_RST, HIGH); // no reset +} +void PN5180::begin() { + pinMode(PN5180_NSS, OUTPUT); digitalWrite(PN5180_NSS, HIGH); // disable - digitalWrite(PN5180_RST, HIGH); // no reset + beginControlPins(); #if defined(BOARD_SHARED_SPI) if (!SharedSPIBus::begin(PN5180_SCK, PN5180_MISO, PN5180_MOSI, diff --git a/lib/PN5180/PN5180.h b/lib/PN5180/PN5180.h index 0156e0d..a10333c 100644 --- a/lib/PN5180/PN5180.h +++ b/lib/PN5180/PN5180.h @@ -140,6 +140,8 @@ class PN5180 { int8_t SCKpin = -1, int8_t MISOpin = -1, int8_t MOSIpin = -1); void begin(); + // Initialize only BUSY and RST when the caller already prepared SPI and NSS. + void beginControlPins(); void end(); /* diff --git a/src/HardwareNFCConnection.cpp b/src/HardwareNFCConnection.cpp index 4b6e628..397df2d 100644 --- a/src/HardwareNFCConnection.cpp +++ b/src/HardwareNFCConnection.cpp @@ -1,5 +1,4 @@ #include "HardwareNFCConnection.h" -#include "ConfigurationManager.h" #include "LogBuffer.h" #include #include @@ -48,6 +47,12 @@ HardwareNFCConnection::HardwareNFCConnection() { memset(&hal_, 0, sizeof(hal_)); } +HardwareNFCConnection::HardwareNFCConnection(const NfcReaderPins& pins) + : HardwareNFCConnection() { + pins_ = pins; + explicitPins_ = true; +} + HardwareNFCConnection::~HardwareNFCConnection() { delete nfc_; delete iso14443a_; @@ -107,57 +112,41 @@ void HardwareNFCConnection::getReaderInfo(char* buf, size_t len) const { } bool HardwareNFCConnection::begin() { - // Configure additional input pins for future use - // Shared-SPI boards use explicit -1 sentinels for signals that are not - // connected, preserving scarce GPIOs for the TFT control lines. - if (PIN_PN5180_IRQ >= 0) pinMode(PIN_PN5180_IRQ, INPUT); - if (PIN_PN5180_GPIO >= 0) pinMode(PIN_PN5180_GPIO, INPUT); - if (PIN_PN5180_AUX >= 0) pinMode(PIN_PN5180_AUX, INPUT); - - // Runtime pin overrides (#201): board defaults unless remapped in config - { - auto& cfg = ConfigurationManager::getInstance(); - pinRst_ = cfg.getNfcPin(NfcPinId::Rst); - pinNss_ = cfg.getNfcPin(NfcPinId::Nss); - pinBusy_ = cfg.getNfcPin(NfcPinId::Busy); - pinSck_ = cfg.getNfcPin(NfcPinId::Sck); - pinMosi_ = cfg.getNfcPin(NfcPinId::Mosi); - pinMiso_ = cfg.getNfcPin(NfcPinId::Miso); - } + if (!explicitPins_ && !prepareDefaultNfcReader(NfcReaderType::PN5180, pins_)) return false; - nfc_ = new PN5180ISO15693(pinNss_, pinBusy_, pinRst_, - pinSck_, pinMiso_, pinMosi_); - iso14443a_ = new PN5180ISO14443(pinNss_, pinBusy_, pinRst_, - pinSck_, pinMiso_, pinMosi_); + nfc_ = new PN5180ISO15693(pins_.nss, pins_.busy, pins_.rst, + pins_.sck, pins_.miso, pins_.mosi); + iso14443a_ = new PN5180ISO14443(pins_.nss, pins_.busy, pins_.rst, + pins_.sck, pins_.miso, pins_.mosi); // Boot lines go through LogBuffer (Serial + web /logs ring) so a reader // that dies in the field can be diagnosed without a USB cable (#293). auto& logBuf = LogBuffer::getInstance(); logBuf.logPrintf("HardwareNFCConnection: Starting PN5180...\n"); logBuf.logPrintf("HardwareNFCConnection: Pins — NSS=%d BUSY=%d RST=%d SCK=%d MISO=%d MOSI=%d\n", - pinNss_, pinBusy_, pinRst_, - pinSck_, pinMiso_, pinMosi_); + pins_.nss, pins_.busy, pins_.rst, + pins_.sck, pins_.miso, pins_.mosi); // Bench diagnostics: confirms whether a PN5180_SPI_HZ build-flag override // reached this image. "Requested" because Arduino-ESP32 derives a hardware // divider from this — e.g. 7 MHz actually runs at ~6.67 MHz on the wire. logBuf.logPrintf("HardwareNFCConnection: PN5180 requested SPI clock %lu Hz\n", (unsigned long)PN5180_SPI_HZ); PN5180::setTimeoutLogSink(pn5180TimeoutToLogBuffer); - nfc_->begin(); - iso14443a_->begin(); + // Both protocol adapters address the same physical reader. + nfc_->beginControlPins(); logBuf.logPrintf("HardwareNFCConnection: SPI begin done, resetting...\n"); - logBuf.logPrintf("HardwareNFCConnection: BUSY pin=%d before reset\n", digitalRead(pinBusy_)); + logBuf.logPrintf("HardwareNFCConnection: BUSY pin=%d before reset\n", digitalRead(pins_.busy)); // Manual reset with timeout: PN5180::reset() library call has no timeout, blocking on hung chips - digitalWrite(pinRst_, LOW); + digitalWrite(pins_.rst, LOW); delay(10); - digitalWrite(pinRst_, HIGH); + digitalWrite(pins_.rst, HIGH); logBuf.logPrintf("HardwareNFCConnection: RST pin released, waiting for boot...\n"); // BUSY signal indicates RF subsystem boot state; timeout detects dead/unreliable chips unsigned long start = millis(); bool busyLow = true; - while (digitalRead(pinBusy_) == HIGH) { + while (digitalRead(pins_.busy) == HIGH) { if (millis() - start > 2000) { logBuf.logPrintf("HardwareNFCConnection: TIMEOUT waiting for BUSY LOW after reset!\n"); busyLow = false; @@ -231,14 +220,14 @@ bool HardwareNFCConnection::hardwareReset() { Serial.println("HardwareNFC: hardwareReset() - toggling RST pin"); // Toggle RST pin to force full hardware reset - digitalWrite(pinRst_, LOW); + digitalWrite(pins_.rst, LOW); delay(10); - digitalWrite(pinRst_, HIGH); + digitalWrite(pins_.rst, HIGH); PN5180::clearBusWedged(); // fresh chip — release the fail-fast latch // Wait for BUSY to go LOW with timeout unsigned long start = millis(); - while (digitalRead(pinBusy_) == HIGH) { + while (digitalRead(pins_.busy) == HIGH) { if (millis() - start > 2000) { Serial.println("HardwareNFC: hardwareReset TIMEOUT waiting for BUSY LOW"); return false; diff --git a/src/HardwareNFCConnection.h b/src/HardwareNFCConnection.h index a101883..7a3b68f 100644 --- a/src/HardwareNFCConnection.h +++ b/src/HardwareNFCConnection.h @@ -2,6 +2,7 @@ #define HARDWARE_NFC_CONNECTION_H #include "NFCConnectionI.h" +#include "NfcReaderPins.h" #include "BoardPins.h" #include #include @@ -11,6 +12,8 @@ class HardwareNFCConnection : public NFCConnectionI { public: HardwareNFCConnection(); + // For use after prepareNfcReaders() with the complete set of readers. + explicit HardwareNFCConnection(const NfcReaderPins& pins); ~HardwareNFCConnection() override; bool begin() override; @@ -41,10 +44,8 @@ class HardwareNFCConnection : public NFCConnectionI { bool isPN5180Ready() const { return pn5180Ready_; } private: - // Effective NFC pins, captured once from ConfigurationManager at begin() - // — runtime overrides (#201) apply on the post-save reboot - uint8_t pinRst_ = 0, pinNss_ = 0, pinBusy_ = 0; - uint8_t pinSck_ = 0, pinMosi_ = 0, pinMiso_ = 0; + NfcReaderPins pins_{}; + bool explicitPins_ = false; PN5180ISO15693* nfc_ = nullptr; PN5180ISO14443* iso14443a_ = nullptr; opt_nfc_hal_t hal_; diff --git a/src/HardwareNFCConnectionPN532.cpp b/src/HardwareNFCConnectionPN532.cpp index 732a938..b10c3da 100644 --- a/src/HardwareNFCConnectionPN532.cpp +++ b/src/HardwareNFCConnectionPN532.cpp @@ -1,5 +1,4 @@ #include "HardwareNFCConnectionPN532.h" -#include "ConfigurationManager.h" #include "openprinttag_adafruit_pn532.h" #include "BoardPins.h" #include "SharedSPIBus.h" @@ -22,31 +21,18 @@ HardwareNFCConnectionPN532::HardwareNFCConnectionPN532() { memset(currentUid_, 0, sizeof(currentUid_)); } +HardwareNFCConnectionPN532::HardwareNFCConnectionPN532(const NfcReaderPins& pins) + : HardwareNFCConnectionPN532() { + pins_ = pins; + explicitPins_ = true; +} + HardwareNFCConnectionPN532::~HardwareNFCConnectionPN532() { delete pn532_; } bool HardwareNFCConnectionPN532::begin() { - // Runtime pin overrides (#201): PN532 shares the NFC pin slots (SS=NSS; BUSY unused). - // BOARD_SHARED_SPI injects the same global bus used by the TFT. - { - auto& cfg = ConfigurationManager::getInstance(); - pinRst_ = cfg.getNfcPin(NfcPinId::Rst); - pinSs_ = cfg.getNfcPin(NfcPinId::Nss); - pinSck_ = cfg.getNfcPin(NfcPinId::Sck); - pinMosi_ = cfg.getNfcPin(NfcPinId::Mosi); - pinMiso_ = cfg.getNfcPin(NfcPinId::Miso); - } -#if defined(BOARD_SHARED_SPI) - if (!SharedSPIBus::begin(pinSck_, pinMiso_, pinMosi_, pinSs_, PIN_TFT_CS)) { - Serial.println("PN532: shared SPI initialization failed"); - return false; - } - SPIClass* spiBus = &SharedSPIBus::bus(); -#else - SPI.begin(pinSck_, pinMiso_, pinMosi_, pinSs_); - SPIClass* spiBus = &SPI; -#endif + if (!explicitPins_ && !prepareDefaultNfcReader(NfcReaderType::PN532, pins_)) return false; SharedSPIBus::Guard spiGuard; if (!spiGuard) { @@ -54,7 +40,7 @@ bool HardwareNFCConnectionPN532::begin() { return false; } - pn532_ = new Adafruit_PN532(pinSs_, spiBus); + pn532_ = new Adafruit_PN532(pins_.nss, &SPI); if (!pn532_) { Serial.println("PN532: Failed to allocate"); return false; @@ -116,10 +102,10 @@ bool HardwareNFCConnectionPN532::hardwareReset() { } // Toggle RST pin for hardware reset: forces state machine reboot - pinMode(pinRst_, OUTPUT); - digitalWrite(pinRst_, LOW); + pinMode(pins_.rst, OUTPUT); + digitalWrite(pins_.rst, LOW); delay(10); - digitalWrite(pinRst_, HIGH); + digitalWrite(pins_.rst, HIGH); delay(50); // PN532 boot time before first SPI command if (pn532_) { diff --git a/src/HardwareNFCConnectionPN532.h b/src/HardwareNFCConnectionPN532.h index 647a1af..e6f4844 100644 --- a/src/HardwareNFCConnectionPN532.h +++ b/src/HardwareNFCConnectionPN532.h @@ -2,6 +2,7 @@ #define HARDWARE_NFC_CONNECTION_PN532_H #include "NFCConnectionI.h" +#include "NfcReaderPins.h" #include "BoardPins.h" #include @@ -9,6 +10,8 @@ class HardwareNFCConnectionPN532 : public NFCConnectionI { public: HardwareNFCConnectionPN532(); + // For use after prepareNfcReaders() with the complete set of readers. + explicit HardwareNFCConnectionPN532(const NfcReaderPins& pins); ~HardwareNFCConnectionPN532() override; bool begin() override; @@ -33,8 +36,8 @@ class HardwareNFCConnectionPN532 : public NFCConnectionI { bool getDiagnosticSnapshot(ReaderDiagnostics& out) override; private: - // Effective NFC pins from ConfigurationManager (#201); BUSY slot unused on PN532 - uint8_t pinRst_ = 0, pinSs_ = 0, pinSck_ = 0, pinMosi_ = 0, pinMiso_ = 0; + NfcReaderPins pins_{}; + bool explicitPins_ = false; Adafruit_PN532* pn532_ = nullptr; opt_nfc_hal_t hal_; uint8_t currentUid_[10]; diff --git a/src/HardwareNFCConnectionRC522.cpp b/src/HardwareNFCConnectionRC522.cpp index f516840..2431d7a 100644 --- a/src/HardwareNFCConnectionRC522.cpp +++ b/src/HardwareNFCConnectionRC522.cpp @@ -1,7 +1,6 @@ #include "HardwareNFCConnectionRC522.h" #include "BoardPins.h" -#include "ConfigurationManager.h" #include "SharedSPIBus.h" #include "NfcSpiBus.h" @@ -44,6 +43,12 @@ HardwareNFCConnectionRC522::HardwareNFCConnectionRC522() { hal_.user_ctx = this; } +HardwareNFCConnectionRC522::HardwareNFCConnectionRC522(const NfcReaderPins& pins) + : HardwareNFCConnectionRC522() { + pins_ = pins; + explicitPins_ = true; +} + HardwareNFCConnectionRC522::~HardwareNFCConnectionRC522() { releaseReader(); } @@ -62,33 +67,12 @@ void HardwareNFCConnectionRC522::releaseReader() { } bool HardwareNFCConnectionRC522::begin() { - // Implements NFCConnectionI::begin(). Pin values come from the same - // ConfigurationManager slots used by PN5180/PN532; BUSY is not used. - auto& cfg = ConfigurationManager::getInstance(); - pinRst_ = cfg.getNfcPin(NfcPinId::Rst); - pinSs_ = cfg.getNfcPin(NfcPinId::Nss); - pinSck_ = cfg.getNfcPin(NfcPinId::Sck); - pinMosi_ = cfg.getNfcPin(NfcPinId::Mosi); - pinMiso_ = cfg.getNfcPin(NfcPinId::Miso); + if (!explicitPins_ && !prepareDefaultNfcReader(NfcReaderType::RC522, pins_)) return false; Serial.printf("RC522: starting — RST=%u SS=%u SCK=%u MOSI=%u MISO=%u SPI=%lu Hz\n", - pinRst_, pinSs_, pinSck_, pinMosi_, pinMiso_, + pins_.rst, pins_.nss, pins_.sck, pins_.mosi, pins_.miso, static_cast(RC522_SPI_HZ)); -#if defined(BOARD_SHARED_SPI) - if (!SharedSPIBus::begin(pinSck_, pinMiso_, pinMosi_, pinSs_, PIN_TFT_CS)) { - Serial.println("RC522: shared SPI initialization failed"); - return false; - } - SPIClass* spiBus = &SharedSPIBus::bus(); -#else - // Arduino's global SPI is the TFT's host on the WROOM (VSPI) and the - // S3-Zero (SPI2). Take the peripheral the PN5180 uses on this board. - static SPIClass nfcSpi(PN5180_SPI_BUS); - nfcSpi.begin(pinSck_, pinMiso_, pinMosi_, pinSs_); - SPIClass* spiBus = &nfcSpi; -#endif - SharedSPIBus::Guard spiGuard; if (!spiGuard) { Serial.println("RC522: shared SPI lock timeout during initialization"); @@ -99,20 +83,20 @@ bool HardwareNFCConnectionRC522::begin() { // Most RC522 breakouts expose the MFRC522 NRSTPD pin as RST. Give the // chip a deterministic power-down/reset pulse before the library's soft reset. - pinMode(pinRst_, OUTPUT); - digitalWrite(pinRst_, LOW); + pinMode(pins_.rst, OUTPUT); + digitalWrite(pins_.rst, LOW); delay(5); - digitalWrite(pinRst_, HIGH); + digitalWrite(pins_.rst, HIGH); delay(50); - chipSelect_ = new MFRC522DriverPinSimple(pinSs_); + chipSelect_ = new MFRC522DriverPinSimple(pins_.nss); if (!chipSelect_) { Serial.println("RC522: failed to allocate chip-select driver"); return false; } driver_ = new MFRC522DriverSPI( - *chipSelect_, *spiBus, SPISettings(RC522_SPI_HZ, MSBFIRST, SPI_MODE0)); + *chipSelect_, nfcSpiBus(), SPISettings(RC522_SPI_HZ, MSBFIRST, SPI_MODE0)); if (!driver_) { Serial.println("RC522: failed to allocate SPI driver"); releaseReader(); @@ -186,9 +170,9 @@ bool HardwareNFCConnectionRC522::hardwareReset() { return false; } - digitalWrite(pinRst_, LOW); + digitalWrite(pins_.rst, LOW); delay(5); - digitalWrite(pinRst_, HIGH); + digitalWrite(pins_.rst, HIGH); delay(50); if (!reader_->PCD_Init()) return false; diff --git a/src/HardwareNFCConnectionRC522.h b/src/HardwareNFCConnectionRC522.h index 0041cb9..e51be8d 100644 --- a/src/HardwareNFCConnectionRC522.h +++ b/src/HardwareNFCConnectionRC522.h @@ -2,6 +2,7 @@ #define HARDWARE_NFC_CONNECTION_RC522_H #include "NFCConnectionI.h" +#include "NfcReaderPins.h" #include #include @@ -16,6 +17,8 @@ class HardwareNFCConnectionRC522 : public NFCConnectionI { public: HardwareNFCConnectionRC522(); + // For use after prepareNfcReaders() with the complete set of readers. + explicit HardwareNFCConnectionRC522(const NfcReaderPins& pins); ~HardwareNFCConnectionRC522() override; bool begin() override; @@ -41,15 +44,13 @@ class HardwareNFCConnectionRC522 : public NFCConnectionI { bool getDiagnosticSnapshot(ReaderDiagnostics& out) override; private: + NfcReaderPins pins_{}; + bool explicitPins_ = false; + // These objects are unique to the RC522 backend. The MFRC522v2 library // separates chip-select control, SPI transport, and the reader protocol // into three objects, while the PN5180 and PN532 backends use their own // libraries' reader objects directly. - uint8_t pinRst_ = 0; - uint8_t pinSs_ = 0; - uint8_t pinSck_ = 0; - uint8_t pinMosi_ = 0; - uint8_t pinMiso_ = 0; MFRC522DriverPinSimple* chipSelect_ = nullptr; MFRC522DriverSPI* driver_ = nullptr; diff --git a/src/NfcReaderPins.cpp b/src/NfcReaderPins.cpp new file mode 100644 index 0000000..9472f7c --- /dev/null +++ b/src/NfcReaderPins.cpp @@ -0,0 +1,44 @@ +#include "NfcReaderPins.h" +#include "BoardPins.h" +#include "ConfigurationManager.h" +#include "NfcSpiBus.h" +#include "SharedSPIBus.h" + +bool prepareNfcReaders(NfcReaderType type, const NfcReaderPins* readers, size_t count) { + if (!readers || !count) return false; + const auto& first = readers[0]; +#if defined(BOARD_SHARED_SPI) + SharedSPIBus::Guard guard; + if (!guard) return false; + // SharedSPIBus::begin handles the first reader and the TFT under this lock. + const size_t firstUnprepared = 1; +#else + const size_t firstUnprepared = 0; +#endif + for (size_t i = firstUnprepared; i < count; ++i) { + pinMode(readers[i].nss, OUTPUT); + digitalWrite(readers[i].nss, HIGH); + } +#if defined(BOARD_SHARED_SPI) + return SharedSPIBus::begin(first.sck, first.miso, first.mosi, first.nss, PIN_TFT_CS); +#else + SPIClass& bus = type == NfcReaderType::PN532 ? SPI : nfcSpiBus(); + return bus.begin(first.sck, first.miso, first.mosi, + type == NfcReaderType::PN5180 ? -1 : first.nss); +#endif +} + +bool prepareDefaultNfcReader(NfcReaderType type, NfcReaderPins& pins) { + auto& cfg = ConfigurationManager::getInstance(); + pins = {cfg.getNfcPin(NfcPinId::Rst), cfg.getNfcPin(NfcPinId::Nss), + cfg.getNfcPin(NfcPinId::Busy), cfg.getNfcPin(NfcPinId::Sck), + cfg.getNfcPin(NfcPinId::Mosi), cfg.getNfcPin(NfcPinId::Miso)}; + // Legacy single-reader board wiring only. Explicit readers do not own + // these unused inputs (S3-Zero GPIO10/12 can be other readers' selects). + if (type == NfcReaderType::PN5180) { + if (PIN_PN5180_IRQ >= 0) pinMode(PIN_PN5180_IRQ, INPUT); + if (PIN_PN5180_GPIO >= 0) pinMode(PIN_PN5180_GPIO, INPUT); + if (PIN_PN5180_AUX >= 0) pinMode(PIN_PN5180_AUX, INPUT); + } + return prepareNfcReaders(type, &pins, 1); +} diff --git a/src/NfcReaderPins.h b/src/NfcReaderPins.h new file mode 100644 index 0000000..0ef354a --- /dev/null +++ b/src/NfcReaderPins.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +struct NfcReaderPins { + uint8_t rst, nss, busy, sck, mosi, miso; +}; + +enum class NfcReaderType { PN5180, PN532, RC522 }; + +// Before beginning explicitly pinned readers, prepare their common SPI bus +// and deselect every reader. All sets must describe the same SPI wires. +bool prepareNfcReaders(NfcReaderType type, const NfcReaderPins* readers, size_t count); + +// Standalone default connections resolve the effective configuration at begin(). +bool prepareDefaultNfcReader(NfcReaderType type, NfcReaderPins& pins);