Go Down

Topic: SD Card Info Returns wrong size (Read 2503 times) previous topic - next topic

Melbfella

G'day all,

I'm working with a 1.8" TFT display, which has an SD card reader installed into it.  The display was bought off EBay :

http://www.ebay.com.au/itm/1-8-Serial-SPI-TFT-LCD-Module-Display-PCB-Adapter-Power-IC-SD-Socket-128X160-/200926655502?pt=AU_B_I_Electrical_Test_Equipment&hash=item2ec829740e&_uhb=1

The display works great, and I'm able to create and write files to a 16GB SD card with no problem.

The issue I have is when I run 'Card Info' from the SD example library, it returns these values :

Code: [Select]

Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT32

Volume size (bytes): 3038248960
Volume size (Kbytes): 2967040
Volume size (Mbytes): 2897


It reports that it's size is 3038248960 bytes, equating to a little under 29GB.  This is obviously incorrect, as the card is only 16GB to start with.

I used SD formatter from Adafruit to format the card, and it happily reports it's size as 14.8GB formatted, which I believe is correct.

Any thoughts would be greatly appreciated - I need to monitor how much free space is on the card, and I really would prefer not to have some type of offset to arrive at the correct number.

Thanks in advance :)

fat16lib

This is a well known bug in SD.h, not sure why the Arduino company has not fixed it.  CardInfo was written by Adafruit.

The reported value in bytes is truncated to 32-bits so it is incorrect for cards over 4 GB.

3038248960 is not 29 GB.  It is 3,038,248,960 bytes, about 3 GB,

Melbfella

Thanks for your reply fat16lib.

How embarrassing, you are correct re the 29gb.  Stupid decimal points ;)

So, given that the reported value is incorrect, am I still able to write to all 16gb of the card?

Adrian_the_Canadian

Looks like the SD "CardInfo" example sketch still has not been fixed in IDE 1.6.0.
The issue is that the long variable used to hold the calculated storage capacity overflows for cards with more than 4GB.
The fix is straightforward but does not appear to have made it into the latest Arduino release.
Anyone?
... Adrian_The_Canadian

penjoseph

#4
Nov 04, 2015, 11:39 am Last Edit: Nov 06, 2015, 06:37 am by penjoseph
The answer to this issue is in adafruit forum:
http://forums.adafruit.com/viewtopic.php?f=31&t=35455

- It is a workaround for uint32_t (or unsigned long) declaration which is limited to positive integer values of  4,294,967,295. The solution for sd cards >4GB is that the volume size bytes calculation (3955228672) is omitted, only Kbytes & Mbytes are displayed.

Have been successful in connecting a SD card module to Arduino. Tested in Arduino 1.0.6 IDE with both 4GB & 16GB micro sd cards.

Left 4GB sd card | Right 16GB sd card


As observed the bytes calculation is omitted for the 16GB card.

For 4GB -
Volume size (bytes): 3955228672
Volume size (Kbytes): 3862528
Volume size (Mbytes): 3772
For 16GB -
Volume size (Kbytes): 15542720
Volume size (Mbytes): 15178

Have the edited CardInfo.ino & uploaded to google drive as on 4th Nov '15 for support of >4GB SD card: edited 4Nov'15 CardInfo.ino

Slow Coach

penjoseph

#5
Nov 05, 2015, 09:15 am Last Edit: Nov 06, 2015, 06:43 am by penjoseph
I am working on the code to omit bytes calculation and include giga byte (GB) calculation so that the display format will be same for all cards from 2GB to 32GB.  Am testing it. Will upload the new CardInfo.ino file.

1) Result with 4GB sd card reporting 3.68 GB in Windows 8 H:\ .....


New Arduino SD CardInfo output reporting 3.68 GB .....
Code: [Select]

Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT32

Volume size
 KBytes : 3862528
 MBytes : 3772
 GBytes : 3.68

Files found on the card (name, date and size in bytes):
SYSTEM~1/     2015-11-04 15:21:52
  INDEXE~1      2015-11-04 15:21:52 76



Slow Coach

penjoseph

#6
Nov 05, 2015, 11:05 am Last Edit: Nov 05, 2015, 11:45 am by penjoseph
2) Result with 16GB sd card reporting 14.8 GB in Windows 8 H:\ .....



New Arduino SD CardInfo output reporting 14.82 GB ......
Code: [Select]
Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT32

Volume size
 KBytes : 15542720
 MBytes : 15178
 GBytes : 14.82

Files found on the card (name, date and size in bytes):
SYSTEM~1/     2015-11-04 11:37:52
  INDEXE~1      2015-11-04 11:37:52 76
Slow Coach

penjoseph

#7
Nov 05, 2015, 11:32 am Last Edit: Nov 06, 2015, 06:48 am by penjoseph
This is my latest edited CardInfo.ino version uploaded to google drive as on 5th Nov '15 to display SD card size in GB too: edited 5Nov15 CardInfo.ino

The file size has increased from 12.53  to 14.29 kbytes since float variable had to be  incorporated  to perform decimal calculation.

Use SD Formatter tool to format SD cards to have it detected in Arduino properly - SD Formatter   
In the 'OPTION' button select FORMAT TYPE 'QUICK' & FORMAT SIZE ADJUSTMENT 'ON'
Slow Coach

penjoseph


For advanced SD card features fat16lib \ Bill Greiman who has done extensive research has created another library SdFat (replacement for default Arduino SD library) https://github.com/greiman/SdFat

This is 4GB sd card output of Sdfat library -> Sdinfo
Code: [Select]


SdFat version: 20150324

Assuming the SD is the only SPI device.
Edit DISABLE_CHIP_SELECT to disable another device.

Assuming the SD chip select pin is: 4
Edit SD_CHIP_SELECT to change the SD chip select pin.

type any character to start

init time: 17 ms

Card type: SDHC

Manufacturer ID: 0X3
OEM ID: SD
Product: SS04G
Version: 8.0
Serial number: 0XEA5CE30C
Manufacturing date: 4/2015

cardSize: 3965.19 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0XC0FF8000

SD Partition Table
part,boot,type,start,length
1,0X0,0XB,8192,7733248
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT32
blocksPerCluster: 64
clusterCount: 120704
freeClusters: 120701
freeSpace: 3955.13 MB (MB = 1,000,000 bytes)
fatStartBlock: 14496
fatCount: 2
blocksPerFat: 944
rootDirStart: 2
dataStartBlock: 16384

type any character to start



This is 16GB sd card output of Sdfat library -> Sdinfo
Code: [Select]

SdFat version: 20150324

Assuming the SD is the only SPI device.
Edit DISABLE_CHIP_SELECT to disable another device.

Assuming the SD chip select pin is: 4
Edit SD_CHIP_SELECT to change the SD chip select pin.

type any character to start

init time: 25 ms

Card type: SDHC

Manufacturer ID: 0X3
OEM ID: SD
Product: SU16G
Version: 8.0
Serial number: 0X9F12CF20
Manufacturing date: 8/2013

cardSize: 15931.54 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0XC0FF8000

SD Partition Table
part,boot,type,start,length
1,0X0,0XC,8192,31093648
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT32
blocksPerCluster: 64
clusterCount: 485710
freeClusters: 485707
freeSpace: 15915.65 MB (MB = 1,000,000 bytes)
fatStartBlock: 8794
fatCount: 2
blocksPerFat: 3795
rootDirStart: 2
dataStartBlock: 16384

type any character to start


The reported card size in MB is more accurate with fat16lib's Sdfat library & code
Slow Coach

fat16lib

Note that SD.h is just a wrapper for an old version of SdFat.  The SD.h wrapper and examples have not been well maintained by the Arduino Company.  The Card size problem should have been fixed many years ago.

bit432

I had the same problem. Sorry about replying to old thread, but search led me to this.  Sd2card.cpp file is incorrect. I figured a solution:

the previous code in function
Code: [Select]

else if (csd.v2.csd_ver == 1) {
    uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16)
                      | (csd.v2.c_size_mid << 8) | csd.v2.c_size_low;
    return (c_size + 1) << 10;


to:
Code: [Select]

else if (csd.v2.csd_ver == 1) {
 
    uint32_t c_size = (csd.v2.c_size_high<<16) | (csd.v2.c_size_mid << 8) | csd.v2.c_size_low ;//combine byte
    return (c_size+1)<<9;//(c_size+1)*512;

  }

The mistake was multipling by 512 they bitshift 10 instead of 9.  So your result ends up being twice.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy