打印

SD 卡 SPI 模式初始化过程,带流程图。

[复制链接]
3358|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
armrunc|  楼主 | 2012-2-3 14:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
流程图如下,该代码能识别 MMC V3, SD V1.X 和 SD V2.00及其后续版本,其中 SD V2.00及其后续版本又可分为标准容量和大容量版本。
沙发
armrunc|  楼主 | 2012-2-3 14:46 | 只看该作者
SD SPI 发送命令 API 如下,所有实现代码参考附件。

/**
  * @brief  Send 6 bytes command to the SD card.
  * @param  Cmd: The command send to SD card.
  * @param  argument: The command argument.
  * @param  response_type: the SPI command response type.
  * @param  *response: the SPI response returned.
  * @retval The SD Response.
  */
uint8_t ARC_sd_send_command(uint8_t cmd, uint32_t argument,  
                            SD_Response response_type, uint8_t *response)
{
    int32_t i = 0;
    uint8_t crc = 0x01;
    int8_t response_length = 0;
    uint8_t tmp;
    uint8_t Frame[6];
    if (cmd & 0x80)  /* Send a CMD55 prior to ACMD<n> */
    {  
        cmd &= 0x7F;
        ARC_sd_send_command(SD_CMD_APP_CMD, 0, R1, response);
        if (response[0] > 0x1)
        {
            ARC_SD_CS_HIGH();
            ARC_SPI_SendByte(SPI1, SD_DUMMY_BYTE);
            return response[0];
        }
    }
    ARC_SD_CS_LOW();
    if(cmd == SD_CMD_GO_IDLE_STATE)
        crc = 0x95;
   
    if(cmd == SD_CMD_SEND_IF_COND)
        crc = 0x87;
   
    /* All data is sent MSB first, and MSb first */
    /* cmd Format:
    cmd[7:6] : 01
    cmd[5:0] : command */
    Frame[0] = ((cmd & 0x3F) | 0x40); /*!< Construct byte 1 */
    Frame[1] = (uint8_t)(argument >> 24); /*!< Construct byte 2 */
    Frame[2] = (uint8_t)(argument >> 16); /*!< Construct byte 3 */
    Frame[3] = (uint8_t)(argument >> 8); /*!< Construct byte 4 */
    Frame[4] = (uint8_t)(argument); /*!< Construct byte 5 */
    Frame[5] = (uint8_t)(crc); /*!< Construct CRC: byte 6 */
    for (i = 0; i < 6; i++)
    {
        ARC_SPI_SendByte(SPI1, Frame[i]); /*!< Send the Cmd bytes */
    }
   
    switch (response_type)
    {
        case R1:
        case R1B:
            response_length = 1;
            break;
        case R2:
            response_length = 2;
            break;
        case R3:
        case R7:
            response_length = 5;
            break;
        default:
            break;
    }
   
    /* Wait for a response. A response can be recognized by the start bit (a zero) */
    i = 0xFF;
    do
    {
        tmp = ARC_SPI_SendByte(SPI1, SD_DUMMY_BYTE);
    }while ((tmp & 0x80) && --i);
    response[0] = tmp;
   
    /* Just bail if we never got a response */
    if (i == 0)
    {
        ARC_SD_CS_HIGH();
        ARC_SPI_SendByte(SPI1, SD_DUMMY_BYTE);
        return response[0];
    }
    if((response[0] & SD_ILLEGAL_COMMAND) == SD_ILLEGAL_COMMAND)
    {
        ARC_SD_CS_HIGH();
        ARC_SPI_SendByte(SPI1, SD_DUMMY_BYTE);
        return response[0];
    }
    i = 1;
    while(i < response_length)
    {
        tmp = ARC_SPI_SendByte(SPI1, SD_DUMMY_BYTE);
        response[i] = tmp;
        i++;
    }
   
    /* If the response is a "busy" type (R1B), then there's some
     * special handling that needs to be done. The card will
     * output a continuous stream of zeros, so the end of the BUSY
     * state is signaled by any nonzero response. The bus idles
     * high.
     */
    if (response_type == R1B)
    {
        do
        {
            tmp = ARC_SPI_SendByte(SPI1, SD_DUMMY_BYTE);;
        }while (tmp != 0xFF);
        ARC_SPI_SendByte(SPI1, SD_DUMMY_BYTE);
    }
   
    ARC_SD_CS_HIGH();
    ARC_SPI_SendByte(SPI1, SD_DUMMY_BYTE);
    return response[0];
}

ARC_SD.rar

2.12 KB

使用特权

评论回复
板凳
armrunc|  楼主 | 2012-2-3 14:47 | 只看该作者
使用说明手册。

14_SD.pdf

323.53 KB

使用特权

评论回复
地板
wu0232| | 2012-2-3 15:57 | 只看该作者
正需要这个东东,非常感谢了

使用特权

评论回复
5
lkl0305| | 2013-8-24 09:06 | 只看该作者
学习!!

使用特权

评论回复
6
gxgclg| | 2013-8-24 10:23 | 只看该作者
很有参考价值的代码,收藏了

使用特权

评论回复
7
newsoule| | 2013-9-17 11:46 | 只看该作者
顶起

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://armrunc.taobao.com http://www.armrunc.com

0

主题

13

帖子

0

粉丝