Skip to main content
GMelo.2
Associate
May 8, 2020
Question

How do I program my STM32L4R9 with SDMMC and FatFS?

  • May 8, 2020
  • 5 replies
  • 1794 views

Hello,

I would like to write a simple program for writing CSV files to a micro SD card on a custom board I made with my STM32L4R9. The card works on 3.3V and my STM on 1.8V, so I'm using a level shifter.

For now, I selected SDMMC1 4 bits wide bus and FatFS. Next, I tried basing myself on the example code for the evaluation board (STM32L4R9I-Discovery), but I get an error when the function SD_PowerOn() (on file stm32l4xx_hal_sd.c) is called. Specifically, I get a timeout error on this line:

 /* SEND CMD55 APP_CMD with RCA as 0 */
 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
 if(errorstate != HAL_SD_ERROR_NONE)
 {
 return errorstate;
 }

And my main looks like this:

int main(void)
{
 /* MCU Configuration--------------------------------------------------------*/
 
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
 /* Configure the system clock */
 SystemClock_Config();
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_SDMMC1_SD_Init();
 MX_FATFS_Init();
 /* USER CODE BEGIN 2 */
 FRESULT res;
 
 res = f_mkfs(SDPath, FM_ANY, 0, workBuffer, sizeof(workBuffer)); // This is where I get problems
 
 if (res != FR_OK)
 {
 Error_Handler();
 }

I cannot get past the f_mkfs() function call.

I also could not take the examples for the STM32L476 on the forum as they rely on DMA interruptions, and I also do not know how to configure them on my STM32L4+, as the SDMMC has its own connection to the DMA bus.

As a design error, I cannot detect when a card is plugged in (the signal from the connector is missing on the board).

Does anyone know what config I shoud use on CubeMX and what functions I should call?

This topic has been closed for replies.

5 replies

Tesla DeLorean
Guru
May 8, 2020

Sorry, don't use CubeMX

Do you have a STM32L4R9I-DISCO to test on?

Do you have a U(S)ART / pins on your board you use for debugging?

Really shouldn't need to use f_mkfs() on preformated/used SD cards. Much better to FULLY validate the read methods in the DISKIO layer first, before moving to destructive writes.

Check the WEAK designator is not used on routines (IRQ / Callbacks) you expect to have linked. Check that they are.

Are you using a transceiver designed for SD cards, with direction control pins, or some generic level shifter? Interface very sensitive to signal skewing, and delays.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
GMelo.2
GMelo.2Author
Associate
May 8, 2020

What do you use instead of CubeMX?

I do not have a STM32L4R9I-DISCO to test on :\

I have free pins on my board I can use for debugging.

I will try the DISKIO layer, so I do not need to use functions on ff.h?

It is a generic level shifter, but it should allow for signals under 50 MHz: http://www.ti.com/lit/ds/symlink/txb0108.pdf, i'll try to check under the oscilloscope.

Thanks a lot!

Tesla DeLorean
Guru
May 8, 2020

Using the HAL libraries directly without using a code generator.

There are assorted working examples, for different boards, under the CubeL4 repository trees

STM32Cube_FW_L4_V1.14.0\Projects\32L4R9IDISCOVERY\Applications\FatFs\FatFs_uSD_Standalone

Yes, you'll need to use the FATFS stuff, but the low level read/write code needs to work properly for there to be any hope that the high level code will work.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..