SD card problem with SPI

Post here first, or if you can't find a relevant section!
Post Reply
Richway
Posts: 1
Joined: Mon Apr 15, 2024 11:59 am

SD card problem with SPI

Post 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(SD2_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 .
User avatar
fpiSTM
Posts: 1745
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: SD card problem with SPI

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

Return to “General discussion”