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

📄 sdcarddriver.c

📁 凌阳SPCE3200多媒体开发板自带源程序。共安排了32个子目录
💻 C
📖 第 1 页 / 共 2 页
字号:
	
	if(CacheCurrType())
		DCacheFrcWrtVa();		// Force write-back entire D-Cache and set valid
//	
	
	i=(U32) outaddr;
	if((i & 0x03) == 0)
	{
		// USE DMA
		*P_DMA_CHANNEL0_CTRL = 0x0000;
		
		*P_DMA_AHB_SA0BA = (U32)outaddr;
		*P_DMA_AHB_EA0BA = (U32)outaddr + ( 512 ) - 4;
		*P_DMA_APB_SA0 = 0x88180000;
		*P_DMA_CHANNEL0_CTRL = DMA_M2P|DMA_REQ|DMA_FIX|DMA_SINGLE_BUF|DMA_32BIT_BURST|DMA_IRQOFF|DMA_ON;
	
		// Loop until DMA interrupt is set
		while ((*P_DMA_INT_STATUS & 0x1) == 0)
		{
			if(!gSDCardInsert)
			{
				// Clear Interrupt and Disable DMA channel
				*P_DMA_INT_STATUS = 0x1;
				*P_DMA_CHANNEL0_CTRL = 0x0000;	
				return 1;
			}
		}
	
		// Clear Interrupt and Disable DMA channel
		*P_DMA_INT_STATUS = 0x1;
		*P_DMA_CHANNEL0_CTRL = 0x0000;
		
	}
	else
	{
		// Do not USE DMA
		
		U32	DataCount;
		U32	Data;
		
		
		DataCount=512 >> 2;	// DataCount = 512 Byte / 4
		do
		{
			if(WaitSDStatus(MASK_S_DataBufEmpty))
				return 1;
			
			Data=(U32) *outaddr;
			outaddr++;
			Data |= ((U32) *outaddr)<<8;
			outaddr++;
			Data |= ((U32) *outaddr)<<16;
			outaddr++;
			Data |= ((U32) *outaddr)<<24;
			outaddr++;
			
			*P_SD_TX_DATA=Data;
			
			DataCount--;
		}while(DataCount != 0x00);
	}
	
	i = 0;
	while((*P_SD_INT_STATUS & MASK_S_DataComplete) == 0x00)
	{
		i++;
		if( i == 20000)
			return 1;
	}
	*P_SD_INT_STATUS = MASK_S_ClrAllBits;
	
	return 0;
}

S32		DrvSDCWriteMulStop(void)
{
	U32 	i;
	U32 	response[4];
	
	i = 0;
	while((*P_SD_INT_STATUS & MASK_S_CardBusy) != 0x00)
	{
		i++;
		if(i == 0xFFFFFF)
			return 1;
	}
	
	// Stop the controller
	*P_SD_COMMAND_SETUP = MASK_CMDSTOP;

	// Loop until controller idle
	while(*P_SD_INT_STATUS & MASK_S_ControllerBusy);
	
	*P_SD_INT_STATUS = MASK_S_ControllerBusy;
	
	// Stop the card
	if(SDCommand(MASK_CMD12, 0x00000000, response))
		return 1;
		
	i = 0;
	do {
		
		*P_SD_INT_STATUS=MASK_S_ClrAllBits;
		if(SDCommand(MASK_CMD13, rca, response))
			return 1;
		
		i++;
		if(i == 60000)
			return 1;
	} while (response[0] != 0x0900);
	
	SDCommand(MASK_CMD7, rca, response);
	return 0;
}

S32 SDDrv_GetMemSize(void)
{
	return SDCardTotalSector;
}


S32 SDDrv_GetSpeed(void)
{
	U32 response[4], tranunit, timevalue, maxspeed;

	U32 SpeedTable[]={
		0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80,		
	};
	
	U32 TranTable[]={
		1, 10, 100, 1000,
	};
	
	SDCommand(MASK_CMD7, 0x00000000, response);
	
	SDCommandr2(MASK_CMD9, rca, response);

	timevalue = ((response[0] & 0x00000078)>>3);
	tranunit=response[0] & 0x00000007;
	
	maxspeed = SpeedTable[timevalue] * TranTable[tranunit] / 100;
	
	if ((timevalue == 0) || (tranunit > 3)) 
		return 1;
	
	SDCommand(MASK_CMD7, rca, response);	
	return maxspeed;
}


S32 SDDrv_Initial(void)
{
	S32 	i, loopcnt=0;
	S32 	cardtype;
	U32 	response[4];
	
	U32 	tranunit, timevalue;
	U32 	maxspeed;
	U32		c_size, mult, blocklen;
	U32 	SpeedTable[]={
		0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80,		
	};
	U32 	TranTable[]={
		1, 10, 100, 1000,
	};
	
	
	if(!gSDCardInsert)
		return 1;
	
	// Disable All SD Interrupt
	*P_SD_INT_CTRL = 0x0000;
	
	// Step 1, Reset SD Controller
	*P_SD_MODE_CTRL = MASK_C_BlockLen_512bytes|INIT_ClockSpeedFactor|MASK_C_ENSDBus|MASK_C_DMAMode;
	
	// Clear All Command
	*P_SD_COMMAND_SETUP = MASK_CMDSTOP;
	while (*P_SD_INT_STATUS & 0x1);
	
	// Clear All Status
	*P_SD_INT_STATUS = MASK_S_ClrAllBits;
	
	loopcnt = 0;
	
	// Step 2, Start 74 cycles on SD Clk Bus
	*P_SD_COMMAND_SETUP = MASK_ClockCycle74;
	
	if (WaitSDStatus(MASK_S_CmdComplete)) 
		return 1;

	*P_SD_INT_STATUS = MASK_S_ClrAllBits;

	// Step 3, Reset Command, Should be no response
	SDCommand(MASK_CMD0, 0x00000000, response);

	//////////////////////////////////////////////////////////////////
	//		SECTION 2.  CARD IDEN									//
	//////////////////////////////////////////////////////////////////

	loopcnt = 0;
	cardtype = SDCARD;

	// Step 4, Run ACMD 41 until card finish power up sequence
	do
	{
		if (cardtype == SDCARD) 
		{
			if (SDCommand(MASK_CMD55, 0x00000000, response)) 
				cardtype = MMCCARD;
		}
			
		if (cardtype == SDCARD) 
			// ACMD 41
			SDCommand(MASK_ACMD41, 0x00200000, response);	// SD Card
		else
			SDCommand(MASK_CMD1, 0x00200000, response);	// MMC Card
	
		// Check until Bit 31 in OCR register is set
		loopcnt = loopcnt + 1;
	} while (((response[0] & 0x80000000) == 0) && loopcnt < 2000);

	if (loopcnt == 20000) 
		return 1;

	// Step 5, CMD 2 Read CID Register
	if(SDCommandr2(MASK_CMD2, 0x00000000, response))
		return 1;
	
	if (cardtype == SDCARD)
	{
		// Step 6, CMD 3 Read New RCA, SD will generate RCA itself
		if(SDCommand(MASK_CMD3, 0x00000000, response))
			return 1;
		rca = response[0] & 0xFFFF0000;
	} else {
		// Step 6, CMD 3 Set New RCA, MMC need to be assigned a new RCA
		if(SDCommand(MASK_CMD3, 0xFFFF0000, response))
			return 1;
		rca = 0xFFFF0000;
	}
	
	// Read Status
	SDCommand(MASK_CMD13, rca, response);

	if (response[0] != 0x0700) 
		return 1;

	//////////////////////////////////////////////////////////////////
	//		SECTION 3.  SET BUSWIDTH AND CLOCK SPEED				//
	//////////////////////////////////////////////////////////////////

	// Step 7, CMD 9 Read CSD Register
	for (i=0;i<4;i++) 
		response[i] = 0;
	if(SDCommandr2(MASK_CMD9, rca, response))
		return 1;
	
	// Calculate Totoal Memory Size
	blocklen = ((response[1] & 0x000F0000)>>16)-8;
	c_size = ((response[1] & 0x000003FF)<<2) + (response[2]>>30);
	mult = ((response[2] & 0x00030000)>>15) + ((response[2] & 0x0000FFFF)>>15);
	
	SDCardTotalSector = (blocklen*(c_size+1))<<(mult + 2);
	//
	
	
	timevalue 	= ((response[0] & 0x00000078)>>3);
	tranunit	= response[0] & 0x00000007;
	
	if((timevalue == 0) || (tranunit > 3))
		return 1;
	
	maxspeed 	= SpeedTable[timevalue] * TranTable[tranunit] / 100;
	
	if (maxspeed > 13)
		//Increase Clock Speed
		*P_SD_MODE_CTRL = MASK_C_BlockLen_512bytes|MIN_ClockSpeedFactor|MASK_C_ENSDBus|MASK_C_DMAMode;
	else 
		//Increase Clock Speed
		*P_SD_MODE_CTRL = MASK_C_BlockLen_512bytes|MMC_ClockSpeedFactor|MASK_C_ENSDBus|MASK_C_DMAMode;
	
	
	SDCommand(MASK_CMD13, rca, response);
	SDCommand(MASK_CMD7, rca, response);

	// Only SD has 4 bits mode
	if (cardtype == SDCARD)
	{
		//Short block size to 8 bytes
	    *P_SD_MODE_CTRL = MASK_C_BlockLen_8bytes|(*P_SD_MODE_CTRL&0xffff);
		
		//ACMD 51
		SDCommand(MASK_CMD55, rca, response);
		if(SDCommandr3(MASK_ACMD51, rca, response))
			return 1;
		
		if ((response[0] & 0x00000400) != 0x00)
		{
			//Set Bus width to 4 bits
			SDCommand(MASK_CMD55, rca, response);
			SDCommand(MASK_ACMD6, 0x00000002, response);
			*P_SD_MODE_CTRL = MASK_C_BlockLen_512bytes|MIN_ClockSpeedFactor|MASK_C_ENSDBus|MASK_C_DMAMode|MASK_C_BUSWIDTH_4;
		}
	
		else
			*P_SD_MODE_CTRL = MASK_C_BlockLen_512bytes|MIN_ClockSpeedFactor|MASK_C_ENSDBus|MASK_C_DMAMode;
	}

	SDCommand(MASK_CMD13, rca, response);

	*P_SD_MODE_CTRL = MASK_C_BlockLen_512bytes|(*P_SD_MODE_CTRL&0xffff);
	
	gSDCardPresent=1;		// SDCard Initial Success, set gSDCardPresent to 1
	
	return 0;
}

