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

📄 main_save.c

📁 基于U(375)盘及SD(SPI模式)卡集成文件系统(包括底层驱动)
💻 C
📖 第 1 页 / 共 2 页
字号:
	temp[0] = SPI_TransferByte(MMC_APP_CMD);
    temp[1] = SPI_TransferByte(0x00);  //39:32  reserve;
    temp[2] = SPI_TransferByte(0x00);  //31:24  reserve;
    temp[3] = SPI_TransferByte(0x00);  //23:20  reserve;  19:16 VHS
    temp[4] = SPI_TransferByte(0x00);  //check pattern
    temp[5] = SPI_TransferByte(0xff);  //CRC
    for(i=0;i<10;i++)
    {
    	temp1 = SPI_TransferByte(0xff);
    	if(temp1&0x80)
    		i=10;
    }
    
    return temp;
}

unsigned char MMC_Init()
{
	unsigned char retry,temp;
	unsigned char temp1[100];
    unsigned char i;
    unsigned int j;
    for (i=0;i<20;i++)
    {
    	temp = SPI_TransferByte(0xff); //send 74 clock at least!!!
    }
    //复位为idle 
    temp1[0] = SPI_TransferByte(MMC_RESET);
    temp1[1] = SPI_TransferByte(0x00);
    temp1[2] = SPI_TransferByte(0x00);
    temp1[3] = SPI_TransferByte(0x00);
    temp1[4] = SPI_TransferByte(0x00);
    temp1[5] = SPI_TransferByte(0x95);
    temp1[6] = SPI_TransferByte(0xff);
    do{
    	temp = SPI_TransferByte(0xff);
    	}while(temp!=0x01);
	retry=0;
   do{	
		temp = Write_Command_MMC(MMC_APP_CMD,0);
		SPI_TransferByte(0xff);
		temp = Write_Command_MMC(MMC_ACMD_SEND_OPC,0);
		SPI_TransferByte(0xff);     
     }while(temp !=0);    //有response  response必须为0才表示数据写成功	
    return (1);
}



void MMC_HardwareInitial()
{
	return;
}

unsigned char MMC_read_sector(unsigned int addr,unsigned char *Buffer)
{
	unsigned char temp;
	unsigned int i;
	
	SPI_TransferByte(0xff);
	temp = Write_Command_MMC(MMC_READ_BLOCK,addr<<9);
	if(temp != 0x00)
	{
		return(READ_BLOCK_ERROR);
	}
	
	while(SPI_TransferByte(0xff) != 0xfe);
	
	for(i=0;i<512;i++)
	{
		*Buffer++ = SPI_TransferByte(0xff);
	}
	SPI_TransferByte(0xff);
	SPI_TransferByte(0xff);
	
	return (1);
}



unsigned char MMC_Write_sector(unsigned int sector, unsigned char *buf)
{
	unsigned char *p, by;
 	unsigned int i,flag;
 	unsigned int startadr;
 	unsigned int inv1,inv2;
 	unsigned char temp;
 	p = buf; 
 	testcount1 = 0;
 	testcount2 = 0;
 	startadr = sector*(unsigned int)BYTE_PER_SEC;
 	inv2 = TIMER_getCount(htimer1);
 	temp = Write_Command_MMC (MMC_WRITE_BLOCK,startadr);  //write command
 	transfertime2 = TIMER_getCount(htimer1) - inv2 - overhead;
 	SPI_TransferByte(0xFF);                        //wait at least 8 clock;
 	SPI_TransferByte(0xFF);
	///////data transfer/////////////
	//1			1111	1110
	//2-513		data	block
	//514-515	CRC
	////////////////////////////
	inv1 = TIMER_getCount(htimer1);
 	SPI_TransferByte(0xFE);                     //first byte
 	for(i=0; i<BYTE_PER_SEC; i++)               //512 data block
 	{
  	//	 SPI_TransferByte(*p++);
  		while (!MCBSP_xrdy(myMcbsp));                      
    	MCBSP_write(myMcbsp,p[i]); 
    	while(!MCBSP_rrdy(myMcbsp));
	//	MCBSP_read(myMcbsp); 
  	}
    MCBSP_read(myMcbsp);
 	SPI_TransferByte(0xFF);						//last two crc          
 	SPI_TransferByte(0xFF);
 	transfertime1 = TIMER_getCount(htimer1) - inv1 - overhead;
	//////data transfer response /////
	inv1 = TIMER_getCount(htimer1);
  	do
 	{
 		flag = SPI_TransferByte(0xFF);    //data transfer response: x x x 0 0 1 0 1  accept
 		by=flag & 0x1F;                   //                        x x x 0 1 0 1 1  CRC error 
 		testcount1++;
 	}while(by != 0x05);                   //                        x x x 0 1 1 0 1  reject
 	transfertime3 = TIMER_getCount(htimer1) - inv1 - overhead;
	////
	inv1 = TIMER_getCount(htimer1);
 	do
  	{
  		flag = SPI_TransferByte(0xFF);
  		testcount2++;
  	}while(flag !=0xFF);                  // 0xFF indicate the bus is in idle
  											//0xFE indicate busy
  	transfertime4 = TIMER_getCount(htimer1) - inv1 - overhead;
 	return 0;
}

