?? sdcmd.c
字號(hào):
/****************************************Copyright (c)**************************************************
** Guangzhou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: sdcmd.c
** Last modified Date: 2005-3-11
** Last Version: V2.0
** Descriptions: SD/MMC 讀寫模塊: 物理層 ---- SD/MMC卡SPI模式支持的命令
** Soft Packet of SD/MMC Card: commands that SD/MMC card supported in SPI mode
**
**------------------------------------------------------------------------------------------------------
** Created by: Ming Yuan Zheng
** Created date: 2005-1-6
** Version: V1.0
** Descriptions: SD/MMC 讀寫模塊: 物理層 ---- SD卡SPI模式支持的命令
**
**------------------------------------------------------------------------------------------------------
** Modified by: Ming Yuan Zheng
** Modified date: 2005-3-11
** Version: V2.0
** Descriptions: 增加了對(duì)MMC卡的支持,并使用本模塊既可以運(yùn)行于前后臺(tái)系統(tǒng),也可運(yùn)行于uCOS-II中.
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
#include "sdhal.h"
#include "sdcrc.h"
#include "sdcmd.h"
#include "Sddriver.h"
#include "LPC2210Uart.h"
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_SendCmd() Name: uint8 SD_SendCmd()
** 功能描述: 向卡發(fā)送命令,并取得響應(yīng) Function: send command to the card,and get a response
** 輸 入: uint8 cmd : 命令字 Input: uint8 cmd : command byte
uint8 *param : 命令參數(shù),長(zhǎng)度為4字節(jié) uint8 *param : command parameter,length is 4 bytes
uint8 resptype : 響應(yīng)類型 uint8 resptype: response type
uint8 *resp : 響應(yīng),長(zhǎng)度為1-5字節(jié) uint8 *resp : response,length is 1-5 bytes
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
********************************************************************************************************************/
uint8 SD_SendCmd(uint8 cmd, uint8 *param, uint8 resptype, uint8 *resp)
{
int32 i,rlen;
uint8 tmp;
SPI_CS_Assert();
SPI_SendByte((cmd & 0x3F) | 0x40); /* 發(fā)送命令頭和命令字 send command header and word */
for (i = 3; i >= 0; i--)
SPI_SendByte(param[i]); /* 發(fā)送參數(shù) send parameters */
#if SD_CRC_EN
tmp = SD_GetCmdByte6((cmd & 0x3F) | 0x40, param);
SPI_SendByte(tmp);
#else
SPI_SendByte(0x95); /* CRC校驗(yàn)碼,只用于第1個(gè)命令 CRC,only used for the first command */
#endif
rlen = 0;
switch (resptype) /* 根據(jù)不同的命令,得到不同的響應(yīng)長(zhǎng)度 */
{ /* 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; /* 返回命令響應(yīng)類型錯(cuò)誤 return error of command response type */
break;
}
i = 0;
do /* 等待響應(yīng),響應(yīng)的開始位為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; /* 返回命令超時(shí) return response timeout of command */
}
for (i = rlen - 1; i >= 0; i--)
{
resp[i] = tmp;
tmp = SPI_RecByte(); /* 循環(huán)的最后發(fā)送8clock at the last recycle,clock out 8 clock */
}
SPI_CS_Deassert();
return SD_NO_ERR; /* 返回執(zhí)行成功 return perform sucessfully */
}
/********************************************************************************************************************
** 函數(shù)名稱: void SD_PackParam() Name: void SD_PackParam()
** 功能描述: 將32位的參數(shù)轉(zhuǎn)為字節(jié)形式 Function: change 32bit parameter to bytes form
** 輸 入: uint8 *parameter: 字節(jié)參數(shù)緩沖區(qū) Input: uint8 *parameter: the buffer of bytes parameter
uint32 value : 32位參數(shù) uint32 value : 32bit parameter
** 輸 出: 無 Output: NULL
*********************************************************************************************************************/
void SD_PackParam(uint8 *parameter, uint32 value)
{
parameter[3] = (uint8)(value >> 24);
parameter[2] = (uint8)(value >> 16);
parameter[1] = (uint8)(value >> 8);
parameter[0] = (uint8)(value);
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_BlockCommand() Name: uint8 SD_BlockCommand()
** 功能描述: 塊命令 Function: command about block operation
** 輸 入: uint8 cmd : 命令字 Input: uint8 cmd : command byte
uint8 resptype : 響應(yīng)類型 uint8 resptype : response type
uint32 parameter: 塊操作參數(shù) uint32 parameter: parameter of block operation
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_BlockCommand(uint8 cmd, uint8 resptype, uint32 parameter)
{
uint8 param[4],resp,ret;
parameter <<= SD_BLOCKSIZE_NBITS; /* 調(diào)整地址:左移9位 adjust address: move 9 bits left */
SD_PackParam(param, parameter); /* 將參數(shù)轉(zhuǎn)化為字節(jié)形式 change the parameter to bytes form */
ret = SD_SendCmd(cmd, param, resptype, &resp);
if (ret != SD_NO_ERR)
return ret; /* 結(jié)束數(shù)據(jù)傳輸失敗 stop transmission operation fail */
if (resp != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
return SD_NO_ERR;
}
/*
************************************************
下面為SD卡SPI命令
************************************************
*/
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ResetSD() Name: uint8 SD_ResetSD()
** 功能描述: 復(fù)位SD/MMC卡 Function: reset SD/MMC card
** 輸 入: 無 Input: NULL
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_ResetSD(void)
{
uint8 param[4] = {0,0,0,0},resp;
return (SD_SendCmd(CMD0, param, CMD0_R, &resp)); /* 復(fù)位命令 command that reset card */
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ReadCSD() Name: uint8 SD_ReadCSD()
** 功能描述: 讀SD/MMC卡的CSD寄存器 Function: read CSD register of SD/MMC card
** 輸 入: uint8 csdlen : 寄存器長(zhǎng)度(固定為16) uint8 csdlen : len of register (fixed,is 16)
uint8 *recbuf : 接收緩沖區(qū) uint8 *recbuf : recbuffer
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_ReadCSD(uint8 csdlen, uint8 *recbuf)
{
uint8 param[4] = {0,0,0,0},resp,ret;
ret = SD_SendCmd(CMD9, param, CMD9_R, &resp); /* 讀CSD寄存器命令 command that read CSD register */
if (ret != SD_NO_ERR)
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
return (SD_ReadRegister(csdlen, recbuf));
}
/*******************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ReadCID() Name: uint8 SD_ReadCID()
** 功能描述: 讀SD卡的CID寄存器 Function: read CID register of sd card
** 輸 入: uint8 cidlen : 寄存器長(zhǎng)度(固定為16) uint8 cidlen : len of register (fixed,is 16)
uint8 *recbuf : 接收緩沖區(qū) uint8 *recbuf : recbuffer
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
********************************************************************************************************************/
#if SD_ReadCID_EN
uint8 SD_ReadCID(uint8 cidlen, uint8 *recbuf)
{
uint8 param[4] = {0,0,0,0},resp,ret;
ret = SD_SendCmd(CMD10, param, CMD10_R, &resp); /* 讀CID寄存器命令 command that read CID register */
if ( ret != SD_NO_ERR)
return ret;
if (resp != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
return (SD_ReadRegister(cidlen, recbuf));
}
#endif
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_StopTransmission() Name: uint8 SD_StopTransmission()
** 功能描述: 停止數(shù)據(jù)傳輸 Function: stop data transmission
** 輸 入: 無 Input: NULL
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_StopTransmission(void)
{
uint8 param[4] = {0,0,0,0},resp;
return (SD_SendCmd(CMD12, param, CMD12_R, &resp)); /* 結(jié)束數(shù)據(jù)傳輸命令失敗 stop transmission command fail */
}
/*********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ReadCard_Status() Name: uint8 SD_ReadCard_Status()
** 功能描述: 讀SD/MMC卡的 Card Status 寄存器 Function: read Card Status register of SD/MMC card
** 輸 入: uint8 len: 寄存器長(zhǎng)度(固定為2) uint8 len: len of register (fixed,is 2)
uint8 *recbuf : 接收緩沖區(qū) uint8 *recbuf : recbuffer
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
**********************************************************************************************************************/
uint8 SD_ReadCard_Status(uint8 len, uint8 *buffer)
{
uint8 param[4] = {0,0,0,0};
return (SD_SendCmd(CMD13, param, CMD13_R, buffer)); /* 讀 Card Status 寄存器 */
/* read register of Card Status */
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_SetBlockLen() Name: uint8 SD_SetBlockLen()
** 功能描述: 設(shè)置一個(gè)塊的長(zhǎng)度 Function: set a block len of card
** 輸 入: uint32 length : 塊的長(zhǎng)度值 Input: uint32 length : the length of a block
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_SetBlockLen(uint32 length)
{
uint8 param[4],resp,ret;
SD_PackParam(param, length); /* 將參數(shù)轉(zhuǎn)化為字節(jié)形式 change the parameter to bytes form */
ret = SD_SendCmd(CMD16, param, CMD16_R, &resp);
if (ret != SD_NO_ERR)
return ret; /* 設(shè)置塊的長(zhǎng)度為length失敗 set the length of block to length fail */
if (resp != 0)
return SD_ERR_CMD_RESP; /* 響應(yīng)錯(cuò)誤 response is error */
return SD_NO_ERR; /* 返回執(zhí)行成功 return perform sucessfully */
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ReadSingleBlock() Name: uint8 SD_ReadSingleBlock()
** 功能描述: 讀單塊命令 Function: read single block command
** 輸 入: uint32 blockaddr: 塊地址 Input: uint32 blockaddr: block address
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_ReadSingleBlock(uint32 blockaddr)
{
return (SD_BlockCommand(CMD17, CMD17_R, blockaddr)); /* 讀單塊命令 command that read single block */
}
/********************************************************************************************************************
** 函數(shù)名稱: uint8 SD_ReadMultipleBlock() Name: uint8 SD_ReadMultipleBlock()
** 功能描述: 讀多塊命令 Function: read multiple block command
** 輸 入: uint32 blockaddr: 塊地址 Input: uint32 blockaddr: block address
** 輸 出: 0: 正確 >0: 錯(cuò)誤碼 Output: 0: right >0: error code
*********************************************************************************************************************/
uint8 SD_ReadMultipleBlock(uint32 blockaddr)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -