Skip to content

Commit

Permalink
Allow specification of SPI interface instead of default on ESP32
Browse files Browse the repository at this point in the history
  • Loading branch information
ncmreynolds committed Apr 16, 2024
1 parent 51dec69 commit 7f6c491
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@
*.exe
*.out
*.app
examples/CustomSPI/CheckCardAuthorisationESP32HSPI/debug.cfg
examples/CustomSPI/CheckCardAuthorisationESP32HSPI/debug.svd
examples/CustomSPI/CheckCardAuthorisationESP32HSPI/debug_custom.json
examples/CustomSPI/CheckCardAuthorisationESP32/debug.cfg
examples/CustomSPI/CheckCardAuthorisationESP32/debug.svd
examples/CustomSPI/CheckCardAuthorisationESP32/debug_custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Example of the Trivial RFID authorisation library
*
* https://github.com/ncmreynolds/TrivialRFIDauthorisation
*
* This example will authorise the card for a single ID when presented to the reader
*
* Here you can see how to change the pins used for connecting the reader to the default SPI interface (VSPI) on an ESP32
*
* Unlike many other microcontrollers ESP32s can be flexibly configured use _most_ of their GPIO pins for hardware SPI
*
* This example was tested with an ESP32S3 but should work with most ESP32s if you set appropriate pins
*
*/
#include <TrivialRFIDauthorisation.h>

#if defined(ESP32)
const int rfidCsPin = 47; //Set the CS pin
const int rfidClkPin = 48; //Set the CLK pin
const int rfidCopiPin = 45; //Set the COPI pin
const int rfidCipoPin = 38; //Set the CIPO pin
TrivialRFIDauthorisation rfid(rfidClkPin, rfidCipoPin, rfidCopiPin, rfidCsPin);
#else
#error This sketch will only work on an ESP32
#endif

uint8_t idToAuthorise = 28;

void setup() {
Serial.begin(115200);
//rfid.debug(Serial); //Enables debugging. Doing this before 'begin' means you can see the reader initialisation information!
if(rfid.begin()) { //Start the RFID reader
Serial.println(F("*******************************"));
Serial.println(F("Checking authorisation of cards"));
Serial.println(F("*******************************"));
}
else
{
Serial.println(F("*******************************"));
Serial.println(F("RFID reader self test failed"));
Serial.println(F("*******************************"));
delay(0);
}
}

