Bugs in sd_diskio.c alignment handling
I've had problems with sd_diskio.c used for SD card I/O with FatFs.
CubeMX version is 6.16.0.
I'm using SDMMC with DMA, so consideration needs to be given to cache coherency and alignment.
I have both these set:
#define ENABLE_SD_DMA_CACHE_MAINTENANCE 1
#define ENABLE_SCRATCH_BUFFER
However, I've found some bugs in the code:
In SD_read, the alignment check:
#if defined(ENABLE_SCRATCH_BUFFER)
if (!((uint32_t)buff & 0x3))
{
#endif
should have the 0x3 changed to 0x1f, as cache alignment requires 32-byte alignment, not 4-byte.
There's another case of this in SD_write.
Furthermore, there's a bracketing problem in SD_write with the code under ENABLE_SCRATCH_BUFFER conditional compilation, which is supposed to be invoked when the buffer is misaligned as an else condition for if (!((uint32_t)buff & 0x1f), but is instead an else condition for a DMA success check. This means that the scratch buffer code does not run at all when a buffer misalignment is detected.
To fix it, I added a closing brace to this code (lines 488-491)
#if defined(ENABLE_SCRATCH_BUFFER)
+ }
else {
/* Slow path, fetch each sector a part and memcpy to destination buffer */
and deleted the closing brace from this code (lines 556-561):
if ((i == count) && (ret == MSD_OK ))
res = RES_OK;
- }
}
#endif
May I request that these are fixed in the template code? The indentation and conditional compilation are quite a mess, which I'm sure led to the latter problem, so perhaps reformatting and commenting the closing braces and #endifs would be beneficial for maintenance.
Regards,
David

