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

📄 sdcarddriver.c

📁 Taiwan sunplus develop spce3200, it is a test program ----- testboard source code
💻 C
📖 第 1 页 / 共 2 页
字号:
//====================================================================================
//File Name: 	SDCardDriver.c
//Description: 	SD card driver
//Update: 		2007.1.17 V1.0 by lijian <lijian@sunnorth.com.cn>
//====================================================================================
#include "SDCardDriver.h"

unsigned int gui_RCA;
int gi_CardType;
int gi_SDCardTotalSector;

//=============================================================
//Prototype:	void invalid_cache (unsigned int *addr)
//Description:	Make cache invalid (Synchronize the cache and RAM)
//Arguments:	addr - Operation address
//Return Value:	None
//=============================================================
static void invalid_cache(unsigned int *addr)
{
	asm volatile("cache 0x18, [%0, 0]"::"r"(addr));
}

//==========================================================================
//Prototype: 	int WaitSDStatus(unsigned int pollbit)
//Description: 	Wait for a certain state of the SD card controller
//Argument: 	pollbit	- The associate bit of that state
//Return Value: 0 - Normal quit
//				1 - Time is expired or an error occurs in the controller
//==========================================================================
int WaitSDStatus(unsigned int pollbit)
{
	int i;
	if(!SDDrv_CheckCard)									// Check if card presents
		return 1;
	i = 0;
	do {
		i = i + 1;
		if(!SDDrv_CheckCard)
			return 1;
	} while (((*P_SD_INT_STATUS & pollbit) == 0) && (i<600000));
	if (i==600000)
		return 1;
	if ((*P_SD_INT_STATUS & (C_SD_RSPINDEX_ERR | C_SD_RSPCRC_ERR | C_SD_DATACRC_ERR | C_SD_TIME_OUT)) != 0)
		return 1;
	return 0;
}

//=========================================================================================================================
//Prototype: 	int SDCommand(unsigned int commandset, unsigned int arg, unsigned int* outaddr)
//Description: 	Send a command to the SD card; receive 32-bit response data
//Arguments: 	commandset - Command
//				arg - Command argument
//				outaddr - The address of the memory storing the response data
//Return Value: 0 - Normal quit
//				1 - Time is expired or an error occurs in the controller
//========================================================================================================================
int SDCommand(unsigned int commandset, unsigned int arg, unsigned int* outaddr)
{
	// Write Argument First
	*P_SD_ARGUMENT_DATA = arg;
	*P_SD_COMMAND_SETUP = commandset;
	if (WaitSDStatus(C_SD_CMD_COMPLETE))
	{
		*P_SD_INT_STATUS = C_SD_CLR_AllBIT;
		return 1;
	}
	*outaddr = *P_SD_RESPONSE_DATA;
	*P_SD_INT_STATUS = C_SD_CLR_AllBIT;
	return 0;
}

//================================================================================================
//Prototype:	int SDCommand2(unsigned int commandset, unsigned int arg, unsigned int* outaddr)
//Description:	Send a command to the SD card; receive 128-bit response data
//Arguments:	commandset - Command
//				arg - Command argument
//				outaddr	- The start address of the buffer storing the response data
// Return Value:0 - Normal quit
//				1 - Time is expired or an error occured in the controller
//================================================================================================
int SDCommandr2(unsigned int commandset, unsigned int arg, unsigned int* outaddr)
{
	int i;
	// Write Argument First
	*P_SD_ARGUMENT_DATA = arg;
	*P_SD_COMMAND_SETUP = commandset;
	for (i=0;i<4;i++)
	{
		if(WaitSDStatus(C_SD_CMDBUF_FULL))
			return 1;
		*outaddr = *P_SD_RESPONSE_DATA;
		outaddr++;
	}
	if (WaitSDStatus(C_SD_CMD_COMPLETE))
		return 1;
	*P_SD_INT_STATUS = C_SD_CLR_AllBIT;
	return 0;
}

//================================================================================================
//Prototype: 	int SDCommand3(unsigned int commandset, unsigned int arg, unsigned int* outaddr)
//Description: 	Send a command to the SD card; receive 64-bit response data
//Arguments: 	commandset - Command
//				arg - Command argument
//				outaddr	- The start address of the buffer storing the response data
//Return Value: 0 - Normal quit
//				1 - Time is expired or an error occured in the controller
//================================================================================================
int SDCommandr3(unsigned int commandset, unsigned int arg, unsigned int* outaddr)
{
	int i;

	// Write Argument First
	*P_SD_ARGUMENT_DATA = arg;
	*P_SD_COMMAND_SETUP = commandset;
	i = 0;
	while ((*P_SD_INT_STATUS & C_SD_DATABUF_FULL) == 0)
	{
		i = i + 1;
		if(i == 600000)
			return 1;

		if(!SDDrv_CheckCard)
			return 1;
	}
	*outaddr = *P_SD_RX_DATA;
	i = 0;
	while ((*P_SD_INT_STATUS & C_SD_DATABUF_FULL) == 0)
	{
		i = i + 1;
		if(i == 600000)
			return 1;

		if(!SDDrv_CheckCard)
			return 1;
	}
	*(outaddr+1) = *P_SD_RX_DATA;
	if (WaitSDStatus(C_SD_DATA_COMPLETE))
	{
		*P_SD_INT_STATUS = C_SD_CLR_AllBIT;
		return 1;
	}
	*P_SD_INT_STATUS = C_SD_CLR_AllBIT;
	return 0;
}

//===========================================================================================================
//Prototype: 	int SDDrv_ReadSector(unsigned int sector, unsigned int sector_num, unsigned char *buf)
//Description: 	Read sectors
//Arguments: 	sector - Sector index
//				sector_num - Number of sectors to be read
//				buf - The start address of the buffer storing the data
//Return Value: 0 - Normal quit
//				1 - Abnormal quit
//==========================================================================================================
int SDDrv_ReadSector(unsigned int sector, unsigned int sector_num, unsigned char *buf)
{
	int i, ret;
	if(DrvSDCReadMulCommand(sector))
		return 1;
	for(i=0; i<sector_num; i++)
	{
		ret = DrvSDCReadMulData(buf+i*512);
		if(ret == 1)
		{
			DrvSDCReadMulStop();
			return ret;
		}
	}
	if(DrvSDCReadMulStop())
		return 1;
	return 0;
}

//==========================================================================================================
//Prototype: 	int DrvSDCReadMulCommand(unsigned int block)
//Description: 	Startup sector reading
//Argument: 	block - Sector index
//Return Value: 0 - Normal quit
//				1 - Abnormal quit
//==========================================================================================================
int	DrvSDCReadMulCommand(unsigned int block)
{
	unsigned int response[4];
	SDCommand(C_SD_CMD7, gui_RCA, response);
	*P_SD_ARGUMENT_DATA = gui_RCA;
	*P_SD_COMMAND_SETUP = C_SD_CMD13;
	while((*P_SD_INT_STATUS & C_SD_CMD_COMPLETE) == 0x00);
	response[0] = *P_SD_RESPONSE_DATA;
	*P_SD_INT_STATUS = C_SD_CLR_AllBIT;
	if(SDCommand(C_SD_CMD18,block<<9,response))
		return 1;
	return 0;
}

