2022-11-24 05:08 AM
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)
2022-11-24 07:02 AM
Interestingly enough, the HAL_SD_InitCard succeeds (so the CID is setup properly) so it's not completely broken
2022-11-24 09:36 AM
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.
2022-11-25 02:23 AM
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).
2022-11-25 06:01 AM
I may have come up with a solution for now.
It's not ideal, but this (together with stopping pData from incrementing too far) means that I can remove and reinsert the SD card.
2022-11-26 04:04 PM
Take a note that everything from HAL/Cube is a broken bloatware, especially the cache maintenance.