예제 #1
0
void USB_EndPoint0 (uint32_t event)
{
	switch (event)
	{
		case USB_EVT_SETUP:
			USB_SetupStage();
			EP0Data.Count = SetupPacket.wLength;
			switch (SetupPacket.bmRequestType.BM.Type)
			{
				case REQUEST_STANDARD:
					switch (SetupPacket.bRequest)
					{
						case USB_REQUEST_SET_ADDRESS:
							switch (SetupPacket.bmRequestType.BM.Recipient)
							{
								case REQUEST_TO_DEVICE:
									USB_DeviceAddress = SetupPacket.wValue.WB.L;//0x80 | SetupPacket.wValue.WB.L;
									USB_StatusInStage();
									USB_SetAddress(USB_DeviceAddress);
									break;
								default:
									break;
              				}
              				break;

						case USB_REQUEST_GET_DESCRIPTOR:
							USB_GetDescriptor();
							break;

					/*	case USB_REQUEST_GET_CONFIGURATION:
							switch (SetupPacket.bmRequestType.BM.Recipient)
							{
								case REQUEST_TO_DEVICE:
									EP0Data.pData = &USB_Configuration;
									USB_DataInStage();
									break;
								default:
									break;
							}
							break;	*/

						case USB_REQUEST_SET_CONFIGURATION:
							switch (SetupPacket.bmRequestType.BM.Recipient)
							{
								case REQUEST_TO_DEVICE:
									if (!USB_SetConfiguration())
									{
										USB_SetStallEP(0x80);
										EP0Data.Count = 0;
										break;
									}
									USB_StatusInStage();
#if USB_CONFIGURE_EVENT
                  					USB_Configure_Event();
#endif
									break;
								default:
									break;
							}
							break;

					default:
						break;

					}
					break;

				case REQUEST_CLASS:
#if USB_CLASS
					switch (SetupPacket.bmRequestType.BM.Recipient)
					{
						case REQUEST_TO_INTERFACE:
#if USB_HID
							if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM)
							{
								switch (SetupPacket.bRequest)
								{
									case HID_REQUEST_GET_REPORT:
										if (HID_GetReport())
										{
											EP0Data.pData = EP0Buf;
											USB_DataInStage();
										}
										break;
									case HID_REQUEST_SET_REPORT:
										EP0Data.pData = EP0Buf;
										break;
									case HID_REQUEST_GET_IDLE:
										if (HID_GetIdle())
										{
											EP0Data.pData = EP0Buf;
											USB_DataInStage();
										}
										break;
									case HID_REQUEST_SET_IDLE:
										if (HID_SetIdle())
										{
											USB_StatusInStage();
										}
										break;
									case HID_REQUEST_GET_PROTOCOL:
										if (HID_GetProtocol())
										{
											EP0Data.pData = EP0Buf;
											USB_DataInStage();
										}
										break;
									case HID_REQUEST_SET_PROTOCOL:
										if (HID_SetProtocol())
										{
											USB_StatusInStage();
										}
										break;
									default:
										break;
								}
							}
#endif  /* USB_HID */
						default:
							break;
					}
					break;
#else
#endif  /* USB_CLASS */

				case REQUEST_VENDOR:
					break;

				default:
					USB_SetStallEP(0x80);
					EP0Data.Count = 0;
					break;
			}
			break;

		case USB_EVT_OUT:
			if (SetupPacket.bmRequestType.BM.Dir == 0)
			{
				if (EP0Data.Count)
				{
					USB_DataOutStage();
					if (EP0Data.Count == 0)
					{
						switch (SetupPacket.bmRequestType.BM.Type)
						{
							case REQUEST_STANDARD:
								USB_SetStallEP(0x80);
								//EP0Data.Count = 0;
								break;
#if (USB_CLASS)
							case REQUEST_CLASS:
								switch (SetupPacket.bmRequestType.BM.Recipient)
								{
									case REQUEST_TO_INTERFACE:
#if USB_HID
										if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM)
										{
											if (!HID_SetReport())
											{
												USB_SetStallEP(0x80);
												//EP0Data.Count = 0;
												//break;
											}
											break;
											//HID_SetReport();
										}
#endif
										USB_SetStallEP(0x80);
										//EP0Data.Count = 0;
										break;
									case REQUEST_TO_ENDPOINT:
										USB_SetStallEP(0x80);
										//EP0Data.Count = 0;
										break;
									default:
										USB_SetStallEP(0x80);
										//EP0Data.Count = 0;
										break;
								}
								break;
#endif
							default:
								USB_SetStallEP(0x80);
								//EP0Data.Count = 0;
								break;
						}
						USB_StatusInStage();
					}
				}
			}
			else
			{
				;//USB_StatusOutStage();
			}


			break;

		case USB_EVT_IN:
			if (SetupPacket.bmRequestType.BM.Dir == 1)
			{
				if(EP0Data.Count > 0) // ysq add
					USB_DataInStage();
			}
			//else
			//{
			//	if (USB_DeviceAddress & 0x80)
			//	{
			//		USB_DeviceAddress &= 0x7F;
			//		USB_SetAddress(USB_DeviceAddress);
			//	}
			//}
			break;

		case USB_EVT_IN_STALL:
			USB_ClrStallEP(0x80);
			break;

		case USB_EVT_OUT_STALL:
			USB_ClrStallEP(0x00);
			break;
	}
}
예제 #2
0
void USB_SetupInterface(void)
{
	USB_INT_DisableAllInterrupts();
	USB_INT_ClearAllInterrupts();

	USB_IsConnected         = false;
	
	#if defined(USB_CAN_BE_BOTH)
	USB_IsInitialized       = false;
	#endif

	#if defined(USB_CAN_BE_HOST)
	USB_HostState           = HOST_STATE_Unattached;
	#endif

	#if defined(USB_CAN_BE_DEVICE)
	USB_ConfigurationNumber  = 0;
	USB_RemoteWakeupEnabled  = false;
	USB_CurrentlySelfPowered = false;
	#endif
	
	#if defined(USB_MODIFIED_FULL_CONTROLLER) && !defined(MANUAL_PLL_CONTROL)
	PLLFRQ = ((1 << PLLUSB) | (1 << PDIV3) | (1 << PDIV1));
	#endif
	
	#if !defined(MANUAL_PLL_CONTROL)
	USB_PLL_On();
	#endif

	while (!(USB_PLL_IsReady()));
		
	USB_Interface_Reset();
	
	#if defined(USB_CAN_BE_BOTH)
	if (UHWCON & (1 << UIDE))
	{
		USB_INT_Clear(USB_INT_IDTI);
		USB_INT_Enable(USB_INT_IDTI);
		USB_CurrentMode = USB_GetUSBModeFromUID();
	}
	#elif defined(USB_DEVICE_ONLY)
		#if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)
		USB_INT_Enable(USB_INT_VBUS);
		#endif
	#endif
		
	if (!(USB_Options & USB_OPT_REG_DISABLED))
	  USB_REG_On();
	
	USB_CLK_Unfreeze();

	#if (defined(USB_CAN_BE_DEVICE) && (defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER)))
	if (USB_CurrentMode == USB_MODE_DEVICE)
	{
		#ifdef CUL_V3
		USB_Device_SetHighSpeed();
		#else
		if (USB_Options & USB_DEVICE_OPT_LOWSPEED)
		  USB_Device_SetLowSpeed();
		else
		  USB_Device_SetHighSpeed();
		#endif
		  
		USB_INT_Enable(USB_INT_VBUS);
	}
	#endif

	#if defined(USB_CAN_BE_HOST)
	USB_INT_Enable(USB_INT_VBERRI);
	
	if (USB_CurrentMode == USB_MODE_HOST)
	  USB_Host_PrepareForDeviceConnect();
	#endif
	
	#if defined(USB_CAN_BE_DEVICE)
	USB_Descriptor_Device_t* DeviceDescriptorPtr;
	uint16_t                 DeviceDescriptorSize;

	if (USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DeviceDescriptorPtr, &DeviceDescriptorSize) == true)
	{		  
		#if defined(USE_RAM_DESCRIPTORS)
			USB_ControlEndpointSize = DeviceDescriptorPtr->Endpoint0Size;
		#elif defined(USE_EEPROM_DESCRIPTORS)
			USB_ControlEndpointSize = eeprom_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
		#else
			USB_ControlEndpointSize = pgm_read_byte(&DeviceDescriptorPtr->Endpoint0Size);
		#endif
	}
	#endif

	USB_Attach();
	
	#if defined(USB_DEVICE_ONLY)	
	USB_INT_Enable(USB_INT_SUSPEND);
	USB_INT_Enable(USB_INT_EORSTI);	
	#elif defined(USB_HOST_ONLY)
	USB_Host_HostMode_On();
	#else
	if (USB_CurrentMode == USB_MODE_DEVICE)
	{
		USB_INT_Enable(USB_INT_SUSPEND);
		USB_INT_Enable(USB_INT_EORSTI);
	}
	else if (USB_CurrentMode == USB_MODE_HOST)
	{
		USB_Host_HostMode_On();
	}
	#endif
	
	#if defined(USB_CAN_BE_BOTH)
	USB_InitTaskPointer();
	#endif
}
예제 #3
0
파일: usbcore.c 프로젝트: ggaavv/G3Firmware
void USB_EndPoint0 (uint32_t event) {

  switch (event) {

    case USB_EVT_SETUP:
      USB_SetupStage();
      USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
      EP0Data.Count = SetupPacket.wLength;
      switch (SetupPacket.bmRequestType.BM.Type) {

        case REQUEST_STANDARD:
          switch (SetupPacket.bRequest) {

            case USB_REQUEST_GET_STATUS:
              if (!USB_GetStatus()) {
                goto stall_i;
              }
              break;

            case USB_REQUEST_CLEAR_FEATURE:
              if (!USB_SetClrFeature(0)) {
                goto stall_i;
              }
              USB_StatusInStage();
#if USB_FEATURE_EVENT
              USB_Feature_Event();
#endif
              break;

            case USB_REQUEST_SET_FEATURE:
              if (!USB_SetClrFeature(1)) {
                goto stall_i;
              }
              USB_StatusInStage();
#if USB_FEATURE_EVENT
              USB_Feature_Event();
#endif
              break;

            case USB_REQUEST_SET_ADDRESS:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_DEVICE:
                  USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
                  USB_StatusInStage();
                  break;
                default:
                  goto stall_i;
              }
              break;

            case USB_REQUEST_GET_DESCRIPTOR:
              if (!USB_GetDescriptor()) {
                goto stall_i;
              }
              break;

            case USB_REQUEST_SET_DESCRIPTOR:
/*stall_o:*/  USB_SetStallEP(0x00);
              EP0Data.Count = 0;
              break;

            case USB_REQUEST_GET_CONFIGURATION:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_DEVICE:
                  EP0Data.pData = &USB_Configuration;
                  USB_DataInStage();
                  break;
                default:
                  goto stall_i;
              }
              break;

            case USB_REQUEST_SET_CONFIGURATION:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_DEVICE:
                  if (!USB_SetConfiguration()) {
                    goto stall_i;
                  }
                  USB_StatusInStage();
#if USB_CONFIGURE_EVENT
                  USB_Configure_Event();
#endif
                  break;
                default:
                  goto stall_i;
              }
              break;

            case USB_REQUEST_GET_INTERFACE:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_INTERFACE:
                  if ((USB_Configuration != 0) &&
                      (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
                    EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
                    USB_DataInStage();
                  } else {
                    goto stall_i;
                  }
                  break;
                default:
                  goto stall_i;
              }
              break;

            case USB_REQUEST_SET_INTERFACE:
              switch (SetupPacket.bmRequestType.BM.Recipient) {
                case REQUEST_TO_INTERFACE:
                  if (!USB_SetInterface()) {
                    goto stall_i;
                  }
                  USB_StatusInStage();
#if USB_INTERFACE_EVENT
                  USB_Interface_Event();
#endif
                  break;
                default:
                  goto stall_i;
              }
              break;

            default:
              goto stall_i;

          }
          break;

        case REQUEST_CLASS:
