miguelbalboa / rfid

Arduino RFID Library for MFRC522
The Unlicense
2.75k stars 1.43k forks source link

Added multiple SPI capability with minimal disruptions to the existing code #520

Closed devrim-oguz closed 4 years ago

devrim-oguz commented 4 years ago

Added a new constructor for changing the selected SPI port and changed all of the SPI class acesses to an SPIClass pointer. Increased compatibility with other devices that has multiple SPI ports (Such as ESP32 or STM32 boards)

Example usage: MFRC522 mfrc522(SS_PIN, RST_PIN, SPI); //Works the same as: MFRC522 mfrc522(SS_PIN, RST_PIN) or MFRC522 mfrc522(SS_PIN, RST_PIN, SPI2); //Given that you created another SPIClass named SPI2

Note: You can still use it as before, like; MFRC522 mfrc522(SS_PIN, RST_PIN); or MFRC522 mfrc522(RST_PIN);

Tested on Arduino UNO and STM32F103RB, works without a problem.

Q A
Bug fix? no
New feature? yes
Doc update? no
BC breaks? no
Deprecations? no
Fixed tickets #
theMubashir919 commented 3 years ago

I'm trying to use 2 spi buses simultaneously on my esp32 [vspi and hspi]. I'm connecting the rfid rc522 sensor on the hspi bus and tried to use your fork but I'm getting the error "invalid user-defined conversion from 'SPIClass*' to 'SPIClass&' [-fpermissive]" Can you explain how do you make SPIClass?? My code is as follows:

#include <SPI.h>
#include <MFRC522.h>

SPIClass * hspi = NULL;

#define RST_PIN 2     // RST-PIN for RC522 - RFID - SPI - Modul GPIO5 
#define SS_PIN  15    // SDA-PIN for RC522 - RFID - SPI - Modul GPIO4 

void setup() {
  hspi = new SPIClass(HSPI);

  MFRC522 mfrc522(SS_PIN, RST_PIN, hspi); // Create MFRC522 instance

  //initialise hspi with default pins
  //SCLK = 14, MISO = 12, MOSI = 13, SS = 15
  hspi->begin();
  Serial.begin(115200);    // Initialize serial communications

  mfrc522.PCD_Init();    // Init MFRC522

  pinMode(15, OUTPUT); //HSPI SS
}

void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    delay(50);
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    delay(50);
    return;
  }
  Serial.print(F("Card UID:"));
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();
  digitalWrite(SS_PIN, HIGH);
}

// Helper routine to dump a byte array as hex values to Serial
void dump_byte_array(byte *buffer, byte bufferSize) {
  String userid;
  for (byte i = 0; i < bufferSize; i++) {
    //Serial.print(buffer[i] < 0x10 ? " 0" : " ");
//    Serial.print(buffer[i], HEX);
      userid += String(buffer[i]);
  }
  Serial.print(userid);
}
devrim-oguz commented 3 years ago

Hello, thanks for using the fork. I think you can simply create it this way;

SPIClass SPI_Two( mosi_pin, miso_pin, sck_pin );
MFRC522 RFID_Reader( ss_pin, rst_pin, SPI_Two );

void rfidInit() {
    SPI_Two.begin();
    RFID_Reader.PCD_Init();
}

Maybe this can be added to the code examples at some point.

devrim-oguz commented 3 years ago

I think in your case, it becomes;

SPIClass hspi(HSPI);
MFRC522 mfrc522(SS_PIN, RST_PIN, hspi);

void setup() {
  hspi.begin();
  mfrc522.PCD_Init();
  ...
devrim-oguz commented 3 years ago

@Rotzbua you could have added the #521 to the fork milestone instead of this version. I closed this pull request on purpose because my editor changed a lot of empty lines because of some configuration problems. It should still work, however commits seems more than what they actually are. #521 is the better version of this.

rickyanwar commented 3 years ago

hi nice fork i trying in esp32 with HSPI with code in a comment on top but did not work


#include <SPI.h>
#include <MFRC522.h>

//initialise hspi with default pins
 //SCLK = 14, MISO = 12, MOSI = 13, SS = 15
#define RST_PIN 2     // RST-PIN for RC522 - RFID - SPI - Modul GPIO5 
#define SS_PIN  15    // SDA-PIN for RC522 - RFID - SPI - Modul GPIO4 
SPIClass hspi(HSPI);
MFRC522 mfrc522(SS_PIN, RST_PIN, hspi);

void setup() {
   hspi.begin();
  mfrc522.PCD_Init();    // Init MFRC522
  Serial.begin(115200);    // Initialize serial communications

  pinMode(15, OUTPUT); //HSPI SS
}

void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    delay(50);
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    delay(50);
    return;
  }
  Serial.print(F("Card UID:"));
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();
  digitalWrite(SS_PIN, HIGH);
}

// Helper routine to dump a byte array as hex values to Serial
void dump_byte_array(byte *buffer, byte bufferSize) {
  String userid;
  for (byte i = 0; i < bufferSize; i++) {
    //Serial.print(buffer[i] < 0x10 ? " 0" : " ");
//    Serial.print(buffer[i], HEX);
      userid += String(buffer[i]);
  }
  Serial.print(userid);
}

image

yoprogramo commented 3 years ago

Excellent solution, @devrim-oguz @miguelbalboa ¿this will be merged in the code or not? I need use this feature right now and I would prefer use the original repo.

devrim-oguz commented 3 years ago

Excellent solution, @devrim-oguz @miguelbalboa ¿this will be merged in the code or not? I need use this feature right now and I would prefer use the original repo.

He says this will not be added to the main repo. You can manually make the changes in the main repo or use my branch if you want. Not many lines have changed, it can easily be done manually.

devrim-oguz commented 3 years ago

I advise you to use the #521 commit.

yoprogramo commented 3 years ago

Excellent solution, @devrim-oguz @miguelbalboa ¿this will be merged in the code or not? I need use this feature right now and I would prefer use the original repo.

He says this will not be added to the main repo. You can manually make the changes in the main repo or use my branch if you want. Not many lines have changed, it can easily be done manually.

Resubmitted on #571, I used the code in Lilygo T-Display and works as expected. Finger crossed...