Pages: [1]   Go Down
Author Topic: A better way to format SD cards  (Read 9590 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1656
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For three years I have been trying to find an easy way to correctly format SD cards for optimal use on the Arduino. 

I finally decided to write a sketch to format SD cards. It is included as one of the examples, SdFormatter.pde, in the 20110414 beta of SdFat:

http://code.google.com/p/beta-lib/downloads/list

I decided to write SdFormatter.pde since the only program I know that complies with the SD File System Specification is here:

http://www.sdcard.org/consumers/formatter_3/

It only runs on PCs and formats small SD cards as FAT12.  PC/Mac/Linux format utilities do not comply with the above standard and do not produce optimal file system structures for the Arduino.

For SD/SDHC cards larger than 64 MB this sketch attempts to match the format generated by the sdcard.org formatter.  For smaller cards, 16 - 64 MB, it use FAT16 instead of FAT12.

It chooses the best file system, FAT16/FAT32, the optimum cluster size, and aligns file system structures on the card's flash erase boundaries.

This version has the chip select pin hard coded as the SPI SS pin.  This will be a menu option in the future.

To change chip select to pin 4 replace line 439
Code:
  if (!card.init()) sdError("card.init failed");
with
 
Code:
  if (!card.init(SPI_HALF_SPEED, 4)) sdError("card.init failed");

Please let me know how it works.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For three years I have been trying to find an easy way to correctly format SD cards for optimal use on the Arduino. 

I decided to write SdFormatter.pde since the only program I know that complies with the SD File System Specification is here:

Please let me know how it works.





This sketch can erase and/or format SD/SDHC cards.



Erase uses the card's fast flash erase command.

Flash erase sets all data to 0X00 for most cards

and 0XFF for a few vendor's cards.



Cards larger than 2 GB will be formatted FAT32 and

smaller cards will be formatted FAT16.



Warning, all data on the card will erased.

Enter 'Y' to continue: Y



Options are:

E - erase the card and skip formatting.

F - erase and then format the card. (recommended)

Q - quick format the card without erase.



Enter option: F

Card Size: 3782 MB, (MB = 1,048,576 bytes)



Erasing

..............................

All data set to 0x00

Erase done



Formatting

Blocks/Cluster: 64

FAT32

........

Format done


--------------------------------------------------
I ran the following tests on the formatted card moved to the computer.....to analyse the file system, hope it helps.....

kalum@d:~$ sudo fdisk  /dev/mmcblk0

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/mmcblk0: 3965 MB, 3965190144 bytes
49 heads, 48 sectors/track, 3292 cylinders
Units = cylinders of 2352 * 512 = 1204224 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1               4        3293     3868160    b  W95 FAT32


kalum@d:~$ sudo fsck.vfat /dev/mmcblk0p1
dosfsck 3.0.7, 24 Dec 2009, FAT32, LFN
/dev/mmcblk0p1: 0 files, 1/120752 clusters





Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1656
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have had some mail about why there are no options for SdFormatter.  The reason is that SD Association's standard specifies FAT16 for card's 64 MB - 2 GB and FAT32 for larger cards.  The cluster size is also specified.

I wrote SdFormatter since people often don’t understand the internal structure of SD cards and the reasons for the SD File System Specification issued by the SD Association.

Here is an extreme example. Note the write speed difference with the correct format and layout. 14.13 KB/sec vs. 291.04 KB/sec. The Windows 7 layout kills the flash controller in this card. With the correct layout it is one of the fastest cards around.

I formatted a 512 MB card FAT32 with small, 1 KB, clusters using Windows 7. Here are the results of running the bench.pde example.
Quote
Type any character to start
Free RAM: 1075
Type is FAT32
File size 1MB
Starting write test. Please wait up to a minute
Write 14.13 KB/sec

Starting read test. Please wait up to a minute
Read 244.56 KB/sec
Done
I reformatted the card with SdFormatter.pde. Here is the results with FAT16 and the layout and cluster size specified by the standard:
Quote
Type any character to start
Free RAM: 1075
Type is FAT16
File size 1MB
Starting write test. Please wait up to a minute
Write 291.04 KB/sec

Starting read test. Please wait up to a minute
Read 326.80 KB/sec
Done
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I reformatted the card with SdFormatter.pde. Here is the results with FAT16 and the layout and cluster size specified by the standard:
Quote
Type any character to start
Free RAM: 1075
Type is FAT16
File size 1MB
Starting write test. Please wait up to a minute
Write 291.04 KB/sec

Starting read test. Please wait up to a minute
Read 326.80 KB/sec
Done

Impressive indeed. I did not notice any speed improvement as i use linux's mkfs.vat which seems to get things correctly. However your latest (sdfat beta) library is much faster than V1, it's read benchmark gives me 270K/sec whereas the old one gave me only 250K/sec.
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1656
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did some tests on Ubuntu 10.04.  With a totally erased SD.  The disk utility formats small cards as a super floppy, no MBR.   This is not allowed in the standard but I accept it in SdFat.

With larger cards it tends to pick a cluster size that is small for optimum performance.

Tried mkfs.vfat and it never chooses a format that complies with the SD File System Specification.  It often fails to align file structures on flash erase group boundaries.  It tends to pick a cluster size that is too small.

Most card perform fairly well with the format mkfs.vfat chooses but some are degraded a bit.

Logged

Québec
Offline Offline
God Member
*****
Karma: 17
Posts: 673
Hey bien!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't want to be a pain, but where is your sd formating programe these days?
Logged

As we fight our way northward into the great unknown, only that one thing remains certain...

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2214
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't want to be a pain, but where is your sd formating programe these days?

I think there is a formatting example in the sdfat library you can use with a duino to format if needed.
https://code.google.com/p/sdfatlib/
Logged


Pages: [1]   Go Up
Jump to: