AnsweredAssumed Answered

Bug in HAL_SD Capacity calculation

Question asked by hartung.robert on Sep 9, 2015
Latest reply on May 24, 2016 by Lori B.
In the HAL Code the Capacity is calculated differently for Low Speed and High Speed Cards.

Low Speed cards use this code:

pCardInfo->CardCapacity  = (pCardInfo->SD_csd.DeviceSize + 1) ;
    pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2));
    pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen);
    pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;

While High Speed Cards use this:

pCardInfo->CardCapacity  = ((pCardInfo->SD_csd.DeviceSize + 1)) * 512 * 1024;


It should be either:

pCardInfo->CardCapacity  = ((uint64_t) (pCardInfo->SD_csd.DeviceSize + 1)) * 512 * 1024;

or

pCardInfo->CardCapacity  = (pCardInfo->SD_csd.DeviceSize + 1));
pCardInfo->CardCapacity *= 1024;
pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;

To avoid the overflow in uint32_t, because DeviceSize is a uint32_t itself.

- Robert

Outcomes