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

📄 chap_9.c

📁 飞利浦公司usb2.0芯片isp1581的通用驱动代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	'c', 0,
	'o', 0,
	'n', 0,
	'd', 0,
	'u', 0,
	'c', 0,
	't', 0,
	'o', 0,
	'r', 0,
	's', 0,
	' ', 0,
	'A', 0,
	'P', 0,
	'I', 0,
	'C', 0}
};


USB_TESTPACKET  bTestPacket =
{
//	0x00, 0x00,
//	0x00, 0x80, // syn pattern
	0xc3,
	0x00, 0x00,
	0x00, 0x00,
	0x00, 0x00,
	0x00, 0x00,
	0x00, 0xaa,
	0xaa, 0xaa,
	0xaa, 0xaa,
	0xaa, 0xaa,
	0xaa, 0xee,  //aa*4
	0xee, 0xee,
	0xee, 0xee,
	0xee, 0xee,
	0xee, 0xfe,  //ee*4
	0xff, 0xff,
	0xff, 0xff,
	0xff, 0xff,
	0xff, 0xff,
	0xff, 0xff,  //FF*11
	0xff, 0x7f,
	0xbf, 0xdf,
	0xef, 0xf7,
	0xfb, 0xfd,
	0xfc, 0x7e,
	0xbf, 0xdf,
	0xef, 0xf7,
	0xfb, 0xfd,
	0x7e,
	0xb6, 0xce  // crc
//	0xff, 0xf7
};

/*
   //*************************************************************************
   // USB Protocol Layer
   //*************************************************************************
*/


/*
   //*************************************************************************
   // USB standard device requests
   //*************************************************************************
*/

void Chap9_GetStatus(void)
{
	UCHAR   endp, txdat[2];
	UCHAR   c;

	UCHAR   bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
	UCHAR   dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;

	if(!(dir))
		Chap9_StallEP0InControlWrite();


	if(ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wLength == 2 ){
		switch(bRecipient)
		{
		case USB_RECIPIENT_DEVICE:
			if(bISP1581flags.bits.remote_wakeup == 1)
				txdat[0] = DEVSTS_SELFPOWERED|DEVSTS_REMOTEWAKEUP;   /* remote wakeup and Self-powered */
			else
				txdat[0] = DEVSTS_SELFPOWERED;   /* Self-powered*/
			txdat[1]=0;

			Chap9_SingleTransmitEP0(txdat, 2);
			break;
		case USB_RECIPIENT_INTERFACE:
			txdat[0]=0;
			txdat[1]=0;

			Chap9_SingleTransmitEP0(txdat, 2);

			break;
		case USB_RECIPIENT_ENDPOINT:
#ifdef debug
			cprintf("in chap9, ControlData.DeviceRequest.wIndex= %hx\r\n", ControlData.DeviceRequest.wIndex);
#endif
			if( ControlData.DeviceRequest.wIndex & USB_ENDPOINT_DIRECTION_MASK)
				endp = (ControlData.DeviceRequest.wIndex*2 + 1);
			else
				endp = (ControlData.DeviceRequest.wIndex*2);

			c = ISP1581_GetEndpointStatus(endp);
//			c = isp1581_stallbit[endp];

#ifdef debug
			cprintf("in chap9, ep= %hx status= %hx \r\n", endp, c);
#endif
			txdat[0] = c & epctlfc_stall;   /* Halt */
			txdat[1] = 0;

			Chap9_SingleTransmitEP0(txdat, 2);

			break;
		default:
			Chap9_StallEP0InControlRead();
			break;
		}
	}
	else
	{
		Chap9_StallEP0InControlRead();
	}
}