//==========================================================================================================
//Prototype: 	int DrvSDCReadMulData(unsigned char *outaddr)
//Description: 	Read sectors
//Argument: 	outaddr	- The start address of the buffer storing the data
//Return Value: 0 - Normal quit
//				1 - Abnormal quit
//==========================================================================================================
int	DrvSDCReadMulData(unsigned char *outaddr)
{
	unsigned int	i;
	unsigned int *realaddr;
	i=(unsigned int) outaddr;
	if((i & 0x03) == 0x00)
	{
		// USE DMA
		*P_DMA_CHANNEL0_CTRL = 0x0000;
		*P_DMA_AHB_SA0BA = (unsigned int)outaddr;
		*P_DMA_AHB_EA0BA = (unsigned int)outaddr + (1 << 9) - 4;
		*P_DMA_APB_SA0 = (unsigned int)P_SD_RX_DATA;
		*P_DMA_CHANNEL0_CTRL = C_DMA_APB_MIU|C_DMA_POLLING_MODE|C_DMA_REGULAR_MODE|C_DMA_SINGLE_BUF|C_DMA_32BIT_BURST|C_DMA_CH_EN;
		// Loop until DMA interrupt is set
		while ((*P_DMA_INT_STATUS & 0x1) == 0)
		{
			if(!SDDrv_CheckCard)
				return 1;
		}
		// Clear Interrupt and Disable DMA channel
		*P_DMA_INT_STATUS = 0x1;
		*P_DMA_CHANNEL0_CTRL = 0x0000;
	}
	else
	{
		// Do Not USE DMA
		unsigned int	DataCount;
		unsigned int	Data;
		DataCount=512 >> 2;	// DataCount = 512 Byte / 4
		do
		{
			if(WaitSDStatus(C_SD_DATABUF_FULL))
				return 1;
			Data=*P_SD_RX_DATA;
			*outaddr++ = (char) Data;
			Data=Data >> 8;
			*outaddr++ = (char) Data;
			Data=Data >> 8;
			*outaddr++ = (char) Data;
			Data=Data >> 8;
			*outaddr++ = (char) Data;
			DataCount--;
		}while(DataCount != 0x00);
	}
	realaddr = (unsigned int *)((unsigned int)outaddr & 0x8fffffff);
	invalid_cache(realaddr);
	i = 0;
	while((*P_SD_INT_STATUS & C_SD_DATA_COMPLETE) == 0x00)
	{
		i++;
		if( i == 0xFFFFFF)
			return 1;
	}
	*P_SD_INT_STATUS = C_SD_CLR_AllBIT;
	return 0;
}
#if 0
//==========================================================================================================
//Prototype: 	int DrvSDCReadMulData_INT(unsigned char *outaddr)
//Description: 	Initialize the DMA reading to sectors
//Argument: 	outaddr	- The start address of the buffer storing the data
//Return Value: 0 - Normal quit
//				1 - Abnormal quit
//==========================================================================================================
int	DrvSDCReadMulData_INT(unsigned char *outaddr)
{
	// USE DMA
	*P_DMA_CHANNEL0_CTRL = 0x0000;
	*P_DMA_AHB_SA0BA = (unsigned int)outaddr;
	*P_DMA_AHB_EA0BA = (unsigned int)outaddr + ( 512 ) - 4;
	*P_DMA_APB_SA0 = (unsigned int)P_SD_RX_DATA;
	*P_DMA_CHANNEL0_CTRL = C_DMA_APB_MIU|C_DMA_POLLING_MODE|C_DMA_REGULAR_MODE|C_DMA_SINGLE_BUF|C_DMA_32BIT_BURST|C_DMA_INT_EN|C_DMA_CH_EN;
	return 0;
}
#endif

//==========================================================================================================
//Prototype: 	int DrvSDCReadMulStop(void)
//Description: 	Stop reading from sectors
//Argument: 	None
//Return Value: 0 - Normal quit
//				1 - Abnormal quit
//==========================================================================================================
int DrvSDCReadMulStop(void)
{
	unsigned int response[4];
	unsigned int loopcnt;
	// Stop the controller
	*P_SD_COMMAND_SETUP = C_SD_CTRL_IDLE;
	// Loop until controller idle
	while(*P_SD_INT_STATUS & C_SD_CTRL_BUSY)
		if(!SDDrv_CheckCard)
			return 1;
	*P_SD_INT_STATUS = C_SD_CLR_AllBIT;
	// Stop the card
	SDCommand(C_SD_CMD12, 0x00000000, response);
	loopcnt=0;
	do
	{
		if(SDCommand(C_SD_CMD13, gui_RCA, response))
			return 1;
		loopcnt++;
		if(loopcnt == 0xFFFFFF)
			return 1;

	} while (response[0] != 0x0900);
	SDCommand(C_SD_CMD7, gui_RCA, response);
	return 0;
}

//==========================================================================================================
//Prototype: 	int SDDrv_WriteSector(unsigned int block, unsigned int blocknum, unsigned char *outaddr)
//Description: 	Write to sectors
//Arguments: 	block -	Sector index
//				blocknum - Number of sectors to be read
//				outaddr	- The start address of the buffer storing the data
//Return Value: 0 - Normal quit
//				1 - Abnormal quit
//==========================================================================================================
int SDDrv_WriteSector(unsigned int block, unsigned int blocknum, unsigned char *outaddr)
{
	int i, ret;
	if(DrvSDCWriteMulCommand(block))
		return 1;
	for(i=0; i<blocknum; i++)
	{
		ret = DrvSDCWriteMulData(outaddr+i*512);
		if(ret == 1)
		{
			DrvSDCWriteMulStop();
			return ret;
		}
	}
	if(DrvSDCWriteMulStop())
		return 1;

⌨️ 快捷键说明

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