?? sdcmd.c
字號(hào):
{
return (SD_BlockCommand(CMD18, CMD18_R, blockaddr)); /* 讀多塊命令 command that read multiple block */
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_WriteSingleBlock() Name: uint8 SD_WriteSingleBlock()
** 功能描述: 寫單塊命令 Function: write single block command
** 輸 入: uint32 blockaddr: block address Input: uint32 blockaddr: block address
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_WriteSingleBlock(uint32 blockaddr)
{
return (SD_BlockCommand(CMD24, CMD24_R, blockaddr)); /* 寫單塊命令 command that write single block */
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_WriteMultipleBlock() Name: uint8 SD_WriteMultipleBlock()
** 功能描述: 寫多塊命令 Function: write multiple block command
** 輸 入: uint32 blockaddr: 塊地址 Input: uint32 blockaddr: block address
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_WriteMultipleBlock(uint32 blockaddr)
{
return (SD_BlockCommand(CMD25, CMD25_R, blockaddr)); /* 寫多塊命令 command that write multiple block */
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ProgramCSD() Name: uint8 SD_ProgramCSD()
** 功能描述: 寫CSD寄存器 Function: write CSD register
** 輸 入: uint8 *buff : CSD寄存器內(nèi)容 Input: uint8 *buff : the content of CSD register
uint8 len : CSD寄存器長(zhǎng)度 uint8 len : the length of CSD register
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
********************************************************************************************************************/
#if SD_ProgramCSD_EN
uint8 SD_ProgramCSD(uint8 len, uint8 *buff)
{
uint8 param[4] = {0,0,0,0},resp,ret;
if (len != 16) return SD_ERR_USER_PARAM;
ret = SD_SendCmd(CMD27, param, CMD27_R, &resp); /* 發(fā)送寫CSD寄存器命令 send command that write CSD */
if (ret != SD_NO_ERR)
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP;
buff[15] = (SD_GetCRC7(buff, 15) << 1) + 0x01; /* 計(jì)算CSD中的crc 位域 calculate crc field in CSD */
return(SD_WriteBlockData(0, 16, buff));
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_GetCRC7() Name: uint8 SD_GetCRC7()
** 功能描述: 計(jì)算CRC7 Function: calculate crc7
** 輸 入: uint8 *pSource: 數(shù)據(jù) Input: uint8 *pSource: data
uint16 len : 數(shù)據(jù)長(zhǎng)度 uint16 len : data length
** 輸 出: CRC7碼 Output: CRC7 code
*********************************************************************************************************************/
uint8 SD_GetCRC7(uint8 *pSource, uint16 len)
{
uint8 i = 0, j;
uint8 reg = 0;
do
{
for (j = 0; j < 8; j++)
{
reg <<= 1;
reg ^= ((((pSource[i] << j) ^ reg) & 0x80) ? 0x9 : 0);
}
i++;
}while(i < len);
return reg;
}
#endif
#if SD_EraseBlock_EN
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_EraseStartBlock() Name: uint8 SD_EraseStartBlock()
** 功能描述: 設(shè)置塊擦除起始地址 Function: select the start block address of erasing operation
** 輸 入: uint32 startblock: 塊地址 Input: uint32 startblock : block address
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_EraseStartBlock(uint32 startblock)
{
if (sds.card_type == CARDTYPE_SD)
return (SD_BlockCommand(CMD32, CMD32_R, startblock)); /* 發(fā)送擦除起始?jí)K地址 send the start block address of erasing operation */
else
return (SD_BlockCommand(CMD35, CMD35_R, startblock)); /* 發(fā)送擦除起始?jí)K地址 send the start block address of erasing operation */
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_EraseEndBlock() Name: uint8 SD_EraseEndBlock()
** 功能描述: 設(shè)置塊擦除終止地址 Function: select the end block address of erasing operation
** 輸 入: uint32 endblock: 塊地址 Input: uint32 Length : block address
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_EraseEndBlock(uint32 endblock)
{
if (sds.card_type == CARDTYPE_SD)
return (SD_BlockCommand(CMD33, CMD33_R, endblock)); /* 發(fā)送擦除終止塊地址 send the end block address of erasing operation */
else
return (SD_BlockCommand(CMD36, CMD36_R, endblock)); /* 發(fā)送擦除終止塊地址 send the end block address of erasing operation */
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_EraseSelectedBlock() Name: uint8 SD_EraseSelectedBlock()
** 功能描述: 擦除已選中的塊 Function: erase block selected
** 輸 入: 無(wú) Input: NULL
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_EraseSelectedBlock(void)
{
uint8 param[4],resp,tmp;
SD_PackParam(param, 0);
tmp = SD_SendCmd(CMD38, param, CMD38_R, &resp); /* 擦除所選擇的塊 erase blocks selected */
if (tmp != SD_NO_ERR)
return tmp;
if (SD_WaitBusy(SD_WAIT_ERASE) != SD_NO_ERR) /* 等待擦除完成 wait for finishing erasing */
return SD_ERR_TIMEOUT_ERASE;
else
return SD_NO_ERR;
}
#endif
/*********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ReadOCR() Name: uint8 SD_ReadOCR()
** 功能描述: 讀操作條件寄存器OCR Function: read OCR register of card
** 輸 入: uint8 ocrlen : 寄存器長(zhǎng)度(固定為4) Input: uint8 ocrlen : len of register (fixed,is 4)
uint8 *recbuf : 接收緩沖區(qū) uint8 *recbuf : recbuffer
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
**********************************************************************************************************************/
uint8 SD_ReadOCR(uint8 ocrlen, uint8 *recbuf)
{
uint8 param[4] = {0,0,0,0},resp[5],tmp;
tmp = SD_SendCmd(CMD58, param, CMD58_R, resp); /* 讀 OCR 寄存器命令 */
if (tmp != SD_NO_ERR) /* read OCR register command */
return tmp;
if (resp[0] != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
for (tmp = 0; tmp < 4; tmp++)
recbuf[tmp] = resp[tmp + 1]; /* 復(fù)制OCR寄存器內(nèi)容到接收緩沖區(qū) */
return SD_NO_ERR;
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_EnableCRC() Name: uint8 SD_EnableCRC()
** 功能描述: 使能SD卡的CRC校驗(yàn)功能 Function: enable crc check of SD Card
** 輸 入: uint8 bEnable : 1:使能 0:禁止 Input: uint8 bEnable : 1:enable 0: disable
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
#if SD_CRC_EN
uint8 SD_EnableCRC(uint8 bEnable)
{
uint8 param[4],resp,ret;
if (bEnable == 1)
param[0] = 1; /* 使能crc enable crc */
else
param[1] = 0; /* 禁止crc disalbe crc */
ret = SD_SendCmd(CMD59, param, CMD59_R, &resp); /* "使能/禁止CRC"命令 enable/disable crc command */
if (ret != SD_NO_ERR)
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
return SD_NO_ERR;
}
#endif
/*********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ReadSD_Status() Name: uint8 SD_ReadSD_Status()
** 功能描述: 讀SD卡的 SD_Status 寄存器 Function: read SD_Status register of sd card
** 輸 入: uint8 sdslen : 寄存器長(zhǎng)度(固定為64) Input: uint8 sdslen: len of register (fixed,is 64)
uint8 *recbuf : 接收緩沖區(qū) uint8 *recbuf: recbuffer
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
** 注 意: 只有SD卡才有SD Status 寄存器 Note: only SD card have SD Status Register
**********************************************************************************************************************/
#if SD_ReadSD_Status_EN
uint8 SD_ReadSD_Status(uint8 sdslen, uint8 *recbuf)
{
uint8 param[4] = {0,0,0,0},resp[2],ret;
ret = SD_SendCmd(CMD55, param, CMD55_R, resp); /* 后續(xù)命令為一個(gè)應(yīng)用命令 */
if (ret != SD_NO_ERR)
return ret; /* command that the followed commnad is a specific application */
if (resp[0] != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
ret = SD_SendCmd(ACMD13, param, ACMD13_R, resp); /* 讀 SD_Status 命令 */
if (ret != SD_NO_ERR)
return ret; /* command that read SD_Status register */
if ((resp[0] != 0) || (resp[1] != 0))
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
return (SD_ReadBlockData(sdslen, recbuf)); /* 讀出寄存器內(nèi)容 read the content of the register */
}
#endif
/*******************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ReadSCR() Name: uint8 SD_ReadSCR()
** 功能描述: 讀SD卡的 SCR 寄存器 Function: read SCR register of SD card
** 輸 入: uint8 scrlen : 寄存器長(zhǎng)度(固定為8) Input: uint8 scrlen : len of register (fixed,is 8)
uint8 *recbuf : 接收緩沖區(qū) uint8 *recbuf : recieve buffer
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
** 備 注: MMC卡沒(méi)有該寄存器 Note: MMC Card have not this register
********************************************************************************************************************/
#if SD_ReadSCR_EN
uint8 SD_ReadSCR(uint8 scrlen, uint8 *recbuf)
{
uint8 param[4] = {0,0,0,0},resp,ret;
ret = SD_SendCmd(CMD55, param, CMD55_R, &resp); /* 后續(xù)命令為一個(gè)應(yīng)用命令 */
if (ret != SD_NO_ERR) /* command that the followed commnad is a specific application */
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
ret = SD_SendCmd(ACMD51, param, ACMD51_R, &resp); /* 發(fā)送讀 SD Status 命令*/
if (ret != SD_NO_ERR) /* command that read SD Status register */
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
return (SD_ReadBlockData(scrlen, recbuf)); /* 讀出寄存器內(nèi)容 read the content of the register */
}
#endif
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_GetNumWRBlcoks() Name: uint8 SD_GetNumWRBlcoks()
** 功能描述: 得到正確寫入的塊數(shù) Function: get the block numbers that written correctly
** 輸 入: uint32 *blocknum: 返回的塊數(shù) Input: uint32 blocknum : the block numbers returned
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
** 注 意: MMC卡沒(méi)有該命令 Note: MMC Card have no this command
*********************************************************************************************************************/
#if SD_WriteMultiBlock_EN
uint8 SD_GetNumWRBlcoks(uint32 *blocknum)
{
uint8 tmp[4] = {0,0,0,0},resp,ret;
ret = SD_SendCmd(CMD55, tmp, CMD55_R, &resp); /* 后續(xù)命令為一個(gè)應(yīng)用命令 */
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); /* 讀取正確寫入的塊數(shù)命令 */
if (ret != SD_NO_ERR) /* command that read the numbers of block written correctly */
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
ret = SD_ReadBlockData(4, tmp); /* 讀塊數(shù) read the numbvers of block */
if (ret != SD_NO_ERR)
return ret;
*blocknum = (tmp[0] << 24) + (tmp[1] << 16) + (tmp[2] << 8) + tmp[3];
/* 轉(zhuǎn)換為32位 change to 32 bits */
return SD_NO_ERR; /* 返回執(zhí)行成功 return perform sucessfully */
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -