-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
fix(sdmmc): correct miscalculated sector size (IDFGH-12789) #13767
Conversation
👋 Hello huming2207, we appreciate your contribution to this project! 📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more. 🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project. Click to see more instructions ...
Review and merge process you can expect ...
|
Previous implementation will restrict the SD card sector size to be no more than 512 bytes. However 512 bytes is the minimum size for some old generic SD cards, and some new ones may come with 1024 or 4096 bytes per sector. We need to support those as well.
@huming2207 thanks for the PR! The fix looks correct to me, we'll merge it soon.
Could you please mention the source for this statement? It doesn't seem to match SD specification. In SD v1 standard, the card provided read_bl_len and write_bl_len fields in the CSD register. These indicated maximum supported read and write block sizes, respectively. The actual block size would default to 512 bytes and could be set using CMD16, up to the limit specified in the CSD. In SD v2 standard, these fields in CSD are no longer variable. E.g.,
The description of CMD16 says the same:
|
Hi @igrr
Sorry that was straight out from my memory, I've double checked and I think I have misunderstood the standard and I thought all SD card sector size are fixed to whatever it was provided in the Meanwhile indeed on some SD cards, the Based on the spec, I guess the original implemetation (which fixed to 512) should still works, but it may affect the I/O performance? Regards, |
I think I have this chip somewhere, I'll give it a try. I guess only the total size of the transfer should matter for performance, not the the block size. The current implementation effectively always sets card->sector_size to 512, regardless of read_blk_len. esp-idf/components/sdmmc/sdmmc_sd.c Lines 387 to 391 in d4cd437
So we are not using the possibility to increase the read/write block lengths, always setting the effective block length to 512: esp-idf/components/sdmmc/sdmmc_cmd.c Lines 273 to 278 in d4cd437
Basically, the existing code should work even on cards with read_bl_len > 9. But anyway, your fix is logically correct, so we'll change this, even though this is effectively dead code... |
@huming2207 Somewhat unrelated, I find that XTSD01GLGEAG is marked as "discontinued" at LCSC. And XTX website mentions a different variant, XTSDG01GWSIGA, but it's not in stock. |
Ah... this will definitely cause some undefined behaviours on one of our products, so it needs to be fixed. For that product we need to deal with some real-time data. We can't use any filesystems because they are often too slow, so we wrote our own block-level storage instead. We do rely on the
We haven't reached out to XTX for now, and we never tested their chips in a larger scale. We only have a few demo devices with their 1Gbit chip assembled but it seems working anyway. But I can't remember the part number and I'm not sure which one it is...😅 We planned to use Create Storage World's chip for production later. The CSW's chip seems to always report 512 bytes on their |
Sorry, could you explain why this will cause undefined behavior? To be clear, it is totally supported by SD standard to set the read/write block length to 512 bytes for SDSC cards, regardless of the maxium supported read_blk_len/write_blk_len. SDSC cards allow for the read/write block length to be larger than 512 bytes, but the host can still set the actual block length to 512. Linux and Zephyr SDMMC stacks do the same thing. Neither of them even issue MMC_SET_BLOCKLEN command in the standard card initialization flow, as far as I can see. If you use |
Hmm nevermind, sorry I double checked our code against the SD spec and I think I might have misunderstood the spec again... It actually doesn't matter at all even with a "wrongly" reported |
@huming2207 I found that I don't actually have any SDSC card which right now which I can use to verify this change. Given that we have concluded that your application should work fine even with 512 byte Thanks again for taking your time on this PR. |
I got one of those SD NAND chips with csd_ver=1 (i.e. SDSC) and read_bl_len=10. On a quick look, the tests seem to pass, but I've reopened the internal issue to verify that everything works correctly. |
Previous implementation will restrict the SD card sector size to be no more than 512 bytes. However 512 bytes is the minimum size for some old generic SD cards, and some new ones may come with 1024 or 4096 bytes per sector. We need to support those as well.
This (partially) fixes: #13749 (comment)