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

📄 sd_cmd.c

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

#ifndef __SD_CONFIG_H
#include "sd_cfg.h"
#endif
/*!
*@brief
*        SD_SendCMD()
*        send the command
*
*@param  uint8 cmd:the command sended
*@param  uint8 *param:the command param addr.
*@param  uint8 resType:the response type
*@param  uint8 *response:the response data addr.
*@retval 0:  right	>0:  error code
*/
uint32 SD_SendCMD(uint8 cmd,uint8 *param,uint8 resType,uint8 *response)
{
       uint8 i,rlen;
#if SD_CRC_EN 
       uint8 CMD_CRC;
#endif                                            
       SD_SPI_SendByte((cmd&0x3F)|0x40);                          
       for(i=0;i<=3;i++){
          SD_SPI_SendByte(param[3-i]);
       };
#if SD_CRC_EN 
           CMD_CRC=SD_GetCmdByte6(cmd,param);
           SD_SPI_SendByte(CMD_CRC);
#else
           SD_SPI_SendByte(0x95);
#endif
       rlen=0;
       switch(resType){
            case R1:  rlen=1; break;
            case R2:  rlen=2; break;
            case R3:  rlen=5; break;
            default:  return CMD_PARAM_ERR; break;
       };
       if(SD_SPI_WaitForLow(200)==SPI_TIMEOUT){
          return SD_ERR_CMD_TIMEOUT;
       };
       /**< receive the response */
       for(i=0;i<=rlen-1;i++){
 
           response[rlen-1-i]=SD_SPI_RecByte();
           
       };
       SD_SPI_RecByte();
       return 0;   
}

/*!
*@brief
*        SD_PackParam(uint8 *param, uint32 value)
*        format the param,div value to 4 Byte
*
*@param  *param:param addr(8bits)
*@param  uint32 value
*@retval none
*/
void SD_PackParam(uint8 *param, uint32 value)
{
    param[3] = (uint8)(value >> 24);
    param[2] = (uint8)(value >> 16);
    param[1] = (uint8)(value >> 8);
    param[0] = (uint8)(value);
}



/*!
*@brief
*        SD_BlockCommand(uint8 cmd, uint8 resptype, uint32 parameter)
*        send the block command
*
*@param  uint8 cmd:the command sended
*@param  uint8 *param:the command param addr.
*@param  uint8 resType:the response type
*@param  uint8 *response:the response data addr.
*@retval 0:  right	>0:  error code
*/
uint8 SD_BlockCommand(uint8 cmd, uint8 resptype, uint32 parameter)
{
	uint8 param[4],resp,ret;
	SD_PackParam(param, parameter);					/*! change the parameter to bytes form */	
	ret = SD_SendCMD(cmd, param, resptype, &resp);
	if (ret != SD_NO_ERR)
	   	     return ret;							/*! stop transmission operation fail   */
	if (resp != 0)
		     return resp;		 				    /*! response is error         */
	return 0;
}

⌨️ 快捷键说明

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