cancel
Showing results for 
Search instead for 
Did you mean: 

F103 SDIO Init Problem

Zhi Pang
Associate III
Posted on March 30, 2018 at 05:34

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:

0690X00000604WUQAY.jpg

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
7 REPLIES 7
Posted on April 02, 2018 at 14:37

Try using the SDIO examples under the HAL trees rather than the CubeMX generated code.

One of the STM3210x-EVAL boards should support SDIO, the 'E' series as I recall. See BSP code and examples

http://www.st.com/en/evaluation-tools/stm3210e-eval.html

 
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 03, 2018 at 11:25

I checked with HAL firmware folder which list below.

...\STM32Cube\Repository\STM32Cube_FW_F1_V1.6.1\Projects

The only one I can find related to SDIO is a so-called 'Application' type project�?�?

FatFs_uSD

.

It is not only a clean SDIO example project, but also include FATFS middleware.

I looked into its code, in some critical C files.

  • HAL driver source file is totally the same with

    CubeMX generated. No surprise, because it is a firmware pack provide to CubeMX.

  • The bsp_driver_sd.c and stm3210e_eval_sd.c. Can't find any actual difference in init part, just some basic grammar equally change.

  • Files in Middlewares group is totally the same.

So, I don't know if somewhere has a clean SDIO demo, or success practice of F103 SD crad based on HAL.

Posted on April 03, 2018 at 12:37

But does it WORK?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 04, 2018 at 04:07

YES IT WORK.

This example use F103ZG and my MCU is F103VE. So that SDIO pins and DMA channel are compliant.

 What I do is change building target MCU model and the startup file.

Then f_mount ,f_open, f_write,f_close all of them return FR_OK.

I mount the SD card on my computer and found STM32.TXT file.

I just wondering what is the different between example project and CubeMX

generated.

Now I try to migrate the example code to my original project.

Zhi Pang
Associate III
Posted on April 04, 2018 at 09:54

I simply did these two operation make my CubeMX project works.

1.Go to sdio.c file, MX_SDIO_Init(void) function, line 74,

Change this line to below:

hsd.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;   //CubeMX advised to place 0 here, but won't work for mine

2.Go to fatfs_platform.c file, BSP_PlatformIsDetected(void) function,line 53,

Change this line to below:

if (HAL_GPIO_ReadPin(SD_PORT,SD_PIN) != GPIO_PIN_RESET) { //if CD pin connect to GND,means card is inserted

Now build the the project, and FATFS will work, if your situation is the same to mine.

Imen.D
ST Employee
Posted on April 05, 2018 at 17:49

Hello

Pang.Zhi

,

Thank you for your issues found and details provided.

I will take in charge your request for check and raise it internally to the appropriate team.

Best Regards,

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Posted on May 10, 2018 at 14:59

Hi

Pang.Zhi

,

Thanks for your feedback and sorry for late response.

Unfortunately, I'm not able to reproduce the problem. In fact, using CubeMX, I just enable SDIO in SD 4 bits Wide bus (without enabling FATFS). Then, I have succeed,without any error, to get the SD card info via the following function:

HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)

So, I would be grateful if you give me more details about the scenario to reproduce this issue and please let me know if you have been able to connect your SD card successfully using only SDIO (without FATFS).

thanks in advance for helping to complete this thread.

Best regards,

Mohamed