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

📄 usbdev.c

📁 S3C2443的USB 的驱动程序
💻 C
📖 第 1 页 / 共 4 页
字号:
	U32  g_uDownloadFileSize;
	U32  g_uDownloadAddress;
	U32  g_cpucnt;

	USB_DEVICE_DESCRIPTOR* g_poDescDevice;
	USB_GET_STATUS* g_poStatusGet;
	USB_INTERFACE_GET* g_poInterfaceGet;
	USB_DESCRIPTORS* g_poDesc;
	DEVICE_REQUEST* g_poDeviceRequest;

	U32  g_uEp0State;
	U32  g_uEp0SubState;	
	USB_SPEED g_eSpeed;
	U32  g_uBulkInCount;
	U32  g_uEp0MaxPktSize;
	U32  g_uEp1MaxPktSize;
	U32  g_uEp3MaxPktSize;	
	U32  g_uBulkInAddr;
	U32  g_uEnumerationDone;



void WrPktEp0(U8 *buf, int num)
{
	int i;
	U16 Wr_Data=0;

	if (num&0x1) num++;
	for(i=0;i<num;i+=2)
	{
		Wr_Data=((*(buf+1))<<8)|*buf;
		Outp32(EP0_FIFO, Wr_Data);
		buf +=2;
	}
}

void WrPktEp1(U8 *buf, int num)
{
	int i;
	U16 Wr_Data=0;

	if (num&0x1) num++;
	for(i=0;i<num;i+=2)
	{
		Wr_Data=((*(buf+1))<<8)|*buf;
	   Outp32(EP1_FIFO, Wr_Data);
	   buf +=2;
	}
}

void PrintEp0Pkt(U8 *pt, U8 count)
{
	int i;
	DbgUsb(("[DBG:"));
	for(i=0;i<count;i++)
		DbgUsb(("%x,", pt[i]));
	DbgUsb(("]"));
}

void RdPktEp3(U8 *buf, int num)
{
	int i;
	U16 Rdata;

	for(i=0;i<num;i+=2)
	{
		Inp32(EP3_FIFO, Rdata);
		buf[i] = (U8)Rdata;
		buf[i+1] = (U8)(Rdata>>8);
	}

	g_pDownPt += num;
}

bool IsEnumerationDone(void)
{
	if (g_uEnumerationDone == 0)
		return FALSE;
	else
		return TRUE;
}

void PrepareEp1Fifo(U32 BaseAddr)
{
	int i;
	U32 in_csr1;
	U8* BulkInBuf = (U8*)BaseAddr;

	if (g_uBulkInCount > g_uEp1MaxPktSize)
	{
		Outp32(INDEX_REG, EP1);
		Outp32(BYTE_WRITE_CNT_REG, g_uEp1MaxPktSize);
		WrPktEp1(BulkInBuf, g_uEp1MaxPktSize);

		g_uBulkInAddr = BaseAddr + g_uEp1MaxPktSize;
		g_uBulkInCount -= g_uEp1MaxPktSize;
	}
	else
	{
		Outp32(INDEX_REG, EP1);
		Outp32(BYTE_WRITE_CNT_REG, g_uBulkInCount);
		WrPktEp1(BulkInBuf, g_uBulkInCount);

		g_uBulkInAddr = BaseAddr + g_uBulkInCount;
		g_uBulkInCount = 0;
	}


}

void FlushEp1Fifo(void)
{
	U32 OrgValue;
	Outp32(INDEX_REG, EP1);
	Inp32(EP_CON_REG, OrgValue);
	Outp32(EP_CON_REG, (1<<6));
	Outp32(EP_CON_REG, OrgValue);
}

void SetMaxPktSizes(USB_SPEED eSpeed)
{
	if (eSpeed == USB_HIGH)
	{
		g_eSpeed = USB_HIGH;
		g_uEp0MaxPktSize = 64;
		g_uEp1MaxPktSize = 512;
		g_uEp3MaxPktSize = 512;
	}
	else
	{
		g_eSpeed = USB_FULL;
		g_uEp0MaxPktSize = 8;
		g_uEp1MaxPktSize = 64;
		g_uEp3MaxPktSize = 64;
	}
	// EP0 Max Packet size settings
	Outp32(INDEX_REG, EP0);
	Outp32(MAX_PKT_REG, g_uEp0MaxPktSize); 	// max packet size

	// EP1 OUT Max Packet size settings
	Outp32(INDEX_REG, EP1);
	Outp32(MAX_PKT_REG, g_uEp1MaxPktSize); 	// max packet size

	// EP2 IN Max Packet size settings
	Outp32(INDEX_REG, EP3);
	Outp32(MAX_PKT_REG, g_uEp3MaxPktSize);	// max packet size
}

void SetOpMode(USB_OP mode)
{
	U32 i;
	g_uDownloadFileSize = 0;
	g_eOpMode = mode;
}

bool VerifyChecksum(void)
{
	U8* CalcCSPt;
	U16 dnCS;
	U16 checkSum;
    U16 i=0;
    
	// checksum calculation
	CalcCSPt = (U8*)g_uDownloadAddress;
	checkSum = 0;
	while((U32)CalcCSPt < (g_uDownloadAddress+(g_uDownloadFileSize-8)))
	{
		checkSum += *CalcCSPt++;
		i++;
	}
	// checkSum was calculated including dnCS. So, dnCS should be subtracted.
	checkSum=checkSum - *((unsigned char *)(g_uDownloadAddress+g_uDownloadFileSize-8-2))
		- *( (unsigned char *)(g_uDownloadAddress+g_uDownloadFileSize-8-1) );

	dnCS=*((unsigned char *)(g_uDownloadAddress+g_uDownloadFileSize-8-2))+
		(*( (unsigned char *)(g_uDownloadAddress+g_uDownloadFileSize-8-1) )<<8);

	if (checkSum!=dnCS)
	{
		printf("Checksum Error!!! MEM:%x DN:%x\n", checkSum, dnCS);
		return FALSE;
	}
	else
	{
		return TRUE;
	}

}

void GetDownFileInfo(U32* uDownAddr, U32* uDownFileSize)
{
	*uDownAddr = g_uDownloadAddress;
	*uDownFileSize = g_uDownloadFileSize;
}

void AllocateUSBDEV(void)
{
	g_poDescDevice = (USB_DEVICE_DESCRIPTOR*)malloc(sizeof (USB_DEVICE_DESCRIPTOR));
	g_poStatusGet = (USB_GET_STATUS*)malloc(sizeof (USB_GET_STATUS));
	g_poInterfaceGet = (USB_INTERFACE_GET*)malloc(sizeof (USB_INTERFACE_GET));
	g_poDesc = (USB_DESCRIPTORS*)malloc(sizeof (USB_DESCRIPTORS));
	g_poDeviceRequest = (DEVICE_REQUEST*)malloc(sizeof (DEVICE_REQUEST));
}

void FreeUSBDEV(void)
{
	if (g_poDescDevice)
		free(g_poDescDevice);
	if (g_poStatusGet)
		free(g_poStatusGet);
	if (g_poInterfaceGet)
		free(g_poInterfaceGet);
	if (g_poDesc)
		free(g_poDesc);
	if (g_poDeviceRequest)
		free(g_poDeviceRequest);
}


void Init(void)
{

	g_uEnumerationDone = 0;
	SetEndpoint();

	g_uEp0State = EP0_STATE_INIT;
	g_uEp0SubState = 0;
}

