[Solved] How to get SdFat to work on the ST Core/STM32F103

Post here first, or if you can't find a relevant section!
Franke39
Posts: 30
Joined: Sat Mar 28, 2020 6:39 pm

[Solved] How to get SdFat to work on the ST Core/STM32F103

Post by Franke39 »

I thought I could figure this out... but since I am asking here, you know that I didn't :(
I can't get SdFat to work.

Setup:
• STM32F103C8 BluePill clone
• Official ST Core
• Arduino IDE 1.8.13
• SD module wired to SPI1 (SS = PA4, SCK = PA5, MISO = PA6, and MOSI = PA7)
• SdFat v2.0.4

The standard Arduino SD example "Cardinfo.ino" (uses SD.h) works, so my wiring and power are correct:
(I printed the various SPI pins to check their value)

Code: Select all

SD Example Program Cardinfo uses SD.h

Changed 4 to SS
const int chipSelect = SS;

SPI 1 Settings
MISO is:26 PA6 is:26
MOSI is:27 PA7 is:27
SCK  is:25 PA5 is:25
SS   is:24 PA4 is:24

SPI 2 Settings
SPI2 MISO/PB14 is:14
SPI2 MOSI/PB15 is:13
SPI2  SCK/PB13 is:15
SPI2  CSS/PB12 is:16

Initializing SD card...Wiring is correct and a card is present.
Then I compiled and upload the "bench" example program and get this:

Code: Select all

Use a freshly formatted SD for best performance.

Type any character to start
FreeStack: 17576
begin() failed
Do not reformat the SD.
SdError: 0XC,0X7F
To troubleshoot, I used Serial.print to show the values of the various pins:

Code: Select all

SPI 1 Settings
MISO is:26 PA6 is:26
MOSI is:27 PA7 is:27
SCK  is:25 PA5 is:25
SS   is:24 PA4 is:24

SPI 2 Settings
SPI2 MISO/PB14 is:14
SPI2 MOSI/PB15 is:13
SPI2  SCK/PB13 is:15
SPI2  CSS/PB12 is:16

Use a freshly formatted SD for best performance.

Type any character to start
FreeStack: 17576
begin() failed
Do not reformat the SD.
SdError: 0XC,0X7F
I assume SdFat does work on the ST Core, so what am I doing wrong and how can I troubleshoot this further?
Thanks in advance for any suggestion or help.
Last edited by Franke39 on Wed Jan 20, 2021 6:55 pm, edited 1 time in total.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Does SdFat work on the ST Core on a STM32F103?

Post by mrburnette »

It works. Both STM core and Roger's core... But API's are different.
viewtopic.php?p=5693#p5693

You can search the forum from the "Search..." dialog in the upper-right of the webpage.
In this case, "SD Card" gave me the answer ...
Franke39
Posts: 30
Joined: Sat Mar 28, 2020 6:39 pm

Re: Does SdFat work on the ST Core on a STM32F103?

Post by Franke39 »

Further testing...
On the latest version of SdFat (v2.0.4) there is an example program called "BackwardCompatability" that allows you how to easily switch between SD and SdFat.

Setting USE_SD_H to 1 uses SD.h, resulting in this:

Code: Select all

Type any character to begin.
Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
Then I set USE_SD_H to 0 to use SdFat and the result is:

Code: Select all

Using SdFat.h. Set USE_SD_H nonzero to use SD.h.

Type any character to begin.
Initializing SD card...initialization failed!
Using SD.h. Set USE_SD_H zero to use SdFat.h.
Franke39
Posts: 30
Joined: Sat Mar 28, 2020 6:39 pm

Re: Does SdFat work on the ST Core on a STM32F103?

Post by Franke39 »

mrburnette wrote: Wed Jan 20, 2021 12:33 am It works. Both STM core and Roger's core... But API's are different.
viewtopic.php?p=5693#p5693

You can search the forum from the "Search..." dialog in the upper-right of the webpage.
In this case, "SD Card" gave me the answer ...
Thanks for the quick reply!
I have spent hours searching and looking for an answer before I posted here.
I have read the thread you provided, but I am not trying to use SPI2. Just the default SPI1
I understand the APIs are different, that is why I use the author provided example programs as they as known to work.

Mini update: Have a look at my previous post where I used the v2 SdFat example program "BackwardCompatability" where you can switch between SD and SdFat.
For me, it only works when I use SD.h :(
Last edited by Franke39 on Wed Jan 20, 2021 12:54 am, edited 1 time in total.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Does SdFat work on the ST Core on a STM32F103?

Post by mrburnette »

Maybe fpiSTM can add some insight.
Franke39
Posts: 30
Joined: Sat Mar 28, 2020 6:39 pm

Re: Does SdFat work on the ST Core on a STM32F103?

Post by Franke39 »

mrburnette wrote: Wed Jan 20, 2021 12:53 am Maybe fpiSTM can add some insight.
That would be nice :)
In the meantime, I will set up the logic analyzer and scope to verify SPI levels and waveforms.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Does SdFat work on the ST Core on a STM32F103?

Post by fpiSTM »

Well, I never use SDFat.
The STM32 core API follows the Arduino API reference so if the library use them then it should work.
Anyway I've tried the SDinfo default example and it can access my SD card info but the begin failed.
I've have no more clue and does not have time to dig into this :?
Franke39
Posts: 30
Joined: Sat Mar 28, 2020 6:39 pm

Re: Does SdFat work on the ST Core on a STM32F103?

Post by Franke39 »

fpiSTM wrote: Wed Jan 20, 2021 9:00 am Well, I never use SDFat.
The STM32 core API follows the Arduino API reference so if the library use them then it should work.
Anyway I've tried the SDinfo default example and it can access my SD card info but the begin failed.
I've have no more clue and does not have time to dig into this :?
No worries, I understand. Thanks for looking into it :)
Franke39
Posts: 30
Joined: Sat Mar 28, 2020 6:39 pm

Re: [Solved] How to get SdFat to work on the ST Core/STM32F103

Post by Franke39 »

Bill Greiman (author of SdFat) has quickly provided the fix:
(Thank you Bill!)

Simply change:

Code: Select all

 if (!SD.begin(SD_CS_PIN)) { 
to:

Code: Select all

 if (!SD.begin(SD_CS_PIN, SD_SCK_MHZ(4) )) { 
and SdFat works on the STM32F103 using the ST core.

What is interesting, is that the actual SPI clock speed didn't change... the logic analyzer still measured it at ~2.28 Mhz before and after the above change. That's kind of odd, but it works :)
User avatar
Bakisha
Posts: 139
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 5
Contact:

Re: [Solved] How to get SdFat to work on the ST Core/STM32F103

Post by Bakisha »

Franke39 wrote: Wed Jan 20, 2021 6:56 pm What is interesting, is that the actual SPI clock speed didn't change... the logic analyzer still measured it at ~2.28 Mhz before and after the above change. That's kind of odd, but it works :)
As i recall, SdFat is rounding down requested speed to actual SPI speed available.
You can only have SPI working on 36MHz, 18MHz, 9Mhz, 4.5MHz, 2.25MHz, 1.125Mhz, etc..
What i found annoying in SdFat is broken compatibility between 1.1.4 and 2.0.0 version.
Post Reply

Return to “General discussion”