📄 usb.c
字号:
// remote wakeup and self powered states. // case USB_RT_RECIPIENT_DEVICE: { // // The player is self powered and does not support remote wakeup. // ucStatus[0] = USB_DEVICE_STATUS_SELF_POWERED; ucStatus[1] = 0; // // Send our response back to the host. // USBSendControl(ucStatus, 2); // // We're done handling this request. // break; } // // If the recipient is a device interface, return a value of // 0x00 as required by the USB spec. // case USB_RT_RECIPIENT_INTERFACE: { // // The USB spec. requires a GetStatus request for an interface // return a pair of zero bytes. // ucStatus[0] = 0; ucStatus[1] = 0; // // Send our response back to the host. // USBSendControl(ucStatus, 2); // // We're done handling this request. // break; } // // If the recipient is an endpoint, determine whether it is stalled or // not and return that information to the host. // case USB_RT_RECIPIENT_ENDPOINT: { // // Find out which endpoint is the recipient of the request. // ulEndpoint = sUSB.sControlOut.wIndex & USB_ENDPOINT_ADDRESS_MASK; // // Determine whether the IN or the OUT endpoint is being addressed // in the device request. // ulEndpoint *= 2; if(sUSB.sControlOut.wIndex & USB_ENDPOINT_DIRECTION_MASK) { ulEndpoint++; } // // Make sure that the specified endpoint is valid. // if((ulEndpoint != USB_ENDPOINT_CONTROL_OUT) && (ulEndpoint != USB_ENDPOINT_CONTROL_IN) && (ulEndpoint != USB_ENDPOINT_BULK_OUT) && (ulEndpoint != USB_ENDPOINT_BULK_IN)) { // // An invalid endpoint was specified, so stall both control // endpoints. // USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1); USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1); // // There is nothing further to do. // return; } // // Read the endpoint status. // USBWriteCommand(USB_COMMAND_SELECT_ENDPOINT + ulEndpoint); ulEndpoint = USBReadData(); // // Send the endpoint's status to the host. // if(ulEndpoint & USB_ENDPOINT_STALL) { ucStatus[0] = USB_ENDPOINT_STATUS_STALLED; } else { ucStatus[0] = 0; } ucStatus[1] = 0; // // Send our response back to the host. // USBSendControl(ucStatus, 2); // // We're done handling this request. // break; } // // If an invalid request is received, stall the control endpoint. // default: { // // Stall the both control endpoints. // USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1); USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1); // // We're done handling this request. // break; } }}//****************************************************************************//// USBClearFeature implements the USB Clear_Feature device request.////****************************************************************************static voidUSBClearFeature(void){ unsigned long ulEndpoint; // // The only feature we support is stall on an endpoint. // if(((sUSB.sControlOut.bmRequestType & USB_RT_RECIPIENT_MASK) == USB_RT_RECIPIENT_ENDPOINT) && (sUSB.sControlOut.wValue == USB_FEATURE_ENDPOINT_STALL)) { // // Compute the endpoint number. // ulEndpoint = (sUSB.sControlOut.wIndex & USB_ENDPOINT_ADDRESS_MASK) * 2; if(sUSB.sControlOut.wIndex & USB_ENDPOINT_DIRECTION_MASK) { ulEndpoint++; } // // Make sure that the specified endpoint is valid. // if((ulEndpoint != USB_ENDPOINT_CONTROL_OUT) && (ulEndpoint != USB_ENDPOINT_CONTROL_IN) && (ulEndpoint != USB_ENDPOINT_BULK_OUT) && (ulEndpoint != USB_ENDPOINT_BULK_IN)) { // // An invalid endpoint was specified, so stall both control // endpoints. // USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1); USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1); // // There is nothing further to do. // return; } // // Clear the stall condition on the specified endpoint. // USBStallEndpoint(ulEndpoint, 0); } else { // // An unknown feature was specified, so stall both control endpoints. // USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1); USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1); }}//****************************************************************************//// USBSetFeature implements the USB Set_Feature device request.////****************************************************************************static voidUSBSetFeature(void){ unsigned long ulEndpoint; // // The only feature we support is stall on an endpoint. // if(((sUSB.sControlOut.bmRequestType & USB_RT_RECIPIENT_MASK) == USB_RT_RECIPIENT_ENDPOINT) && (sUSB.sControlOut.wValue == USB_FEATURE_ENDPOINT_STALL)) { // // Compute the endpoint number. // ulEndpoint = (sUSB.sControlOut.wIndex & USB_ENDPOINT_ADDRESS_MASK) * 2; if(sUSB.sControlOut.wIndex & USB_ENDPOINT_DIRECTION_MASK) { ulEndpoint++; } // // Make sure that the specified endpoint is valid. // if((ulEndpoint != USB_ENDPOINT_CONTROL_OUT) && (ulEndpoint != USB_ENDPOINT_CONTROL_IN) && (ulEndpoint != USB_ENDPOINT_BULK_OUT) && (ulEndpoint != USB_ENDPOINT_BULK_IN)) { // // An invalid endpoint was specified, so stall both control // endpoints. // USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1); USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1); // // There is nothing further to do. // return; } // // Set the stall condition on the specified endpoint. // USBStallEndpoint(ulEndpoint, 1); } else { // // An unknown feature was specified, so stall both control endpoints. // USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1); USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1); }}//****************************************************************************//// USBSetAddress implements the USB Set_Address device request.////****************************************************************************static voidUSBSetAddress(void){ // // Configure our UBS controller for the USB address assigned by the host. // USBWriteCommand(USB_COMMAND_SET_ADDRESS_ENABLE); USBWriteData(sUSB.sControlOut.wValue | 0x80); // // Respond to the host with an empty packet. // USBSendControl(0, 0);}//****************************************************************************//// USBGetDescriptor implements the USB Get_Descriptor device request.////****************************************************************************static voidUSBGetDescriptor(void){ const unsigned char *pucDescriptor = 0; unsigned long ulLength = 0; // // Determine how to handle this request based on the requested descriptor. // switch(sUSB.sControlOut.wValue & USB_DESCRIPTOR_TYPE_MASK) { // // The device descriptor was requested. // case USB_DESCRIPTOR_DEVICE: { // // Prepare to return the device descriptor. // pucDescriptor = ucDeviceDescriptor; ulLength = sizeof(ucDeviceDescriptor); // // We're done handling this request. // break; } // // The configuration descriptor was requested. // case USB_DESCRIPTOR_CONFIGURATION: { // // Prepare to return the configuration descriptor. // pucDescriptor = ucConfigurationDescriptor; ulLength = sizeof(ucConfigurationDescriptor); // // We're done handling this request. // break; } // // A string descriptor was requested. // case USB_DESCRIPTOR_STRING: { // // Make sure that the language and string index requested are // valid. // if(((sUSB.sControlOut.wValue & USB_DESCRIPTOR_INDEX_MASK) != 0) && ((sUSB.sControlOut.wIndex != 0x0409) || ((sUSB.sControlOut.wValue & USB_DESCRIPTOR_INDEX_MASK) > 3))) { // // Stall both of the control endpoints. // USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1); USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1); // // We're done handling this request. // return; } // // Prepare to return the requested string. // switch(sUSB.sControlOut.wValue & USB_DESCRIPTOR_INDEX_MASK) { // // String index 0 is the language ID string. // case 0x00: { pucDescriptor = ucString0; ulLength = sizeof(ucString0); break; } // // String index 1 is the manufacturer name. // case 0x01: { pucDescriptor = ucString1; ulLength = sizeof(ucString1); break; } // // String index 2 is the product name. // case 0x02: { pucDescriptor = ucString2; ulLength = sizeof(ucString2); break; } // // String index 3 is the configuration name. // case 0x03: { pucDescriptor = ucString3; ulLength = sizeof(ucString3); break; } } // // We're done handling this request. // break; } // // An invalid request is received, so stall both control endpoints. // default: { // // Stall both of the control endpoints. // USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1); USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1); // // We're done handling this request. // return; } } // // If the requested length is less than the length of the descriptor to be // returned, then simply return the requested portion of the descriptor. // if(sUSB.sControlOut.wLength < ulLength) { ulLength = sUSB.sControlOut.wLength; } // // Send the descriptor back to the host. // USBSendControl(pucDescriptor, ulLength);}//****************************************************************************//// USBGetConfiguration implements the USB Get_Configuration device request.////****************************************************************************static voidUSBGetConfiguration(void){ unsigned char ucConfiguration = sUSB.ucFlags & 1; // // Send the current configuration value back to the host. // USBSendControl(&ucConfiguration, 1);}//****************************************************************************//// USBSetConfiguration implements the USB Set_Configuration device request.//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -