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

📄 f320_usb_standard_requests.c

📁 USB_Audio c8051Fxxx
💻 C
📖 第 1 页 / 共 2 页
字号:
   if ((Setup.bmRequestType != IN_DEVICE) || (Setup.wIndex.i != 0) ||
   (Setup.wLength.i != 0) || (Setup.wValue.i > 127))
   {
     Force_Stall();                    // Send stall if setup data invalid
   }

   // Set endpoint zero status to update address next status phase
   Ep_Status[0] = EP_ADDRESS;

   // Indicate that device state is now addressed or
   // If new address was 0x00, return device to default state
   if (Setup.wValue.c[LSB] != 0) USB_State = DEV_ADDRESS;
   else USB_State = DEV_DEFAULT;

   // Indicate setup packet has been serviced
   if (Ep_Status[0] != EP_STALL) POLL_WRITE_BYTE (E0CSR, rbSOPRDY);
}

//-----------------------------------------------------------------------------
// Get_Descriptor
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine sets the data pointer and size to correct descriptor and sets
// the endpoint status to transmit mode.
//
//-----------------------------------------------------------------------------
void Get_Descriptor (void) using USB_REGISTER_BANK
{
   // Determine which type of descriptor was requested, set data ptr and size
   switch (Setup.wValue.c[MSB])
   {
      case DSC_DEVICE:
         DataPtr = (BYTE*) &DeviceDesc;
         DataSize = DeviceDesc.bLength;
         break;

      case DSC_CONFIG:
         DataPtr = (BYTE*) &ConfigDesc;
         // Compiler Specific - the next statement reverses bytes in the
         // configuration descriptor for the compiler
         DataSize = (ConfigDesc.cd.wTotalLength.c[MSB] +
         256*ConfigDesc.cd.wTotalLength.c[LSB]);
         break;

      case DSC_STRING:                 // Can have a maximum of 255 strings
         DataPtr = StringDescTable[Setup.wValue.c[LSB]];
         DataSize = *DataPtr;
         break;

      case DSC_HID_CLASS:              // HID Specific (HID class descriptor)
		   DataPtr = (BYTE*) &ConfigDesc.hd;
		   DataSize = ConfigDesc.hd.bLength;
		   break;

	   case DSC_HID_REPORT:             // HID Specific (HID report descriptor)
		   DataPtr = (BYTE*) &HidReportDesc;
		   DataSize = HID_REPORT_SIZE;
		   break;

      default:
         Force_Stall ();               // Send Stall if unsupported request
         break;
   }

   // Send only requested amount of data
   if (Setup.wLength.i < DataSize) DataSize = Setup.wLength.i;

   if (Ep_Status[0] != EP_STALL)
   {
      // Set serviced setup packet bit, put endpoint in transmit mode and reset
      // data sent counter
      POLL_WRITE_BYTE (E0CSR, rbSOPRDY);
      Ep_Status[0] = EP_TX;
      DataSent = 0;
   }
}

//-----------------------------------------------------------------------------
// Get_Configuration
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine returns current configuration value.
//
//-----------------------------------------------------------------------------
void Get_Configuration (void) using USB_REGISTER_BANK
{
   // This request must be directed to the device with value word and index set
   // to zero, and setup length set to one
   if ((Setup.bmRequestType != OUT_DEVICE) || (Setup.wValue.i != 0) ||
   (Setup.wIndex.i != 0) || (Setup.wLength.i != 1))
   {
      Force_Stall ();                  // Otherwise send a stall to host
   }

   else
   {
      DataSize = 1;
      // If the device is configured return 0x01 since this software supports
      // one configuration, otherwise return 0x00 since it isn't configured
      if (USB_State == DEV_CONFIGURED) DataPtr = (BYTE*)&ONES_PACKET;
      else DataPtr = (BYTE*)&ZERO_PACKET;
   }

   if (Ep_Status[0] != EP_STALL)
   {
      // Set serviced setup packet bit, put endpoint in transmit mode and reset
      // data sent counter
      POLL_WRITE_BYTE (E0CSR, rbSOPRDY);
      Ep_Status[0] = EP_TX;
      DataSent = 0;
   }
}

