Skip to main content
rwmao
Senior
December 10, 2016
Solved

microSD card problem

  • December 10, 2016
  • 7 replies
  • 2680 views
Posted on December 10, 2016 at 22:07

I am testing the microSD card. The weird thing is that it works well in debug mode, but doesn't work in normal mode.

I set the breakpoint to a line at the very end in debug mode, before which the operation of microSD card is done. In debug mode, it works very well. But when I quit the debug mode, the sd card doesn't work. 

I am using keil, uvision as the IDE. The chip is STM32F405RGT6;

What should I do?

    This topic has been closed for replies.
    Best answer by rwmao
    Posted on January 15, 2017 at 22:43

    found the problem. The hal file generated by cubemx has a bug(maybe not really).

    https://community.st.com/0D50X00009XkeRpSAJ

    suggests the solution.

    In the file:\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_sd.c,

    find the function,

    SD_PowerON, move HAL_Delay(1); behind the clock enable.

    Problem solved.

    static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd){ SDIO_CmdInitTypeDef sdio_cmdinitstructure; __IO HAL_SD_ErrorTypedef errorstate = SD_OK; uint32_t response = 0U, count = 0U, validvoltage = 0U; uint32_t sdtype = SD_STD_CAPACITY; /* Power ON Sequence -------------------------------------------------------*/ /* Disable SDIO Clock */ __HAL_SD_SDIO_DISABLE(); /* Set Power State to ON */ SDIO_PowerState_ON(hsd->Instance); /* 1ms: required power up waiting time before starting the SD initialization sequence */ // HAL_Delay(1); //should be after __HAL_SD_SDIO_ENABLE() /* Enable SDIO Clock */ __HAL_SD_SDIO_ENABLE();//moved to here /* 1ms: required power up waiting time before starting the SD initialization sequence */ HAL_Delay(1);�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

    7 replies

    Tesla DeLorean
    Guru
    December 12, 2016
    Posted on December 12, 2016 at 20:41

    Watch timing and clocks. Pins and peripherals the debugger enables for it's own usage.

    Add diagnostic or telemetry output

    (info to USART) 

    from SDIO related code, review that output/time in Debug vs Regular execution.

    Review signalling on logic analyzer

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    rwmao
    rwmaoAuthor
    Senior
    January 13, 2017
    Posted on January 13, 2017 at 03:12

    Update.

    I tested it is not because if it is in the debug mode.

    Here is the procedure I tested:

    1. I turned on the board. The program runs into an error ( I set to light up RED led if anything is wrong).

    2. I reset the stm32 chip by short the NRST pin to ground while I keep the power of the board ON. The chip is reseted. the program has gone through without problem (write file and read file) by lighting up green led.

    What should I do for next step?Apparently it is not because of clock. 

    Thanks

    Oliver Beirne
    Associate II
    December 13, 2016
    Posted on December 13, 2016 at 16:24

    Hi

    mao.rongwei

    I have moved this post to the

    https://community.st.com/community/stm32-community/stm32-forum?sr=search&ampsearchId=1f075d14-1117-4f25-85a1-96ea3fb8cda9&ampsearchIndex=0

    where product related questions are posted.

    Thanks

    Oli

    rwmao
    rwmaoAuthor
    Senior
    January 13, 2017
    Posted on January 13, 2017 at 03:21

    The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6rB&d=%2Fa%2F0X0000000bwo%2FXwiQUFZIzGsZz.cMMQ7EcQUxg86SJV.LwGZ6vdxtde8&asPdf=false
    rwmao
    rwmaoAuthor
    Senior
    January 14, 2017
    Posted on January 14, 2017 at 02:48

    The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6mH&d=%2Fa%2F0X0000000bvA%2FblDvQvUA2rIsvDPQPIJcLbxO8aQFrIMkHooDRbR76Eo&asPdf=false
    rwmao
    rwmaoAuthor
    Senior
    January 15, 2017
    Posted on January 15, 2017 at 20:24

    I dig into the problem step by step:

    error happens in:

    DSTATUS SD_initialize(BYTE lun)
    {
     Stat = STA_NOINIT;
     /* Configure the uSD device */
     if(BSP_SD_Init() == MSD_OK)
     {
     Stat &= ~STA_NOINIT;
     }
    return Stat;
    }�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

    Error happens in BSP_SD_Init(). With further dig, it bump into :

    HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo)
    { 
     __IO HAL_SD_ErrorTypedef errorstate = SD_OK;
     SD_InitTypeDef tmpinit;
    /* Allocate lock resource and initialize it */
     hsd->Lock = HAL_UNLOCKED;
     /* Initialize the low level hardware (MSP) */
     HAL_SD_MspInit(hsd);
    /* Default SDIO peripheral configuration for SD card initialization */
     tmpinit.ClockEdge = SDIO_CLOCK_EDGE_RISING;
     tmpinit.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
     tmpinit.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
     tmpinit.BusWide = SDIO_BUS_WIDE_1B;
     tmpinit.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
     tmpinit.ClockDiv = SDIO_INIT_CLK_DIV;
    /* Initialize SDIO peripheral interface with default configuration */
     SDIO_Init(hsd->Instance, tmpinit);
    /* Identify card operating voltage */
     errorstate = SD_PowerON(hsd);
    if(errorstate != SD_OK) 
     {
     return errorstate;
     }
    /* Initialize the present SDIO card(s) and put them in idle state */
     errorstate = SD_Initialize_Cards(hsd);
    if (errorstate != SD_OK)
     {
     return errorstate;
     }
    /* Read CSD/CID MSD registers */
     errorstate = HAL_SD_Get_CardInfo(hsd, SDCardInfo);
    if (errorstate == SD_OK)
     {
     /* Select the Card */
     errorstate = SD_Select_Deselect(hsd, (uint32_t)(((uint32_t)SDCardInfo->RCA) << 16));
     }
    /* Configure SDIO peripheral interface */
     SDIO_Init(hsd->Instance, hsd->Init);
    return errorstate;
    }�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

    The problem happens in /* Identify card operating voltage */

    errorstate = SD_PowerON(hsd); ; returns SD_COM_CRC_FAILED.

    Any idea?

    rwmao
    rwmaoAuthorBest answer
    Senior
    January 15, 2017
    Posted on January 15, 2017 at 22:43

    found the problem. The hal file generated by cubemx has a bug(maybe not really).

    https://community.st.com/0D50X00009XkeRpSAJ

    suggests the solution.

    In the file:\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_sd.c,

    find the function,

    SD_PowerON, move HAL_Delay(1); behind the clock enable.

    Problem solved.

    static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd){ SDIO_CmdInitTypeDef sdio_cmdinitstructure; __IO HAL_SD_ErrorTypedef errorstate = SD_OK; uint32_t response = 0U, count = 0U, validvoltage = 0U; uint32_t sdtype = SD_STD_CAPACITY; /* Power ON Sequence -------------------------------------------------------*/ /* Disable SDIO Clock */ __HAL_SD_SDIO_DISABLE(); /* Set Power State to ON */ SDIO_PowerState_ON(hsd->Instance); /* 1ms: required power up waiting time before starting the SD initialization sequence */ // HAL_Delay(1); //should be after __HAL_SD_SDIO_ENABLE() /* Enable SDIO Clock */ __HAL_SD_SDIO_ENABLE();//moved to here /* 1ms: required power up waiting time before starting the SD initialization sequence */ HAL_Delay(1);�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?