cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H7 SDMMC Removal and reinsertion crashes

SHewi.1
Associate II

Hi,

I'm having some issues regarding removing and reinserting an SD card on the STM32H722 using the provided HAL. I'm not doing anything special (no DMA, no interrupt). Just using the provided SDMMC1 and FATFS.

First, I noticed that the library won't initialise an SD card if it's not present the first time disk_initialize is called (f_mount -> find_volume). I fixed this by only setting the initialised flag to 1 if the disk actually initialised. I also added an uninitialise function (to be called if check_fs returns 4 (FR_DISK_ERR).

I've successfully used this same fix on a couple of other projects (using STM32F7 and STM32L4).

However, something is causing the stack to get trashed if the SD card is removed and reinserted.

I tracked this down to SD_SendSDStatus, where *pData is incremented beyond the 16bytes allocated to it. I've added a hotfix to stop this pointer behaviour, but I can't figure out how and why this is happening.

I can't get beyond this, so I'm a bit stuck. I know it's possible to recover somehow, as resetting my debug session brings the SD card back.

Any ideas as to what I'm missing??

Thanks in advance

-- EDIT --

SD_SendSDStatus fails due DCRCFAIL (every time it fails, but I only see this if I remove and reinsert the SD card)

5 REPLIES 5
SHewi.1
Associate II

Interestingly enough, the HAL_SD_InitCard succeeds (so the CID is setup properly) so it's not completely broken

AScha.3
Chief III

for remove+insert card you need some extra routine, as in win/linux click"remove media"....can remove now. system should know, what you wanna do, to flush buffers etc.

if you write to card, you have to do this. when only reading and never write to card...maybe lucky - or kill a sd-card, when remove, while on action. ymmv.

If you feel a post has answered your question, please click "Accept as Solution".
SHewi.1
Associate II

Thanks, but this isn't the solution.

I mount and unmount my SD card only when I need to read from/write to it. This isn't an issue with the SD card exactly, it's an issue with the sdmmc/fatfs stacks - given the SD card is successfully read from if I restart my firmware (power cycle not required).

SHewi.1
Associate II

I may have come up with a solution for now.

  1. Set the SDMMC1 registers to 0 within HAL_SD_MspDeInit.
  2. within BSP_SD_Init, if HAL_SD_Init fails, call HAL_SD_DeInit

It's not ideal, but this (together with stopping pData from incrementing too far) means that I can remove and reinsert the SD card.