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

📄 usbrx.c

📁 mx21的NAND Flash Bootloader源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
#include "usbotg_internal.h"#include "usb.h"//*******************************************************************U32 CSFAddress = 0;U32 HWConfigAddress = 0;U32 ExecuteAddress = 0;U32 EpNextBufToService = 0xFFFFFFFF;		//initial all to XBuffvoid ToggleNextBufferToService(U8 mask);void SetClearFillStatus(U8 mask);void EPConfig(U32 ep, U32 dir, U32 stall, U32 setup, U32 MPS, U32 format, U32 xbsa, U32 ybsa, U32 Bufsize, U32 Tbyte);void Enable_EP(U8 ep_num);void Ready_EP_OUT(U8 ep);void unready_ep_in(U8 ep);void EP0OutForRequest(void);extern void EUARTputString(U8 *line);extern void EUARTputHex(U8 data);#define EchoMsg EUARTputString//i2c related functionvoid func_i2cSingleRegWrite(U32 qRegAddr, U8 qWrData);void func_i2cDpPullUp( void );void Func_Init_I2C( void );void Func_Init_I2C_disable( void ) ;//USBOTG.cvoid UsbotgSystemConfig(void);void OtgFuncCommonInit(void);void func_i2cDpDisablePullUp( void );void HandleGetDescriptor(void);void HandleSetAddress(void);void HandleSetConfiguration(void);void HandleGetDeviceDscptr(void);void HandleGetConfigDscptr(void);void HandleGetStringDscptr(void);void UsbOtgReturnStrDscptr0(void);void UsbOtgReturnStrDscptr1(void);void UsbOtgReturnStrDscptr2(void);void UsbOtgReturnStrDscptr3(void);void EP0Out_StatusStageTransferDone(void);void processCommand();U8 UsbInUse;U8 _gUsbOtgDevReq[8];U32 EP1XBuff = 0;U32 EP1YBuff = 0;P_U32	_gpUsbBufPtr;            // pointer to buffer dataU32   _gUsbNumSector;            // number of sector to read/writeU32   _gUsbCurSector;            // current sector being read/writeU32   _gUsbPacketCount;          // USB packet countU32 _gSetConfigurationReceived=0;U8	_gCBW[32/*31*/]; 	// command block wrapper, the 32nd byte is meaningless.U8		quit;U32	gLastSector;//#define 	DISK_LAST_SECTOR	0x0000dFFF		// last sector of a 14M diskU8 bootSector[512] = {	0xEB,0x3C,0x90,0x4D,0x53,0x44,0x4F,0x53,0x35,0x2E,0x30,0x00,0x02,0x10,0x01,0x00,	0x02,0x00,0x02,0x00,0xE0,0xF8,0x16,0x00,0x3F,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,	0x00,0x00,0x00,0x00,0x00,0x00,0x29,0x8F,0x08,0x77,0xFE,0x4E,0x4F,0x20,0x4E,0x41,	0x4D,0x45,0x20,0x20,0x20,0x20,0x46,0x41,0x54,0x31,0x32,0x20,0x20,0x20,0x33,0xC9,	0x8E,0xD1,0xBC,0xF0,0x7B,0x8E,0xD9,0xB8,0x00,0x20,0x8E,0xC0,0xFC,0xBD,0x00,0x7C,	0x38,0x4E,0x24,0x7D,0x24,0x8B,0xC1,0x99,0xE8,0x3C,0x01,0x72,0x1C,0x83,0xEB,0x3A,	0x66,0xA1,0x1C,0x7C,0x26,0x66,0x3B,0x07,0x26,0x8A,0x57,0xFC,0x75,0x06,0x80,0xCA,	0x02,0x88,0x56,0x02,0x80,0xC3,0x10,0x73,0xEB,0x33,0xC9,0x8A,0x46,0x10,0x98,0xF7,	0x66,0x16,0x03,0x46,0x1C,0x13,0x56,0x1E,0x03,0x46,0x0E,0x13,0xD1,0x8B,0x76,0x11,	0x60,0x89,0x46,0xFC,0x89,0x56,0xFE,0xB8,0x20,0x00,0xF7,0xE6,0x8B,0x5E,0x0B,0x03,	0xC3,0x48,0xF7,0xF3,0x01,0x46,0xFC,0x11,0x4E,0xFE,0x61,0xBF,0x00,0x00,0xE8,0xE6,	0x00,0x72,0x39,0x26,0x38,0x2D,0x74,0x17,0x60,0xB1,0x0B,0xBE,0xA1,0x7D,0xF3,0xA6,	0x61,0x74,0x32,0x4E,0x74,0x09,0x83,0xC7,0x20,0x3B,0xFB,0x72,0xE6,0xEB,0xDC,0xA0,	0xFB,0x7D,0xB4,0x7D,0x8B,0xF0,0xAC,0x98,0x40,0x74,0x0C,0x48,0x74,0x13,0xB4,0x0E,	0xBB,0x07,0x00,0xCD,0x10,0xEB,0xEF,0xA0,0xFD,0x7D,0xEB,0xE6,0xA0,0xFC,0x7D,0xEB,	0xE1,0xCD,0x16,0xCD,0x19,0x26,0x8B,0x55,0x1A,0x52,0xB0,0x01,0xBB,0x00,0x00,0xE8,	0x3B,0x00,0x72,0xE8,0x5B,0x8A,0x56,0x24,0xBE,0x0B,0x7C,0x8B,0xFC,0xC7,0x46,0xF0,	0x3D,0x7D,0xC7,0x46,0xF4,0x29,0x7D,0x8C,0xD9,0x89,0x4E,0xF2,0x89,0x4E,0xF6,0xC6,	0x06,0x96,0x7D,0xCB,0xEA,0x03,0x00,0x00,0x20,0x0F,0xB6,0xC8,0x66,0x8B,0x46,0xF8,	0x66,0x03,0x46,0x1C,0x66,0x8B,0xD0,0x66,0xC1,0xEA,0x10,0xEB,0x5E,0x0F,0xB6,0xC8,	0x4A,0x4A,0x8A,0x46,0x0D,0x32,0xE4,0xF7,0xE2,0x03,0x46,0xFC,0x13,0x56,0xFE,0xEB,	0x4A,0x52,0x50,0x06,0x53,0x6A,0x01,0x6A,0x10,0x91,0x8B,0x46,0x18,0x96,0x92,0x33,	0xD2,0xF7,0xF6,0x91,0xF7,0xF6,0x42,0x87,0xCA,0xF7,0x76,0x1A,0x8A,0xF2,0x8A,0xE8,	0xC0,0xCC,0x02,0x0A,0xCC,0xB8,0x01,0x02,0x80,0x7E,0x02,0x0E,0x75,0x04,0xB4,0x42,	0x8B,0xF4,0x8A,0x56,0x24,0xCD,0x13,0x61,0x61,0x72,0x0B,0x40,0x75,0x01,0x42,0x03,	0x5E,0x0B,0x49,0x75,0x06,0xF8,0xC3,0x41,0xBB,0x00,0x00,0x60,0x66,0x6A,0x00,0xEB,	0xB0,0x4E,0x54,0x4C,0x44,0x52,0x20,0x20,0x20,0x20,0x20,0x20,0x0D,0x0A,0x4E,0x54,	0x4C,0x44,0x52,0x20,0x69,0x73,0x20,0x6D,0x69,0x73,0x73,0x69,0x6E,0x67,0xFF,0x0D,	0x0A,0x44,0x69,0x73,0x6B,0x20,0x65,0x72,0x72,0x6F,0x72,0xFF,0x0D,0x0A,0x50,0x72,	0x65,0x73,0x73,0x20,0x61,0x6E,0x79,0x20,0x6B,0x65,0x79,0x20,0x74,0x6F,0x20,0x72,	0x65,0x73,0x74,0x61,0x72,0x74,0x0D,0x0A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xAC,0xBF,0xCC,0x55,0xAA	};	//================================================================================================	// DEVICE DESCRIPTORconst U32 _gUsbOtgDevDscptr[5] = {	//bLength = 0x12		Size of this descriptor in bytes	//bDescriptorType = 0x01 	//bdUSB = 0x1001	//0x12011001, 	  0x01100112,		//bDeviceClass = 0x00	//bDeviceSubClass = 0x00	//bDeviceProtocol = 0x00	//bMaxPacketSize0 = 0x08	//0x00000008,	  0x08000000, 	//idVendor = 0xC4AB	//idProduct = 0x0005	//0xC4AB0005, 	  0x05000425,	//bcdDevice = 0x0001	//iManufacturer = 0x01	//iProduct = 0x02	//0x00010102, 	  0x02010100,	//iSerialNumber = 0x03 	//bNumConfigurations= 0x01		//0x0301 	 0x00000103};													//================================================================================================	// CONFIGURATION DESCRIPTORconst U32 _gUsbOtgConfDscptr[8] = {   //_gUsbConfDscptr[0]to[8] (Configuration Descriptor)	//bLength 			= 0x09		Size of this descriptor in bytes	//bDescriptorType 	= 0x02 	//wTotalLength 		= 0x2000	//bNumInterfaces 	= 0x01	//bConfigurationValue = 0x01	//iConfiguration 	= 0x00	//bmattributes 		= 0xC0	//MaxPower 			= 0x01		in next word 	//0x09022000,  	//0x010100C0, 	0x00200209,	0xC0000101,//_gUsbConfDscptr[9]to[17] (Interface Descriptor)	//bLength 			= 0x09 		Size of this descriptor in bytes	//bDescriptorType 	= 0x04 	//bInterfaceNumber 	= 0x00	//bAlternateSetting = 0x00	//bNumEndpoints 	= 0x02	//bInterfaceClass 	= 0x08	//bInterfaceSubClass = 	0x06	//bInterfaceProtocol = 0x50			in next word	//iInterface 		= 0x00			in next word	//0x01090400,	//0x00020806,	0x00040901,	0x06080200,	//_gUsbConfDscptr[18]to[24] (Endpoint Descriptor ==> EP1)	//bLength 			= 0x07  	Size of this descriptor in bytes	//bDescriptorType 	= 0x05 	//bEndpointAddress  = 0x81	//bmAttributes		= 0x02	//wMaxPacketSize	= 0x2000	//bInterval			= 0x00		in next word    //0x50000705,     //0x81022000, 	0x05070050,	0x00200281,	//_gUsbConfDscptr[25]to[31] (Endpoint Descriptor ==> EP2)	//bLength 			= 0x07		Size of this descriptor in bytes  	//bDescriptorType 	= 0x05 	//bEndpointAddress  = 0x02	//bmAttributes		= 0x02	//wMaxPacketSize	= 0x2000	//bInterval			= 0x00			//0x00070502, 	//0x02200000	0x02050700,	0x00002002};//============================================================================================    // STRING DESCRIPTOR//_gUsbStrDscptor0//bLength =0x04		Size of this descriptor in bytes//bDescriptorType = 0x03//bString = 0x0904//const U32 _gUsbStrDscptr0 = 0x04030904;const U32 _gUsbOtgStrDscptr0[1] = {0x04090304};//=============================================================================================// STRING DESCRIPTOR// iManufacturer//_gUsbStrDscptor1[7]//bLength =0x18		Size of this descriptor in bytes//bDescriptorType = 0x03//bString = //const U32 _gUsbStrDscptr1[7] =  {//	 0x12034D00, 0x6F007400, 0x6F007200, 0x6F006C00,//	 0x6100	};const U32 _gUsbOtgStrDscptr1[7] =  {	 0x004D0312, 0x0074006F, 0x0072006F, 0x006C006F,	 0x00610000};	 //=============================================================================================// STRING DESCRIPTOR//iProduct//_gUsbStrDscptor2[7]//bLength =0x18		Size of this descriptor in bytes//bDescriptorType = 0x03//bString = Motorola MX2//const U32 _gUsbStrDscptr2[7] =  {//	 0x1A034D00, 0x6F007400, 0x6F007200, 0x6F006C00,//	 0x61002000, 0x4D005800, 0x3100	};const U32 _gUsbOtgStrDscptr2[7] =  {	 0x004D031A, 0x0074006F, 0x0072006F, 0x006C006F,	 0x00200061, 0x0058004D, 0x00000032	};//=============================================================================================// STRING DESCRIPTOR// iSerialNumber//_gUsbStrDscptor3[6]//bLength =0x16		Size of this descriptor in bytes//bDescriptorType = 0x03//bString = MX1 SN:1234//const U32 _gUsbStrDscptr3[6] = {//    0x18034D00, 0x58003100, 0x20005300, 0x4E003A00,//	0x31003200, 0x33003400};const U32 _gUsbOtgStrDscptr3[6] = {    0x004D0318, 0x00320058, 0x00530020, 0x003A004E,	0x00320031, 0x00340033};void formatDrive(){	U32	i, j, k;	U8		*p;	p = (U8 *)USB_DISK_START;	// write boot sector	for (i=0; i<512; i++)	{		*(p++) = bootSector[i];	}	// write FAT (2 copies)	for (i=0; i<2; i++)	{		*(p++) = 0xF8;		*(p++) = 0xFF;		*(p++) = 0xFF;		for (j=3; j<512; j++)			*(p++) = 0;		for (j=1; j<22; j++)			for (k=0; k<512; k++)				*(p++) = 0;	}	// write directory entries	for (i=0; i<32; i++)		for (j=0; j<512; j++)			*(p++) = 0;}void writeSectors(){	U32 EpStartAddress;	U32 *DataBuf;	U32 tmp;	EPConfig(EP2, EP_OUT_DIR,   0,  0, 32, BLK_TYPE, 0x300, 0x200, 32, 32);	unready_ep_out(EP2);	//read the Start Address (xbsa, ybsa)	if (EpNextBufToService & (EP2OUT))	//X Buffer		EpStartAddress = ((*(VP_U32)(OTG_EP_BASE+(16*4)+4))&0x0000FFFF);	else		//Y Buffer		EpStartAddress = (((*(VP_U32)(OTG_EP_BASE+(16*4)+4))>>16)&0x0000FFFF);	for (;_gUsbPacketCount;_gUsbPacketCount--)	{		DataBuf=(U32 *)(OTG_DATA_BASE + EpStartAddress);		EPConfig(EP2, EP_OUT_DIR,   0,  0, 32, BLK_TYPE, 0x300, 0x200, 32, 32);		Ready_EP_OUT(2);					//check for EP2 IN transfer done		while (!(*(VP_U32)OTG_FUNC_EP_DSTAT & EP2OUT));		//clear EP0 IN transfer done 		*(VP_U32)OTG_FUNC_EP_DSTAT = EP2OUT;		//un ready the EP out		unready_ep_out(EP2);					// Send a package once a loop		*(_gpUsbBufPtr++) =*(DataBuf++);		*(_gpUsbBufPtr++) =*(DataBuf++);		*(_gpUsbBufPtr++) =*(DataBuf++);		*(_gpUsbBufPtr++) =*(DataBuf++);		*(_gpUsbBufPtr++) =*(DataBuf++);		*(_gpUsbBufPtr++) =*(DataBuf++);		*(_gpUsbBufPtr++) =*(DataBuf++);		*(_gpUsbBufPtr++) =*(DataBuf++);		SetClearFillStatus(EP2OUT);		ClearXYBufferInt();	}		EPConfig(EP1, EP_IN_DIR,   0,  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;		*((U8*)DataBuf)=0;        //Set/Clear Fill Status       	ClearXYBufferInt();       	SetClearFillStatus(EP1IN);		Ready_EP_IN(1);		//ready EP2		//check for EP2 IN transfer done		while (!(*(VP_U32)OTG_FUNC_EP_DSTAT & EP1IN));		//clear EP0 IN transfer done 		*(VP_U32)OTG_FUNC_EP_DSTAT = EP1IN;		ClearXYBufferInt();		unready_ep_in(EP1);}//******************************// OTG related function//******************************void SetOtgPort_FunctionMode(void){/* force otg to be function mode  */	*(VP_U32)OTG_CORE_HWMODE &= 0xFFFFFFFC; 	*(VP_U32)OTG_CORE_HWMODE |= 0x2;}void OtgPort_ExtPullUpDisabled(void){  *(VP_U32)(OTG_CORE_HNP_T3PCR) = 0x00000000;}/*============Function related tasks==========*///	EPConfig(0, EP_OUT_DIR, EP_NO_STALL, EP_NO_SETUP, 8, CTL_TYPE, 0, 0, 7, 8); void EPConfig(U32 ep, U32 dir, U32 stall, U32 setup, U32 MPS, U32 format, U32 xbsa, U32 ybsa, U32 Bufsize, U32 Tbyte){  U32 DWORD0 = 0;  U32 DWORD1 = 0;//  U32 DWORD2 = 0;  U32 DWORD3 = 0;  U32 localEp = ep;  U32 localEp_table_entry = 0;  U32 DWORD0_addr,DWORD1_addr,DWORD2_addr,DWORD3_addr ;  U8 rAlreadyReady;          /*Unready the ep if it is currently Ready */  rAlreadyReady = check_ep_ready(((ep * 2) + dir));  if (rAlreadyReady){    /*$display("INFO: EPConfig was called but EP %0x (%x) was already enabled! Clearing ready.", endpt_num, direction);*/    toggle_ready_ep(((ep * 2) + dir), 0);  }    DWORD0_addr = OTG_EP_BASE + (16*((localEp*2) + dir));   DWORD1_addr = DWORD0_addr + 4;		    			  DWORD2_addr = DWORD1_addr + 4;		      DWORD3_addr = DWORD2_addr + 4;      /*Configuring DWORD0*/  DWORD0 = (stall << 31)| (setup << 30) | (0 << 26) | (MPS<< 16) | (format << 14) | 0;  *(VP_U32)DWORD0_addr = DWORD0;      			  /*Configuring DWORD1*/  DWORD1 = (ybsa << 18) | (0 << 16) | (xbsa << 2) |0 ;  *(VP_U32)DWORD1_addr = DWORD1;      /*Configuring DWORD2*/	/* reserved */  /*Configuring DWORD3*/  //Bufsize = buffersize - 1  DWORD3 = (Bufsize << 21) | Tbyte;    *(VP_U32)DWORD3_addr = DWORD3;  /*Configure EP Table Entry*/    dgcEpFillDrainN = dir; /* dir: 0 => OUT, 1 => IN */  dgcEpXStatus = dir;  dgcEpYStatus = dir;  /*devId parameter not used in EP config */  /*activedirection not used in EP's environment */  localEp_table_entry = (0 << 30) | (0 << 26) | (0 << 15) | (0 << 4) | (0 << 2) | (dgcEpFillDrainN << 27) | (dgcEpXStatus << 28) | (dgcEpYStatus << 29) | format;  EP_table_entry[(ep*2) + dir] = localEp_table_entry;    /*Configure EP totalbyte table*/  EP_totalbyte[(ep*2) + dir] = Tbyte;  }U8 check_ep_ready(U8 qEpNumberDirection){  U8 mask = 1 <<qEpNumberDirection;  //check whether the EP ready set or not  //if ready, return 1  //else return 0;  if (*(VP_U32)OTG_FUNC_EP_RDY & mask)  	return 1;	//ready  else  	return 0;	//not ready	}

⌨️ 快捷键说明

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