bianchi77
Advanced Member level 4
- Joined
- Jun 11, 2009
- Messages
- 1,313
- Helped
- 21
- Reputation
- 44
- Reaction score
- 20
- Trophy points
- 1,318
- Location
- California
- Activity points
- 9,442
Guys, I've been trying to read the capacity of sd card, but always fail, anything that I can work around ?
thanks for reading and helping,
Here's the function :
thanks for reading and helping,
Here's the function :
Code:
uint32 MMC_SD_ReadCapacity()
{
uint8 r1;
uint16 i;
uint16 temp;
uint8 buffer[16];
uint32 Capacity;
uint16 retry =0;
//uint8 retry=0;
r1 = MMC_SD_SendCommand(9, 0);//дÃüÁî //send command //READ CSD
if(r1 != 0x00)
return r1;
SPI_CS_Assert();
while(SPI_WriteByte(0xff) != 0xfe)if(retry++ > 0xfffe){SPI_CS_Deassert();return 1;}
for(i=0;i<16;i++)
{
buffer[i]=SPI_WriteByte(0xff);
}
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_CS_Deassert();
SPI_WriteByte(0xff);// extra 8 CLK
/*********************************/
// C_SIZE
i = buffer[6]&0x03;
i<<=8;
i += buffer[7];
i<<=2;
i += ((buffer[8]&0xc0)>>6);
/**********************************/
// C_SIZE_MULT
r1 = buffer[9]&0x03;
r1<<=1;
r1 += ((buffer[10]&0x80)>>7);
/**********************************/
// BLOCKNR
r1+=2;
temp = 1;
while(r1)
{
temp*=2;
r1--;
}
Capacity = ((uint32)(i+1))*((uint32)temp);
/////////////////////////
// READ_BL_LEN
i = buffer[5]&0x0f;
/*************************/
//BLOCK_LEN
temp = 1;
while(i)
{
temp*=2;
i--;
}
/************************/
/************** formula of the capacity ******************/
//
// memory capacity = BLOCKNR * BLOCK_LEN
//
// BLOCKNR = (C_SIZE + 1)* MULT
//
// C_SIZE_MULT+2
// MULT = 2
//
// READ_BL_LEN
// BLOCK_LEN = 2
/**********************************************/
//The final result
Capacity *= (uint32)temp;
return Capacity;
}