[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

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

Post by Franke39 »

Bakisha wrote: Wed Jan 20, 2021 7:28 pm 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.
Agreed on the SPI clock speed.
In my case SdFat with the ST Core/STM32F1 didn't work on 1.1.4 either.
Maybe it is my board... I will try a few different ones tomorrow.
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 »

[Update]
I spent the day reading the SD Assoc "Physical Layer Simplified Spec v8", and then decoded what was on the SPI bus.
I was curious to understand what was going on and the low level commands.

It starts off with a slow SPI CLK of 280 Khz, then does a card reset, and checks and sets the SD cards voltages.
Then the clock frequency switches to a higher freq (in my case to 2.2xx Mhz) followed by the set up and data transfer.
(I still need to decode that section)

Here is what happens with the unmodified example code:

Code: Select all

if (!SD.begin(SD_CS_PIN)) {
SdFat+STM32F1_SD_fail_point.png
SdFat+STM32F1_SD_fail_point.png (79.77 KiB) Viewed 1872 times

As you can see, the SPI clock stops.


Here is the CLK frequency transition point with the fix:

Code: Select all

if (!SD.begin(SD_CS_PIN, SD_SCK_MHZ(4) )) {
SD+STM32F1_SD_non-fail_point.png
SD+STM32F1_SD_non-fail_point.png (78.26 KiB) Viewed 1872 times

It seems like the STM32F1 couldn't switch to the higher SPI CLK frequency. -OR- it detected an error and quit.
I will test this on a few new boards... I am curious to see if I have a weak/bad one :)
User avatar
Bakisha
Posts: 140
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 »

When i was doing project with SdFat (but 1.1.4 version) on (original and fake) bluepills. It compiled and run on both cores (plus, it also works on STM32F401CC/F411CE/F407VE boards that i have). Maximum speed was 18MHz, but it run fine on 1MHz too. I tested on various SD Cards, but maximum size was 8GB, FAT32 formatted. It even worked with soft SPI.
I can't say about latest version, because project can't be compiled anymore.

Did you tried different SD Cards? Or faster SPI speed?
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 »

The SD card I used is a full size (not micro) 32GB SanDisk Ultra (30 MBs). I have used this exact card in many Uno/Nano/ESP32 projects and it has been very reliable. The SD reader is one of the cheap ones, that doesn't have a logic level shifter. BUT since the STM32 is 3.3 volt, a level shifter is not required.

I did some testing yesterday.
When I use SD_SCK_MHZ(4), the measured SPI CLK is about 2.2 Mhz. I assume that is 72 Mhz system clock divided by 32 or 2.25 Mhz
Using SD_SCK_MHZ(10), the measured SPI CLK is about 8 Mhz. (72/8 or 9 Mhz)

For the current project, the 8-9 Mhz SPI clock is more than enough speed.
Post Reply

Return to “General discussion”