用SPI写一个块到SD,来自SD的奇怪响应

时间:2013-07-31 10:46:19

标签: c embedded sd-card spi

这是我将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;

    }

问题出在交换机的最后一种情况。

2 个答案:

答案 0 :(得分:2)

看起来卡片正在返回0xFF,直到它完成写入。根据{{​​3}}:

  

只要卡正忙于编程,就会向主机发送连续的忙标记流(有效地将DataOut线保持在低位)。

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

您要做的是重复调用Spi_readData,直到返回的字节变为0'010' accepted

答案 1 :(得分:1)

如果你查看SD规范上的图表,繁忙的令牌在数据响应之后出现,你看到的是正常的,在你发送最后一个字节后你应该继续读取数据,直到你得到数据响应令牌,然后该卡将开始编程,您将获得繁忙的令牌,直到编程完成。当卡正在编程时,您将读取0(数据线保持低电平)。在传输完最后一个字节之后我不认为规范说明数据行的状态是什么,我总是得到0xFF,但你不应该依赖它,所以只要继续检查响应令牌,直到你得到它。 / p>