void check_test(void)
{
	int i;
	
	for(i=0;i<512;i++)
    	buff_w[i] = i;
//    MMC_Erase_MultiSector(5,2);
    start = TIMER_getCount(htimer1);
    
	MMC_Write_sector(5,buff_w);					//139560*8 cpu clock   183k byte/s
												//126465
	writetime = TIMER_getCount(htimer1) - start - overhead;
//	MMC_Write_MultiSector(6,10, buff_w);
	start = TIMER_getCount(htimer1);
	MMC_read_sector(5,buff_r);                 	//58902*8 cpu clock     430k byte/s
												//34782
	readtime = TIMER_getCount(htimer1) - start - overhead;
	
	for(i=0;i<512;i++)
	{
		if(buff_w[i] != buff_r[i])
		{
			printf("SD/MMC TEST ERROR!\r\n");
			break;
		}
	}
	printf("SD/MMC TEST OK!\r\n");

}
/*
void TestReadRegister(unsigned char cmd, unsigned int addr, unsigned char* Buffer)
{
	unsigned char temp;
	unsigned int i;
	
	SPI_TransferByte(0xff);
	temp = Write_Command_MMC(cmd,addr<<9);
	if(temp != 0x00)
	{
		return(READ_BLOCK_ERROR);
	}
	
	while(SPI_TransferByte(0xff) != 0xfe);
	
	for(i=0;i<2;i++)
	{
		*Buffer++ = SPI_TransferByte(0xff);
	}
	SPI_TransferByte(0xff);
	SPI_TransferByte(0xff);
	
	return (1);
}
*/
unsigned char testocr()
{
	unsigned char temp , i;
	unsigned char OCRBuffer[4], CIDBuffer[2], CSDBuffer[2];
	SPI_TransferByte(0xff);
	temp = Write_Command_MMC(MMC_READ_OCR,0);
	if(temp != 0x00)
	{
		return(READ_BLOCK_ERROR);
		}
	for(i=0;i<4;i++)
	{
		OCRBuffer[i] = SPI_TransferByte(0xff);
	}
	
	SPI_TransferByte(0xff);
	temp = Write_Command_MMC(MMC_READ_CID,0);
	if(temp != 0x00)
	{
		return(READ_BLOCK_ERROR);
		}
	for(i=0;i<2;i++)
	{
		CIDBuffer[i] = SPI_TransferByte(0xff);
	}
	
	SPI_TransferByte(0xff);
	temp = Write_Command_MMC(MMC_READ_CSD,0);
	if(temp != 0x00)
	{
		return(READ_BLOCK_ERROR);
		}
	for(i=0;i<2;i++)
	{
		CSDBuffer[i] = SPI_TransferByte(0xff);
	}
	return 1;
}

void testMMC()
{

}

void check_security()
{
	unsigned char temp;
	unsigned char temp1[1000];
	int i;
	temp = 10;
	do{
		temp = Write_Command_MMC(MMC_APP_CMD,0);
		SPI_TransferByte(0xff);
		temp = Write_Command_MMC(MMC_ACMD44,0);	
		do
		{
			temp=SPI_TransferByte(0xff);
		}while(temp==0xff);
		for(i=0;i<100;i++)
			temp1[i] = SPI_TransferByte(0xff);
		SPI_TransferByte(0xff);
	}while(temp!=0);
}
void SwiMainFunc() {

  TIMER_Config MyTimerConfig = {   	0x000002C0, /* ctl */   
  									0xFFFFFFFF, /* prd */   
  									0x00000000  /* cnt */ 
  								};

 

  /* The following code                                         */
  /* configures the serial port for digital loopback mode. We   */
  /* then use the CPU to write/read from the port. In loopback  */
  /* mode, we should read the same value as written, this is    */
  /* checked for in a loop.                                     */
  
  /* Let's open up serial port 1 */
  myMcbsp = MCBSP_open(MCBSP_DEV0, MCBSP_OPEN_RESET);

  MCBSP_config(myMcbsp , &myInitialConfig );
  /* Now that the port is setup, let's enable it in steps. */

  
  MCBSP_enableSrgr(myMcbsp);
  printf("wait for two clock");
  MCBSP_enableRcv(myMcbsp);
  MCBSP_enableXmt(myMcbsp);
  
  
  
  htimer1 = TIMER_open(TIMER_DEVANY,0);
  TIMER_config(htimer1,&MyTimerConfig);
  start = TIMER_getCount(htimer1);
  end = TIMER_getCount(htimer1);
  overhead = end - start ; 
  
    MMC_HardwareInitial();
	MMC_Init();
	MCBSP_config(myMcbsp, &myNormalConfig);
	MCBSP_enableSrgr(myMcbsp);
  	printf("wait for two clock");
  	MCBSP_enableRcv(myMcbsp);
  	MCBSP_enableXmt(myMcbsp);
//	testocr();
//	TestReadRegister(address);
//	check_security();
	while(1)
		check_test();
 
  
 
  /* All done now, close the port. */
  MCBSP_close(myMcbsp);
  LOG_printf(&LogMain,"<DONE>");
}

/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
void main() {

  /* Initialize the chip support library, required */
  CSL_init();
 // SwiMainFunc();
  SWI_post(&SwiMain);
}  

/*----------------------------------------------------------------------------*/
/******************************************************************************\
* End of main.c
\******************************************************************************/

⌨️ 快捷键说明

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