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

📄 l2_sd.c

📁 dz3000_51.0.0.4.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
/*++

Copyright (c) 2001 Sunplus Technology Co., Ltd.

Module Name:

        L2_SD.c

Abstract:

        Module related to L2 NAND flash functions

Environment:

        Keil C51 Compiler

Revision History:

        11/12/2001      WZH    created                 
--*/



//=============================================================================
//Header file
//=============================================================================
#include "general.h"

//=============================================================================
//Symbol
//=============================================================================
//-----------------------------------------------------------------------------
//Constant
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//Variable
//-----------------------------------------------------------------------------

//=============================================================================
//Program       
//=============================================================================

#if (SD)

//-----------------------------------------------------------------------------
//L2_SDCardDetect
//-----------------------------------------------------------------------------
/* 
routine description:
        Detect whether SD card is inserted
arguments:
        Detect: 
          0: SD card is not inserted
		1: SD card is inserted
return value:
        0x00   - success
        others - error
*/
/*UCHAR L2_SDCardDetect(UCHAR* detect) USING_0 //ada@0218
{
     *detect = 0;

     XBYTE[0x2405] &= 0xdf;

     if (!(XBYTE[0x2409] & 0x20))    
     {
          *detect = 1; 
     }

	return L2K_SUCCESS;	
}
*/

//-----------------------------------------------------------------------------
//L2_SDInit
//-----------------------------------------------------------------------------
/*
routine description:
        SD interface initialization
arguments:
        None
return value:
        None
*/
void L2_SDInit(void) USING_0
{
     XBYTE[0x2450] = 0x03;   // SD reset, CRC reset
	XBYTE[0x2457] = 0xff;   // Rsp timer setting
     XBYTE[0x2458] = 0x08;   // CRC timer setting
     XBYTE[0x2451] = 0x30;   // enable Rsp timer & CRC timer,SD card mode
}

//-----------------------------------------------------------------------------
//L2_SDConfig
//-----------------------------------------------------------------------------
/*
routine description:
        Set SD operation frequency , data bus width , Response type
arguments:
        OpFreq         0:24MHz, 1:12MHz, 2:6MHz 3:375KHz
        DataBusWidth   0:1 bit data bus, 1:4 bits data bus
return value:
        None
*/
void L2_SDConfig(UCHAR OpFreq,UCHAR DataBusWidth) USING_0
{
        UCHAR tmp;
        tmp = XBYTE[0x2451];
        tmp = (tmp&0xf0)|(OpFreq&0x03)|((DataBusWidth&0x01)<<2);
        XBYTE[0x2451] = tmp;
}

//-----------------------------------------------------------------------------
//L2_SDBlockSize
//-----------------------------------------------------------------------------
/*
routine description:
        Set SD data length of read/write one block
arguments:
        BlockSize : the length of read/write one block     
return value:
        None
*/
void L2_SDBlockSize(USHORT BlockSize) USING_0
{
        XBYTE[0x2455] = (UCHAR)(BlockSize);
        XBYTE[0x2456] = (UCHAR)(BlockSize>>8);
}

//-----------------------------------------------------------------------------
//L2_SDReset
//-----------------------------------------------------------------------------
/*
routine description:
        reset the SD interface
arguments:
        None
return value:
        None
*/
void L2_SDReset(void) USING_0
{
        XBYTE[0x2450] = 0x03;
}

//-----------------------------------------------------------------------------
//L2_SDTxCommand
//-----------------------------------------------------------------------------
/*
routine description:
        Send command to SD card
arguments:
        CmdBuf : the command to be x'fer
return value:
        None
*/
//patch4.5@ada@Add timeout count begin
UCHAR L2_SDTxCommand(UCHAR* CmdBuf) USING_0
//void L2_SDTxCommand(UCHAR* CmdBuf) USING_0
{
	USHORT timeout_count = 0xffff;

	XBYTE[0x245B] = CmdBuf[0];
	XBYTE[0x245C] = CmdBuf[1];
	XBYTE[0x245D] = CmdBuf[2];
	XBYTE[0x245E] = CmdBuf[3];
	XBYTE[0x245F] = CmdBuf[4];

	XBYTE[0x2452] = 0x01;						// trigger to TX command

	while ((XBYTE[0x2454] & 0x0F) != 0x00)
	{
		if (timeout_count > 0)
		{
			timeout_count--;
		}	
		else	
		{
			return L2K_ERROR_GENERAL;
		}
	}

	return L2K_SUCCESS;
        //PRINT_L2("CRC7=%x\n",(USHORT)XBYTE[0x2466]);
}
//patch4.5@ada@Add timeout count end

//-----------------------------------------------------------------------------
//L2_SDRxResponse
//-----------------------------------------------------------------------------
/*
routine description:
        receive SD card response
arguments:
        RspType - 0: length of 6 bytes, 1: length of 17 bytes
return value:
        0x00 - No error
        0x01 - crc7 error
        0x02 - response timeout
*/
//patch4.5@ada@Add timeout count begin
UCHAR L2_SDRxResponse(UCHAR* RspBuf, UCHAR RspType) USING_0
{
	UCHAR crc7;
	UCHAR status = L2K_SUCCESS;
	UCHAR i;
	UCHAR value;

	USHORT timeout_count = 0xffff;	

     if (RspType == 0)
	{  
		XBYTE[0x2451] &= 0xF7; // Set response type to 6 bytes
	}
     else
	{
          XBYTE[0x2451] |= 0x08; // Set response type to 17 bytes
	}

	XBYTE[0x2452] = 0x02;			          // start receiving response

     //version4.0@ada@0513 for MMC
	do
     {
     	value = (XBYTE[0x2453] & 0x02);           // wait until response buffer full
     } while ((value == 0) && ((XBYTE[0x2453] & 0x40) == 0));

	if ((value & 0x20) && (XBYTE[0x2453] & 0x40))
     {    
		// timeout
          XBYTE[0x2450] = 0x03;

		status = 0x02;            
		return status;						  // return
	}

	RspBuf[0]=XBYTE[0x2460];
	RspBuf[1]=XBYTE[0x2461];
	RspBuf[2]=XBYTE[0x2462];
	RspBuf[3]=XBYTE[0x2463];
	RspBuf[4]=XBYTE[0x2464];
	RspBuf[5]=XBYTE[0x2465];
	crc7=RspBuf[5];

        if(RspType==1) 
        {
                for(i=6; i<17; i++) 
                {
                    //version4.0@ada@0513 for MMC
               	do
                    {
                          value = XBYTE[0x2453] & 0x02 ;           // wait until response buffer full
                    } while((value==0) && ((XBYTE[0x2453] & 0x40) == 0));


               	if((value & 0x20) && (XBYTE[0x2453] & 0x40))
                       {                  // timeout
                          XBYTE[0x2450] = 0x03;            
	                   	return 0x02;						  // return
	                   }

     			RspBuf[i]=XBYTE[0x2465];
                }

                crc7=RspBuf[16];
        }
	
	if(crc7!=XBYTE[0x2466]) status=0x01;		                    // CRC7 error

	XBYTE[0x2452] = 0x20;

     while ((XBYTE[0x2454] & 0x0F) != 0x00)  // send dummy
	{
		if (timeout_count > 0)
		{
			timeout_count--;
		}
		else
		{
			status = 2;
			return status;
		}
	}

     return status;
}
//patch4.5@ada@Add timeout count end

/*UCHAR L2_SDRxResponse(UCHAR* RspBuf, UCHAR RspType) USING_0
{
	UCHAR crc7;
	UCHAR status=0;
	UCHAR i;
	UCHAR value;
	
        if(RspType==0)  XBYTE[0x2451] &= 0xF7; // Set response type to 6 bytes
        else            XBYTE[0x2451] |= 0x08; // Set response type to 17 bytes
	
	XBYTE[0x2452] = 0x02;			          // start receiving response

	do
        {
                value = XBYTE[0x2453] & 0x02 ;           // wait until response buffer full
        } while(value==0);

	if((value & 0x20) !=0)
        {                  // timeout
                XBYTE[0x2450] = 0x03;            
		return 0x02;						  // return
	}

	RspBuf[0]=XBYTE[0x2460];
	RspBuf[1]=XBYTE[0x2461];
	RspBuf[2]=XBYTE[0x2462];
	RspBuf[3]=XBYTE[0x2463];
	RspBuf[4]=XBYTE[0x2464];
	RspBuf[5]=XBYTE[0x2465];
	crc7=RspBuf[5];

        if(RspType==1) 
        {
                for(i=6; i<17; i++) 
                {
                        do
                        {
                                value = XBYTE[0x2453] & 0x02 ;   // wait until response buffer full
                        } while(value==0);

                        if((value & 0x20) !=0)
                        {             // timeout
                                XBYTE[0x2450] = 0x03;            
                                return 0x02;                         // return
                        }
			RspBuf[i]=XBYTE[0x2465];
                }

                crc7=RspBuf[16];
        }
	
	if(crc7!=XBYTE[0x2466]) status=0x01;		                    // CRC7 error

	XBYTE[0x2452] = 0x20;  while ((XBYTE[0x2454] & 0x0F) != 0x00);  // send dummy

        return status;
}
*/
//-----------------------------------------------------------------------------
//L2_SDReadRspBuf
//-----------------------------------------------------------------------------
/* 

⌨️ 快捷键说明

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