Page 1 of 1

SD card problem with SPI

Posted: Mon Apr 15, 2024 2:06 pm
by Richway
Hi all , I want to use SD card with STM32F411CEU6 Blackpill board with SPI.

Pinout says the SPI pin:

MISO1 is PA6
MOSI1 is PA7
SCK1 is PA5
my CS pin is PA4

I am using SdFat Library with "Adafruit Fork by Bukk Greiman" and using SD card module with FAT32 card.
And using the SPI API like this:
SPIClass::SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)

my code:

Code: Select all

 
#include <SPI.h>
//#include <SD.h>
#include "SdFat.h"

#define SD_CS_PIN PA4
static SPIClass mySPI1(PA7, PA6, PA5, SD_CS_PIN);
#define SD1_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_FULL_SPEED, &mySPI1)

// #define SD_CS_PIN PB12
// static SPIClass mySPI2(PB15, PB14, PB13, SD_CS_PIN);
// #define SD2_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_FULL_SPEED, &mySPI2)

SdFat SD;
File myFile;

void setup() {
  Serial.begin(9600);
  Serial.print("Initializing SD card...");

  if (!SD.begin(SD1_CONFIG)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  myFile = SD.open("test.txt", FILE_WRITE);


  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("test123");
 
    myFile.close();
    Serial.println("done.");
  } else {

    Serial.println("error opening test.txt");
  }


  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");


    while (myFile.available()) {
      Serial.write(myFile.read()); 
    }

    myFile.close();
  } else {

    Serial.println("error opening test.txt");
  }
}

void loop() {
  
}
no matter SPI1 or SPI2 , i still get "initialization failed".

i have checked the module and card is working fine on Arduino uno.

Can anyone please help me to get SPI SD working with my STM32F411CEU6 Blackpill board ? Thanks .

Re: SD card problem with SPI

Posted: Mon Apr 15, 2024 2:29 pm
by fpiSTM
You should use the official SDFat library from Bill Greiman.
https://github.com/greiman/

We made some fixes within the 2.7.1 core version and works as expected.