Skip to main content
Explorer
June 25, 2024
Solved

Using SDMMC/SDIO and FATfs for reading SD Card for STM32L471VGT6

  • June 25, 2024
  • 3 replies
  • 6162 views

MCU: STM32L471VGT6

I am trying to get an SD Card to work for my system. I am using a custom board and I cant share any drawings. However, what I am using for the SD Card:

Sd card adapter: https://learn.adafruit.com/adafruit-microsd-spi-sdio

Sd card: SDHC 32 GIG micro sd card.

The wiring is okay, I checked so many times.

The sd card and adapter works for spi, tried it on arduino.

What I have tried in STM32CubeMX:

Enable SDMMC1 (Tried both SD 1 bit and wide 4 bit)

- Formatted it on windows to FAT32

- Interrrupt on

- DMA on

- Pullup on every pin except CK

- Put sdmmc clock to 48 MHz

- DMA template enabled on FATFS

I have tried different PCB, tried different sd adapter, tried different sd card

What My code is crashing on is the f_mount. It returns 

FR_DISK_ERR after a while.
    This topic has been closed for replies.
    Best answer by morris

    I found it, I had to run 

    PeriphCommonClock_Config() after exited my sleep mode becuase the sdmmc runs on a clock that doesnt get started by  SystemClock_Config().

    3 replies

    Technical Moderator
    June 25, 2024

    Hello @morris  and welcome to the community.

    Please read these recommendations on this link on how to post a thread on the community. Especially the details of your HW (MCU part number), a concise description of your problem  etc ..

    So please share schematics/project etc to let community members to help you efficiently.

    morrisAuthor
    Explorer
    June 25, 2024

    As I said I cant share any schematics

    Super User
    June 25, 2024

    @morris wrote:

    As I said I cant share any schematics


    That really cripples the possibilities for remote assistance!

    Can you not at least show the connections of the SD Card adaptor to the microcontroller?

    And give the full microcontroller part number.

    Do you have a logic analyser to see what's happening on the wires?

    Can you reproduce the issue on an ST board?

     

    EDIT:

     


    @morris wrote:

    The sd card and adapter works for spi, tried it on arduino


    Are you sure it works for SDIO ?

    Super User
    June 25, 2024

    Hi,

    f_mount is the first , that has to work. If not - no useful access to card.

    Connection cpu to card at max. 50mm length;

    + What you should do :

    1. set port/pins to medium or med./high speed , not high; + pullups ON an all pins to sd-card.

    2. set 1-bit mode (4-bit much more critical...for later playing around...)

    3. no DMA template (also...later)

    4. in Cube->fatfs : enable ex-fat and long filenames (working also with 64GB ex-fat cards, as you buy them, bigger i dont have, but should work also; dont format cards, usually they have "best" matching format when new..)

    Then try mount (...1); (1 = now)

    morrisAuthor
    Explorer
    June 25, 2024

    The pins speed made it worked. You are amazing!!! Now I can use the sd card at:

    - 1 bit mode

    - interrupt ON

    - DMA TX/DMA RX

    - Medium speed with pullup

     

    The solution breaks when I try to:

    -Enable dma template

    - Enable 4 bit mode

     

    Do I want Dma template, I have read somewhere that it acheives best performace. How do make 4 bit mode work?

     

     

    Super User
    June 25, 2024

    :)

    - DMA : didnt work for me, but never tried much with this, because : WHEN i read (or write) the sd-card, i always need it NOW, so nothing different, if the cpu waits for data and transfers "itself", or waits for "dma-ready", done, same time.

    But it could be better and important in other area, in RTOS system etc, because no cpu load for data transfer.

     

    - 4.bit mode: sure, is faster, but also much more sensitive to connection (reflection, ringing etc.) on the lines;

    thats also why pin-speed is important: the cpu pins are so fast switching, that this produces a lot of EMI and rf-ringing.

    And also on line length: i had an adapter, about 80mm from cpu - never 4bit mode working. Same with 30mm distance working on 4bit mode, full speed (50 MHz on sdmmc module).

    So dont think, 4-bit is 4 times as fast, real is: if you read lets say 8kB from card, at 25Mbit this needs about 2,6ms; but at first the command is send to card, some xx us, then card access its memory area, needs about 0.5 ...3ms, sometimes 100ms (no joke!) , so reading 8KB needs about 4ms and if you can do same at 4-bit mode, it will need about 1...3ms .

    morrisAuthor
    Explorer
    June 26, 2024

    My new problem is not I cant use the sd Card after the stm32 processor have been in sleep mode. The sd card refuse to open a file or make a volume, mouting is fine. I have tried many differemt pf init/deint but nothing seems to work. Any ideas?

     

    Note: The cards works before sleep mode

    Super User
    June 26, 2024

    Do you power-down the card while the STM32 is asleep?

    What do you do with the SDIO lines while the STM32 is asleep?

    morrisAuthor
    Explorer
    June 26, 2024

    It goes into Stop mode 2:

    "

    Enter Stop 2 mode.
      * @note  In Stop 2 mode, only low power voltage regulator is ON.
      * @note  In Stop 2 mode, all I/O pins keep the same state as in Run mode.
      * @note  All clocks in the VCORE domain are stopped, the PLL, the MSI,
      *        the HSI and the HSE oscillators are disabled. Some peripherals with wakeup capability
      *        (LCD, LPTIM1, I2C3 and LPUART) can switch on the HSI to receive a frame, and switch off the HSI after
      *        receiving the frame if it is not a wakeup frame. In this case the HSI clock is propagated only
      *        to the peripheral requesting it.
      *        SRAM1, SRAM2 and register contents are preserved.
      *        SRAM3 content is preserved depending on RRSTP bit setting (not available on all devices).
      *        The BOR is available.
      *        The voltage regulator is set in low-power mode but LPR bit must be cleared to enter stop 2 mode.
      *        Otherwise, Stop 1 mode is entered.
      * @note  When exiting Stop 2 mode by issuing an interrupt or a wakeup event,
      *         the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
      *         is set; the MSI oscillator is selected if STOPWUCK is cleared.
      * @PAram STOPEntry  specifies if Stop mode in entered with WFI or WFE instruction.
      *          This parameter can be one of the following values:
      *            @arg @ref PWR_STOPENTRY_WFI  Enter Stop mode with WFI instruction
      *            @arg @ref PWR_STOPENTRY_WFE  Enter Stop mode with WFE instruction"
    All sdio lines reamin at 3V under Stop mode, I just checked with multimeter. Can it have someting to do with the clock?