void Chap9_ClearFeature(void)
{
	UCHAR   endp;
	UCHAR   bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
	USHORT  wFeature = ControlData.DeviceRequest.wValue;
	USHORT	wEPCFG;
	UCHAR   dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;
	if(dir)
		Chap9_StallEP0InControlRead();

	if( ControlData.DeviceRequest.wLength == 0 ){
		switch(bRecipient)
		{
		case USB_RECIPIENT_DEVICE:
			if(wFeature == USB_FEATURE_REMOTE_WAKEUP)
			{
				bISP1581flags.bits.remote_wakeup = 0;
				Chap9_ControlWriteHandshake();
			}
			else
			{
				Chap9_StallEP0InControlWrite();
			}
			break;
		case USB_RECIPIENT_ENDPOINT:
#ifdef debug
			debug_out(debug_code_clearfeature);
			cprintf("clear feature, stalled? \r\n");
#endif
			if( ControlData.DeviceRequest.wIndex & USB_ENDPOINT_DIRECTION_MASK)
				endp = (ControlData.DeviceRequest.wIndex*2 + 1);
			else
				endp = (ControlData.DeviceRequest.wIndex*2);

			if(wFeature == USB_FEATURE_ENDPOINT_STALL)
			{
//clear data toggle bit to '0' and clear buffers before clear stall of the endpoint.
				wEPCFG = ISP1581_GetEndpointConfig(endp);
				ISP1581_SetEndpointConfig(endp, 0); // disable endpoint
				ISP1581_SetEndpointConfig(endp, wEPCFG); // enable endpoint, clear buffer and set data toggle bit to ZERO.
				ISP1581_SetEndpointStatus(endp, 0);
				bISP1581flags.bits.BULKIN_EP1_STALL = 0;
				Chap9_ControlWriteHandshake();
			}
			else
			{
				Chap9_StallEP0InControlWrite();
			}
			break;
		default:
			Chap9_StallEP0InControlWrite();
			break;
		}
	}
	else
	{
		Chap9_StallEP0InControlWrite();
	}
}

void Chap9_SetFeature(void)
{
	UCHAR   endp;
	UCHAR   bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
	USHORT  wFeature = ControlData.DeviceRequest.wValue;

	UCHAR   dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;

	if(dir)
		Chap9_StallEP0InControlRead();

	if( ControlData.DeviceRequest.wLength == 0 )
	{
#ifdef debug
//		cprintf("%x,",bISP1581flags.bits.testmode);
#endif
		bISP1581flags.bits.testmode = (unsigned char)(ControlData.DeviceRequest.wIndex >> 8);
#ifdef debug
//		cprintf("%x, %x",bISP1581flags.bits.testmode,ControlData.DeviceRequest.wIndex);
#endif
		switch(bRecipient)
		{
		case USB_RECIPIENT_DEVICE:
			if(wFeature == USB_FEATURE_REMOTE_WAKEUP)
			{
				bISP1581flags.bits.remote_wakeup = 1;
				Chap9_ControlWriteHandshake();
			}
			// added for highspeed;
			else if((wFeature == USB_FEATURE_TEST_MODE) &&\
					((ControlData.DeviceRequest.wIndex & 0xf8ff) == 0) &&\
					(bISP1581flags.bits.ConnectSpeed == HighSpeed) &&\
					((bISP1581flags.bits.testmode < 5) && (bISP1581flags.bits.testmode > 0)))
			{
#ifdef debug
//				cprintf("%x",bISP1581flags.bits.testmode);
#endif
				bISP1581flags.bits.testmodeen = 1;
				Chap9_ControlWriteHandshake();
			}
			else
			{
				Chap9_StallEP0InControlWrite();
			}
			break;
		case USB_RECIPIENT_ENDPOINT:

			if( ControlData.DeviceRequest.wIndex & USB_ENDPOINT_DIRECTION_MASK)
				endp = (ControlData.DeviceRequest.wIndex*2 + 1);
			else
				endp = (ControlData.DeviceRequest.wIndex*2);

			if( wFeature == USB_FEATURE_ENDPOINT_STALL)
			{

				ISP1581_SetEndpointStatus(endp, epctlfc_stall);
//				isp1581_stallbit[endp] = 1;
#ifdef debug
//				cprintf("in chap9, set endp halt, ep= %hx \r\n",endp+1);
#endif
				Chap9_ControlWriteHandshake();
			}
			else
			{
				ISP1581_SetEndpointStatus(endp, 0);
				Chap9_ControlWriteHandshake();
			}
			break;
		default:
			Chap9_StallEP0InControlWrite();
			break;
		}
	}else
	{
		Chap9_StallEP0InControlWrite();
	}
}

