cancel
Showing results for 
Search instead for 
Did you mean: 

How do I program my STM32L4R9 with SDMMC and FatFS?

GMelo.2
Associate II

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?

5 REPLIES 5

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
Up vote any posts that you find helpful, it shows what's working..

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!

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
Up vote any posts that you find helpful, it shows what's working..

Thank you. I will try to do that as well. I will come back as soon as I have made some progress.

The problem might also come from the TXB0108 level translator, as you've suggested. I've seen some complaints about it on the internet. I'll try lowering the frequency and maybe changing it for its more powerful brother, TXS0108E.

Doesn't the L4+ have direction and clock feed back pins to support transceivers?

The H7 does, and uses an NXP MCM on the EVAL board

https://www.st.com/resource/en/application_note/dm00525510-getting-started-with-stm32h7-series-sdmmc-host-controller-stmicroelectronics.pdf

Having a working hardware example would help establish is the issue is HW or SW related.

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