how to determine number of sectors of mmc/sd card?

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there - I'm working on writing some MMC/SD initialization code. I'm trying to figure out how to determine the number of sectors in a card. Where is this defined? I can't seem to figure it out, and Googling doesn't seem to be helping.

Thanks!

Last Edited: Fri. May 12, 2017 - 04:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

size in bytes / 528?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
size in bytes / 528?

OK, sure - (though you mean 512 I believe) - but how do I find out the size in bytes?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

take a look at the SD specification,
It's determined by the CSD register.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Get back to me on the number of overhead bytes per sector... especially if its different from 16

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The sectors are really 512 bytes long like on hard drives and floppy disks. However sometimes there is a 16-bit CRC involved in the transmission, but has nothing to do with the sector size.

Atmel DataFlash devices have x bytes extra per sector.

- Jani

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Samsung's NAND flash have 528bytes per sector.
But I don't think the extra bytes is the part of the capacity in MMC/SD or other storage media which use this NAND flash.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

According to the spec, you can determine the card size by examining the contents of the CSD register, which is 128 bits long and read with CMD9. You can then calculate it.

read_bl_len is bits 80-83
c_size is bits 62-73
c_size_mult is bits 47-49

Capacity = block_len * block_nr

block_nr = (c_size + 1) * mult

mult = 2^(c_size_mult + 2)

block_len = 2^read_bl_len

For cards <=1 GB the block_len is 512, for 2GB it is 1024.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dtlinker wrote:
According to the spec, you can determine the card size by examining the contents of the CSD register, which is 128 bits long and read with CMD9. You can then calculate it.

read_bl_len is bits 80-83
c_size is bits 62-73
c_size_mult is bits 47-49

Capacity = block_len * block_nr

block_nr = (c_size + 1) * mult

mult = 2^(c_size_mult + 2)

block_len = 2^read_bl_len

For cards <=1 GB the block_len is 512, for 2GB it is 1024.


I found the SD spec earlier today and discovered this. I'm glad to see I was looking at the right thing!

Just a couple quick questions:

Is CMD9 just like CMD17, except that no address is given, and you only read 16 data bytes + 2 CRC bytes?

Also, what order does the CSD arrive? I'm assuming the first byte is bits 0-7. So, this is how I'm computing the number of sectors:

C_SIZE = csd[7]>>6 + csd[8]<<2 + (csd[9] & 0x03)<<10;
C_SIZE_MULT = csd[5]>>7 + (csd[6] & 0x03)<<1;
sectorCount = (C_SIZE + 1) * (1<<(C_SIZE_MULT + 2));

(where csd is an unsigned char array containing the CSD, with csd[0] being the first byte read over SPI)

I would test the code myself, to see if it works, but my hardware (which I've tested initialization, block read, and block write on) - is halfway across the country, and I won't have access to it till June.

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The bytes are returned MSB first, that is, the opposite of what you assumed. The first byte is bits 127:120.

You are correct that CMD9 is like CMD17, except that there is no address, and only 16 bytes are read.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dtlinker wrote:
The bytes are returned MSB first, that is, the opposite of what you assumed. The first byte is bits 127:120.

You are correct that CMD9 is like CMD17, except that there is no address, and only 16 bytes are read.


Ah - oops. Here is how I'll calculate it then:

C_SIZE = csd[8]>>6 + csd[7]<<2 + (csd[6] & 0x03)<<10;
C_SIZE_MULT = csd[10]>>7 + (csd[9] & 0x03)<<1;
sectorCount = (C_SIZE + 1) * (1<<(C_SIZE_MULT + 2));

I love how easy they split it over as many bytes as possible. Who needs simplicity when you can make things annoyingly complicated?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry for commenting in an old thread.But..

What if it's an SDIO only SD card? We can't use CMD9 as it's not supported. How do we find the number of sectors in this case?

Clament John | Still in school 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Exactly how many threads do you need for this? Either there is someone here who knows and will have read one of the other threads or there isn't. Just pasting the same thing on multiple threads is not going to rustle up a reply any quicker.

 

As such I'll lock this old thread.

 

Moderator

Topic locked