gpt4 book ai didi

c - 使用 SPI 向 SD 写入 block ,来自 SD 的奇怪响应

转载 作者:行者123 更新时间:2023-12-04 04:47:26 30 4
gpt4 key购买 nike

这是我将 512 字节块写入 SD 卡的代码。代码工作正常,但是当我检查一切正常时(通过阅读 SD 的响应),我读到 0xFF .

该值应该类似于(来自 SD 引用手册):

‘010’—Data accepted.
‘101’—Data rejected due to a CRC error.
‘110’—Data rejected due to a Write Error

这是代码:
uint8_t SdCard_SendBlock(uint32_t block, uint8_t * data) 
{


switch (sd_write_blk_machine.fields.state)
{

case WRITE_START:

//Enable Card
GPIOC_PDOR &= ~GPIO_PDOR_PDO(GPIO_PIN(10));

sd_cmd_arg.sd_cmd_tot_argument = block << SD_BLOCK_SHIFT;
sd_write_blk_machine.fields.state = WRITE_SEND_CMD24;

/*INIZIALIZZO LE VARIABILI LOCALI*/
write_send_data_counter = 0;
sd_cmd_machine.sd_cmd_machine = 0;
break;

case WRITE_SEND_CMD24:
send_command_return = SdSendCmd(CMD24|0x40,ASPECTED_OK_RESPONSE);

if( send_command_return == SDCARD_CMD_FAILS)
{
//Disable Card
GPIOC_PDOR |= GPIO_PDOR_PDO(GPIO_PIN(10));
sd_write_blk_machine.fields.complete = 1;
system_error.flags.sdcard_error = SDCARD_WRITE_FAIL;
return(SDCARD_WRITE_FAIL);
}
sd_write_blk_machine.fields.state = WRITE_SEND_START_TOKEN;
}
break;

case WRITE_SEND_START_TOKEN:
Spi_writeData(SPI0,SD_TOK_WRITE_SBLOCK); //SD_TOK_WRITE_SBLOCK = 0xFE ,

spi_control_machine.spi_control_machine = 0;
sd_write_blk_machine.fields.state = WRITE_SEND_DATA;

break;

case WRITE_SEND_DATA:

if (write_send_data_counter < SDCARD_BLOCK_SIZE) //SDCARD_BLOCK_SIZE = 512 byte
{
Spi_writeData(SPI0, data[write_send_data_counter]);
{
spi_control_machine.spi_control_machine = 0;
write_send_data_counter++;
}
}
else
sd_write_blk_machine.fields.state = WRITE_SEND_IDLE_1;
break;

case WRITE_SEND_IDLE_1:
Spi_writeData(SPI0,0xFF); // 0xFF = SPI_IDLE



sd_write_blk_machine.fields.state = WRITE_SEND_IDLE_2;


break;

case WRITE_SEND_IDLE_2:
Spi_writeData(SPI0,0xFF); // 0xFF = SPI_IDLE


sd_write_blk_machine.fields.state = WRITE_READ_RESPONSE_TOKEN;

break;

case WRITE_READ_RESPONSE_TOKEN:
/*Every data block written to the card will be acknowledged by a data response token. It is one byte long
and has the following format:
x x x 0 Status 1
The meaning of the status bits is defined as follows:
010 - Data accepted.
101 - Data rejected due to a CRC error.
110 - Data Rejected due to a Write Error*/



spi_control_machine.spi_control_machine = 0;
Spi_readData(SPI0, &write_read_response); // HERE IS THE PROBLEM !!!! write_read_response = 0xFF

if ( (write_read_response & 0x0F) != SD_ACCEPTED_WRITE_DATA )
{
//disabilita carta
GPIOC_PDOR |= GPIO_PDOR_PDO(GPIO_PIN(10));
system_error.flags.sdcard_error = SDCARD_WRITE_FAIL;
sd_write_blk_machine.fields.complete = 1;
SendBlockReturn=0;
return (SDCARD_WRITE_FAIL);

}


sd_write_blk_machine.fields.complete = 1;
status.flags.sdwrite_wait_attemp = 1;
SendBlockReturn=1;
return (TERMINATE_OK);
break;

}

问题出在交换机的最后一个案例中。

最佳答案

看起来卡在完成写入之前返回 0xFF。根据Physical Layer Simplified Specification in section 7.2.4 :

As long as the card is busy programming, a continuous stream of busy tokens will be sent to the host (Effectively holding the DataOut line low).



也就是说,我不确定为什么该卡没有返回 '010' accepted首先响应 - 这可能是卡制造商不遵守规范的情况。

你想要做的是重复调用 Spi_readData 直到它返回的字节变成 0'010' accepted .

关于c - 使用 SPI 向 SD 写入 block ,来自 SD 的奇怪响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17968257/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com