void Chap9_SetAddress(void)
{

	UCHAR   dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;

	if(dir)
		Chap9_StallEP0InControlRead();

	if( ControlData.DeviceRequest.wIndex == 0 && ControlData.DeviceRequest.wLength == 0 ){

		RaiseIRQL();

		if(!ControlData.Abort)
		{
			ISP1581_SetAddressEnable((UCHAR)(ControlData.DeviceRequest.wValue &
				DEVICE_ADDRESS_MASK), 1);
		}
		LowerIRQL();

		/*
		// Minimize the MISSING Window for SETUP Packet
		// Here give a chance to ISR whose interrupt is caused by Setup Packet
		*/
		Chap9_ControlWriteHandshake();
	}
	else
	{
		Chap9_StallEP0InControlWrite();
	}
}


void Chap9_GetDescriptor(void)
{
	UCHAR   bDescriptor = MSB(ControlData.DeviceRequest.wValue);
	UCHAR   bDescriptorIndex = LSB(ControlData.DeviceRequest.wValue);

	UCHAR   dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;
	if(!(dir))
		Chap9_StallEP0InControlWrite();

	switch(bDescriptor)
	{
	case USB_DEVICE_DESCRIPTOR_TYPE:
		{
			if(bISP1581flags.bits.ConnectSpeed == HighSpeed)
				Chap9_BurstTransmitEP0((PUCHAR)&DeviceDescr_H, sizeof(USB_DEVICE_DESCRIPTOR));
			else
				Chap9_BurstTransmitEP0((PUCHAR)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR));
			break;
		}
	case USB_CONFIGURATION_DESCRIPTOR_TYPE:
		{
			if(bISP1581flags.bits.ConnectSpeed == HighSpeed)
				Chap9_BurstTransmitEP0((PUCHAR)&ConfigDescr_H, CONFIG_DESCRIPTOR_LENGTH);
			else
				Chap9_BurstTransmitEP0((PUCHAR)&ConfigDescr, CONFIG_DESCRIPTOR_LENGTH);
			break;
		}
	case USB_OTEHRSPEEDCONFIGURATION_DESCRIPTOR_TYPE:
		{
#ifdef debug
//			cprintf("SPEED %x", HighSpeed);
#endif
			if(bISP1581flags.bits.ConnectSpeed == HighSpeed)
			{
				Chap9_BurstTransmitEP0((PUCHAR)&Otherspeed_ConfigDescr_H, CONFIG_DESCRIPTOR_LENGTH);
#ifdef debug
//				cprintf("otherspeed descriptor sent!!!!");
#endif
			}
			else
			{
				Chap9_StallEP0InControlRead();
#ifdef debug
//				cprintf("|get descriptor not success, otherspeed|");
#endif
//				Chap9_BurstTransmitEP0((PUCHAR)&OtherSpeedConfigDescr, CONFIG_DESCRIPTOR_LENGTH);
			}
			break;
		}
	case USB_STRING_DESCRIPTOR_TYPE:
		switch(bDescriptorIndex)
		{
		case STR_INDEX_LANGUAGE:
			Chap9_BurstTransmitEP0((PUCHAR)&strLanguage, sizeof(USB_STRING_LANGUAGE_DESCRIPTOR));
			break;
		case STR_INDEX_MANUFACTURER:
			Chap9_BurstTransmitEP0((PUCHAR)&strManufacturer, sizeof(USB_STRING_MANUFACTURER_DESCRIPTOR));
			break;
		case STR_INDEX_PRODUCT:
			Chap9_BurstTransmitEP0((PUCHAR)&strProduct, sizeof(USB_STRING_PRODUCT_DESCRIPTOR));
			break;
		case STR_INDEX_SERIALNUMBER:
			Chap9_BurstTransmitEP0((PUCHAR)&strSerialNum, sizeof(USB_STRING_SERIALNUMBER_DESCRIPTOR));
			break;
		case STR_INDEX_CONFIGURATION:
			Chap9_BurstTransmitEP0((PUCHAR)&strConfiguration, sizeof(USB_STRING_CONFIGURATION_DESCRIPTOR) );
			break;
		case STR_INDEX_INTERFACE:
			Chap9_BurstTransmitEP0((PUCHAR)&strInterface, sizeof(USB_STRING_INTERFACE_DESCRIPTOR) );
			break;
		default:
			Chap9_StallEP0InControlRead();
			break;
		}
		break;
	case USB_DEVICEQUALIFIER_DESCRIPTOR_TYPE:
			Chap9_BurstTransmitEP0((PUCHAR)&DeviceQualifierDescr_H, sizeof(USB_DEVICEQUALIFIER_DESCRIPTOR));
		break;
	case USB_INTERFACE_DESCRIPTOR_TYPE:
	case USB_ENDPOINT_DESCRIPTOR_TYPE:
//	case USB_POWER_DESCRIPTOR_TYPE:
	default:
		Chap9_StallEP0InControlRead();
#ifdef debug
		cprintf("|get descriptor not success|");
#endif
		break;
	}
}

