1
\$\begingroup\$

I am working with an SD card using AVR microcontroller.

Questions:

  • Is it possible to change the sector size of SD cards or is it fixed 512 bytes in SD card's case?
  • What things should be considered if I want to change the sector size of SD card with MCU?

Waiting for suggestion, Thank you.

\$\endgroup\$
2
\$\begingroup\$

The sector size of an SD card is fix, so you can not change it. Most cards use 512 or 1024 byte sector size. To get the best performance out of the card, you should try to write in blocks of the sector size (or multiples), since sectors are written internally as a whole.

\$\endgroup\$
1
\$\begingroup\$

I would strongly recommend to stick to standard as much as possible, and not change block size from 512 bytes. Furthermore, I recommend to explicitly issue CMD16 command with 512 byte block size during card initialization to ensure compatibility.

Reasons are the following:

  • Several (or maybe many) cards will refuse you setting/using block size other than 512 bytes. I do not think you want to run into the problem when you are locked to specific manufacturer or even model of the card because of non-512-byte-block limitation you set;
  • file systems are used to default to 512 sector size (do not confuse with cluster size), thus 512 bytes is actually very convenient size from software perspective;
  • it probably does not make sense to support partial block operations, it will add complexity to the code and make interoperability harder;

If you really want to access card with larger data transfers, use CMD18/CMD25 (read/write multiple block) instead. It will increase throughput, but you will have slightly more complexity properly managing data stream and stopping the command execution.

\$\endgroup\$
0
\$\begingroup\$

You may be able to increase the size of the data block returned by the SD card when you issue a SINGLE_BLOCK_READ command (CMD17). This is done with CMD16 and the maximum/minimum block size allowed depends on the SD version as well as if you're using an SDSC, SDHC or SDXC card. From what I remember, later versions of SDSC cards support a block length of 1024 bytes and I think SDHC and SDXC support up to 4096 bytes block length (verify!). You will have to read the SD specs for more details.

Even if you change the block length though, you will have to account for this in your FAT driver, assuming the card is FAT-formatted. The BYTES_PER_SEC field of the Boot Parameter Block is usually set to 512 bytes by most SD formatters for maximum compability with most card readers. This means a single block read == a single FAT sector, for most readers. If you increase yours to, say, 1024 though, it means a single block read == 2 FAT sectors, assuming the FAT system uses 512-byte sectors and you arent formatting the cards to your specific needs. This means if you want to read/modify a single sector, you have to clock out 2 sectors--inefficient. However, you could format the card and change BYTES_PER_SEC to match the 1024-byte block length of the card (this depends on the FAT type), though you will have to be careful to ensure any affected fields are similarly adjusted.

Increasing the block length also means increasing the size of the buffer you use to store blocks you've read. In general, its a lot safer to increase the block length and account for this in your code than it is to modify the FAT fields and risk rendering the card unreadable on some card readers (though Microsoft readers will always support anything within FAT specs).

As far as the optimal block size is concerned, please see this article, (MicroSD flash block size), which discusses the native write/erase block size for SD cards. Buffering by this value may improve performance and/or reduce power consumption.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.