#if USB_CLASS
          switch (SetupPacket.bmRequestType.BM.Recipient) {
            case REQUEST_TO_INTERFACE:
#if USB_HID
              if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {
                switch (SetupPacket.bRequest) {
                  case HID_REQUEST_GET_REPORT:
                    if (HID_GetReport()) {
                      EP0Data.pData = EP0Buf;
                      USB_DataInStage();
                      goto class_ok;
                    }
                    break;
                  case HID_REQUEST_SET_REPORT:
                    EP0Data.pData = EP0Buf;
                    goto class_ok;
                  case HID_REQUEST_GET_IDLE:
                    if (HID_GetIdle()) {
                      EP0Data.pData = EP0Buf;
                      USB_DataInStage();
                      goto class_ok;
                    }
                    break;
                  case HID_REQUEST_SET_IDLE:
                    if (HID_SetIdle()) {
                      USB_StatusInStage();
                      goto class_ok;
                    }
                    break;
                  case HID_REQUEST_GET_PROTOCOL:
                    if (HID_GetProtocol()) {
                      EP0Data.pData = EP0Buf;
                      USB_DataInStage();
                      goto class_ok;
                    }
                    break;
                  case HID_REQUEST_SET_PROTOCOL:
                    if (HID_SetProtocol()) {
                      USB_StatusInStage();
                      goto class_ok;
                    }
                    break;
                }
              }
#endif  /* USB_HID */
#if USB_MSC
              if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) {
                switch (SetupPacket.bRequest) {
                  case MSC_REQUEST_RESET:
                    if (MSC_Reset()) {
                      USB_StatusInStage();
                      goto class_ok;
                    }
                    break;
                  case MSC_REQUEST_GET_MAX_LUN:
                    if (MSC_GetMaxLUN()) {
                      EP0Data.pData = EP0Buf;
                      USB_DataInStage();
                      goto class_ok;
                    }
                    break;
                }
              }