//-----------------------------------------------------------------------------
// Set_Configuration
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine allows host to change current device configuration value.
//
//-----------------------------------------------------------------------------
void Set_Configuration (void) using USB_REGISTER_BANK
{
   // Device must be addressed before configured and request recipient must be
   // the device, the index and length words must be zero
   // Verify configuration is set to 0 or 1
   if ((USB_State == DEV_DEFAULT) || (Setup.bmRequestType != IN_DEVICE) ||
   (Setup.wIndex.i != 0) || (Setup.wLength.i != 0) ||  (Setup.wValue.i > 1))
   {
      Force_Stall();                   // Send stall if setup data is invalid
   }
   else
   {
      if (Setup.wValue.c[LSB] > 0)
      {
         USB_State = DEV_CONFIGURED;
         Ep_Status[1] = EP_IDLE;       // Set endpoint status to idle (enabled)
         Ep_Status[2] = EP_IDLE;
         Ep_Status[3] = EP_IDLE;

         POLL_WRITE_BYTE (INDEX, 1);   // Change index to endpoint 1
		   // Set endpoint in direction with interrupt settings
         POLL_WRITE_BYTE (EINCSR2, rbInDIRSEL);

         POLL_WRITE_BYTE (INDEX, 2);   // Change index to endpoint 2
		   // Set endpoint out direction with double buffered interrupt settings
         POLL_WRITE_BYTE (EOUTCSR2, rbOutDBOEN);

         POLL_WRITE_BYTE (INDEX, 3);   // Change index to endpoint 3
		   // Set endpoint in direction with isochronous settings
         POLL_WRITE_BYTE (EINCSR2, rbInDIRSEL | rbInISO);
         POLL_WRITE_BYTE (INDEX, 0);   // Set index back to endpoint 0
      }
      else
      {
         USB_State = DEV_ADDRESS;      // Unconfigures device by setting state
         Ep_Status[1] = EP_HALT;       // to address, and changing endpoints
         Ep_Status[2] = EP_HALT;       // status to halt
         Ep_Status[3] = EP_HALT;
      }
   }

   // Indicate setup packet has been serviced
   if (Ep_Status[0] != EP_STALL) POLL_WRITE_BYTE (E0CSR, rbSOPRDY);
}

//-----------------------------------------------------------------------------
// Get_Interface
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine returns the currently selected interface.
//
//-----------------------------------------------------------------------------
void Get_Interface (void) using USB_REGISTER_BANK
{
   // If device is not configured or recipient is not an interface or non-zero
   // value field or data length not equal to one
   if ((USB_State != DEV_CONFIGURED) || (Setup.wValue.i != 0) ||
   (Setup.bmRequestType != OUT_INTERFACE) || (Setup.wLength.i != 1))
   {
      Force_Stall ();                  // Then return stall to host
   }

   else
   {
      DataSize = 1;
      if ((Setup.wIndex.i == 0) || (Setup.wIndex.i == 2))
      {
         // Return 0x00 to host
         DataPtr = (BYTE*)&ZERO_PACKET;
      }
      else
      {
         if (Setup.wIndex.i == 1) DataPtr = (BYTE*)&Selected_Interface1;
         else Force_Stall ();
      }
   }

   if (Ep_Status[0] != EP_STALL)
   {
      // Set serviced setup packet bit, put endpoint in transmit mode and reset
      // data sent counter
      POLL_WRITE_BYTE (E0CSR, rbSOPRDY);
      Ep_Status[0] = EP_TX;
      DataSent = 0;
   }
}

//-----------------------------------------------------------------------------
// Set_Interface
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine allows the host to change the selected interface.
//
//-----------------------------------------------------------------------------
void Set_Interface (void) using USB_REGISTER_BANK
{
   // Make sure request is correct length and is directed at interface
   if ((Setup.bmRequestType != IN_INTERFACE) || (Setup.wLength.i != 0))
   {
      Force_Stall ();                  // Otherwise send a stall to host
   }
   else
   {
      if (Setup.wIndex.i == 1) Selected_Interface1 = (BYTE)Setup.wValue.i;
   }

   // Indicate setup packet has been serviced
   if (Ep_Status[0] != EP_STALL) POLL_WRITE_BYTE (E0CSR, rbSOPRDY);
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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