void loop() {
rfid.pollForCard(); //Must run regularly to read and process the card
if(rfid.cardPresent() == true && rfid.cardChanged() == true)
{
bool result = rfid.checkCardAuthorisation(idToAuthorise);
Serial.println(F("********************************"));
Serial.print(F("Checking card for authorisation against ID:"));
Serial.print(idToAuthorise);
Serial.print(F(" - "));
if(result == true)
{
Serial.println(F("authorised"));
}
else
{
Serial.println(F("not authorised"));
}
Serial.println(F("********************************"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Example of the Trivial RFID authorisation library
*
* https://github.com/ncmreynolds/TrivialRFIDauthorisation
*
* This example will authorise the card for a single ID when presented to the reader
*
* Here you can see how to change the pins used for connecting the reader to the second SPI interface (HSPI) on an ESP32
*
* Unlike many other microcontrollers ESP32s can be flexibly configured use _most_ of their GPIO pins for hardware SPI and have multiple SPI interfaces
*
* This example was tested with an ESP32S3 but should work with most ESP32s if you set appropriate pins
*
*/
#include <TrivialRFIDauthorisation.h>

#if defined(ESP32)
const int rfidCsPin = 47; //Set the CS pin
const int rfidClkPin = 48; //Set the CLK pin
const int rfidCopiPin = 45; //Set the COPI pin
const int rfidCipoPin = 38; //Set the CIPO pin
SPIClass hspi = SPIClass(HSPI); //Create an instance of an SPI driver specifically for the RFID reader
TrivialRFIDauthorisation rfid(hspi, rfidClkPin, rfidCipoPin, rfidCopiPin, rfidCsPin);
#else
#error This sketch will only work on an ESP32
#endif

uint8_t idToAuthorise = 28;

void setup() {
Serial.begin(115200);
//rfid.debug(Serial); //Enables debugging. Doing this before 'begin' means you can see the reader initialisation information!
if(rfid.begin()) { //Start the RFID reader
Serial.println(F("*******************************"));
Serial.println(F("Checking authorisation of cards"));
Serial.println(F("*******************************"));
}
else
{
Serial.println(F("*******************************"));
Serial.println(F("RFID reader self test failed"));
Serial.println(F("*******************************"));
delay(0);
}
}

void loop() {
rfid.pollForCard(); //Must run regularly to read and process the card
if(rfid.cardPresent() == true && rfid.cardChanged() == true)
{
bool result = rfid.checkCardAuthorisation(idToAuthorise);
Serial.println(F("********************************"));
Serial.print(F("Checking card for authorisation against ID:"));
Serial.print(idToAuthorise);
Serial.print(F(" - "));
if(result == true)
{
Serial.println(F("authorised"));
}
else
{
Serial.println(F("not authorised"));
}
Serial.println(F("********************************"));
}
}
15 changes: 11 additions & 4 deletions src/TrivialRFIDauthorisation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@
#endif

TrivialRFIDauthorisation::TrivialRFIDauthorisation(const uint8_t cspin) : //Constructor function and member constructors
rfid_ss_pin_(cspin),
rfid_driver_{rfid_ss_pin_},
rfid_cs_pin_(cspin),
rfid_driver_{rfid_cs_pin_},
rfid_reader_{rfid_driver_}
{
}

#if defined(ESP32)
TrivialRFIDauthorisation::TrivialRFIDauthorisation(const uint8_t clkPin, const uint8_t cipoPin, const uint8_t copiPin, const uint8_t csPin) : //Constructor function and member constructors
rfid_ss_pin_(csPin),
rfid_driver_{rfid_ss_pin_},
rfid_cs_pin_(csPin),
rfid_driver_{rfid_cs_pin_},
rfid_reader_{rfid_driver_}
{
SPI.begin(clkPin, cipoPin, copiPin, csPin); //Start the default SPI interface with these pins
}
TrivialRFIDauthorisation::TrivialRFIDauthorisation(SPIClass &spiInterface, const uint8_t clkPin, const uint8_t cipoPin, const uint8_t copiPin, const uint8_t csPin) : //Constructor function and member constructors
rfid_cs_pin_(csPin),
rfid_driver_{rfid_cs_pin_, spiInterface},
rfid_reader_{rfid_driver_}
{
spiInterface.begin(clkPin, cipoPin, copiPin, csPin); //Start the specified SPI interface with these pins
}
#endif

TrivialRFIDauthorisation::~TrivialRFIDauthorisation() //Destructor function
Expand Down
4 changes: 3 additions & 1 deletion src/TrivialRFIDauthorisation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class TrivialRFIDauthorisation {
#if defined(ESP32)
TrivialRFIDauthorisation(uint8_t clkPin, uint8_t cipoPin,
uint8_t copiPin, uint8_t csPin); //Constructor function with non-default SPI pins, which can be set freely on an ESP32
TrivialRFIDauthorisation(SPIClass &spiInterface, uint8_t clkPin, uint8_t cipoPin,
uint8_t copiPin, uint8_t csPin); //Constructor function with non-default SPI interface and pins, which can be set freely on an ESP32
#endif
~TrivialRFIDauthorisation(); //Destructor function
bool begin(uint8_t sector = 1); //Start the RFID authentication on a specific sector
Expand Down Expand Up @@ -54,7 +56,7 @@ class TrivialRFIDauthorisation {
#ifdef TrivialRFIDauthorisationSupportDebugging
Stream *debugStream_ = nullptr; //The stream used for debugging
#endif
MFRC522DriverPinSimple rfid_ss_pin_;
MFRC522DriverPinSimple rfid_cs_pin_;
MFRC522DriverSPI rfid_driver_;
MFRC522 rfid_reader_;
MFRC522::MIFARE_Key keyA_;
Expand Down

0 comments on commit 7f6c491

Please sign in to comment.