⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sd_api.c

📁 收集了单片机对SD卡读写源码
💻 C
字号:
 /*!
 *@file sd_api.h
 *
 * 
 *
 *@version v1.0.0
 *@date    2007
 *@author  jacky291@126.com
 */
#include "sd_api.h"

#ifndef  __SD_CONFIG_H
#include "sd_cfg.h"
#endif
//fndef  SD_CRC_EN
#include "sd_crc.h"
//#endif
#ifndef __SD_CMD_H
#include "sd_cmd.h"
#endif
#ifndef __SD_DATA_H
#include "sd_dat.h"
#endif
/*!
*@brief
*        SD_ResetSD()
*        reset sd card
*
*@param  none
*@retval 0:  right	>0:  error code
*/
uint8 SD_ResetSD(void)		
{
	uint8 param[4] = {0,0,0,0},resp;          
	uint8 ret;                                                 /* command that reset sd card */
    ret=SD_SendCMD(CMD0, param,R1, &resp);
    if(ret!=0x00){
        return 0xFF;
     }else{
        return 0;
    };        
}

/*!
*@brief
*        SD_EnableCRC()
*        enable crc check of SD Card 
*
*@param  bEnable : 1:enable  0: disable
*@retval 0:  right	   >0:  error code
*/
uint8 SD_EnableCRC(uint8 bEnable)
{
	uint8 param[4],resp,ret;
	if (bEnable == 1)
		param[0] = 1;										/* enable crc */
	else
		param[1] = 0; 										/* crc disalbe crc */
	ret = SD_SendCMD(CMD59, param, R1, &resp);			    /* enable/disable crc command */
	if (ret != SD_NO_ERR)
        return ret;							 
    if (resp != 0)    
       	return SD_ERR_CMD_RESP;								/* response is error */		
	return SD_NO_ERR;
}

/*!
*@brief
*        SD_Read_SD_Status()
*        read SD_Status register of sd card 
*
*@param  sdslen: len of register (fixed,is 64)
*@param  recbuf: recbuffer	
*@retval 0:  right	   >0:  error code
*/
uint8 SD_ReadSD_Status(uint8 sdslen, uint8 *recbuf)
{
    uint8 param[4] = {0,0,0,0},resp[2],ret;
    ret = SD_SendCMD(CMD55, param, R1, resp);			
    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;								/* response is error */
    ret = SD_SendCMD(CMD13, param, R2, resp);		
    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;								/* response is error */ 
	return (SD_ReadBlockData(sdslen, recbuf));				/* read the content of the register */
}

/*!
*@brief
*        SD_ReadCard_Status()
*        read Card Status register of sd card 
*
*@param  len:len of register (fixed,is 2)
*@param  *recbuf : recbuffer
*@retval 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, R2, buffer));          /* read register of Card Status */								 	 			
}

/*!
*@brief
*        SD_ReadCSD()
*        command that read CSD register
*
*@param  none
*@retval error: SD_ERR_CMD_RESP,or return the read value.
*/
uint8 SD_ReadCSD(uint8 csdlen, uint8 *recbuf)
{
    uint8 param[4] = {0,0,0,0},resp,ret;
    ret = SD_SendCMD(CMD9, param, R1, &resp);		     /* command that read CSD register */
    if (ret != SD_NO_ERR) 									
        return ret;									
    if (resp != 0)
        return SD_ERR_CMD_RESP;				             /* response is error */
	return (SD_ReadRegister(csdlen, recbuf));
}

/*!
*@brief
*        SD_ReadCID()
*        read CID register of sd card
*
*@param  cidlen  : len of register (fixed,is 16)
*@param  recbuf : recbuffer	
*@retval 0:  right		>0:  error code
*/
uint8 SD_ReadCID(uint8 len, uint8 *recbuf)
{
    uint8 param[4] = {0,0,0,0},resp,ret;
    ret = SD_SendCMD(CMD10, param, R1, &resp);		    /* command that read CID register */
    if ( ret != SD_NO_ERR)
   		return ret;			  									
    if (resp != 0)
        return SD_ERR_CMD_RESP;						    /*  response is error */
  	return (SD_ReadRegister(len, recbuf));
}

/*!
*@brief
*        SD_ProgramCSD()
*        write CSD register
*
*@param  *buff: the content of CSD register	
*@param  len  : the length of CSD register
*@retval 0:  right		>0:  error code
*/
uint8 SD_ProgramCSD(uint8 len, uint8 *buff)
{
	uint8 param[4] = {0,0,0,0},resp,ret;
	if (len != 16) return 2;
	ret = SD_SendCMD(CMD27, param, R1, &resp); 	         /* 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;       /* calculate crc field in CSD */
	return(SD_WriteBlockData(0, 16, buff));
}

/*!
*@brief
*        SD_ReadOCR()
*        read OCR register of sd card
*
*@param  ocrlen  : len of register (fixed,is 4)
*@param  *recbuf : recbuffer	
*@retval 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,R3, resp);		
    if (tmp != SD_NO_ERR)								/* read OCR register command */
    	return tmp;		 																		
    for (tmp = 0; tmp < 4; tmp++)
    	recbuf[tmp] = resp[tmp + 1];			
    return SD_NO_ERR;
}




/*!
*@brief
*        SD_ReadSCR()
*        read SCR register of sd card 
*
*@param  scrlen		 : len of register (fixed,is 8)
*@param  *recbuf		 : recbuffer	
*@retval 0:  right	   >0:  error code
*/
uint8 SD_ReadSCR(uint8 scrlen, uint8 *recbuf)
{
    uint8 param[4] = {0,0,0,0},resp,ret;
    ret = SD_SendCMD(CMD55, param, R1, &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(CMD51, param,R1, &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, recbuf));	 	        /* read the content of the register */
}




/*!
*@brief
*  SD_StopTransmission()
*  stop data transmission 
*
*@param  none
*@retval 0:  right		>0:  error code
*/
uint8 SD_StopTransmission(void)
{
	uint8 param[4] = {0,0,0,0},resp;
 	return (SD_SendCMD(CMD12, param, R1b, &resp));	         /*stop transmission command fail */
}


/*!
*@brief
*  Sd_SetBlockLen()
*  set a block len of sd card
*
*@param  length	: the length of a block
*@retval 0:  right		>0:  error code
*/
uint8 SD_SetBlockLen(uint32 length)
{
    
	uint8 param[4],resp,ret;
    SD_PackParam(param, length);					        /*change the parameter to bytes form */						
    ret = SD_SendCMD(CMD16, param, R1, &resp);
    if (ret != SD_NO_ERR)
 		return ret;									        /* set the length of block to length fail */
	if (resp != 0)
    	return SD_ERR_CMD_RESP;			   			        /*response is error */
    return SD_NO_ERR; 								        /*return perform sucessfully */			
}




/*!
*@brief
*        SD_ReadSingleBlock()
*        read single block command
*
*@param  blockaddr: block address
*@retval 0:  right		>0:  error code
*/
uint8 SD_ReadSingleBlock(uint32 blockaddr)
{
	return (SD_BlockCommand(CMD17, R1, blockaddr));         /* command that read single block */
}

/*!
*@brief
*        SD_ReadMultipleBlock()
*        read multiple block command 
*
*@param  blockaddr: block address
*@retval 0:  right		>0:  error code
*/
uint8 SD_ReadMultipleBlock(uint32 blockaddr)
{
	return (SD_BlockCommand(CMD18, R1, blockaddr));         /* command that read multiple block */
}

/*!
*@brief
*        SD_WriteSingleBlock()
*        write single block command
*
*@param  blockaddr: block address
*@retval 0:  right		>0:  error code
*/
uint8 SD_WriteSingleBlock(uint32 blockaddr)
{
	return (SD_BlockCommand(CMD24, R1, blockaddr));         /* command that write single block */
}

/*!
*@brief
*        SD_WriteMultipleBlock()
*        write multiple block command
*
*@param  blockaddr: block address
*@retval 0:  right		>0:  error code
*/
uint8 SD_WriteMultipleBlock(uint32 blockaddr)
{
	return (SD_BlockCommand(CMD25, R1, blockaddr));         /* command that write multiple block */
}

/*!
*@brief
*        SD_GetNumWRBlcoks()
*        get the block numbers that written correctly
*
*@param  blocknum	: the block numbers returned
*@retval 0:  right	   >0:  error code
*/
uint8 SD_GetNumWRBlcoks(uint32 *blocknum)
{
    uint8 tmp[4] = {0,0,0,0},resp,ret;
    ret = SD_SendCMD(CMD55, tmp, R1, &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(CMD22, tmp, R1, &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];	
    															/**< change to 32 bits */
	return SD_NO_ERR;    										/**< return perform sucessfully */		
}

/*!
*@brief
*        SD_EraseStartBlock()
*        select the start block address of erasing operation 
*
*@param  *pSource: data
*@param  startblock	: block address
*@retval 0:  right	   >0:  error code
*/
uint8 SD_EraseStartBlock(uint32 startblock)
{
	return (SD_BlockCommand(CMD32, R1, startblock));	        /*send the start block address of erasing operation */
}

/*!
*@brief
*        SD_EraseEndBlock()
*        select the end block address of erasing operation  
*
*@param  Length	: block address
*@retval 0:  right	   >0:  error code
*/
uint8 SD_EraseEndBlock(uint32 endblock)
{
	return (SD_BlockCommand(CMD33, R1, endblock));              /* send the end block address of erasing operation */
}

/*!
*@brief
*        SD_EraseSelectedBlock()
*        erase block selected
*
*@param  none
*@retval 0:  right	   >0:  error code
*/
uint8 SD_EraseSelectedBlock(void)
{
	uint8 param[4],resp,tmp;
	SD_PackParam(param, 0);
	tmp = SD_SendCMD(CMD38, param, R1b, &resp);	 	             /*  erase blocks selected */
	if (tmp != 0)
		return tmp;							 	
	SD_WaitBusy(SD_WAIT_ERASE_TIME);                     	     /* wait for finishing erasing */
    return 0;									
}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -