cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H7 HAL + SDHC(4GB SD Card) + FatFS R0.12C

Boram Youn
Associate II
Posted on July 11, 2018 at 11:56

Hi,

I'm currently working on a project that needs to 'Read' a File in SDCARD.

My MCU is STM32H743XI, and the base for the project, I modified the FatFS uSD standalone application example in Cube FW for H7 V1.2.0, for STM32H743I-EVAL board.

I'm not using the Eval board for the project, so what I've done is; remove MFX I/O related codes from BSP sd.c driver file. 

I've tested the project on my own board and it worked as expected when I use 2GB SD card, but it failed at f_open() when I use 4GB SD card.

So I searched this forum and found 

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

 , and modified SD_InitCard() function in stm32h7xx_hal_sd.c and inserted CMD1 code like below. The Bold line is changed. 

static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)

{

HAL_SD_CardCSDTypedef CSD;

uint32_t errorstate = HAL_SD_ERROR_NONE;

uint16_t sd_rca = 1;

/* Check the power State */

if(SDMMC_GetPowerState(hsd->Instance) == 0)

{

/* Power off */

return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;

}

if(hsd->SdCard.CardType != CARD_SECURED)

{

/* Send CMD2 ALL_SEND_CID */

errorstate = SDMMC_CmdSendCID(hsd->Instance);

if(errorstate != HAL_SD_ERROR_NONE)

{

return errorstate;

}

/* Get Card identification number data */

hsd->CID[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);

hsd->CID[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);

hsd->CID[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);

hsd->CID[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);

}

if(hsd->SdCard.CardType != CARD_SECURED)

{

/* Send CMD3 SET_REL_ADDR with argument 0 */

/* SD Card publishes its RCA. */

errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);

if(errorstate != HAL_SD_ERROR_NONE)

{

return errorstate;

}

}

if(hsd->SdCard.CardType != CARD_SECURED)

{

/* Get the SD card RCA */

hsd->SdCard.RelCardAdd = sd_rca;

/* Send CMD9 SEND_CSD with argument as card's RCA */

errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));

if(errorstate != HAL_SD_ERROR_NONE)

{

return errorstate;

}

else

{

/* Get Card Specific Data */

hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);

hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);

hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);

hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);

}

}

/* Get the Card Class */

hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20);

/* Get CSD parameters */

HAL_SD_GetCardCSD(hsd, &CSD);

/* Select the Card */

errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16));

if(errorstate != HAL_SD_ERROR_NONE)

{

return errorstate;

}

//20180710

SDMMC_CmdOpCondition(hsd->Instance, 0x40000000);

//0xC0100000);//0x40000000);//0x00000000);//0x40000000);

/* All cards are initialized */

return HAL_SD_ERROR_NONE;

}

Here's My Questions:

1. After I changed the code, 4GB SD Card works, but total file-read speed has been significantly slowed down.

(It took 70 'seconds' after the debugging, before the modification it took 1 second when I used 2GB SD card.) Can you tell me why? 

2. As you can see, I've tested with variety of arguments for CMD1. I know the [30:29] have to be [1:0], but it seems it doesn't matter. I've tried 0, 0xFFFFFFFF, but 4GB SD card still works.I don't understand why.

3. My Previous board used STM32F743NI MCU and I used SDCARD before, and didn't have any trouble with capacity of SD Card I was using. I assumed SD card initialization is not properly working now, but can't see any critical differences between H7 and F7 HAL Library. What should I do to regain the f_read speed using H7 library?

Thank you in advance.

#fatfs #sd-card #stm32h7
4 REPLIES 4
Posted on July 11, 2018 at 17:53

I really can't root-cause your specific issue, as it would involve a lot of my time. I have SD/MMC and eMMC working on the platform. The ST code may well have issues currently with low capacity SD or early V2/SDHC cards.

When porting from the EVAL board make sure to consider the bus transceiver and additional pins that uses, and the USE_SD_TRANSCEIVER define.

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

Hi, Thank you for your reply. 

Is USE_SD_TRANSCEIVER define related with H/W spec? I don't use 1.8V but didn't change USE_SD_TRANSCEIVER define, which was set to 1U.

Posted on July 12, 2018 at 04:44

The USE_SD_TRANSCEIVER set to 1 implies the hardware is there.  If you have done your own board and it is not there set it to 0.

Posted on July 12, 2018 at 04:59

Turns out USE_SD_TRANSCEIVER was defined at 2 different spot in the project. I only changed 1 of them, so I set both to 0 and project works without CMD1 code.

Thank you all.