void Chap9_GetConfiguration(void)
{
	UCHAR   c = bISP1581flags.bits.configuration;

	UCHAR   dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;
	if(!(dir))
		Chap9_StallEP0InControlWrite();

	if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0 && ControlData.DeviceRequest.wLength == 1)
	{
#ifdef debug
//		cprintf("befor config function = x%xh, x%xh\r\n", c, &c);
#endif
		Chap9_SingleTransmitEP0(&c, 1);
	}
	else
		Chap9_StallEP0InControlRead();

}

void Chap9_SetConfiguration(void)
{
	UCHAR   dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;
//-----------------------------
//	jiang wrong direction defined
//	if(!(dir))
	if(dir)
//------------------------------
		Chap9_StallEP0InControlWrite();

	if(ControlData.DeviceRequest.wIndex == 0 && ControlData.DeviceRequest.wLength == 0){
		if (ControlData.DeviceRequest.wValue == 0) {
			/* put device in unconfigured state */
			bISP1581flags.bits.configuration = 0; /* This flag should be set before Unconfig and Config */
			ISP1581_UnconfigDevice();
			bInterface_number = 0; // reset to defauct interface (0);
			/*
			// Minimize the MISSING Window for SETUP Packet
			// Here give a chance to ISR whose interrupt is caused by Setup Packet
			*/
			Chap9_ControlWriteHandshake();
		} else if (ControlData.DeviceRequest.wValue == 1) {
			/* Configure device */
			bISP1581flags.bits.configuration = 1; /* This flag should be set before Unconfig and Config */
			ISP1581_UnconfigDevice();
			ISP1581_ConfigDevice();
			bInterface_number = 0; // use interface 0 when first configured
			/*
			// Minimize the MISSING Window for SETUP Packet
			// Here give a chance to ISR whose interrupt is caused by Setup Packet
			*/
				Chap9_ControlWriteHandshake();
		} else
			Chap9_StallEP0InControlWrite();
	}else
		Chap9_StallEP0InControlWrite();
}

void Chap9_GetInterface(void)
{
	UCHAR   dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;
	if(!(dir))
		Chap9_StallEP0InControlWrite();

	if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0 && ControlData.DeviceRequest.wLength == 1)
		Chap9_SingleTransmitEP0(&bInterface_number, 1);
	else
		Chap9_StallEP0InControlRead();
}

void Chap9_SetInterface(void)
{
	UCHAR   dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;
	if(dir)
		Chap9_StallEP0InControlRead();

	if (/*ControlData.DeviceRequest.wValue == 0 && */ControlData.DeviceRequest.wIndex == 0)
	{
		bInterface_number = (unsigned char)(ControlData.DeviceRequest.wValue);
		Chap9_ControlWriteHandshake();
#ifdef debug
		if(bInterface_number)
			cprintf(" Alter Interface %x ", bInterface_number);
		else
			cprintf(" Default Interface %x ", bInterface_number);
#endif
	}
	else
	{
		Chap9_StallEP0InControlWrite();
	}
}

