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!
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!
size in bytes / 528?
size in bytes / 528?
take a look at the SD specification,
It's determined by the CSD register.
Get back to me on the number of overhead bytes per sector... especially if its different from 16
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
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.
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.
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-49Capacity = 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.
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!
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.
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.
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?
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?
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