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

📄 usbd.c

📁 W90P910的BOOTLOADER,难找呀,我想应该是第一份吧.
💻 C
📖 第 1 页 / 共 3 页
字号:

				default:
					ReqErr=1;
					break;
			}	//end of switch
			if (ReqErr == 0)
			{
				outpw(REG_USBD_CEP_IRQ_STAT, 0x408);
				outpw(REG_USBD_CEP_IRQ_ENB, 0x408);		//suppkt int ,status and in token
			}
			break;

		case USBR_SET_ADDRESS:

			ReqErr = ((_usb_cmd_pkt.bmRequestType == 0) && ((_usb_cmd_pkt.wValue & 0xff00) == 0)
			 && (_usb_cmd_pkt.wIndex == 0) && (_usb_cmd_pkt.wLength == 0)) ? 0 : 1;

			if ((_usb_cmd_pkt.wValue & 0xffff) > 0x7f)	//within 7f
			{
				ReqErr=1;	//Devaddr > 127
				MSG_DEBUG("ERROR -  Request Error - Device address greater than 127\n");
			}

			if (_usbd_devstate == 3)
			{
				ReqErr=1;	//Dev is configured
				MSG_DEBUG("ERROR - Request Error - Device is already in configure state\n");
			}

			if (ReqErr==1) 
			{
				MSG_DEBUG("ERROR - CepEvHndlr:USBR_SET_ADDRESS <= Request Error\n");
				break;		//break this switch loop
			}

			if(_usbd_devstate == 2)
			{
				if(_usb_cmd_pkt.wValue == 0)
					_usbd_devstate = 1;		//enter default state
				_usbd_address = _usb_cmd_pkt.wValue;	//if wval !=0,use new address	
			}

			if(_usbd_devstate == 1)
			{
				if(_usb_cmd_pkt.wValue != 0)
				{//enter addr state whether to write or not NOW!!!
					_usbd_address = _usb_cmd_pkt.wValue;
					_usbd_devstate = 2;
				}
			}
			outpw(REG_USBD_CEP_CTRL_STAT, CEP_NAK_CLEAR);	//clear nak so that sts stage is complete
			outpw(REG_USBD_CEP_IRQ_STAT, 0x400);
			outpw(REG_USBD_CEP_IRQ_ENB, 0x400);		// enable status complete interrupt
			break;

		case USBR_GET_CONFIGURATION:

			ReqErr = ((_usb_cmd_pkt.bmRequestType == 0x80) && (_usb_cmd_pkt.wValue == 0) &&
			(_usb_cmd_pkt.wIndex == 0) && (_usb_cmd_pkt.wLength == 0x1) ) ? 0 : 1;

			if (ReqErr==1)
			{
				MSG_DEBUG("ERROR - CepEvHndlr:USBR_GET_CONFIGURATION <= Request Error\n");
				break;	//break this switch loop
			}

			usbdClearAllFlags();
			usbdGetConfig=1;

			outpw(REG_USBD_CEP_IRQ_STAT, 0x408);
			outpw(REG_USBD_CEP_IRQ_ENB, 0x408);		//status and in token
			break;

		case USBR_SET_CONFIGURATION:
			ReqErr = ((_usb_cmd_pkt.bmRequestType == 0) && ((_usb_cmd_pkt.wValue & 0xff00) == 0) &&
			((_usb_cmd_pkt.wValue & 0x80) == 0) && (_usb_cmd_pkt.wIndex == 0) && 
			(_usb_cmd_pkt.wLength == 0)) ? 0 : 1;

			if (_usbd_devstate == 1)
			{
				MSG_DEBUG("ERROR - Device is in Default state\n");
				ReqErr=1;
			}

			if ((_usb_cmd_pkt.wValue != 1) && (_usb_cmd_pkt.wValue != 0) )  //Only configuration one is supported
			{
				MSG_DEBUG("ERROR - Configuration choosed is invalid\n");
				ReqErr=1;
			}
					
			if(ReqErr==1) 
			{
				MSG_DEBUG("ERROR - CepEvHndlr:USBR_SET_CONFIGURATION <= Request Error\n");
				break;	//break this switch loop
			}

			if (_usb_cmd_pkt.wValue == 0)
			{
				_usbd_confsel = 0;
				_usbd_devstate = 2;
			}
			else
			{
				_usbd_confsel = _usb_cmd_pkt.wValue;
				_usbd_devstate = 3;
			}

			outpw(REG_USBD_CEP_CTRL_STAT, CEP_NAK_CLEAR);	//clear nak so that sts stage is complete
			break;

		case USBR_GET_INTERFACE:

			ReqErr = ((_usb_cmd_pkt.bmRequestType == 0x81) && (_usb_cmd_pkt.wValue == 0) &&
			(_usb_cmd_pkt.wIndex == 0) && (_usb_cmd_pkt.wLength == 0x1)) ? 0 : 1;

			if ((_usbd_devstate == 1) || (_usbd_devstate == 2))
			{
				MSG_DEBUG("ERROR - Device state is not valid\n");
				ReqErr=1;
			}

			if(ReqErr == 1) 
			{
				MSG_DEBUG("ERROR - CepEvHndlr:USBR_GET_INTERFACE <= Request Error\n");
				break;	//break this switch loop
			}

			usbdClearAllFlags();
			usbdGetInterface=1;

			outpw(REG_USBD_CEP_IRQ_STAT, 0x408);
			outpw(REG_USBD_CEP_IRQ_ENB, 0x408);		//suppkt int ,status and in token
			break;

		case USBR_SET_INTERFACE:

			ReqErr = ((_usb_cmd_pkt.bmRequestType == 0x1) && ((_usb_cmd_pkt.wValue & 0xff80) == 0) 
			&& ((_usb_cmd_pkt.wIndex & 0xfff0) == 0) && (_usb_cmd_pkt.wLength == 0)) ? 0 : 1;

			if ((_usbd_devstate == 0x3) && (_usb_cmd_pkt.wIndex == 0x0) && (_usb_cmd_pkt.wValue == 0x0))
				MSG_DEBUG("interface choosen is already is use, so stall was not sent\n");
			else
			{
				MSG_DEBUG("ERROR - Choosen interface was not supported\n");
				ReqErr=1;
			}

			if (ReqErr == 1)
			{
				MSG_DEBUG("CepEvHndlr:USBR_SET_INTERFACE <= Request Error\n");
				break;	//break this switch loop
			}
			_usbd_altsel = _usb_cmd_pkt.wValue;
			outpw(REG_USBD_CEP_CTRL_STAT, CEP_NAK_CLEAR);	//clear nak so that sts stage is complete
			break;

		case USBR_SET_FEATURE:
			ReqErr = (((_usb_cmd_pkt.bmRequestType & 0xfc) == 0) && ((_usb_cmd_pkt.wValue & 0xfffc) == 0) 
			&& ((_usb_cmd_pkt.wIndex & 0xff00) == 0) && (_usb_cmd_pkt.wLength == 0)) ? 0 : 1;

			if (_usbd_devstate == 1)
			{
				if((_usb_cmd_pkt.bmRequestType & 0x3) == 0x0) // Receipent is Device
				{
					if((_usb_cmd_pkt.wValue & 0x3) == TEST_MODE)
					{
						enabletestmode = 1;
						testselector = (_usb_cmd_pkt.wIndex >> 8);
					}
				}
				else
				{
					MSG_DEBUG("ERROR - Device is in Default State\n");
					ReqErr=1;
				}
			}

			if (_usbd_devstate == 2)
			{
				if ((_usb_cmd_pkt.bmRequestType & 0x3 == 2) && ((_usb_cmd_pkt.wIndex & 0xff) != 0))	//ep but not cep
				{
					MSG_DEBUG("ERROR - Device is in Addressed State, but for noncep\n");
					ReqErr =1;
				}
				else if ((_usb_cmd_pkt.bmRequestType & 0x3) == 0x1)
				{
					MSG_DEBUG("ERROR - Device is in Addressed State, but for interfac\n");
					ReqErr=1;
				}
			}

			if (ReqErr == 1) 
			{
				MSG_DEBUG("CepEvHndlr:USBR_SET_FEATURE <= Request Error\n");
				break;	//break this switch loop
			}

			//check if recipient and wvalue are appropriate	
			switch(_usb_cmd_pkt.bmRequestType & 0x3)
			{
				case 0:		//device
					if ((_usb_cmd_pkt.wValue & 0x3) == DEVICE_REMOTE_WAKEUP)
						enableremotewakeup = 1;
					else if((_usb_cmd_pkt.wValue & 0x3) == TEST_MODE)
					{
						enabletestmode = 1;
						testselector = (_usb_cmd_pkt.wIndex >> 8);
					}
					else
					{
						MSG_DEBUG("ERROR - No such feature for device\n");
						ReqErr=1;
					}
					break;

				case 1:		//interface
					break;

				case 2:		//endpoint
					if((_usb_cmd_pkt.wValue & 0x3) == ENDPOINT_HALT)
					{
						if((_usb_cmd_pkt.wIndex & 0x3) == 0) //endPoint zero
						{
							_usbd_haltep = 0;
						}
						else if((_usb_cmd_pkt.wIndex & 0x3) == 1) //endPoint one
						{
							_usbd_haltep = 1;
						}
						else if((_usb_cmd_pkt.wIndex & 0x3) == 2) //endPoint two
						{
							_usbd_haltep = 2;
						}
						else
						{
							MSG_DEBUG("ERROR - Selected endpoint was not present\n");
							ReqErr=1;
						}
					}
					else
					{
						MSG_DEBUG("ERROR - Neither device,endpoint nor interface was choosen\n");
						ReqErr=1;
					}
					break;

				default:
					break;
			}//device

			outpw(REG_USBD_CEP_CTRL_STAT, CEP_NAK_CLEAR);	//clear nak so that sts stage is complete
			break;

		case USBR_CLEAR_FEATURE:
			ReqErr = (((_usb_cmd_pkt.bmRequestType & 0xfc) == 0) && ((_usb_cmd_pkt.wValue & 0xfffc) == 0) 
			&& ((_usb_cmd_pkt.wIndex & 0xff00) == 0) && (_usb_cmd_pkt.wLength == 0)) ? 0 : 1;

			if (_usbd_devstate == 1) 
			{
				MSG_DEBUG("ERROR - Device is in default state\n");
				ReqErr =1;
			}

			if (_usbd_devstate == 2)
			{
				if((_usb_cmd_pkt.bmRequestType == 2) && (_usb_cmd_pkt.wIndex != 0)) //ep but not cep
				{
					MSG_DEBUG("ERROR - Device is in Addressed State, but for noncep\n");
					ReqErr =1;
				}
				else if(_usb_cmd_pkt.bmRequestType == 0x1)	//recip is interface
				{
					MSG_DEBUG("ERROR - Device is in Addressed State, but for interface\n");
					ReqErr=1;
				}
			}

			if(ReqErr == 1) 
			{
				MSG_DEBUG("ERROR - CepEvHndlr:USBR_CLEAR_FEATURE <= Request Error\n");
				break;	//break this switch loop
			}

			switch((_usb_cmd_pkt.bmRequestType & 0x3))
			{
				case 0:		//device 
					if((_usb_cmd_pkt.wValue & 0x3) == DEVICE_REMOTE_WAKEUP)
						disableremotewakeup = 1;
					else
					{
						MSG_DEBUG("ERROR - No such feature for device\n");
						ReqErr=1;
					}
					break;

				case 1:		//interface
					break;

				case 2:		//endpoint
					if((_usb_cmd_pkt.wValue & 0x3) == ENDPOINT_HALT)
					{
						if((_usb_cmd_pkt.wIndex & 0x3) == 0) //endPoint zero
							_usbd_unhaltep = 0;
						else if((_usb_cmd_pkt.wIndex & 0x3) == 1) //endPoint one
							_usbd_unhaltep = 1;
						else if((_usb_cmd_pkt.wIndex & 0x3) == 2) //endPoint two
							_usbd_unhaltep = 2;
						else
						{
							MSG_DEBUG("ERROR - endpoint choosen was not supported\n");
							ReqErr=1;
						}
					}
					else
					{
						MSG_DEBUG("ERROR - Neither device,interface nor endpoint was choosen\n");
						ReqErr=1;
					}
					break;

				default:
					break;
			}	//device

			outpw(REG_USBD_CEP_IRQ_STAT, 0x400);
			outpw(REG_USBD_CEP_IRQ_ENB, 0x400);		//suppkt int ,status and in token
			outpw(REG_USBD_CEP_CTRL_STAT, CEP_NAK_CLEAR);	//clear nak so that sts stage is complete
			outpw(REG_USBD_CEP_CTRL_STAT, inpw(REG_USBD_CEP_CTRL_STAT) | 0x04);	// zero len
			break;

		case USBR_GET_STATUS:
			//check if this is valid
			ReqErr = (((_usb_cmd_pkt.bmRequestType & 0xfc) == 0x80) && (_usb_cmd_pkt.wValue == 0) 
			&& ((_usb_cmd_pkt.wIndex & 0xff00) == 0) && (_usb_cmd_pkt.wLength == 0x2)) ? 0 : 1;

			if (_usbd_devstate == 1)
			{
				MSG_DEBUG("ERROR - Device is in default State\n");
				ReqErr =1;
			}
			if (_usbd_devstate == 2)
			{
				if ((_usb_cmd_pkt.bmRequestType & 0x3 == 0x2) && (_usb_cmd_pkt.wIndex != 0))
				{
					MSG_DEBUG("ERROR - Device is in Addressed State, but for noncep\n");
					ReqErr =1;
				}
				else if (_usb_cmd_pkt.bmRequestType & 0x3 == 0x1)
				{
					MSG_DEBUG("ERROR - Device is in Addressed State, but for interface\n");
					ReqErr =1;
				}
			}

			if (ReqErr == 1)
			{
				MSG_DEBUG("ERROR - CepEvHndlr:USBR_GET_STATUS <= Request Error\n");
				break;	//break this switch loop
			}

			switch (_usb_cmd_pkt.bmRequestType & 0x3)
			{
				case 0:
					MSG_DEBUG("value of dx->remotewakeup is %d\n", remotewakeup);
					if (remotewakeup == 1)
						usbdGetStatusData = 0x3;
					else 
						usbdGetStatusData = 0x1;
					break;

				case 1:	//interface
					if (_usb_cmd_pkt.wIndex == 0)
					{
						MSG_DEBUG("Status of interface zero\n");
						usbdGetStatusData = 0;
					}
					else
					{
						MSG_DEBUG("Error - Status of interface non zero\n");
						ReqErr=1;
					}
					break;

				case 2:	//endpoint
					if (_usb_cmd_pkt.wIndex == 0x0)
					{
						MSG_DEBUG("Status of Endpoint zero\n");
						usbdGetStatusData = 0;
					}
					else if (_usb_cmd_pkt.wIndex == 0x81)
					{
						MSG_DEBUG("Status of Endpoint one\n");
						if (_usbd_haltep == 1)
							usbdGetStatusData = 0x1;
						else
							usbdGetStatusData = 0;
					}
					else if (_usb_cmd_pkt.wIndex == 0x2)
					{
						MSG_DEBUG("Status of Endpoint two\n");
						if (_usbd_haltep == 2)
							usbdGetStatusData = 0x1;
						else
							usbdGetStatusData = 0;
					}
					else
					{
						MSG_DEBUG("Error - Status of non-existing Endpoint\n");
						ReqErr=1;
					}
					break;

				default:
					break;
			}
			outpw(REG_USBD_CEP_IRQ_STAT, 0x408);
			outpw(REG_USBD_CEP_IRQ_ENB, 0x408);		//suppkt int ,status and in token
			outpw(REG_USBD_CEP_CTRL_STAT, CEP_NAK_CLEAR);	//clear nak so that sts stage is complete
			break;

		default:
			break;
	}
	if (ReqErr == 1)
	{
		// not supported request, send stall
		outpw(REG_USBD_CEP_CTRL_STAT, CEP_SEND_STALL);
	}
}