/*
   //*************************************************************************
   // Chap9 support functions
   //*************************************************************************
*/

void Chap9_StallEP0(void)
{
	UCHAR dir;
	dir = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_DIR_MASK;
	if(dir)
	{
		ISP1581_StallEP0InControlRead();
	}
	else
	{
		ISP1581_StallEP0InControlWrite();
	}

	RaiseIRQL();
	if(!ControlData.Abort)
	{
		bISP1581flags.bits.DCP_state = USBFSM4DCP_STALL;
#ifdef debug
		cprintf("^_^%hxEP0", bISP1581flags.bits.DCP_state);
#endif
	}
	LowerIRQL();
}

void Chap9_StallEP0InControlWrite(void)
{
	ISP1581_StallEP0InControlWrite();

	RaiseIRQL();
	if(!ControlData.Abort)
	{
		bISP1581flags.bits.DCP_state = USBFSM4DCP_STALL;
#ifdef debug
		cprintf("^_^%hxcontrolwrite", bISP1581flags.bits.DCP_state);
#endif
	}
	LowerIRQL();
}

void Chap9_StallEP0InControlRead(void)
{
	ISP1581_StallEP0InControlRead();
	RaiseIRQL();
	if(!ControlData.Abort)
	{
		bISP1581flags.bits.DCP_state = USBFSM4DCP_STALL;
#ifdef debug
		cprintf("^_^%hxcontrolread", bISP1581flags.bits.DCP_state);
#endif
	}
	LowerIRQL();
}

void Chap9_ControlWriteHandshake(void)
{
	RaiseIRQL();
	if(!ControlData.Abort)
	{
		ISP1581_ControlWriteHandshake();
		bISP1581flags.bits.DCP_state = USBFSM4DCP_CONTROLWRITEHANDSHAKE;
#ifdef debug
		cprintf("^_^%hx", bISP1581flags.bits.DCP_state);
#endif
	}
	LowerIRQL();
}

void Chap9_SingleTransmitEP0(PUCHAR buf, USHORT len)
{

	ISP1581_WriteControlEndpoint(buf, len);

	RaiseIRQL();
	if(!ControlData.Abort)
	{
		ControlData.wLength = ControlData.wCount = len;

		bISP1581flags.bits.DCP_state = USBFSM4DCP_CONTROLREADHANDSHAKE;
#ifdef debug
		cprintf("^_^%hx", bISP1581flags.bits.DCP_state);
#endif
	}
	LowerIRQL();
}

void Chap9_BurstTransmitEP0(PUCHAR pData, USHORT len)
{
	RaiseIRQL();
	if(!ControlData.Abort)
	{
		if(ControlData.wLength > len)
			ControlData.wLength = len;

		ControlData.Addr.pData = pData;

			if( ControlData.wLength >= EP0_PACKET_SIZE)
			{
				ControlData.wCount += ISP1581_WriteControlEndpoint(pData, EP0_PACKET_SIZE);
				/*
				//	Minimize the MISSING Window for SETUP Packet
				// Here give a chance to ISR whose interrupt is caused by Setup Packet
				*/
				bISP1581flags.bits.DCP_state = USBFSM4DCP_DATAIN;
#ifdef debug
				cprintf("^_^%hx", bISP1581flags.bits.DCP_state);
#endif
			}
			else
			{
				ControlData.wCount += ISP1581_WriteControlEndpoint(pData, ControlData.wLength);
				/*
				// Minimize the MISSING Window for SETUP Packet
				// Here give a chance to ISR whose interrupt is caused by Setup Packet
				*/
				bISP1581flags.bits.DCP_state = USBFSM4DCP_CONTROLREADHANDSHAKE;
#ifdef debug
				cprintf("^_^%hx", bISP1581flags.bits.DCP_state);
#endif
			}
	}
}

⌨️ 快捷键说明

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