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

📄 sdcmd.c

📁 SD卡的SPI驱动程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzhou ZLG-MCU Development Co.,LTD.
**                                      graduate school
**                                 http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name:			sdcmd.c
** Last modified Date:	2005-1-6
** Last Version:		V1.0
** Descriptions:		Soft Packet of SD Card Driver: commands that sd card supported in spi mode(header file)
**
**------------------------------------------------------------------------------------------------------
** Created by:			Ming Yuan Zheng
** Created date:		2005-1-6
** Version:				V1.0
** Descriptions:		The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:			Jing.Zhang
** Modified date:		2005-12-19
** Version:				
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Modified by: 
** Modified date:
** Version:	
** Descriptions: 
**
********************************************************************************************************/
#define __SDCMD_GLOBAL__
#include "sdconfig.h"
#include "sdhal.h"
#include "sdcrc.h"
#include "sdcmd.h"
#include "sddriver.h"
#include "zlg_avalon_spi.h"
#include "stdio.h"

/*********************************************************************************************************
** Function name:    SD_SendCmd
**
** Descriptions:     send command to the card,and get a response
**                   
** input parameters:   INT8U cmd     : command byte
**                     INT8U *param	 : command parameter,length is 4 bytes
**                     INT8U resptype: response type
**                     INT8U *resp	 : response,length is 1-5 bytes
**            
** 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_SendCmd(INT8U cmd, INT8U *param, INT8U resptype, INT8U *resp)
{
	INT32S i,rlen;
	INT8U tmp;
    
	SPI_CS_Assert();
   
    SPI_SendByte((cmd & 0x3F) | 0x40);				 /* send command header and word */
    
    for (i = 3; i >= 0; i--)
        SPI_SendByte(param[i]);						 /* send parameters */

#if SD_CRC_EN
	tmp = SD_GetCmdByte6((cmd & 0x3F) | 0x40, param);
	SPI_SendByte(tmp);
#else
    SPI_SendByte(0x95);							 	 /* CRC,only used for the first command */
#endif 
    
    rlen = 0;
    switch (resptype)					/* Get the length of response according to the command */			 
    {												 
  		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; /*  return error of command response type */
    		      break;
    }
    
    i = 0;				
    do 									     /* Wait for a response,a response with a start bit(zero) */ 	 
    {												 
        tmp = SPI_RecByte();
        //printf("tmp is %d \n",tmp);
        i++;
    }while (((tmp & 0x80) != 0) && (i < SD_CMD_TIMEOUT));
    
    if (i >= SD_CMD_TIMEOUT)
    {												 
        SPI_CS_Deassert();
        return SD_ERR_CMD_TIMEOUT;			 /* return response timeout of command */
    }
    
    for (i = rlen - 1; i >= 0; i--)
    {
        resp[i] = tmp;
        tmp = SPI_RecByte();			 	 /* Send the Nec */
    }
      
    SPI_CS_Deassert();
    return SD_NO_ERR;						/* return success */
}

/*********************************************************************************************************
** Function name:    SD_PackParam
**
** Descriptions:     Change 32bit parameter to bytes form
**                   
** input parameters:   INT8U *parameter: the buffer of bytes parameter
**                     INT32U value    : 32bit parameter
**            
** 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_PackParam(INT8U *parameter, INT32U value)
{
    parameter[3] = (INT8U)(value >> 24);
    parameter[2] = (INT8U)(value >> 16);
    parameter[1] = (INT8U)(value >> 8);
    parameter[0] = (INT8U)(value);
}

/*********************************************************************************************************
** Function name:    SD_BlockCommand
**
** Descriptions:     Command about block operation
**                   
** input parameters:   INT8U cmd       : command byte
**                     INT8U resptype  : response type
**                     INT32U parameter: parameter of block operation
**            
** 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_BlockCommand(INT8U cmd, INT8U resptype, INT32U parameter)
{
	INT8U param[4],resp,ret;
	
	parameter <<= SD_BLOCKSIZE_NBITS;					 /* adjust address: shift 9 bits left */

	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 SD_ERR_CMD_RESP;		 				 /* response is error */
		 
	return SD_NO_ERR;
}

/*********************************************************************************************************
** Function name:    SD_ResetSD
**
** Descriptions:     Reset SD card
**                   
** input parameters:   None
**            
** 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_ResetSD(void)
{
	INT8U param[4] = {0,0,0,0},resp;
	
    return (SD_SendCmd(CMD0, param, CMD0_R, &resp));	/* command that reset SD card */
}

/*********************************************************************************************************
** Function name:    SD_ReadCSD
**
** Descriptions:     read CSD register of SD card
**                   
** input parameters:   INT8U csdlen  : len of register (fixed,is 16)
**                     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
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/	

int SD_ReadCSD(INT8U csdlen, INT8U *recvbuf)
{
    INT8U param[4] = {0,0,0,0},resp,ret;
  
    ret = SD_SendCmd(CMD9, param, CMD9_R, &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, recvbuf));
}

/*********************************************************************************************************
** Function name:    SD_ReadCID
**
** Descriptions:     read CSD register of SD card
**                   
** input parameters:   INT8U cidlen  : len of register (fixed,is 16)
**                     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_ReadCID_EN
int SD_ReadCID(INT8U cidlen, INT8U *recvbuf)
{
    INT8U param[4] = {0,0,0,0},resp,ret;
 
    ret = SD_SendCmd(CMD10, param, CMD10_R, &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(cidlen, recvbuf));
}
#endif

/*********************************************************************************************************
** Function name:    SD_StopTransmission
**
** Descriptions:     stop data transmission 
**                   
** input parameters:   None
**
** 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_StopTransmission(void)
{
	INT8U param[4] = {0,0,0,0},resp;
	
 	return (SD_SendCmd(CMD12, param, CMD12_R, &resp));	/* stop transmission command fail */
}

/*********************************************************************************************************
** Function name:    SD_ReadCard_Status
**
** Descriptions:     Read status register of SD card
**                   
** input parameters:   INT8U len     : length of register (fixed,is 2)
**                     INT8U *buffer : Get response information
**
** 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_ReadCard_Status(INT8U len, INT8U *buffer)
{
    INT8U param[4] = {0,0,0,0};

    return (SD_SendCmd(CMD13, param, CMD13_R, buffer)); /* read register of Card Status */
}


/*********************************************************************************************************
** Function name:    SD_SetBlockLen
**
** Descriptions:     Set a block len of the SD card 
**                   
** input parameters:   INT32U length	: the length of a block
**
** 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_SetBlockLen(INT32U length)
{
	INT8U param[4],resp,ret;
  
    SD_PackParam(param, length);					/* change the parameter to bytes form */
          												
    ret = SD_SendCmd(CMD16, param, CMD16_R, &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 successfully */			
}

/*********************************************************************************************************
** Function name:    SD_ReadSingleBlock
**
** Descriptions:     Issue read single block command 
**                   
** input parameters:   INT32U blockaddr: block address
**
** Returned value:     SD_NO_ERR:  successful
**                     Others   :  fail,errors occur
**                       

⌨️ 快捷键说明

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