void usbd_EP_ISR(UINT32 IrqSt)
{
	if (IrqSt & EP_BUFF_FULL);
		//MSG_DEBUG("Buffer Full Interrupt\n");

	if (IrqSt & EP_IN_TOK);
		//MSG_DEBUG("In Token Interrupt\n");

	if (IrqSt & EP_DATA_TXD)
	{
		MSG_DEBUG("Data Txd \n");
	}

	if (IrqSt & EP_BUFF_EMPTY);
		//MSG_DEBUG("Buffer Empty Interrupt\n");

	if (IrqSt & EP_SHORT_PKT);
		//MSG_DEBUG("Short Packet Interrupt\n");

	if (IrqSt & EP_OUT_TOK);
		//MSG_DEBUG("Out Token Interrupt\n");

	if (IrqSt & EP_DATA_RXD)
	{
		MSG_DEBUG("Data Rxd\n");
	}

	if (IrqSt & EP_PING_TOK);
		//MSG_DEBUG("Ping Token Interrupt\n");

	if (IrqSt & EP_NAK_SENT);
		//MSG_DEBUG("Nak Sent Interrupt\n");

	if (IrqSt & EP_STALL_SENT);
		//MSG_DEBUG("Stall Sent Interrupt\n");

	if (IrqSt & EP_USB_ERR);
		//MSG_DEBUG("USB Error Interrupt\n");
}

void usb_isr(void)
{
	UINT32 volatile IrqStL, IrqEnL, IrqSt, IrqEn;

⌨️ 快捷键说明

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