Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from

Conversation

devrim-oguz
Copy link

@devrim-oguz devrim-oguz commented Jul 15, 2020

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 #

@Rotzbua Rotzbua added this to the fork milestone Jul 25, 2020
@theMubashir919
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

@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
Copy link

rickyanwar commented Jan 22, 2021

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
Copy link

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
Copy link
Author

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
Copy link
Author

I advise you to use the #521 commit.

@yoprogramo
Copy link

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants