F103 SDIO Init Problem
The problem is when code run to
HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B), it always return HAL_ERROR.And then SD init failed and stuck in _Error_Handler function.
MCU: STM32F103VETx
Cube MX Version: 4.25.0
Firmware Package: FW_F1 V1.6.1
Toolchain: TrueSTUDIO 9.0
I was started a new project by CubeMX. Only turn on the [RCC-HSE-Crystal] for clock, the [SYS-Serial Wire] for debug and trace, the [SDIO-SD 4 bits Wide bus] for microSD card and which I meet the problem.Other peripherals remain in default settings.
MX_SDIO_SD_Init(void) function generated by Cube is below:
void MX_SDIO_SD_Init(void)
{ HAL_StatusTypeDef ret; hsd.Instance = SDIO; hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING; hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE; hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE; hsd.Init.BusWide = SDIO_BUS_WIDE_1B; hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE; hsd.Init.ClockDiv = 0; if (HAL_SD_Init(&hsd) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }}
And here is my PCB SD card wiring part:

I trace the call hierarchy step by step:
1. sdio.c → void MX_SDIO_SD_Init(void) → LINE 66:
HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) return HAL_ERROR
2.stm32f1xx_hal_sd.c → HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
→ LINE 1991:SD_WideBus_Enable(hsd) return 1;
3.
stm32f1xx_hal_sd.c → SD_WideBus_Enable(SD_HandleTypeDef *hsd) → LINE 2688:
SD_FindSCR(hsd, scr) return 1;
4.stm32f1xx_hal_sd.c → SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR) → LINE 2782:
SDMMC_CmdBlockLength(hsd->Instance, 8U) return 1ï¼›
5.stm32f1xx_II_sdmmc.c → SDMMC_CmdBlockLength(SDIO_TypeDef *SDIOx, uint32_t BlockSize) → LINE 542:
SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SET_BLOCKLEN, SDIO_CMDTIMEOUT) return 1;
6.stm32f1xx_II_sdmmc.c → SDMMC_GetCmdResp1(SDIO_TypeDef *SDIOx, uint8_t SD_CMD, uint32_t Timeout)
→ LINE 1193:__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL) return 1;
THAT'S ALL.
My microSD is a production of Sandisk SDHC 8GB CLASS 4. It work normally when I use card reader on PC.I even done some I/O test and capacity test on it.
I tried to solve the problem by myself about three days, but did't get any clue.
Any suggestion will be helpful, Thanks.
Updated__________________________________________________________________
I tried to use SPL v3.5 instead of HAL.
And I got good result. MicroSD Card single/multiple block(s) read/write test normal.
Then I migrated FATFS R0.09 into the same project. Everything works fine.
Now I am highly doubt that the SDIO init template generated by CUBE which is based on FW_F1 V1.6.1 firmware has bug(s).
I will try to compare the SDIO init difference step by step between two lib.
If I can't get any lucky, I will use SPL until next update of HAL address.
:)
Updated__________________________________________________________________
I found something interesting.
In CubeMX generated code SD plug-in detecting function look like this:
uint8_t BSP_PlatformIsDetected(void) {
uint8_t status = (uint8_t)0x01; /* Check SD card detect pin */ if (HAL_GPIO_ReadPin(SD_PORT,SD_PIN) == GPIO_PIN_RESET) { status = (uint8_t)0x00; } /* USER CODE BEGIN 1 */ /* user code can be inserted here */ /* USER CODE END 1 */ return status;}So we know if the card detecting pin which is pin 9 in above figure connect to GND, this function will return 0. And result 0 will cause the caller function return a SD_NOT_PRESENT status.
Now take a look in demo project provided by firmware pack:
&sharpdefine SD_PRESENT ((uint8_t)0x01)
&sharpdefine SD_NOT_PRESENT ((uint8_t)0x00)uint8_t BSP_SD_IsDetected(void)
{ __IO uint8_t status = SD_PRESENT;/* Check SD card detect pin */
if(HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != GPIO_PIN_RESET) { status = SD_NOT_PRESENT; } return status;}In this case, the same pin state will get opposite result.
DAHMEN.IMEN
don't know if it is a reported bug or some setting I can configure in CubeMX. Normally, we connect the CD pin to a pull-up resistor, when a card insert it connect to ground. #sdio #microsd #sd-card