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

📄 sdcarddriver.c

📁 凌阳32位单片机开发的小游戏
💻 C
📖 第 1 页 / 共 2 页
字号:
	return 0;
}

S32		DrvSDCWriteMulData(U8 *outaddr)
{
	U32 	i;
	
	i = 0;
	while((*P_SDC_Status & MASK_S_CardBusy) != 0x00)
	{
		i++;
		if(i == 0xFFFFFF)
			return 1;
	}
	
	i=(U32) outaddr;
	if((i & 0x03) == 0)
	{
		// USE DMA
		*P_DMA_CR1 = 0x0000;
		
		*P_DMA_AHB_SA1A = (U32)outaddr;
		*P_DMA_AHB_EA1A = (U32)outaddr + ( 512 ) - 4;
		*P_DMA_APB_SA1 = 0x88180000;
		*P_DMA_CR1 = 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 & 0x1) == 0)
		{
			if(!gSDCardInsert)
				return 1;
		}
	
		// Clear Interrupt and Disable DMA channel
		*P_DMA_INT = 0x1;
		*P_DMA_CR1 = 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_SDC_DataTx=Data;
			
			DataCount--;
		}while(DataCount != 0x00);
	}
	
	i = 0;
	while((*P_SDC_Status & MASK_S_DataComplete) == 0x00)
	{
		i++;
		if( i == 20000)
			return 1;
	}
	*P_SDC_Status = MASK_S_ClrAllBits;
	
	return 0;
}

S32		DrvSDCWriteMulData_INT(U8 *outaddr)
{
	U32 	i;
	
	i = 0;
	while((*P_SDC_Status & MASK_S_CardBusy) != 0x00)
	{
		i++;
		if(i == 0xFFFFFF)
			return 1;
	}
	
	
	// USE DMA
	*P_DMA_CR1 = 0x0000;
	
	*P_DMA_AHB_SA1A = (U32)outaddr;
	*P_DMA_AHB_EA1A = (U32)outaddr + (1 << 9) - 4;
	*P_DMA_APB_SA1 = 0x88180000;
	*P_DMA_CR1 = DMA_M2P|DMA_REQ|DMA_FIX|DMA_SINGLE_BUF|DMA_32BIT_BURST|DMA_IRQON|DMA_ON;
	
	// Clear Interrupt and Disable DMA channel
//	*P_DMA_INT = 0x1;
//	*P_DMA_CR1 = 0x0000;
	
	
//	i = 0;
//	while((*P_SDC_Status & MASK_S_DataComplete) == 0x00)
//	{
//		i++;
//		if( i == 20000)
//			return 1;
//	}
//	*P_SDC_Status = MASK_S_ClrAllBits;
	
	return 0;
}

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

	// Loop until controller idle
	while(*P_SDC_Status & MASK_S_ControllerBusy);
	
	*P_SDC_Status = MASK_S_ControllerBusy;
	
	// Stop the card
	if(SDCommand(MASK_CMD12, 0x00000000, response))
		return 1;
		
	i = 0;
	do {
		
		*P_SDC_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 WriteSectors(U32 block, U32 blocknum, U8 *outaddr)
//S32 SDDrv_WriteSector(U32 block, U32 blocknum, U8 *outaddr)
{
	U32 	i;
	U32 	response[4];

	i=(U32) outaddr;
	if((i & 0x03) == 0)
	{
		// USE DMA
		*P_DMA_AHB_SA1A = (U32)outaddr;
		*P_DMA_AHB_EA1A = (U32)outaddr + (blocknum<<9) - 4;
		*P_DMA_APB_SA1 = 0x88180000;
		*P_DMA_CR1 = 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 & 0x1) == 0)
		{
			if(!gSDCardInsert)
				return 1;
		}
	
		// Clear Interrupt and Disable DMA channel
		*P_DMA_INT = 0x1;
		*P_DMA_CR1 = 0x0000;
		
	}
	else
	{
		// Do not USE DMA
		
		U32	DataCount;
		U32	Data;
		int block_cnt;
		
		for(block_cnt=0; block_cnt<blocknum; block_cnt++)
		{
			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_SDC_DataTx=Data;
				
				DataCount--;
			}while(DataCount != 0x00);
			
			if (WaitSDStatus(MASK_S_DataComplete)) {
				*P_SDC_Status = MASK_S_ClrAllBits;
					return 1;
			}
		}
	}

	if (WaitSDStatus(MASK_S_DataComplete)) {
		*P_SDC_Status = MASK_S_ClrAllBits;
		return 1;
	}
	
	*P_SDC_Status = MASK_S_DataComplete;
	
	// Stop the controller
	*P_SDC_Command = MASK_CMDSTOP;

	// Loop until controller idle
	while(*P_SDC_Status & MASK_S_ControllerBusy);

	*P_SDC_Status = MASK_S_ControllerBusy;
	
	do
	{
		*P_SDC_Status=MASK_S_ClrAllBits;
		if(SDCommand(MASK_CMD13, rca, response))
			return 1;
	}while((response[0] & 0x1e00) != 0x0c00);
	
	// Stop the card
	if(SDCommand(MASK_CMD12, 0x00000000, response))
		return 1;
		
	i = 0;
	do {
		
		*P_SDC_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)
{
	/*
	U32 response[4];
	
	U32 c_size, mult, capacity, blocklen;

	SDCommand(MASK_CMD7, 0, response);

	SDCommandr2(MASK_CMD9, rca, response);

	blocklen = ((response[1] & 0x000F0000)>>16)-8;
	c_size = ((response[1] & 0x000003FF)<<2) + (response[2]>>30);
	mult = ((response[2] & 0x00030000)>>15) + ((response[2] & 0x0000FFFF)>>15);
	
	capacity = (blocklen*(c_size+1))<<(mult + 2);

 	SDCommand(MASK_CMD7, rca, response);
	return capacity;
	*/
	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_SDC_IntEn = 0x0000;
	
	// Step 1, Reset SD Controller
	*P_SDC_Control = MASK_C_BlockLen_512bytes|INIT_ClockSpeedFactor|MASK_C_ENSDBus|MASK_C_DMAMode;
	
	// Clear All Command
	*P_SDC_Command = MASK_CMDSTOP;
	while (*P_SDC_Status & 0x1);
	
	// Clear All Status
	*P_SDC_Status = MASK_S_ClrAllBits;
	
	loopcnt = 0;
	
	// Step 2, Start 74 cycles on SD Clk Bus
	*P_SDC_Command = MASK_ClockCycle74;
	
	if (WaitSDStatus(MASK_S_CmdComplete)) 
		return 1;

	*P_SDC_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];
	} 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_SDC_Control = MASK_C_BlockLen_512bytes|MIN_ClockSpeedFactor|MASK_C_ENSDBus|MASK_C_DMAMode;
	else 
		//Increase Clock Speed
		*P_SDC_Control = 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_SDC_Control = MASK_C_BlockLen_8bytes|(*P_SDC_Control&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_SDC_Control = MASK_C_BlockLen_512bytes|MIN_ClockSpeedFactor|MASK_C_ENSDBus|MASK_C_DMAMode|MASK_C_BUSWIDTH_4;
		}
	
		else
			*P_SDC_Control = MASK_C_BlockLen_512bytes|MIN_ClockSpeedFactor|MASK_C_ENSDBus|MASK_C_DMAMode;
	}

	SDCommand(MASK_CMD13, rca, response);

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

⌨️ 快捷键说明

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