New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persist DMA buffer in sdmmc_read_sectors/sdmmc_write_sectors (IDFGH-12770) #13749
Comments
Hmm I think that's not true? There are quite a lot of those big SD cards that comes with 1024 or even 4096 bytes sector. Even some SD NAND chips for industrial purposes uses 1024 bytes sector. I've definitely seen a 16GB one doing that but I can't remember the part number |
Hi @ducalex, |
That is what I thought at first, but when I looked up the actual code in esp-idf, the only assignment I've found was the line I've quoted. Which would result in 512 or less. Perhaps I missed another code path somewhere? |
Ah crap, yea I look down the code and indeed I can find them here:
I think this might be a bug?? Maybe it supposed to be Meanwhile, I believe these sort of DMA buffers shouldn't be statically allocated, because we do have use cases where two or more SD cards are needed (that shares with the same SPI but different CS). We have PSRAM here so we have sufficient RAM for heap allocations anyway. |
I agree that it shouldn't be static, it was hyperbole on my part! I also agree that running out of DMA-capable memory indicates bigger problems. However I maintain that it would be beneficial to keep the buffer after it's been used for the first time, because it will undoubtedly be needed again so that space is already pseudo-reserved. Making it explicit is simply more efficient and improves resilience. That being said, my proposal hinges on the fact that the buffer is small, 1KB or less. If there is a bug and the sector size can in fact be 4KB or even more then yes, it is obviously unreasonable to keep that around! |
Hmm yep, we do need to allocate at initialisation and free on deinit, not on every write op. I will submit a pull request first to fix the |
Thanks for the idea @ducalex! I agree that avoiding repeated allocations/deallocations could help with reliability. Recently we have added a new |
Is your feature request related to a problem?
When internal memory runs out, SD card reads and writes start returning
ESP_ERR_NO_MEM
because this line fails:void* tmp_buf = heap_caps_malloc(block_size, MALLOC_CAP_DMA);
In theory
SPIRAM_MALLOC_RESERVE_INTERNAL
should ensure that memory is always available but in practice it doesn't always work.There are likely also performance implications to allocating/deallocating all the time (in some cases).
Having no DMA-capable memory left is obviously an undesirable situation in general, but it can happen and when it does we might still need SD card access.
Describe the solution you'd like.
My understanding is that block_size is at most 512 bytes:
out_csd->sector_size = MIN(read_bl_size, 512);
.Therefore there is no reason to not keep the buffer around after it's first allocated for future use. In fact with that size it could be allocated preemptively when the SD card is initialized, or even placed in static BSS. Because if it's needed once, it will very likely be needed again!
Describe alternatives you've considered.
My current solution is this:
sdmmc_host_t's do_transaction()
to detect errorsBut this is far from ideal and doesn't always work...
Additional context.
#6596 suggested the same but was closed because of the emphasis on performance. I think the emphasis should be on stability improvements if this change is introduced.
The text was updated successfully, but these errors were encountered: