/**--------------File Info-------------------------------------------------------------------------------
** File name: sddriver.c
** Last modified Date: 2005-1-6
** Last Version: V1.0
** Descriptions: sd 卡驱动软件包: SD卡物理层 ---- SD卡SPI模式支持的命令
** Soft Packet of SD Card Driver: commands that sd card supported in spi mode
**
**------------------------------------------------------------------------------------------------------
** Created by: gan peng
** Created date: 2005-1-6
** Version: V1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
#include "sdconfig.h"
#include "sdhal.h"
#include "sdcrc.h"
#include "sdcmd.h"
#include "sddriver.h"
/********************************************************************************************************************
** 函数名称: INT8U SD_SendCmd() Name: INT8U SD_SendCmd()
** 功能描述: 向卡发送命令,并取得响应 Function: send command to the card,and get a response
** 输 入: INT8U cmd : 命令字 Input: INT8U cmd : command byte
INT8U *param : 命令参数,长度为4字节 INT8U *param : command parameter,length is 4 bytes
INT8U resptype : 响应类型 INT8U resptype: response type
INT8U *resp : 响应,长度为1-5字节 INT8U *resp : response,length is 1-5 bytes
** 输 出: 0: 正确 >0: 错误码 Output: 0: right >0: error code
********************************************************************************************************************/
INT8U SD_SendCmd(INT8U cmd, INT8U *param, INT8U resptype, INT8U *resp)
{
INT32 i,rlen;
INT8U tmp;
SPI_CS_Assert();
SPI_SendByte((cmd & 0x3F) | 0x40); /* 发送命令头和命令字 send command header and word */
for (i = 3; i >= 0; i--)
SPI_SendByte(param[i]); /* 发送参数 send parameters */
#if SD_CRC_EN
tmp = SD_GetCmdByte6((cmd & 0x3F) | 0x40, param);
SPI_SendByte(tmp);
#else
SPI_SendByte(0x95); /* CRC校验码,只用于第1个命令 CRC,only used for the first command */
#endif
rlen = 0;
switch (resptype) /* 根据不同的命令,得到不同的响应长度 */
{ /* according various command,get the various response length */
case R1:
case R1B: rlen = 1; break;
case R2: rlen = 2; break;
case R3: rlen = 5; break;
default: SPI_SendByte(0xFF);
SPI_CS_Deassert();
return SD_ERR_CMD_RESPTYPE; /* 返回命令响应类型错误 return error of command response type */
break;
}
i = 0;
do /* 等待响应,响应的开始位为0 */
{ /* Wait for a response,a response is a start bit(zero) */
tmp = SPI_RecByte();
i++;
}while (((tmp & 0x80) != 0) && (i < SD_CMD_TIMEOUT));
if (i >= SD_CMD_TIMEOUT)
{
SPI_CS_Deassert();
return SD_ERR_CMD_TIMEOUT; /* 返回命令超时 return response timeout of command */
}
for (i = rlen - 1; i >= 0; i--)
{
resp[i] = tmp;
tmp = SPI_RecByte(); /* 循环的最后发送8clock at the last recycle,clock out 8 clock */
}
SPI_CS_Deassert();
return SD_NO_ERR; /* 返回执行成功 return perform sucessfully */
}
/********************************************************************************************************************
** 函数名称: void SD_PackParam() Name: void SD_PackParam()
** 功能描述: 将32位的参数转为字节形式 Function: change 32bit parameter to bytes form
** 输 入: INT8U *parameter: 字节参数缓冲区 Input: INT8U *parameter: the buffer of bytes parameter
INT32U value : 32位参数 INT32U value : 32bit parameter
** 输 出: 无 Output: NULL
*********************************************************************************************************************/
void SD_PackParam(INT8U *parameter, INT32U value)
{
parameter[3] = (INT8U)(value >> 24);
parameter[2] = (INT8U)(value >> 16);
parameter[1] = (INT8U)(value >> 8);
parameter[0] = (INT8U)(value);
}
/********************************************************************************************************************
** 函数名称: INT8U SD_BlockCommand() Name: INT8U SD_BlockCommand()
** 功能描述: 块命令 Function: command about block operation
** 输 入: INT8U cmd : 命令字 Input: INT8U cmd : command byte
INT8U resptype : 响应类型 INT8U resptype : response type
INT32U parameter: 块操作参数 INT32U parameter: parameter of block operation
** 输 出: 0: 正确 >0: 错误码 Output: 0: right >0: error code
*********************************************************************************************************************/
INT8U SD_BlockCommand(INT8U cmd, INT8U resptype, INT32U parameter)
{
INT8U param[4],resp,ret;
parameter <<= SD_BLOCKSIZE_NBITS; /* 调整地址:左移9位 adjust address: move 9 bits left */
SD_PackParam(param, parameter); /* 将参数转化为字节形式 change the parameter to bytes form */
/* 结束数据传输失败 stop transmission operation fail */
if (resp != 0)
return SD_ERR_CMD_RESP; /* 响应错误 response is error */
return SD_NO_ERR;
}
/*
************************************************
下面为SD卡SPI命令
************************************************
*/
/********************************************************************************************************************
** 函数名称: INT8U SD_ResetSD() Name: INT8U SD_ResetSD()
** 功能描述: 复位SD卡 Function: reset sd card
** 输 入: 无 Input: NULL
** 输 出: 0: 正确 >0: 错误码 Output: 0: right >0: error code
*********************************************************************************************************************/
INT8U SD_ResetSD(void)
{
INT8U param[4] = {0,0,0,0},resp;
return (SD_SendCmd(CMD0, param, CMD0_R, &resp)); /* 复位命令 command that reset sd card */
}
/********************************************************************************************************************
** 函数名称: INT8U SD_ReadCSD() Name: INT8U SD_ReadCSD()
** 功能描述: 读SD卡的CSD寄存器 Function: read CSD register of sd card
** 输 入: INT8U csdlen : 寄存器长度(固定为16) INT8U csdlen : len of register (fixed,is 16)
INT8U *recbuf : 接收缓冲区 INT8U *recbuf : recbuffer
** 输 出: 0: 正确 >0: 错误码 Output: 0: right >0: error code
*********************************************************************************************************************/
INT8U SD_ReadCSD(INT8U csdlen, INT8U *recbuf)
{
INT8U param[4] = {0,0,0,0},resp,ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* 响应错误 response is error */
return (SD_ReadRegister(csdlen, recbuf));
}
/*******************************************************************************************************************
** 函数名称: INT8U SD_ReadCID() Name: INT8U SD_ReadCID()
** 功能描述: 读SD卡的CID寄存器 Function: read CID register of sd card
** 输 入: INT8U cidlen : 寄存器长度(固定为16) INT8U cidlen : len of register (fixed,is 16)
INT8U *recbuf : 接收缓冲区 INT8U *recbuf : recbuffer
** 输 出: 0: 正确 >0: 错