cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H7 and u-SD card on sdmmc1 - Using STM32CubeMX and HAL

Jack3
Senior II

Dear friends,

I'm Using STM32Cube_FW_H7_V1.4.0 with TrueStudio 9.3.0, on an STM32H753.

Has anybody got an micro-SD card working on the SDMMC1 peripheral for an STM32H7?

For my it, seems to come with a Timeout error.

I initialized it like:

void MX_SDMMC1_SD_Init(void)
{
  HAL_StatusTypeDef status;
 
  hsd1.Instance = SDMMC1;
  hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
  hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B;
  hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd1.Init.ClockDiv = 2;  // 0 - 1023
  status = HAL_SD_Init(&hsd1);
  if (status != HAL_OK)
  {
    dmc_puts("!HAL_OK\n");
    _Error_Handler2(__FILE__, __LINE__, status);
//    Error_Handler();
  }
}
 
void HAL_SD_MspInit(SD_HandleTypeDef* sdHandle)
{
 
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(sdHandle->Instance==SDMMC1)
  {
  /* USER CODE BEGIN SDMMC1_MspInit 0 */
 
  /* USER CODE END SDMMC1_MspInit 0 */
    /* SDMMC1 clock enable */
    __HAL_RCC_SDMMC1_CLK_ENABLE();
  
    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOD_CLK_ENABLE();
 
    /**SDMMC1 GPIO Configuration    
    PC8     ------> SDMMC1_D0
    PC9     ------> SDMMC1_D1
    PC10     ------> SDMMC1_D2
    PC11     ------> SDMMC1_D3
    PC12     ------> SDMMC1_CK
    PD2     ------> SDMMC1_CMD 
    */
 
    GPIO_InitStruct.Pin = SD_D0_Pin|SD_D1_Pin|SD_D2_Pin|SD_D3_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF12_SDIO1;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = SD_CK_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF12_SDIO1;
    HAL_GPIO_Init(SD_CK_GPIO_Port, &GPIO_InitStruct);   // GPIOC
 
    GPIO_InitStruct.Pin = SD_CMD_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF12_SDIO1;
    HAL_GPIO_Init(SD_CMD_GPIO_Port, &GPIO_InitStruct);  // GPIOD
 
    /* SDMMC1 interrupt Init */
    HAL_NVIC_SetPriority(SDMMC1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(SDMMC1_IRQn);
  /* USER CODE BEGIN SDMMC1_MspInit 1 */
 
  /* USER CODE END SDMMC1_MspInit 1 */
  }
}

The call MX_SDMMC1_SD_Init(); was missing the lines to call HAL_SD_Init(&hsd1);, which I added.

I printed some messages, which I added to the HAL Libraries:

MX_SDMMC1_SD_Init

SDMMC_ERROR_TIMEOUT

ERROR: SD_PowerON

ERROR: HAL_SD_InitCard

!HAL_OK

Besides 4-bit mode, I tried 1-bit mode, and I experimented with the clock speed.

And I tried different SD Cards, all with the same result.

What can I check?

My source code is also on github, I will comment back and update it once it works, so it can help others.

https://github.com/bkht/STM32_SD_SDMMC

I have used an eMMC on SDMMC (8-bit) on an STM32F7, but this SD Card (4-bit) is on an STM32H7.

10 REPLIES 10
Garnett.Robert
Senior III

Hi,

I have got an SD Card working with an STM32H743VITX.

You can find a copy of the complete project in the share part of this community (https://community.st.com/s/group/0F90X000000AXrvSAG)

Initially I had a bit of trouble getting the SD Card to work as I was using an SD Card with a Nucleo 144 H7 with hookup wire between the sd socket and the processor board.

The HAL SD card drivers use automatic speed setting and as I was using high speed SD cards the bus clock was being set to 50+ MHz. Of course the wiring betwwen the sd card and the processor was skewing the clock so badly it failed. I went in an hacked the code to prevent the speed from automatically being set and I found with the breadboard setup I could get the card working reliably at 12 MHz. if you look for the comment rjgDebug or rjgMod in the code you will find the hacks I made.

When I manufactured the power monitor project and had equal track lengths to the sd card I could push the clock speed up to 100 MHz with the right SD card and get reliable operation. I am currently running the SD Card at 50 MHz.

I bolted sqLite into the software system and have had reasonable success with this, but I am getting random errors after a few thousand transactions. There is no database corruption occurring as I am using the Write Ahead Log (WAL) to manage transactions.

I am in the process of debugging this and when I sort it out I will update the project in the shared area. The project I posted is a bit of a dog's breakfast as I have had to do a lot of experimentation to get it all to work.

A fragment of debug output is shown below:

00> Insert Started
00> vfsDirectWrite = 0  File Handle = 24060C80
00> Disk fatFS Write OK Res = 0 Disk LUN = 0  Buff = 24060CD4  Sector = 395856  count = 1 
00> Disk fatFS Write OK Res = 0 Disk LUN = 0  Buff = 24004418  Sector = 395857  count = 7 
00> Disk fatFS Read OK Res = 0 Disk LUN = 0  Buff = 24060CD4  Sector = 395864  count = 1 
00> vfsDirectWrite = 0  File Handle = 24060C80
00> vfsDirectWrite = 0  File Handle = 24060C80
00> Disk fatFS Write OK Res = 0 Disk LUN = 0  Buff = 24060CD4  Sector = 395864  count = 1 
00> Disk fatFS Write OK Res = 0 Disk LUN = 0  Buff = 2400C900  Sector = 395865  count = 7 
00> Disk fatFS Read OK Res = 0 Disk LUN = 0  Buff = 24060CD4  Sector = 395872  count = 1 
00> vfsDirectWrite = 0  File Handle = 24060C80
00> Step Done
00> SqLite Mem Hi Water Mark = 294400
00> Insert Complete

The file handle xxxx80 is the WAL file, the file handle xxxx118 is the main sqLite database file.

Using sqLite is a lot better than logging data to raw files although it does need a lot of ram. My system is using around 300 k of heap for sqLite. I have allocated 420K in the AXI ram dedicated to the sqLite heap using memsys5.

It would be nice if ST added sqLite to MX cube for use with the larger processors and included a Virtual File System that interfaces between fatFS, sqLite and SD Cards.

Best regards

Rob