我目前正在使用MSP430F2618 MCU和SanDisk 4GB SDHC卡的記錄器工作。嵌入式:SDHC SPI寫入問題
卡初始化按預期工作,我也可以讀取MBR和FAT表。
問題是我無法在其上寫入任何數據。我已經檢查過它是否由刻痕保護,但事實並非如此。 Windows 7操作系統讀取/寫入沒有問題。
雖然,我已經使用了一個名爲「HxD」的工具,我試圖改變一些扇區(在Windows下)。當我嘗試將內容保存到SD卡時,該工具會彈出一個窗口,告訴我「訪問被拒絕!」。
然後,我回到了我的代碼寫入到SD卡:
do
{
SdWrite(0xFF);
dataResp = SdRead();
}
while (dataResp == 0x00);
我在這裏等待:
uint8_t SdWriteBlock(uchar_t *blockData, const uint32_t address)
{
uint8_t result = OP_ERROR;
uint16_t count;
uchar_t dataResp;
uint8_t idx;
for (idx = RWTIMEOUT; idx > 0; idx--)
{
CS_LOW();
SdCommand(CMD24, address, 0xFF);
dataResp = SdResponse();
if (dataResp == 0x00)
{
break;
}
else
{
CS_HIGH();
SdWrite(0xFF);
}
}
if (0x00 == dataResp)
{
//send command success, now send data starting with DATA TOKEN = 0xFE
SdWrite(0xFE);
//send 512 bytes of data
for (count = 0; count < 512; count++)
{
SdWrite(*blockData++);
}
//now send tow CRC bytes ,through it is not used in the spi mode
//but it is still needed in transfer format
SdWrite(0xFF);
SdWrite(0xFF);
//now read in the DATA RESPONSE TOKEN
do
{
SdWrite(0xFF);
dataResp = SdRead();
}
while (dataResp == 0x00);
//following the DATA RESPONSE TOKEN are a number of BUSY bytes
//a zero byte indicates the SD/MMC is busy programing,
//a non_zero byte indicates SD/MMC is not busy
dataResp = dataResp & 0x0F;
if (0x05 == dataResp)
{
idx = RWTIMEOUT;
do
{
SdWrite(0xFF);
dataResp = SdRead();
if (0x0 == dataResp)
{
result = OP_OK;
break;
}
idx--;
}
while (idx != 0);
CS_HIGH();
SdWrite(0xFF);
}
else
{
CS_HIGH();
SdWrite(0xFF);
}
}
return result;
}
這個問題似乎是,當我在等待卡狀態是類型爲「X5」的響應(十六進制值),其中X未定義。
但大多數情況下,響應是0x00(十六進制值),我不走出循環。幾乎沒有什麼時候響應是0xFF(十六進制值)。
我找不出什麼問題。
任何人都可以幫助我嗎?謝謝!
(您在代碼中和您的問題中使用了幻數,這使得它很難理解)。我無法在SD卡協議規範中的任何地方找到在WRITE和READ數據上沒有使用CRC的情況該文件僅聲稱CRC不用於命令。 CRC錯誤使卡無法響應。 – 2013-03-20 18:35:34
再次閱讀規範,一旦卡進入SPI模式,CRC被禁用。 – 2013-03-20 21:51:37
也許你應該再次閱讀規範@TurboJ。 '在CRC關閉模式下,COMMAND的CRC位被定義爲'不關心'發送器並被接收器忽略。 默認情況下,SPI接口初始化爲CRC關閉模式。當文本顯示CRC禁用COMMAND時,文本清晰。還有更多的:'一個有效的數據塊後綴有由標準CCITT多項式x16 + x12 + x5 + 1生成的16位CRC。' – 2013-03-21 11:58:10