ESP32/ESP8266 RFID to VSPI and SD card to HSPI

442 Views Asked by At

I'm trying to read rfid tag and save it to sd card so I added sd to HSPI and rfid to VSPI, when I only run RFID code it works but when I run both code togather sd card does not works, later I did sd.begin(cs_pin, HSPISD); where HSPISD is the class and now sd card works but RFID doesn't. I tried both esp32 and 8266 and I need this in both device ESP32 PIN::::

RFID_MISO = G19
RFID_MOSI = G23
RFID_SCK = G18
RFID_CS = G5

SD_MISO = G12
SD_MOSI = G13
SD_SCK = G14
SD_CS = G15
#include <SPI.h>
#include <SD.h>
#include <MFRC522.h>

File myFile;
const int CS = 15;


constexpr uint8_t RST_PIN = 22;
constexpr uint8_t SS_PIN = 5;
String tag;
SPIClass VSPIID(VSPI);
SPIClass HSPISD(HSPI);
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;


void WriteFile(const char * path, const char * message){
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open(path, FILE_WRITE);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.printf("Writing to %s ", path);
    myFile.println(message);
    myFile.close(); // close the file:
    Serial.println("completed.");
  } 
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening file ");
    Serial.println(path);
  }
}


void ReadFile(const char * path){
  // open the file for reading:
  myFile = SD.open(path);
  if (myFile) {
     Serial.printf("Reading file from %s\n", path);
     // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close(); // close the file:
  } 
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  digitalWrite(CS, HIGH);    //  DESELECT (high) SD Card SPI
  SD.end();
}

void setup() {
  Serial.begin(115200);
  pinMode(SS_PIN, OUTPUT);
  pinMode(CS, OUTPUT);
  VSPIID.begin();
  HSPISD.begin();
  // pinMode(vspi->pinSS(), OUTPUT); //VSPI SS
  // pinMode(hspi->pinSS(), OUTPUT); //HSPI SS
  if (!SD.begin(CS, HSPISD)) {
    Serial.println("initialization failed!");
  }
  Serial.println("initialization done.");
  WriteFile("/test.txt", "ElectronicWings.com");
  ReadFile("/test.txt");
  rfid.PCD_Init();
  Serial.println(digitalRead(SS_PIN));
  Serial.println(digitalRead(CS));
  digitalWrite(CS, HIGH);
  digitalWrite(SS_PIN, LOW);
}

void loop() {
  if (rfid.PICC_IsNewCardPresent()){
    if (rfid.PICC_ReadCardSerial()) {
      for (byte i = 0; i < 4; i++) {
        tag += rfid.uid.uidByte[i];
      }
      rfid.PICC_HaltA();
      rfid.PCD_StopCrypto1();
      Serial.println(tag);
      tag = "";
      // if (digitalRead(D3) == LOW){
      //   while (true) {
      //     delay(100);
      //     if (digitalRead(D3) == HIGH)
      //         break;
      //   }

      // }
    }
  }
}
0

There are 0 best solutions below