cancel
Showing results for 
Search instead for 
Did you mean: 

Bug for large SD cards in SDIO library code

infoinfo989
Associate III
Posted on June 01, 2011 at 22:33

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.

#sdio-standard-peripheral-library-stm32
10 REPLIES 10
Gawie
Associate II
Posted on October 04, 2011 at 13:22

Hi James

I have mentioned in a previous post that I experienced problems with 2G card on the STM32F2xx processor. I have attached my updatedstm32_eval_sdio_sd.c for your reference.

The changes are as follows:

1. Changed the address variable for the SD_ReadBlock,SD_ReadMultiBlocks, SD_WriteBlock,SD_WriteMultiBlocks andSD_Erase function calls to reflect the sector address. Fixes the out-of-range issue for bigger cards.

2.Set Block Size for Card in theSD_ReadBlock,SD_WriteBlock andSD_WriteMultiBlocks function calls.

Hope this helps.

Cheers,

Gawie

________________

Attachments :

stm32_eval_sdio_sd.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HtTY&d=%2Fa%2F0X0000000aQU%2FaR0yq_zyaNUthyZnMFcZ.XFiP0IerrdLZY4b28dWOUo&asPdf=false