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

📄 usbrx.c

📁 mx21的NAND Flash Bootloader源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
		else		//Y Buffer			EpStartAddress = (((*(U32 *)(OTG_EP_BASE+(16*3)+4))>>16)&0x0000FFFF);		DataBuf=(U32 *)(OTG_DATA_BASE + EpStartAddress);		//write data to buffer	#if 1		*(DataBuf++) = 0x00050070;	    *(DataBuf++) = 0x0c000000;	    *(DataBuf++) = 0;	    *(DataBuf++) = 0x24;	    *((U16 *)DataBuf)  = 0;#else		*(DataBuf++) = 0x70000500;	    *(DataBuf++) = 0x0000000C;	    *(DataBuf++) = 0;	    *(DataBuf++) = 0x20000000;	    *((U16 *)DataBuf)  = 0;#endif		ClearXYBufferInt();		SetClearFillStatus(EP1IN);		Ready_EP_IN(EP1);		//ready EP1		//check for EP1 IN transfer done		while (!(*(U32 *)OTG_FUNC_EP_DSTAT & EP1IN));		//clear EP1 IN transfer done 		*(U32 *)OTG_FUNC_EP_DSTAT = EP1IN;		ClearXYBufferInt();		unready_ep_in(EP1);		EPConfig(EP1, EP_IN_DIR, EP_NO_STALL,  0 , 32, BLK_TYPE, 0x100, 0x200, 32, 13);		DataBuf=(U32 *)(OTG_DATA_BASE + EpStartAddress);		// now the CSW	    *(DataBuf++) =0x53425355; // signature	    tmp = 0;	    tmp |= _gCBW[7];	    tmp<<=8;	    tmp |= _gCBW[6];	    tmp<<=8;	    tmp |= _gCBW[5];	    tmp<<=8;	    tmp |= _gCBW[4];	    *(DataBuf++) = tmp;		*(DataBuf++) = 0; 		//1 byte	    *((U8 *)DataBuf) = 0; //status: pass		ClearXYBufferInt();       	SetClearFillStatus(EP1IN);		Ready_EP_IN(EP1);		//ready EP1		//check for EP1 IN transfer done		while (!(*(U32 *)OTG_FUNC_EP_DSTAT & EP1IN));		//clear EP1 IN transfer done 		*(U32 *)OTG_FUNC_EP_DSTAT = EP1IN;		ClearXYBufferInt();		unready_ep_in(EP1);		break;		case 0x25:	// READ CAPACITY				//Disable_EP_IN(EP2);	//disable EP0 IN		EPConfig(EP1, EP_IN_DIR,   0,  0, 32, BLK_TYPE, 0x100, 0x200, 32, 8);		//read the Start Address (xbsa, ybsa)		if (EpNextBufToService & (EP1IN))	//X Buffer			EpStartAddress = ((*(U32 *)(OTG_EP_BASE+(16*3)+4))&0x0000FFFF);		else		//Y Buffer			EpStartAddress = (((*(U32 *)(OTG_EP_BASE+(16*3)+4))>>16)&0x0000FFFF);		DataBuf=(U32 *)(OTG_DATA_BASE + EpStartAddress);		//write data to buffer	#if 0		*(DataBuf++) = DISK_LAST_SECTOR;	// last sector of disk	    *DataBuf = 0x200; // block size#else		*(DataBuf++) = /*0xff6f0000;*/ 0xdfff0000;	// last sector of disk	    *DataBuf = 0x00020000; // block size#endif		ClearXYBufferInt();       	SetClearFillStatus(EP1IN);		Ready_EP_IN(EP1);		//ready EP1		//check for EP1 IN transfer done		while (!(*(U32 *)OTG_FUNC_EP_DSTAT & EP1IN));		//clear EP0 IN transfer done 		*(U32 *)OTG_FUNC_EP_DSTAT = EP1IN;		ClearXYBufferInt();		unready_ep_in(EP1);		    	     // now the CSW		EPConfig(EP1, EP_IN_DIR,   0,  0, 32, BLK_TYPE, 0x100, 0x200, 32, 13);		DataBuf=(U32 *)(OTG_DATA_BASE + EpStartAddress);        *(DataBuf++) = 0x53425355; // signature	    tmp = 0;	    tmp |= _gCBW[7];	    tmp<<=8;	    tmp |= _gCBW[6];	    tmp<<=8;	    tmp |= _gCBW[5];	    tmp<<=8;	    tmp |= _gCBW[4];	    *(DataBuf++) = tmp;	    *(DataBuf++) = 0; //data residue	    *((U8 *)DataBuf) = 0;        	        		ClearXYBufferInt();       	SetClearFillStatus(EP1IN);		Ready_EP_IN(EP1);		//ready EP1		//check for EP1 IN transfer done		while (!(*(U32 *)OTG_FUNC_EP_DSTAT & EP1IN));		//clear EP0 IN transfer done 		*(U32 *)OTG_FUNC_EP_DSTAT = EP1IN;		ClearXYBufferInt();		unready_ep_in(EP1);		//ready EP1		break;				case 0x28:	// READ		// extract start sector		_gUsbCurSector = 0;		for (i=0; i<4; i++)			_gUsbCurSector = (_gUsbCurSector << 8) | (U32)_gCBW[17+i];		// extract number of sectors		_gUsbNumSector = (((U32)_gCBW[22]) << 8) | ((U32)_gCBW[23]);		_gUsbPacketCount = _gUsbNumSector * 16;		_gpUsbBufPtr = (P_U32)(USB_DISK_START + _gUsbCurSector * 512);			//Disable_EP_IN(EP2);	//disable EP0 IN		EPConfig(EP1, EP_IN_DIR,   0,  0, 32, BLK_TYPE, 0x100, 0x200, 32, 32);		//read the Start Address (xbsa, ybsa)		if (EpNextBufToService & (EP1IN))	//X Buffer			EpStartAddress = ((*(U32 *)(OTG_EP_BASE+(16*3)+4))&0x0000FFFF);		else		//Y Buffer			EpStartAddress = (((*(U32 *)(OTG_EP_BASE+(16*3)+4))>>16)&0x0000FFFF);		for (;_gUsbPacketCount;_gUsbPacketCount--)		{			EPConfig(EP1, EP_IN_DIR,   0,  0, 32, BLK_TYPE, 0x100, 0x200, 32, 32);			DataBuf=(U32 *)(OTG_DATA_BASE + EpStartAddress);			// Send a package once a loop			*(DataBuf++) = *(_gpUsbBufPtr++);			*(DataBuf++) = *(_gpUsbBufPtr++);			*(DataBuf++) = *(_gpUsbBufPtr++);			*(DataBuf++) = *(_gpUsbBufPtr++);			*(DataBuf++) = *(_gpUsbBufPtr++);			*(DataBuf++) = *(_gpUsbBufPtr++);			*(DataBuf++) = *(_gpUsbBufPtr++);			*(DataBuf++) = *(_gpUsbBufPtr++);			SetClearFillStatus(EP1IN);			ClearXYBufferInt();			Ready_EP_IN(EP1);		//ready EP1			//check for EP1 IN transfer done			while (!(*(U32 *)OTG_FUNC_EP_DSTAT & EP1IN));			//clear EP1 IN transfer done 			*(U32 *)OTG_FUNC_EP_DSTAT = EP1IN;			ClearXYBufferInt();			//un ready the EP1IN			unready_ep_in(EP1);				}				//now the CSW		EPConfig(EP1, EP_IN_DIR,   0,  0, 32, BLK_TYPE, 0x100, 0x200, 32, 13);	    *(DataBuf++) = 0x53425355; // signature	    tmp = 0;	    tmp |= _gCBW[7];	    tmp<<=8;	    tmp |= _gCBW[6];	    tmp<<=8;	    tmp |= _gCBW[5];	    tmp<<=8;	    tmp |= _gCBW[4];	    *(DataBuf++) = tmp;	    *(DataBuf++) = 0; // data residue	    *((U8 *)DataBuf) = 0;			ClearXYBufferInt();		SetClearFillStatus(EP1IN);		Ready_EP_IN(EP1);		//ready EP1		//check for EP1 IN transfer done		while (!(*(U32 *)OTG_FUNC_EP_DSTAT & EP1IN));		//clear EP1 IN transfer done 		*(U32 *)OTG_FUNC_EP_DSTAT = EP1IN;		ClearXYBufferInt();		//un ready the EP1IN		unready_ep_in(EP1);		break;		case 0x00://EUARTputString("0x00\n ");	// NULL COMMAND, return CSW with pass		case 0x1E://EUARTputString("0x1e\n ");	// PREVENT-ALLOW MEDIUM REMOVAL, return CSW with pass		case 0x2F://EUARTputString("0x2f\n ");	// VERIFY		EPConfig(EP1, EP_IN_DIR,   0,  0, 32, BLK_TYPE, 0x100, 0x200, 32, 13);		//read the Start Address (xbsa, ybsa)		if (EpNextBufToService & (EP1IN))	//X Buffer			EpStartAddress = ((*(U32 *)(OTG_EP_BASE+(16*3)+4))&0x0000FFFF);		else		//Y Buffer			EpStartAddress = (((*(U32 *)(OTG_EP_BASE+(16*3)+4))>>16)&0x0000FFFF);		DataBuf=(U32 *)(OTG_DATA_BASE + EpStartAddress);		//write data to buffer		        // now the CSW	        *(DataBuf++) = 0x53425355; // signature	        tmp = 0;	        tmp |= _gCBW[7];	        tmp<<=8;	        tmp |= _gCBW[6];	        tmp<<=8;	        tmp |= _gCBW[5];	        tmp<<=8;	        tmp |= _gCBW[4];	        *(DataBuf++) = tmp;	        *(DataBuf++) = 0;	        *((U8 *)DataBuf) = 0;	        	        //Set/Clear Fill Status		ClearXYBufferInt();       	SetClearFillStatus(EP1IN);		Ready_EP_IN(EP1);				//check for EP1 IN transfer done		while (!(*(U32 *)OTG_FUNC_EP_DSTAT & EP1IN));		//clear EP0 IN transfer done 		*(U32 *)OTG_FUNC_EP_DSTAT = EP1IN;		ClearXYBufferInt();		unready_ep_in(EP1);		break;		case 0x1B:	// START-STOP UNIT (when host EJECT)		EPConfig(EP1, EP_IN_DIR,   0,  0, 32, BLK_TYPE, 0x100, 0x200, 32, 13);		//read the Start Address (xbsa, ybsa)		if (EpNextBufToService & (EP1IN))	//X Buffer			EpStartAddress = ((*(U32 *)(OTG_EP_BASE+(16*3)+4))&0x0000FFFF);		else		//Y Buffer			EpStartAddress = (((*(U32 *)(OTG_EP_BASE+(16*3)+4))>>16)&0x0000FFFF);		DataBuf=(U32 *)(OTG_DATA_BASE + EpStartAddress);		//write data to buffer		        // now the CSW	        *(DataBuf++) = 0x53425355; // signature	        tmp = 0;	        tmp |= _gCBW[7];	        tmp<<=8;	        tmp |= _gCBW[6];	        tmp<<=8;	        tmp |= _gCBW[5];	        tmp<<=8;	        tmp |= _gCBW[4];	        *(DataBuf++) = tmp;	        *(DataBuf++) = 0;	        *((U8 *)DataBuf) = 0;	        	        //Set/Clear Fill Status		ClearXYBufferInt();       	SetClearFillStatus(EP1IN);		Ready_EP_IN(EP1);				//check for EP1 IN transfer done		while (!(*(U32 *)OTG_FUNC_EP_DSTAT & EP1IN));		//clear EP0 IN transfer done 		*(U32 *)OTG_FUNC_EP_DSTAT = EP1IN;		ClearXYBufferInt();		unready_ep_in(EP1);		quit = 1;		break;		case 0x2A:	// WRITE			// extract start sector			_gUsbCurSector = 0;			for (i=0; i<4; i++)				_gUsbCurSector = (_gUsbCurSector << 8) | (U32)_gCBW[17+i];			// extract number of sectors			_gUsbNumSector = (((U32)_gCBW[22]) << 8) | ((U32)_gCBW[23]);/*			EUARTputData('(');			EUARTputHex((U8)(_gUsbCurSector>>8));			EUARTputHex((U8)_gUsbCurSector);			EUARTputData('/');			EUARTputHex((U8)_gUsbNumSector);			EUARTputData(')');*/			_gUsbPacketCount = _gUsbNumSector * 16;			_gpUsbBufPtr = (P_U32)(USB_DISK_START + _gUsbCurSector * 512);			// check last sector			{				U32 lastSector = _gUsbCurSector + _gUsbNumSector - 1;				if (lastSector == DISK_LAST_SECTOR)		// host is formatting disk					gLastSector = 54;				else if (lastSector > gLastSector)					gLastSector = lastSector;			}			// write sectors			writeSectors();			//EUARTputString(".");			break;		default://			EUARTputString("Unknown command : 0x");//			EUARTputHex(_gCBW[15]);//			EUARTputString(" !\n");			break;	}}//********************************************************************// End of HandleDevReq.c//********************************************************************inline void FunctionSetup(void){//	int UsbPresent = 0;				// Check for USBOTG present or not!	// set to diff, diff and Configure the OTG to behave as function	*(VP_U32)OTG_CORE_HWMODE &= 0xFFFFFF3F;	*(VP_U32)OTG_CORE_HWMODE |= 0x2;	OtgFuncCommonInit();			//  Configure the OTG to behave as Func 			// Configure EP2 for BULK OUT	EPConfig(EP2,EP_OUT_DIR, EP_NO_STALL, EP_NO_SETUP, 32, BLK_TYPE, 0, 0, 32, 31);	Enable_EP(4);                          	Ready_EP_OUT(2);	// Configure EP1 for BULK IN	EPConfig(EP1, EP_IN_DIR,   0,  0, 32, BLK_TYPE, 0x100,  0x200,   32, 31);	Enable_EP(3);}// This poll-only driver make use of the last 16M of the SDRAM (i.e. 0x0B000000 - 0x0BFFFFFF) to// simulate a removable disk. The File Explorer on the Windows 2000 will see this 16M drive and copy// a single file to it.// The file will be stored starting from sector 55 and following consecutive sectors.// The file size is estimated by the number of sectors written.// There is one trip here: if the Windows 2000 File Explorer format this disk, their will be 2 cases:// 1. a true format - write every sector of disk// 2. a quick format - only FAT is written// For case 1, we reset the variable gLastSector to 54 if sector 0x7FFF is written// For case 2, we don't have to do anything since FAT is at sectors < 55.U32 usbrx(){	UsbInUse=1;				//force UsbInUse = 1 to enable USBOTG is in use	gLastSector = 76;		// to make the return value 0, if no file is downloaded	formatDrive();/*   // Initialize all peripheral in AIPI1 PSR[1:0] => 10=32bit, 01=16bit, 00=8bit	*(VP_U32)AIPI1_PSR0 = 0x00040304;	*(VP_U32)AIPI1_PSR1 = 0xFFFBFCFB;    // Initialize all peripheral in AIPI1 PSR[1:0] => 10=32bit, 01=16bit, 00=8bit	*(VP_U32)AIPI2_PSR0 = 0x0;	*(VP_U32)AIPI2_PSR1 = 0xFFFFFFFF; 	//write to the FMCR [31:24] (CLKMODE[1:0]) in order to get the write enable signal active 	*(VP_U32)SYS_FMCR |= 0xAA000000;*/	UsbotgSystemConfig();			//  PLL, GPIO, MAX setting	FunctionSetup();	Usbotg_main();	// go to USB_main to accept command from window	return ((gLastSector - 76) * 512);} void Func_Init_I2C_disable( void ) {/*---Set the I2C otg xcvr device address------*/    *(VP_U8)(SCLK_TO_SCL_HPER_ADD) = I2C_TEST_SCLK_SCL;/*---"Enable I2C interface and Set I2C to software Mode-----*/    *(VP_U8)(I2C_OP_CTRL_REG_ADD) = 0x01;/*---clear all interrupts----*/ //   i2cRemoveIntEnable();	*(VP_U8)(I2C_MASTER_INT_REG_ADD) &= 0x0f;  //   i2cClearInt();	*(VP_U8)(I2C_MASTER_INT_REG_ADD) &= 0xf0;    func_i2cDpDisablePullUp();}void Func_Init_I2C( void ) { /*---Set the I2C otg xcvr device address------*/    *(VP_U8)(SCLK_TO_SCL_HPER_ADD) = I2C_TEST_SCLK_SCL; /*---"Enable I2C interface and Set I2C to software Mode-----*/    *(VP_U8)(I2C_OP_CTRL_REG_ADD) = 0x01; /*---clear all interrupts----*/ //   i2cRemoveIntEnable();	*(VP_U8)(I2C_MASTER_INT_REG_ADD) &= 0x0f;  //   i2cClearInt();    *(VP_U8)(I2C_MASTER_INT_REG_ADD) &= 0xf0;    func_i2cDpPullUp();}void func_i2cDpPullUp( void ){#if 0  func_i2cSingleRegWrite(OTG_CTRL_REG1_SET_ADD, (U8)I2C_MASK_DP_PULLUP);  func_i2cSingleRegWrite(OTG_CTRL_REG1_CLR_ADD, (U8)I2C_MASK_DP_PULLDOWN);#else  func_i2cSingleRegWrite(OTG_CTRL_REG1_SET_ADD, (U8)I2C_MASK_DP_PULLUP);#endif}void func_i2cDpDisablePullUp( void ){#if 0  func_i2cSingleRegWrite(OTG_CTRL_REG1_SET_ADD, (U8)I2C_MASK_DP_PULLDOWN);  func_i2cSingleRegWrite(OTG_CTRL_REG1_CLR_ADD, (U8)I2C_MASK_DP_PULLUP );#else  func_i2cSingleRegWrite(OTG_CTRL_REG1_CLR_ADD, 0xfe/*(U8)(~I2C_MASK_DP_PULLUP)*/);//  func_i2cSingleRegWrite(OTG_CTRL_REG1_CLR_ADD, (U8)I2C_MASK_DP_PULLUP );#endif}void func_i2cSingleRegWrite(U32 qRegAddr, U8 qWrData){	U32 timeout = TIMEOUT_VALUE;	  *(VP_U8)(qRegAddr) = qWrData;  i2cSetSeqOpReg(1);  i2cSetSeqRdStartAd(qRegAddr);  *(VP_U8)(OTG_XCVR_DEVAD_ADD) = I2C_DEV_ADDR;  /* 

⌨️ 快捷键说明

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