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

📄 sd_protocol.c

📁 TI的DM6446的硬件平台搭建的相关例子
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
    DaVinci ARM Evaluation Software

    (c)Texas Instruments 2004
*/


/**
    \file sd_protocol.c
    \brief SD Related APIs, which are protocol specific
*/

#include <cslr_mmcsd.h>
#include <memcd_rettypes.h>
#include <mmcsd_protocol.h>
#include <sd_protocol.h>



/**
    \brief    Send command to check if the Operating Voltage Conditions of the SD, match that of the host

    \param  voltageWindow   voltage window supported by the host
    \param  opTimeOut   timeout value to check if the card is powered-up, after sending the command

    \return if success, \c E_PASS, else error code

*/
STATUS SD_sendOpCond( Uint32 voltageWindow, Uint32 opTimeOut )
{

	STATUS status;
	Uint16 mode, OCRword;


	/* Check whether the operating mode is native MMC or SPI */
	mode = 0;


   do{
        /* Format and send cmd: Volt. window is usually 0x00300000 (3.4-3.2v)*/
        status = MMCSD_sendCmd( SD_APP_OP_COND, voltageWindow, (Bool)1, MMCSD_STAT0_RSPDNE);
	    if( status != E_PASS )
			return status;
		if(mode)
		{  /* SPI mode */

          if(CSL_FEXT(CSL_MMCSD_0_REGS->MMCRSP67,MMCSD_MMCRSP67_MMCRSP7))
          {
           return(E_DEVICE);
          }else
          {
            return(E_PASS);
          }
        }

        OCRword = CSL_FEXT(CSL_MMCSD_0_REGS->MMCRSP67,MMCSD_MMCRSP67_MMCRSP7);
        if( OCRword & 0x8000 ) {
		   return( E_PASS );
    	}
        opTimeOut--;

        /* Send CMD55 with RCA = 0 for the next iteration */
	    status = MMCSD_appCmd( 0 );

   } while ( opTimeOut > 0 );

   /* Time-out occurred : reset the card */
   return( E_DEVICE );
}


/**
    \brief  Send command to set the Relative Card Address (RCA) of the SD

    \param  rca  Relative Card Address assigned to the card

    \return if success, \c E_PASS, else error code

*/
STATUS SD_sendRCA( Uint32 *rcaRcvd )
{
	STATUS status;

      /* rca is given a value >= 2, as 0 or 1(in case of SanDisk cards) is
         the default value for all cards. Also, the rca value is sent as
		 the upper 16 bits of the command argument */
	  status = MMCSD_sendCmd( SD_SEND_RELATIVE_ADDR, MMCSD_STUFF_BITS, (Bool)1, MMCSD_STAT0_RSPDNE);

	  if( status == E_PASS ) {
        /* Response expected: R1 */
        *rcaRcvd = MMCSD_RCA_RCVD();
        return ( E_PASS );
      }

      return ( status );
}


/**
    \brief  Send command to erase a range of blocks on the SD

    \param  eraseStartAddr Start address of blocks selected to be erased
    \param  eraseEndAddr End address of blocks selected to be erased

    \return if success, \c E_PASS, else error code

*/
STATUS SD_erase( Uint32 eraseStartAddr, Uint32 eraseEndAddr )
{
	STATUS status;


	  status = MMCSD_sendCmd( SD_ERASE_WR_BLK_START, eraseStartAddr, (Bool)1, MMCSD_STAT0_RSPDNE);

	  if( status == E_PASS ) {
	    status = MMCSD_sendCmd( SD_ERASE_BLK_END, eraseEndAddr, (Bool)1, MMCSD_STAT0_RSPDNE);
	    if ( status == E_PASS ){
		  status = MMCSD_sendCmd( SD_ERASE, MMCSD_STUFF_BITS, (Bool)1, MMCSD_STAT0_RSPDNE );
		  if( status == E_PASS )
		     status = MMCSD_checkStatus(	MMCSD_STAT0_BSYDNE, 0, 0 );  //Timeout =0
        }
      }

	  return ( status );
}


/**
    \brief  Send command to set a password or replace the existing password on the SD

    \param  oldPasswordSize  Size in bytes of the existing password
    \param  newPasswordSize  Size in bytes of the new password to be set
    \param  oldPassword  Existing password
    \param  newPassword  New password

    \return if success, \c E_PASS, else error code

*/
STATUS SD_setPassword( Uint8 oldPasswordSize, Uint8 newPasswordSize, Uint8 *oldPassword, Uint8 *newPassword,Uint32 cardMemAddr)
{
	STATUS status;
	Uint8 lockCardDataStructure[18];
	int i=0;
	Uint32 numWords = 0;

      if( oldPasswordSize != 0 ) {
        numWords = (2+newPasswordSize+oldPasswordSize)/2;
      } else {
        numWords = (2+newPasswordSize)/2;
	  }

      status = MMCSD_sendCmd( MMCSD_SET_BLOCKLEN, numWords*2, (Bool)1, MMCSD_STAT0_RSPDNE);

      if( status == E_PASS ){
      	status = MMCSD_setDataSize( 1, numWords*2 ); /* num_blks = 1, num_of_bytes = numWords */
        if( status == E_PASS ) {
          //< Set the lockCardDataStructure elements
		  lockCardDataStructure[0] = (Uint8)MMCSD_SET_PASSWD;
		  if( oldPasswordSize != 0 ) {
			  lockCardDataStructure[1] = newPasswordSize+oldPasswordSize;
			  for ( i=0; i<oldPasswordSize; i++)
			  {
				  lockCardDataStructure[i+2]= oldPassword[i];
			  }
  			  for ( i=0; i<newPasswordSize; i++)
			  {
				  lockCardDataStructure[i+2+oldPasswordSize]= newPassword[i];
			  }
          } else {
			  lockCardDataStructure[1] = newPasswordSize;
			  for ( i=0; i<newPasswordSize; i++)
			  {
				  lockCardDataStructure[i+2]= newPassword[i];
			  }
		  }

		  status = MMCSD_sendCmd( (0x2880 | SD_LOCK_UNLOCK), MMCSD_STUFF_BITS, (Bool)0, MMCSD_STAT0_RSPDNE );
		  if( status == E_PASS ) {
			 status = MMCSD_writeNWords( (Uint32*)lockCardDataStructure, numWords,cardMemAddr,DM_FALSE );
 	 		 if( status == E_PASS )
  		        status = MMCSD_checkStatus( MMCSD_STAT0_DATDNE, 0, 0 );
			    if( status == E_PASS )
					status = MMCSD_LOCK_UNLOCK_FAIL_CHK();    //< If fail, old password does not change
		  }
		}
	  }

	  return ( status );
}


/**
    \brief  Send command to reset the existing password on the SD

    \param  currentPasswordSize  Size in bytes of the existing password
    \param  currentPassword  Existing password

    \return if success, \c E_PASS, else error code

*/
STATUS SD_resetPassword( Uint8 currentPasswordSize, Uint8 *currentPassword,Uint32 cardMemAddr )
{
	STATUS status;
	Uint8 lockCardDataStructure[18];
	int i=0;

      status = MMCSD_sendCmd( MMCSD_SET_BLOCKLEN, 2+currentPasswordSize, (Bool)1, MMCSD_STAT0_RSPDNE);

      if( status == E_PASS ){
      	status = MMCSD_setDataSize( 1, 2+currentPasswordSize ); /* num_blks = 1, num_of_bytes = 2+currentPasswordSize */
        if( status == E_PASS ) {
          //< Set the lockCardDataStructure elements
		  lockCardDataStructure[0] = (Uint8)MMCSD_CLR_PASSWD;
		  lockCardDataStructure[1] = currentPasswordSize;
		  for ( i=0; i<currentPasswordSize; i++)
		  {
			  lockCardDataStructure[i+2]= currentPassword[i];
		  }

		  status = MMCSD_sendCmd( (0x2880 | SD_LOCK_UNLOCK), MMCSD_STUFF_BITS,(Bool)0, MMCSD_STAT0_RSPDNE );
		  if( status == E_PASS ) {
			 status = MMCSD_writeNWords( (Uint32*)lockCardDataStructure, (2+currentPasswordSize)/2,cardMemAddr,DM_FALSE );
 	 		 if( status == E_PASS )
  		        status = MMCSD_checkStatus( MMCSD_STAT0_DATDNE, 0, 0 );
			    if( status == E_PASS )
					status = MMCSD_LOCK_UNLOCK_FAIL_CHK();    //< If fail, old password does not change
		  }
	    }
      }

	  return ( status );
}


/**
    \brief  Send command to lock the SD and/or set a password or replace the existing password on the SD

    \param  cardLockMode  Card lock mode - 1: set passwd, 2: clr passwd, 4: lock; 8: forced erase
    \param  oldPasswordSize  Size in bytes of the existing password
    \param  newPasswordSize  Size in bytes of the new password to be set
    \param  oldPassword  Existing password
    \param  newPassword  New password

    \return if success, \c E_PASS or \c CardLock and Passwd Set status, else error code

*/
Uint8 SD_cardLock( Uint8 cardLockMode, Uint8 oldPasswordSize, Uint8 newPasswordSize, Uint8 *oldPassword, Uint8 *newPassword,Uint32 cardMemAddr )
{
	STATUS status;
	Uint8 lockCardDataStructure[18];
	int i=0;
	Uint32 numWords = 0;

      if( (oldPasswordSize != 0) && (newPasswordSize != 0) ) {
        numWords = (2+newPasswordSize+oldPasswordSize)/2;
      } else if( oldPasswordSize == 0 ) {
        numWords = (2+newPasswordSize)/2;
	  } else if( newPasswordSize == 0 ) {
        numWords = (2+oldPasswordSize)/2;
	  }

      status = MMCSD_sendCmd( MMCSD_SET_BLOCKLEN, numWords*2, (Bool)1, MMCSD_STAT0_RSPDNE);

      if( status == E_PASS ){
      	status = MMCSD_setDataSize( 1, numWords*2 ); /* num_blks = 1, num_of_bytes = numWords */
        if( status == E_PASS ) {
          //< Set the lockCardDataStructure elements
		  lockCardDataStructure[0] = cardLockMode;
		  if( (oldPasswordSize != 0 ) && (newPasswordSize != 0) ) {
			  lockCardDataStructure[1] = newPasswordSize+oldPasswordSize;
			  for ( i=0; i<oldPasswordSize; i++)
			  {
				  lockCardDataStructure[i+2]= oldPassword[i];
			  }
  			  for ( i=0; i<newPasswordSize; i++)
			  {
				  lockCardDataStructure[i+2+oldPasswordSize]= newPassword[i];
			  }
          } else if( newPasswordSize == 0 ) {
			  lockCardDataStructure[1] = oldPasswordSize;
			  for ( i=0; i<oldPasswordSize; i++)
			  {
				  lockCardDataStructure[i+2]= oldPassword[i];
			  }
		  } else if( oldPasswordSize == 0 ) {
			  lockCardDataStructure[1] = newPasswordSize;
			  for ( i=0; i<newPasswordSize; i++)
			  {
				  lockCardDataStructure[i+2]= newPassword[i];
			  }
		  }

		  status = MMCSD_sendCmd( (0x2880 | SD_LOCK_UNLOCK), MMCSD_STUFF_BITS, (Bool)0, MMCSD_STAT0_RSPDNE );
		  if( status == E_PASS ) {
			 status = MMCSD_writeNWords( (Uint32*)lockCardDataStructure, numWords,cardMemAddr,DM_FALSE );
 	 		 if( status == E_PASS )
  		        status = MMCSD_checkStatus( MMCSD_STAT0_DATDNE, 0, 0 );
			    if( status == E_PASS )
					//< If 00: Card unlocked, passwd no error; 01: Card unlocked, passwd error; 10: Card locked, passwd no error; 11: Card locked; passwd error
					status = MMCSD_CARD_LOCK_AND_PASSWD_CHK();
		  }
		}
	  }

	  return ( status );
}


/**
    \brief  Send command to unlock the SD

    \param  cardLockMode  Card lock mode - 1: set passwd, 2: clr passwd, 4: lock; 8: forced erase
    \param  currentPasswordSize  Size in bytes of the existing password
    \param  currentPassword  Existing password

    \return if success, \c E_PASS or \c CardLock and Passwd Set status, else error code

*/
Uint8 SD_cardUnlock( Uint8 cardLockMode, Uint8 currentPasswordSize, Uint8 *currentPassword,Uint32 cardMemAddr )
{
	STATUS status;
	Uint8 lockCardDataStructure[18];
	int i=0;

      status = MMCSD_sendCmd( MMCSD_SET_BLOCKLEN, 2+currentPasswordSize, (Bool)1, MMCSD_STAT0_RSPDNE);

      if( status == E_PASS ){
      	status = MMCSD_setDataSize( 1, 2+currentPasswordSize ); /* num_blks = 1, num_of_bytes = 2+currentPasswordSize */
        if( status == E_PASS ) {
          //< Set the lockCardDataStructure elements
		  lockCardDataStructure[0] = cardLockMode;
		  lockCardDataStructure[1] = currentPasswordSize;
		  for ( i=0; i<currentPasswordSize; i++)

⌨️ 快捷键说明

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