Closed devrim-oguz closed 4 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);
}
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.
I think in your case, it becomes;
SPIClass hspi(HSPI);
MFRC522 mfrc522(SS_PIN, RST_PIN, hspi);
void setup() {
hspi.begin();
mfrc522.PCD_Init();
...
@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.
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);
}
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.
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.
I advise you to use the #521 commit.
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...
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)
orMFRC522 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);
orMFRC522 mfrc522(RST_PIN);
Tested on Arduino UNO and STM32F103RB, works without a problem.