#endif  /* USB_MSC */
#if USB_AUDIO
              if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||
                  (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
                  (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
                if (SetupPacket.bmRequestType.BM.Dir) {
                  if (ADC_IF_GetRequest()) {
                    EP0Data.pData = EP0Buf;
                    USB_DataInStage();
                    goto class_ok;
                  }
                } else {
                  EP0Data.pData = EP0Buf;
                  goto class_ok;
                }
              }
#endif  /* USB_AUDIO */
              goto stall_i;
#if USB_AUDIO
            case REQUEST_TO_ENDPOINT:
              if (SetupPacket.bmRequestType.BM.Dir) {
                if (ADC_EP_GetRequest()) {
                  EP0Data.pData = EP0Buf;
                  USB_DataInStage();
                  goto class_ok;
                }
              } else {
                EP0Data.pData = EP0Buf;
                goto class_ok;
              }
              goto stall_i;
#endif  /* USB_AUDIO */
            default:
              goto stall_i;
          }
class_ok: break;
#else
          goto stall_i;
#endif  /* USB_CLASS */

        case REQUEST_VENDOR:
          goto stall_i;

        default:
stall_i:  USB_SetStallEP(0x80);
          EP0Data.Count = 0;
          break;

      }
      break;

    case USB_EVT_OUT:
      if (SetupPacket.bmRequestType.BM.Dir == 0) {
        if (EP0Data.Count) {
          USB_DataOutStage();
          if (EP0Data.Count == 0) {
            switch (SetupPacket.bmRequestType.BM.Type) {
              case REQUEST_STANDARD:
                goto stall_i;
#if (USB_CLASS)
              case REQUEST_CLASS:
                switch (SetupPacket.bmRequestType.BM.Recipient) {
                  case REQUEST_TO_INTERFACE:
#if USB_HID
                    if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {
                      if (!HID_SetReport()) {
                        goto stall_i;
                      }
                      break;
                    }
#endif
#if USB_AUDIO
                    if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||
                        (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
                        (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
                      if (!ADC_IF_SetRequest()) {
                        goto stall_i;
                      }
                      break;
                    }
#endif
                    goto stall_i;
                  case REQUEST_TO_ENDPOINT:
#if USB_AUDIO
                    if (ADC_EP_SetRequest()) break;
#endif
                    goto stall_i;
                  default:
                    goto stall_i;
                }
                break;
#endif
              default:
                goto stall_i;
            }
            USB_StatusInStage();
          }
        }
      } else {
        USB_StatusOutStage();
      }
      break;

    case USB_EVT_IN:
      if (SetupPacket.bmRequestType.BM.Dir == 1) {
        USB_DataInStage();
      } else {
        if (USB_DeviceAddress & 0x80) {
          USB_DeviceAddress &= 0x7F;
          USB_SetAddress(USB_DeviceAddress);
        }
      }
      break;

    case USB_EVT_IN_STALL:
      USB_ClrStallEP(0x80);
      break;

    case USB_EVT_OUT_STALL:
      USB_ClrStallEP(0x00);
      break;

  }
}