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

📄 mmc_sd.c

📁 在mtk平台上写的gpio口模拟spi协议的SD卡读卡器
💻 C
📖 第 1 页 / 共 2 页
字号:
		//	signal_fsem(SD_DI_hisr_PID);
		//}
	}
		SETBIT(SSC0RIC, 6);   	
	//	PCL_37=GPIO_PIN | OUTPUT_PIN | DATA_PIN(1);
}

/****************************************************************************************
* Function:.... SD_SSC_Tx_lisr
* Parameters:
* Returns:..... 
* Description: SPI RX中断服务程序
* Created:.... zhangnu
* Modified:.... 2007-11-02
****************************************************************************************/
OS_PROCESS(SD_SSC_tx_lisr)
{

	RESETBIT(SSC0TIC, 6);    		// disable int
  	RESETBIT(SSC0TIC, 7);   		//clear int request flags
  	//PCL_37=GPIO_PIN | OUTPUT_PIN | DATA_PIN(0);
	SSC_tx_lisr_tp_value++; 		//for test only

	
	
	//SETBIT(SSC0TIC, 6);   	
	//PCL_37=GPIO_PIN | OUTPUT_PIN | DATA_PIN(1);
}
#endif
//sd卡初始化		//sd card initialize
extern void MMC_SD_Init(void)
{
	SPI_INIT();
	SPI_CS_Deassert();
}

extern int SD_main(void)
{
	unsigned char i;
	unsigned long  Cap;
	unsigned char retry = 0;
	
	MMC_SD_Init();
	MMC_PRINTF("init sd card\n");
	while(MMC_SD_Reset())// retry 100 times
	{
	       MMC_PRINTF("retry=%d\n",retry);
		retry++;
		if(retry>100)break;
	}
	if(retry<100)//if success
	{

		//读SD卡容量
		MMC_PRINTF("sd cap\n");
		Cap = (MMC_SD_ReadCapacity()/512)-1;//读到的是总容量,不是能寻址到的扇区地址,所以要减1处理
		DISK_CAPACITY[0] = ((unsigned char *)(&Cap))[3];//转成大端格式
		DISK_CAPACITY[1] = ((unsigned char *)(&Cap))[2];
		DISK_CAPACITY[2] = ((unsigned char*)(&Cap))[1];
		DISK_CAPACITY[3] = ((unsigned char *)(&Cap))[0];
	}
       MMC_PRINTF("main capover\n");
	//while(1);
	return 0;
}

//sd卡写命令		//sd send command
extern unsigned char MMC_SD_SendCommand(unsigned char cmd, unsigned long arg)
{
	unsigned char r1;
	unsigned char retry=0;
	
	SPI_WriteByte(0xff);
	//con_DelayMs(10);
	SPI_WriteByte(0xff);
	//con_DelayMs(10);
	SPI_WriteByte(0xff);
	//con_DelayMs(10);
	SPI_WriteByte(0xff);
	//con_DelayMs(10);
	SPI_WriteByte(0xff);
	//con_DelayMs(10);
	SPI_WriteByte(0xff);
	//con_DelayMs(10);

	SPI_CS_Assert();
	
	SPI_WriteByte(cmd | 0x40);//分别写入命令	//send command
	//con_DelayMs(10);
	SPI_WriteByte(arg>>24);
	//con_DelayMs(10);
	SPI_WriteByte(arg>>16);
	//con_DelayMs(10);
	SPI_WriteByte(arg>>8);
	//con_DelayMs(10);
	SPI_WriteByte(arg);
	//con_DelayMs(10);
	SPI_WriteByte(0x95);
	//con_DelayMs(10);
	
	while((r1 = SPI_WriteByte(0xff)) == 0xff)//等待响应,	//wait response
		{
	       //MMC_PRINTF("sd sendcommand ri=%x\n",r1);
		if(retry++ > 100) break;//超时退出					//time out error
		}
	SPI_CS_Deassert();
       MMC_PRINTF("MMC_SD_SendCommand return r1=%d\n",r1);
	return r1;//返回状态值					//return state
}

//sd卡复位		//reset sd card (software)
extern unsigned char MMC_SD_Reset(void)
{
	unsigned char i;
	unsigned char retry;
	unsigned char r1=0;
	retry = 0;
	SPI_Low();
       MMC_PRINTF("sd reset\n");
	do
	{
		for(i=0;i<100;i++) SPI_WriteByte(0xff);
		r1 = MMC_SD_SendCommand(0, 0);//发idle命令	//send idle command
		retry++;
		if(retry>10) return 1;//超时退出		//time out
	} while(r1 != 0x01);	


	retry = 0;
	do
	{
		r1 = MMC_SD_SendCommand(1, 0);//发active命令	//send active command
		retry++;
		if(retry>100) return 1;//超时退出		//time out
	} while(r1);
	SPI_High();
	r1 = MMC_SD_SendCommand(59, 0);//关crc		//disable CRC

	r1 = MMC_SD_SendCommand(16, 512);//设扇区大小512	//set sector size to 512
	return 0;//正常返回		//normal return
}

//读一个扇区		//read one sector
extern unsigned char MMC_SD_ReadSingleBlock(unsigned long sector, unsigned char* buffer)
{
	unsigned char r1;
	unsigned int i;
	unsigned char retry=0;

	do
	{
		r1 = MMC_SD_SendCommand(17, sector<<9);//读命令	//read command
		retry++;
		if(retry>10) return 1;//超时退出		//time out
	} while(r1 != 0x00);	


	SPI_CS_Assert();
	//等数据的开始	//wait to start recieve data
	while(SPI_WriteByte(0xff) != 0xfe);//if(retry++ > 50){SPI_CS_Deassert();return 1;}

	for(i=0; i<512; i++)//读512个数据	//read 512 bytes
	{
		*buffer++ = SPI_WriteByte(0xff);
	}

	SPI_WriteByte(0xff);//伪crc
	SPI_WriteByte(0xff);
	
	SPI_CS_Deassert();

	return 0;
}


//写一个扇区		//wirite one sector //not used in this application
extern unsigned char MMC_SD_WriteSingleBlock(unsigned long sector, unsigned char* buffer)
{
	unsigned char r1;
	unsigned int i;
	unsigned char retry=0;

	do
	{
		r1 = MMC_SD_SendCommand(24, sector<<9);//写命令	//send command
		retry++;
		if(retry>10) return 1;//超时退出		//time out
	} while(r1 != 0x00);	



	SPI_CS_Assert();
	
	SPI_WriteByte(0xff);
	SPI_WriteByte(0xff);
	SPI_WriteByte(0xff);
	SPI_WriteByte(0xff);
	SPI_WriteByte(0xff);
	SPI_WriteByte(0xff);

	SPI_WriteByte(0xfe);//发开始符			//send start byte
	
	for(i=0; i<512; i++)//送512字节数据		//send 512 bytes data
	{
		SPI_WriteByte(*buffer++);
	}
	
	SPI_WriteByte(0xff);
	SPI_WriteByte(0xff);
	
	r1 = SPI_WriteByte(0xff);
	
	if( (r1&0x1f) != 0x05)//等待是否成功	//judge if it successful
	{
		SPI_CS_Deassert();
		return r1;
	}
	//等待操作完		//wait no busy
	while(!SPI_WriteByte(0xff));//if(retry++ > 50){SPI_CS_Deassert();return 1;}

	SPI_CS_Deassert();

	return 0;
}

extern unsigned long MMC_SD_ReadCapacity(void)
{
	unsigned char r1;
	unsigned int i;
	unsigned int temp;
	unsigned char buffer[16];
	unsigned long Capacity;
	//unsigned char retry=0;

	r1 = MMC_SD_SendCommand(9, 0);//写命令	//send command  //READ CSD
	if(r1 != 0x00)
		return r1;

	SPI_CS_Assert();
	while(SPI_WriteByte(0xff) != 0xfe);
	
	for(i=0;i<16;i++)
	{
		buffer[i]=SPI_WriteByte(0xff);
	}	

	SPI_WriteByte(0xff);
	SPI_WriteByte(0xff);
	
	SPI_WriteByte(0xff);
	
	SPI_CS_Deassert();

/*********************************/
//	C_SIZE
	i = buffer[6]&0x03;
	i<<=8;
	i += buffer[7];
	i<<=2;
	i += ((buffer[8]&0xc0)>>6);

/**********************************/
//  C_SIZE_MULT

	r1 = buffer[9]&0x03;
	r1<<=2;
	r1 += ((buffer[10]&0x80)>>7);


/**********************************/
// BLOCKNR

	r1+=2;

	temp = 1;
	while(r1)
	{
		temp*=2;
		r1--;
	}
	
	Capacity = ((unsigned long)(i+1))*((unsigned long)temp);

/////////////////////////
// READ_BL_LEN

	i = buffer[6]&0x0f;

/*************************/
//BLOCK_LEN

	temp = 1;
	while(i)
	{
		temp*=2;
		i--;
	}
/************************/


/************** formula of the capacity ******************/
//
//  memory capacity = BLOCKNR * BLOCK_LEN
//	
//	BLOCKNR = (C_SIZE + 1)* MULT
//
//           C_SIZE_MULT+2
//	MULT = 2
//
//               READ_BL_LEN
//	BLOCK_LEN = 2
/**********************************************/

//The final result
	
	Capacity *= (unsigned long)temp;	 
	return Capacity;		
}

⌨️ 快捷键说明

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