SD卡讀CSD寄存器
{
uint8 CSDStruct; // CSD結(jié)構(gòu)
uint8 SysSpecVersion; // 系統(tǒng)規(guī)范版本
uint8 Reserved1; // 保留
uint8 TAAC; // 讀取時(shí)間1
uint8 NSAC; // 數(shù)據(jù)在CLK周期內(nèi)讀取時(shí)間2
uint8 MaxBusClkFrec; // 最大總線速度
uint16 CardComdClasses; // 卡命令集合
uint8 RdBlockLen; // 最大讀取數(shù)據(jù)塊長(zhǎng)
uint8 PartBlockRead; // 允許讀的部分塊
uint8 WrBlockMisalign; // 非線寫(xiě)塊
uint8 RdBlockMisalign; // 非線讀塊
uint8 DSRImpl; // DSR條件
uint8 Reserved2; // 保留
uint32 DeviceSize; // 設(shè)備容量
uint8 MaxRdCurrentVDDMin; // 最小讀取電流 @ VDD min
uint8 MaxRdCurrentVDDMax; // 最大讀取電流 @ VDD max
uint8 MaxWrCurrentVDDMin; // 最小寫(xiě)入電流 @ VDD min
uint8 MaxWrCurrentVDDMax; // 最大寫(xiě)入電流 @ VDD max
uint8 DeviceSizeMul; // 設(shè)備容量乘積因子
uint8 EraseGrSize; // 擦出塊大小
uint8 EraseGrMul; // 擦出扇區(qū)大小
uint8 WrProtectGrSize; // 寫(xiě)保護(hù)群大小
uint8 WrProtectGrEnable; // 寫(xiě)保護(hù)群使能
uint8 ManDeflECC; // Manufacturer default ECC
uint8 WrSpeedFact; // 寫(xiě)速度因子
uint8 MaxWrBlockLen; // 最大寫(xiě)數(shù)據(jù)塊長(zhǎng)度
uint8 WriteBlockPaPartial; // 允許寫(xiě)的部分
uint8 Reserved3; // 保留
uint8 ContentProtectAppli; // Content protection application
uint8 FileFormatGrouop; // 文件系統(tǒng)群
uint8 CopyFlag; // 拷貝標(biāo)志
uint8 PermWrProtect; // 永久寫(xiě)保護(hù)
uint8 TempWrProtect; // 暫時(shí)寫(xiě)保護(hù)
uint8 FileFormat; // 文件系統(tǒng)
uint8 ECC; // ECC code
uint8 CSD_CRC; // CSD CRC
uint8 Reserved4; // 始終為 1
} SD_CSD;
/**************************************************************************************
* FunctionName : SD_GetCSDRegister()
* Description : CSD-寄存器。寄存器長(zhǎng)度為128,16字節(jié)
* EntryParameter : csd 寄存器
* ReturnValue : 返回操作狀態(tài):成功-0
**************************************************************************************/
uint8 SD_GetCSDRegister(SD_CSD *csd)
{
uint8 i;
uint8 csdTable[16];
uint8 count = 0xFF;
uint8 rvalue = SD_RESPONSE_FAILURE; // 返回值
SD_Enable(0); // SD卡使能
if (SD_SendCmd(CMD9,0x00,0xFF) == SD_RESPONSE_NO_ERROR)
{
while ((SD_ReadByte() != SD_START_SINGLE_BLOCK_READ) && count) // 等待數(shù)據(jù)接收開(kāi)始,收到0xFE表示開(kāi)始
{
count--; // 等待超時(shí)
}
if (count != 0x00)
{
for (i=0; i<16; i++)
{
csdTable[i] = SD_ReadByte();
}
}
SD_ReadByte(); // 讀CRC
SD_ReadByte();
rvalue = SD_RESPONSE_NO_ERROR; // 設(shè)置成功標(biāo)志
}
SD_Enable(1); // 清除SD卡片選
SD_WriteByte(0xFF); // 8個(gè)時(shí)鐘脈沖的延遲
// 把獲取值放入CSD結(jié)構(gòu)體中
csd->CSDStruct = (csdTable[0] & 0xC0) >> 6; // Byte 0
csd->SysSpecVersion = (csdTable[0] & 0x3C) >> 2;
csd->Reserved1 = csdTable[0] & 0x03;
csd->TAAC = csdTable[1]; // Byte 1
csd->NSAC = csdTable[2]; // Byte 2
csd->MaxBusClkFrec = csdTable[3]; // Byte 3
csd->CardComdClasses = csdTable[4] << 4; // Byte 4
csd->CardComdClasses |= (csdTable[5] & 0xF0) >> 4; // Byte 5
csd->RdBlockLen = csdTable[5] & 0x0F;
csd->PartBlockRead = (csdTable[6] & 0x80) >> 7; // Byte 6
csd->WrBlockMisalign = (csdTable[6] & 0x40) >> 6;
csd->RdBlockMisalign = (csdTable[6] & 0x20) >> 5;
csd->DSRImpl = (csdTable[6] & 0x10) >> 4;
csd->Reserved2 = 0;
csd->DeviceSize = (csdTable[6] & 0x03) << 10;
csd->DeviceSize |= (csdTable[7]) << 2; // Byte 7
csd->DeviceSize |= (csdTable[8] & 0xC0) >> 6; // Byte 8
csd->MaxRdCurrentVDDMin = (csdTable[8] & 0x38) >> 3;
csd->MaxRdCurrentVDDMax = (csdTable[8] & 0x07);
csd->MaxWrCurrentVDDMin = (csdTable[9] & 0xE0) >> 5; // Byte 9
csd->MaxWrCurrentVDDMax = (csdTable[9] & 0x1C) >> 2;
csd->DeviceSizeMul = (csdTable[9] & 0x03) << 1;
csd->DeviceSizeMul |= (csdTable[10] & 0x80) >> 7; // Byte 10
csd->EraseGrSize = (csdTable[10] & 0x40) >> 6;
csd->EraseGrMul = (csdTable[10] & 0x3F) << 1;
csd->EraseGrMul |= (csdTable[11] & 0x80) >> 7; // Byte 11
csd->WrProtectGrSize = (csdTable[11] & 0x7F);
csd->WrProtectGrEnable = (csdTable[12] & 0x80) >> 7; // Byte 12
csd->ManDeflECC = (csdTable[12] & 0x60) >> 5;
csd->WrSpeedFact = (csdTable[12] & 0x1C) >> 2;
csd->MaxWrBlockLen = (csdTable[12] & 0x03) << 2;
csd->MaxWrBlockLen |= (csdTable[13] & 0xC0) >> 6; // Byte 13
csd->WriteBlockPaPartial = (csdTable[13] & 0x20) >> 5;
csd->Reserved3 = 0;
csd->ContentProtectAppli = (csdTable[13] & 0x01);
csd->FileFormatGrouop = (csdTable[14] & 0x80) >> 7; // Byte 14
csd->CopyFlag = (csdTable[14] & 0x40) >> 6;
csd->PermWrProtect = (csdTable[14] & 0x20) >> 5;
csd->TempWrProtect = (csdTable[14] & 0x10) >> 4;
csd->FileFormat = (csdTable[14] & 0x0C) >> 2;
csd->ECC = (csdTable[14] & 0x03);
csd->CSD_CRC = (csdTable[15] & 0xFE) >> 1; // Byte 15
csd->Reserved4 = 1;
return rvalue;
}
評(píng)論