【发布时间】:2013-03-20 12:21:41
【问题描述】:
我目前正在使用 MSP430F2618 MCU 和 SanDisk 4GB SDHC 卡的记录仪工作。
卡初始化按预期工作,我还可以读取 MBR 和 FAT 表。
问题是我不能在上面写任何数据。我检查了它是否受缺口写保护,但事实并非如此。 Windows 7 操作系统读取/写入没有问题。
不过,我使用了一个名为“HxD”的工具,并尝试更改某些扇区(在 Windows 下)。当我尝试将内容保存到 SD 卡时,该工具会弹出一个窗口告诉我“访问被拒绝!”。
然后我回到我的写入 SD 卡的代码:
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;
}
问题似乎是我在等待卡状态时:
do
{
SdWrite(0xFF);
dataResp = SdRead();
}
while (dataResp == 0x00);
我在这里等待“X5”(十六进制值)类型的响应,其中 X 未定义。
但大多数情况下响应是 0x00(十六进制值),我没有跳出循环。响应为 0xFF(十六进制值)的情况很少。
我不知道是什么问题。
谁能帮帮我?谢谢!
【问题讨论】:
-
(您在代码和问题中使用了幻数,这真的很难理解。)我在 SD 卡协议规范中找不到任何地方说没有使用 CRC关于 WRITE 和 READ 数据传输,该文档仅声称 CRC 未用于命令。 CRC 故障导致卡无响应。
-
再次阅读规范,一旦卡进入SPI模式,CRC就会被禁用。
-
也许你应该再次阅读规范@TurboJ。
In the CRC OFF mode, the CRC bits of the COMMAND are defined as 'don't care' for the transmitter and ignored by the receiver. The SPI interface is initialized in the CRC OFF mode in default.当它说 CRC 对 COMMAND 禁用时,文本很清楚。还有更多:A valid data block is suffixed with a 16-bit CRC generated by the standard CCITT polynomial x16+x12+x5+1. -
仔细阅读第 7.2.2 章。目的是完全省略所有 CRC 检查。当您必须将更复杂的 CRC-16 应用于数据时,仅省略 CRC-7 是没有意义的。我的工作代码库证明了这一点 - 我在那里没有使用任何 CRC。