AnsweredAssumed Answered

Bug for large SD cards in SDIO library code

Question asked by van_hooft.frank.001 on Jun 1, 2011
Latest reply on Oct 4, 2011 by de_Vos.Gawie
FYI, there's a bug in the SDIO standard peripheral library code when dealing with SD cards greater than 4 GB in size.

The problem is the library code uses a parameter called "address" or similar in several of its functions. This parameter is of type uint32_t, which means it has a maximum value of 4 GB. Hence it can overflow for cards of greater than 4 GB capacity.

You can see this parameter in the following 5 functions:

SD_ReadBlock (uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)

SD_ReadMultiBlocks (uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)

SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize)

SD_WriteMultiBlocks (uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)

SD_Erase(uint32_t startaddr, uint32_t endaddr)

Each of these functions takes the address, and divides it by 512 to create a sector number (block number) for use with large cards. Given that small cards are pretty much gone these days, one easy solution is simply to "redefine" the address parameter to be a sector parameter. That's what we've done, although this might not work for everyone.

It also crops up in:

SD_GetCardInfo(SD_CardInfo *cardinfo)

where cardinfo->CardCapacity is calculated; again this can overflow because it currently expresses card capacity in bytes into a uint32_t field. Leaving this in sectors (blocks) might make more sense if you're not concerned about small cards.

Hope this helps someone.