void DrvSDCClockEn(void)
{
	*P_SD_CLK_CONF = 0x00000003;		// SDCard Clock Enable
 	*P_NAND_GPIO_PULL = 0xf819f819;		// P_NFLASH_GPIO_PUPD for SDCard Setting
 	*P_NAND_GPIO_SETUP = 0x00200020;		// Nand CS Output High
 	*P_SD_INTERFACE_SEL = 0x00010000;		// SFTCFG SDCard Enable
}

void SDDrv_PowerOn(void)
{
	*P_CSI_GPIO_SETUP |= 0x00000004;
}

void SDDrv_PowerOff(void)
{
	gSDCardInsert = 0;
	gSDCardPresent = 0;
	gSDCardPowerRecycleState = 0;
	
	*P_CSI_GPIO_SETUP &= ~(0x00000004);		// Turn power off
	*P_SD_MODE_CTRL = 0x00;					// Disable SD Controller
	
	*P_NAND_GPIO_SETUP &= ~(0x00000786);	// Set SD CLK, CMD, D0~3 as output low
	*P_NAND_GPIO_SETUP |= 0x07860000;
	
}


void DrvSDCPowerRecycle(void)
{
	
	if(gSDCardPowerRecycleState == 0x00)
	{
		if((*P_CSI_GPIO_INPUT & 0x00000001) == 0)		// Check if SD Card is inserted or not
			gSDCardPowerRecycleState = 0x01;			// change to next state
	}
	else if(gSDCardPowerRecycleState == 0x01)			// Set CMD, CLK, D0~3 as input mode
	{
		*P_NAND_GPIO_SETUP &= ~(0x07860000);
		SDDrv_PowerOn();
		gSDCardPowerRecycleState = 0x02;				// change to next state
	}
	else if(gSDCardPowerRecycleState == 0x02)			// Set gSDCardInsert = 1
	{
		gSDCardInsert = 1;
	}
	
	
	if((gSDCardInsert == 1) && ((*P_CSI_GPIO_INPUT & 0x00000001) != 0))	// Card Removed
		SDDrv_PowerOff();
	
	
	
	if((*P_CSI_GPIO_INPUT & 0x00000002) == 0)
		gSDCardWriteProtect = 1;
	else
		gSDCardWriteProtect = 0;
	
}

⌨️ 快捷键说明

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