Skip to main content
Visitor II
April 27, 2023
Question

STM32h7 problem with SDMMC Initialization

  • April 27, 2023
  • 5 replies
  • 5097 views

Hello,

I'm using an STM32h753i-eval2 board and trying to access an SD card through the SDMMC peripheral.

I have my RCC set on Master Clock Output 1.

I has the SDMMC external transceiver present set to no.

System clock mux is set to HSI

PLL Source is HSI

Using PLL1Q as the SDMMC clock, which I've tried at 25, 50, 100, and 200Mhz (docs suggest the slowest SD cards run at 25mhz).

The problem arises in the generated init code in HAL_SD_InitCard when it calls SD_PowerON:

/* SEND CMD55 APP_CMD with RCA as 0 */

   errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);

always returns SDMMC_FLAG_CTIMEOUT

I've tried increasing the timeout times with no luck. I've stepped through all the clock setup code at runtime and everything looks in order. Any insight would be very helpful and appreciated.

    This topic has been closed for replies.

    5 replies

    Graduate II
    April 27, 2023

    Typically you'd clock PLL1Q at 200 MHz and divide down that at the SDMMC via SDMMC->CLKCK register, initially dividing down to 400 KHz for first connection, and then up to 25 or 50 MHz depending on ClockDiv passed in. All MicroSD form-factor cards should clock at up to 50 MHz, although the lines ringing on most designs will probably be a limiting issue. The 25 MHz was the full-size (postage stamp size) SD/MMC cards

    Watch that the DISKIO or HAL_SDMMC_ routines aren't passing in really quick timeouts, computed as a function of the block counts. Use a minimum of 1000 ms rather than 100 ms.

    Check IRQHandler present and correctly bound, lot of #define, watch the linker vs vector table.

    TLuke.2Author
    Visitor II
    April 27, 2023

    Thank you for your response.

    I've set the 1Q clock to 200mhz and a sdmmc clockDiv of 3 (which I believe means divide by 4, correct?) To give me 50mhz.

    I've increased timeouts to absurd amount (10 seconds). Still have the same issue.

    I'm not using an sdmmc interrupt in the NVIC table, is this required?

    Oddly enough, if I breakpoint just before MODIFY_REG(SDMMCx->CMD, CMD_CLEAR_MASK, tmpreg);

    I can see that the SDMMCx->STA is 0, and then when I step over it, it's instantly 4, which is a response timeout flag. This is before even trying to poll the command response. Not sure if that makes sense, especially if I'm using high timeouts.

    Graduate II
    April 27, 2023

    This all moves far too fast to single step. Probing / inspecting the SDMMC register space will break the FIFO. Instrument code, and report state/flow that way.

    Visitor II
    December 19, 2023

    Hi @TLuke.2 any chance you solve this issue? What was the solution?

    I'm having a very similar issue with a NUCLEO STM32H5 board that I connected to a custom daughter board with an microSD card running in 

    I've check the connections, clock speeds, and every configuration seems correct but when the code hits the SD_PowerON function it returns timeout. Inside the function I can see that the flag SDMMC_FLAG_CTIMEOUT (inside SDMMC1->STA ) is set before the soft timer expires.

    Any suggestions on what should I check next?

    Thanks

    Explorer
    June 5, 2024

    Have you had any luck getting the SD card to work?  I seem to have the same issue that you described.

    Visitor II
    June 26, 2024

    I had the same problem,this is my soultion

    void MX_SDMMC1_SD_Init(void)
    {
    hsd1.Instance = SDMMC1;
    hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
    hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
    hsd1.Init.BusWide = SDMMC_BUS_WIDE_4B;
    hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE;
    hsd1.Init.ClockDiv = 2;
    // step to init card ref form RM0433.pdf-Page2429
    //step1:Reset the SDMMC with the RCC.SDMMCxRST register bit
    __HAL_RCC_SDMMC1_FORCE_RESET();
    __HAL_RCC_SDMMC1_RELEASE_RESET();
    // step to init card step2:Disable the Vcc power to the card
    HAL_GPIO_WritePin(PE2_GPIO_OUT_GPIO_Port, PE2_GPIO_OUT_Pin, GPIO_PIN_RESET); //PE2 used control SD card power!
    HAL_Delay(2);
    //step3:Set the SDMMC in power-cycle state init sd gpio!
    SDMMC_PowerState_Cycle(SDMMC1);
    HAL_SD_MspInit(&hsd1);
    //step4:After minimum 1 ms enable the Vcc power to the card!
    HAL_Delay(2);
    HAL_GPIO_WritePin(PE2_GPIO_OUT_GPIO_Port, PE2_GPIO_OUT_Pin, GPIO_PIN_SET);
    //step5:After the power ramp period set the SDMMC to the power-off state for minimum 1 ms
    HAL_Delay(1);
    (void)SDMMC_PowerState_OFF(SDMMC1);
    HAL_Delay(2);
    // normal init!
    if (HAL_SD_Init(&hsd1) != HAL_OK)
    {
    Error_Handler(__FILE__, __LINE__);
    }
    }

    see detail in here

    Moxizha_0-1719420273349.png

     

    Visitor II
    June 26, 2024

    I use threadx-fileX , need to Enable SDMMC1_IRQn make sure its operation

    HAL_NVIC_SetPriority(SDMMC1_IRQn, 2, 0);
    HAL_NVIC_EnableIRQ(SDMMC1_IRQn);

     

    Visitor II
    June 26, 2024

    1