void SetDescriptorTable(void)
{
	// Standard device descriptor
	g_poDescDevice->bLength=0x12;	// EP0_DEV_DESC_SIZE=0x12 bytes
	g_poDescDevice->bDescriptorType=DEVICE_TYPE;
	g_poDescDevice->bDeviceClass=0xFF; // 0x0
	g_poDescDevice->bDeviceSubClass=0x0;
	g_poDescDevice->bDeviceProtocol=0x0;
	g_poDescDevice->bMaxPacketSize0=g_uEp0MaxPktSize;

	g_poDescDevice->idVendorL=0xe8;     //changed the value 06.10.16
	g_poDescDevice->idVendorH=0x04;     //changed the value 06.10.16

	g_poDescDevice->idProductL=0x34;
	g_poDescDevice->idProductH=0x12;

	g_poDescDevice->bcdDeviceL=0x00;
	g_poDescDevice->bcdDeviceH=0x01;
	g_poDescDevice->iManufacturer=0x1; // index of string descriptor
	g_poDescDevice->iProduct=0x2;	// index of string descriptor
	g_poDescDevice->iSerialNumber=0x0;
	g_poDescDevice->bNumConfigurations=0x1;
	if (g_eSpeed == USB_FULL) {
		g_poDescDevice->bcdUSBL=0x10;
		g_poDescDevice->bcdUSBH=0x01; 	// Ver 1.10
	}
	else {
		g_poDescDevice->bcdUSBL=0x00;
		g_poDescDevice->bcdUSBH=0x02; 	// Ver 2.0
	}

	// Standard configuration descriptor
	g_poDesc->oDescConfig.bLength=0x9;
	g_poDesc->oDescConfig.bDescriptorType=CONFIGURATION_TYPE;
	g_poDesc->oDescConfig.wTotalLengthL=0x20; // <cfg desc>+<if desc>+<endp0 desc>+<endp1 desc>
	g_poDesc->oDescConfig.wTotalLengthH=0;
	g_poDesc->oDescConfig.bNumInterfaces=1;
// dbg    descConf.bConfigurationValue=2; // why 2? There's no reason.
	g_poDesc->oDescConfig.bConfigurationValue=1;
	g_poDesc->oDescConfig.iConfiguration=0;
	g_poDesc->oDescConfig.bmAttributes=CONF_ATTR_SELFPOWERED;//CONF_ATTR_DEFAULT; // bus powered only.
	g_poDesc->oDescConfig.maxPower=25; // draws 50mA current from the USB bus.

	// Standard interface descriptor
	g_poDesc->oDescInterface.bLength=0x9;
	g_poDesc->oDescInterface.bDescriptorType=INTERFACE_TYPE;
	g_poDesc->oDescInterface.bInterfaceNumber=0x0;
	g_poDesc->oDescInterface.bAlternateSetting=0x0; // ?
	g_poDesc->oDescInterface.bNumEndpoints=2;	// # of endpoints except EP0
	g_poDesc->oDescInterface.bInterfaceClass=0xff; // 0x0 ?
	g_poDesc->oDescInterface.bInterfaceSubClass=0x0;
	g_poDesc->oDescInterface.bInterfaceProtocol=0x0;
	g_poDesc->oDescInterface.iInterface=0x0;

	// Standard endpoint0 descriptor
	g_poDesc->oDescEndpt1.bLength=0x7;
	g_poDesc->oDescEndpt1.bDescriptorType=ENDPOINT_TYPE;
	g_poDesc->oDescEndpt1.bEndpointAddress=1|EP_ADDR_IN; // 2400Xendpoint 1 is IN endpoint.
	g_poDesc->oDescEndpt1.bmAttributes=EP_ATTR_BULK;
	g_poDesc->oDescEndpt1.wMaxPacketSizeL=(U8)g_uEp1MaxPktSize; // 64
	g_poDesc->oDescEndpt1.wMaxPacketSizeH=(U8)(g_uEp1MaxPktSize>>8);
	g_poDesc->oDescEndpt1.bInterval=0x0; // not used

	// Standard endpoint1 descriptor
	g_poDesc->oDescEndpt3.bLength=0x7;
	g_poDesc->oDescEndpt3.bDescriptorType=ENDPOINT_TYPE;
	g_poDesc->oDescEndpt3.bEndpointAddress=3|EP_ADDR_OUT; // 2400X endpoint 3 is OUT endpoint.
	g_poDesc->oDescEndpt3.bmAttributes=EP_ATTR_BULK;
	g_poDesc->oDescEndpt3.wMaxPacketSizeL=(U8)g_uEp3MaxPktSize; // 64
	g_poDesc->oDescEndpt3.wMaxPacketSizeH=(U8)(g_uEp3MaxPktSize>>8);
	g_poDesc->oDescEndpt3.bInterval=0x0; // not used
}

void SetEndpoint(void)
{
	// *** End point information ***
	// EP0: control
	U16 SysStatus;
	U16 Temp;

	Outp32(INDEX_REG, EP0);
	// For L18
	Outp32(EP_DIR_REG, 0x02); 		// EP1=> TX, EP2=>RX , 0b=report mode[1], 15b=report mode[2], 3b~8b=ep2 delay_con
	Outp32(EP_INT_EN_REG, 0x4d0f); 	// EP0, 1, 2 Interrupt enable, 15b=report mode[0], 3b~14b=ep0/1 delay_con
	Inp32(EP_DIR_REG, Temp);
	DbgUsb(("EP_DIR_REG : %x \n", Temp));
	Inp32(EP_INT_EN_REG, Temp);
	DbgUsb(("EP_INT_EN_REG : %x \n", Temp));

	Outp32(SYS_CON_REG, 0x4123);		// error interrupt enable, 16bit bus, Little format, suspend&reset enable
	Outp32(EP0_CON_REG, 0x0000);


	// EP1 OUT Max Packet size settings
	Outp32(INDEX_REG, EP1);
	Outp32(EP_CON_REG, 0x0080); // dual enable

	// EP2 IN Max Packet size settings
	Outp32(INDEX_REG, EP3);
	Outp32(EP_CON_REG, 0x0080);    		// dual enable
	Outp32(INDEX_REG, EP0);
}


void HandleEvent(void)
{
	U32 uStatus;
	U32 Temp;
	U16 ep_int_status, ep_int;

	Inp32(SYS_STATUS_REG, uStatus); // System status read
	DbgUsb(("SYS_STATUS_REG : %x \n", uStatus));

	if (uStatus & INT_REG_VBUS)
	{
		Outp32(SYS_STATUS_REG, INT_REG_VBUS); // Interrupt Clear
		DbgUsb(("\n [USB_Diag_Log]  :  INT_REG_VBUS\n"));
	}

	if (uStatus & 0xfc00) 	 // Error interrupt check
	{
		
		Inp32(SYS_STATUS_REG , Temp);
		Outp32(SYS_STATUS_REG, INT_ERR); // Interrupt Clear
		DbgUsb(("\n [USB_Diag_Log]  :  Error_INT\n"));
		
	}

	if (uStatus & INT_REG_SUSPEND)
	{
		Outp32(SYS_STATUS_REG, INT_REG_SUSPEND); // Interrupt Clear
		DbgUsb(("\n [USB_Diag_Log]  : Suspend Mode"));
		
	}

	if (uStatus & INT_REG_RESUME)
	{
		Outp32(SYS_STATUS_REG, INT_REG_RESUME); // Host software send ClearPortFeature. Interrupt Clear
		DbgUsb(("\n [USB_Diag_Log]  : Resume Mode \n"));
		
	}

	if (uStatus & INT_REG_RESET) // Reset interrupt
	{
		Outp32(SYS_STATUS_REG, INT_REG_RESET); // Interrupt Clear
		SetEndpoint();
		g_uEp0State = EP0_STATE_INIT;
		DbgUsb(("\n [USB_Diag_Log]  : Reset Mode \n"));
	}

	if (uStatus & INT_REG_SDE) // Device Speed Detection interrupt
	{
		Outp32(SYS_STATUS_REG, INT_REG_SDE); // Interrupt Clear
		DbgUsb(("\n [USB_Diag_Log]  : Speed Detection interrupt \n"));

		if (uStatus & INT_REG_HSP) // Set if Device is High speed or Full speed
		{
			Outp32(SYS_STATUS_REG, INT_REG_HSP); // High Speed Device Interrupt Clear?? may be not.
			DbgUsb(("\n [USB_Diag_Log]  : High Speed Detection\n"));
			SetMaxPktSizes(USB_HIGH);
			SetDescriptorTable();
		}
		else
		{
			SetMaxPktSizes(USB_FULL);
			SetDescriptorTable();
		}
	}

	

	Inp32(EP_STATUS_REG, ep_int_status); // EP interrrupt status read
	DbgUsb(("EP_STATUS_REG : %x \n", ep_int_status));
	Inp32(EP_INT_REG, ep_int);
	DbgUsb(("EP_INT_REG : %x \n", ep_int));


	if (ep_int & INT_REG_EP0)
	{
 		DbgUsb(("\n [USB_Diag_Log]  :  Control Transfer Interrupt \n"));
		Outp32(EP_INT_REG, INT_REG_EP0); // Interrupt Clear
		HandleEvent_EP0();
	}

	// Endpoint1 bulkIn
	else if (ep_int & INT_REG_EP1)
	{
		Outp32(EP_INT_REG, INT_REG_EP1); // Interrupt Clear
		DbgUsb(("\n [USB_Diag_Log]  :  Ep1 Interrupt  \n"));
		HandleEvent_BulkIn();
	}

	// Endpoint3 bulkOut
	else if (ep_int & INT_REG_EP3)
	{
		DbgUsb(("\n [USB_Diag_Log]  :  Bulk Out Transfer Interrupt  \n"));
		Outp32(EP_INT_REG, INT_REG_EP3); // Interrupt Clear
		HandleEvent_BulkOut();
	}

}

// ======================================================================
// endpointZeroFunction()
//

⌨️ 快捷键说明

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