📄 sdcmd.c
字号:
** INT8U *recvbuf: received buffer
**
** Returned value: SD_NO_ERR: successful
** Others : fail,errors occur
**
**
** Used global variables: None
** Calling modules:
**
** Created by: Ming Yuan Zheng
** Created Date: 2005-1-6
**-------------------------------------------------------------------------------------------------------
** Modified by: Jing.Zhang
** Modified date: 2005-12-21
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#if SD_ReadSCR_EN
int SD_ReadSCR(INT8U scrlen, INT8U *recvbuf)
{
INT8U param[4] = {0,0,0,0},resp,ret;
ret = SD_SendCmd(CMD55, param, CMD55_R, &resp);
if (ret != SD_NO_ERR) /* command that the followed commnad is a specific application */
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* response is error */
ret = SD_SendCmd(ACMD51, param, ACMD51_R, &resp);
if (ret != SD_NO_ERR) /* command that read SD_Status register */
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* response is error */
return (SD_ReadBlockData(scrlen, recvbuf)); /* read the content of the register */
}
#endif
/*********************************************************************************************************
** Function name: SD_GetNumWRBlcoks
**
** Descriptions: Get the block numbers that written correctly
**
** input parameters: INT32U blocknum : the block numbers returned
**
** Returned value: SD_NO_ERR: successful
** Others : fail,errors occur
**
**
** Used global variables: None
** Calling modules:
**
** Created by: Ming Yuan Zheng
** Created Date: 2005-1-6
**-------------------------------------------------------------------------------------------------------
** Modified by: Jing.Zhang
** Modified date: 2005-12-21
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#if SD_WriteMultiBlock_EN
int SD_GetNumWRBlcoks(INT32U *blocknum)
{
INT8U tmp[4] = {0,0,0,0},resp,ret;
ret = SD_SendCmd(CMD55, tmp, CMD55_R, &resp);
if (ret != SD_NO_ERR) /* command that the followed commnad is a specific application */
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP;
ret = SD_SendCmd(ACMD22, tmp, ACMD22_R, &resp);
if (ret != SD_NO_ERR) /* command that read the numbers of block written correctly */
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* response is error */
ret = SD_ReadBlockData(4, tmp); /* read the numbvers of block */
if (ret != SD_NO_ERR)
return ret;
*blocknum = (tmp[0] << 24) + (tmp[1] << 16) + (tmp[2] << 8) + tmp[3];
/* 转换为32位 change to 32 bits */
return SD_NO_ERR; /* 返回执行成功 return perform sucessfully */
}
#endif
/*********************************************************************************************************
** Function name: SD_ReadRegister
**
** Descriptions: Read data from SD card
**
** input parameters: INT32U len : length
** INT8U *recvbuf: receive buffer
**
** Returned value: SD_NO_ERR: successful
** Others : fail,errors occur
**
**
** Used global variables: None
** Calling modules:
**
** Created by: Ming Yuan Zheng
** Created Date: 2005-1-6
**-------------------------------------------------------------------------------------------------------
** Modified by: Jing.Zhang
** Modified date: 2005-12-21
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int SD_ReadRegister(INT32U len, INT8U *recvbuf)
{
INT32U i = 0;
INT8U resp;
SPI_CS_Assert();
do{ /* wait for data start token */
resp = SPI_RecByte();
i++;
}while((resp == 0xFF) && (i < SD_READREG_TIMEOUT));
if (i >= SD_READREG_TIMEOUT)
{
SPI_CS_Deassert();
return SD_ERR_TIMEOUT_READ; /* timeout, return error */
}
if (resp != SD_TOK_READ_STARTBLOCK)
{ /* not receive data start token */
recvbuf[0] = resp;
i = 1; /* still len - 1 bytes will be received */
}
else
i = 0; /* received data start token,still len bytes will be received */
for (; i < len; i++)
recvbuf[i] = SPI_RecByte(); /* receive data */
i = SPI_RecByte();
i = (i << 8) + SPI_RecByte(); /* get 16-bit CRC */
#if SD_CRC_EN
if (i != SD_GetCRC16(recvbuf, len))
{ /* CRC check is error */
SPI_CS_Deassert();
SPI_SendByte(0xFF);
return SD_ERR_DATA_CRC16; /* return error of CRC16 */
}
#endif
SPI_SendByte(0xFF); /* clock out 8 clk before return */
SPI_CS_Deassert();
return SD_NO_ERR;
}
/*********************************************************************************************************
** Function name: SD_ReadBlockData
**
** Descriptions: read block data from sd card
**
** input parameters: INT32U len : length
** INT8U *recvbuf: receive buffer
**
** Returned value: SD_NO_ERR: successful
** Others : fail,errors occur
**
**
** Used global variables: None
** Calling modules:
**
** Created by: Ming Yuan Zheng
** Created Date: 2005-1-6
**-------------------------------------------------------------------------------------------------------
** Modified by: Jing.Zhang
** Modified date: 2005-12-21
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int SD_ReadBlockData(INT32U len, INT8U *recvbuf)
{
INT8U tmp;
INT32U i = 0;
SPI_CS_Assert();
do
{ /* wait for receiving data start token 0xFE */
tmp = SPI_RecByte();
i++;
}while((tmp == 0xFF) && (i < sds.timeout_read));
if (i >= sds.timeout_read)
{
SPI_CS_Deassert();
return SD_ERR_TIMEOUT_READ; /* return error timeout error code of reading */
}
if (tmp != SD_TOK_READ_STARTBLOCK) /* read start block token is error */
{
SPI_SendByte(0xFF);
SPI_CS_Deassert();
return SD_ERR_DATA_START_TOK;
}
for (i = 0; i < len; i++)
recvbuf[i] = SPI_RecByte(); /* receive data */
i = SPI_RecByte();
i = (i << 8) + SPI_RecByte(); /* get 16-bit CRC */
#if SD_CRC_EN
if (i != SD_GetCRC16(recvbuf, len))
{ /* CRC check is error */
SPI_SendByte(0xFF);
SPI_CS_Deassert();
return SD_ERR_DATA_CRC16; /* return error of CRC16 */
}
#endif
SPI_SendByte(0xFF);
SPI_CS_Deassert();
return SD_NO_ERR; /* return function perform sucessfully */
}
/*********************************************************************************************************
** Function name: SD_WriteBlockData
**
** Descriptions: write block data to sd card
**
** input parameters: INT8U bmulti : multi blocks operate 1:Y 0:N
** INT32U len : length
** INT8U *sendbuf : the buffer of send
**
** Returned value: SD_NO_ERR: successful
** Others : fail,errors occur
**
**
** Used global variables: None
** Calling modules:
**
** Created by: Ming Yuan Zheng
** Created Date: 2005-1-6
**-------------------------------------------------------------------------------------------------------
** Modified by: Jing.Zhang
** Modified date: 2005-12-21
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int SD_WriteBlockData(INT8U bmulti, INT32U len, const INT8U *sendbuf)
{
INT16U i;
INT8U tmp;
SPI_CS_Assert();
SPI_SendByte(0xFF); /* clock out 8 clk before start */
if (bmulti == 1)
SPI_SendByte(SD_TOK_WRITE_STARTBLOCK_M); /* start token of write multi blocks */
else
SPI_SendByte(SD_TOK_WRITE_STARTBLOCK); /* start token of write single block */
for (i = 0; i < len; i++)
SPI_SendByte(sendbuf[i]); /* send data */
#if SD_CRC_EN
i = SD_GetCRC16(sendbuf,len); /* calculate CRC16 */
#endif
SPI_SendByte((i >> 8) & 0xFF);
SPI_SendByte(i & 0xFF); /* send CRC16 check code */
tmp = SPI_RecByte();
if ((tmp & SD_RESP_DATA_MSK) != SD_RESP_DATA_ACCETPTED)
{
SPI_SendByte(0xFF); /* clock out 8 clk before return */
SPI_CS_Deassert();
return SD_ERR_DATA_RESP; /* data response error */
}
SPI_CS_Deassert();
if (SD_WaitBusy(SD_WAIT_WRITE) != SD_NO_ERR)
return SD_ERR_TIMEOUT_WRITE; /* write time out */
else
return SD_NO_ERR; /* write right */
}
/*********************************************************************************************************
** Function name: SD_StopMultiToken
**
** Descriptions: Send the token that stop multiple block write
**
** input parameters: None
**
** Returned value: None
**
**
** Used global variables: None
** Calling modules:
**
** Created by: Ming Yuan Zheng
** Created Date: 2005-1-6
**-------------------------------------------------------------------------------------------------------
** Modified by: Jing.Zhang
** Modified date: 2005-12-21
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void SD_StopMultiToken(void)
{
SPI_CS_Assert();
SPI_SendByte(0xFF); /* send 8 clock first */
SPI_SendByte(SD_TOK_STOP_MULTI); /* send stop transmission token */
SPI_RecByte();
SPI_CS_Deassert();
}
/*********************************************************************************************************
** Function name: SD_WaitBusy
**
** Descriptions: poll SD Card wheather it is busy
**
** input parameters: INT32U timeout: time out type
**
** Returned value: SD_NO_ERR: successful
** Others : fail,errors occur
**
**
** Used global variables: None
** Calling modules:
**
** Created by: Ming Yuan Zheng
** Created Date: 2005-1-6
**-------------------------------------------------------------------------------------------------------
** Modified by: Jing.Zhang
** Modified date: 2005-12-21
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int SD_WaitBusy(INT8U waittype)
{
INT32U timeout, i = 0;
INT8U tmp;
if (waittype == SD_WAIT_WRITE)
timeout = sds.timeout_write;
else
timeout = sds.timeout_erase;
SPI_CS_Assert();
do
{ /* 等待忙结束 wait for being busy end */
tmp = SPI_RecByte();
i++;
}while ((tmp != 0xFF) && (i < timeout)); /* 忙时收到的值为0xFF always receive 0xFF when card is busy */
SPI_CS_Deassert(); /* 有错 */
if(i < timeout)
return SD_NO_ERR; /* 返回0,表示没超时 return 0 indicate that operation is not time out */
else
return SD_ERR_TIMEOUT_WAIT; /* 返回错误码,表示超时 return error code indicate that operation is time out */
}
/*********************************************************************************************************
** Function name: SD_SPIDelay
**
** Descriptions: SPI Bus delay
**
** input parameters: INT8U value : delay value,do not beyond 255
**
** Returned value: None
**
**
** Used global variables: None
** Calling modules:
**
** Created by: Ming Yuan Zheng
** Created Date: 2005-1-6
**-------------------------------------------------------------------------------------------------------
** Modified by: Jing.Zhang
** Modified date: 2005-12-21
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void SD_SPIDelay(INT8U value)
{
INT8U i;
for (i = 0; i < value; i++)
SPI_SendByte(0xFF); /* 发送0xFF clock out 0xFF */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -