2016-12-10 01:07 PM
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?
Solved! Go to Solution.
2017-01-15 01:43 PM
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);�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
2016-12-12 11:41 AM
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
2016-12-13 07:24 AM
Hi
mao.rongwei
I have moved this post to the
where product related questions are posted.Thanks
Oli
2017-01-12 06:21 PM
2017-01-12 07:12 PM
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
2017-01-13 05:48 PM
2017-01-15 11:24 AM
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?
2017-01-15 01:43 PM
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);�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?