cancel
Showing results for 
Search instead for 
Did you mean: 

FatFS and D-Cache not working together

AAlis.23
Associate III

Issue with FatFs and D-Cache on STM32 (CubeMX Library)

I'm having issues using FatFs with D-Cache enabled on an STM32. After searching, I found several posts reporting similar problems, but I haven't seen a clear solution.

I’m using the FatFs library that comes with CubeMX, and my goal is to write to a microSD card.

The question is:

        Is there a proper solution to make FatFs work with D-Cache enabled?
        If not, is there an alternative library that does work with D-Cache?


Any insights or recommendations would be greatly appreciated.

4 REPLIES 4

When using DMA and caching you have to manage coherency

https://github.com/STMicroelectronics/STM32CubeH7/blob/master/Projects/STM32H747I-EVAL/Applications/FatFs/FatFs_Dual_Instance/CM7/Src/sd_diskio_dma.c#L51

SCB_InvalidateDCache_by_Addr() after reading

SCB_CleanDCache_by_Addr() before writing, if not write-through

Buffer should be 32-byte aligned, or you need to watch for structures abutting.

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Yes, managing cache coherency is important when using DMA and caching, but ideally, this should be handled by the driver itself. Functions like f_open or f_write should work without requiring manual cache management from the user. If the user has to call SCB_InvalidateDCache_by_Addr() after reading or SCB_CleanDCache_by_Addr() before writing, it indicates that the driver is not handling cache coherency properly. This is not the user's responsibility, and if this behavior is necessary, it means the driver is malfunctioning.

Given that this driver is included in CubeMX, it should function correctly without requiring additional steps when enabling DCache. If it doesn’t, it’s a sign that the driver isn’t working as expected.

 

I tried the file you sent me, but I encountered an issue. The functions it calls, like BSP_SD_Init, have different arguments. For example, the one generated by CubeMX has no arguments, while the one used by the new driver requires an instance, and this is the case with all the functions. Given this, unless I’m mistaken, I believe they are not compatible. In other words, when I generate code with CubeMX, it generates functions that are incompatible with the new sd_diskio.c, so the only solution I see is to remove FAT_FS from CubeMX and manually add all the code. If that’s the case, it’s clear that this driver should be updated because it’s not compatible with CubeMX. If that’s not the case, I would appreciate it if you could explain how I should proceed.

AAlis.23
Associate III

After struggling with the amount of files in the BSP, which I consider to be a poorly designed library with redundant definitions and references to unnecessary things, I have managed to make it work. To my surprise, the library does not support SDHC, whereas the original CubeMX library does. It might be some missing definition somewhere, but while a regular microSD works, using an SDHC results in an error when calling the function SD_PowerON(hsd);.

Regarding SDHC incompatibility, do you have any advice?