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

📄 usb.c

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 C
📖 第 1 页 / 共 5 页
字号:
//****************************************************************************static voidUSBSetConfiguration(void){    //    // If the requested configuration is zero, then go into the unconfigured    // state.    //    if(sUSB.sControlOut.wValue == 0)    {        //        // Clear the global configuration flag.        //        sUSB.ucFlags &= ~1;        //        // Disable the generic endpoints.        //        USBWriteCommand(USB_COMMAND_SET_ENDPOINT_ENABLE);        USBWriteData(0);        //        // Respond to the host with an empty packet.        //        USBSendControl(0, 0);    }    //    // If the requested configuration is one, then go into the configured    // state.    //    else if(sUSB.sControlOut.wValue == 1)    {        //        // Set the global configuration flag.        //        sUSB.ucFlags |= 1;        //        // Disable the generic endpoints.        //        USBWriteCommand(USB_COMMAND_SET_ENDPOINT_ENABLE);        USBWriteData(0);        //        // Enable the generic endpoints.        //        USBWriteCommand(USB_COMMAND_SET_ENDPOINT_ENABLE);        USBWriteData(1);        //        // Respond to the host with an empty packet.        //        USBSendControl(0, 0);    }    //    // If the requested configuration is anything else, then stall both of the    // control endpoints.    //    else    {        USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1);        USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1);    }}//****************************************************************************//// USBGetInterface implements the USB Get_Interface device request.////****************************************************************************static voidUSBGetInterface(void){    unsigned char ucInterface = 0;    //    // We only support a single interface, so the current interface is always    // the first one.  Send our response back to the host.    //    USBSendControl(&ucInterface, 1);}//****************************************************************************//// USBSetInterface implements the USB Set_Interface device request.////****************************************************************************static voidUSBSetInterface(void){    //    // We only support a single interface.    //    if((sUSB.sControlOut.wValue == 0) && (sUSB.sControlOut.wIndex == 0))    {        //        // The first interface was requested, so do nothing.        //        return;    }    else    {        //        // A non-existent interface was requested, so stall both control        // endpoints.        //        USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1);        USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1);    }}//****************************************************************************//// USBReserved handles device requests which are not supported by this USB// device implementation.////****************************************************************************static voidUSBReserved(void){    //    // Stall both control endpoints.    //    USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1);    USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1);}//****************************************************************************//// USBVendor handles vendor specific device requests.////****************************************************************************static voidUSBVendor(void){    //    // Make sure that a valid vendor request was received.    //    if((sUSB.sControlOut.bRequest & 31) >= USB_Vendor_LastCommand)    {        //        // This is an unsupported vendor request.        //        USBReserved();    }    //    // Handle all other requests.    //    else    {        //        // If this is a connect request, then we should start the download        // process.        //        if(sUSB.sControlOut.bRequest == USB_Vendor_Connect)        {            //            // Indicate that a download has been requested.            //            ulSystemFlags |= SYSFLAG_START_DOWNLOAD;        }        //        // We have a vendor message which we must handled outside the interrupt        // handler.        //        sUSB.ucHaveVendorMessage = 1;    }}//****************************************************************************//// USBISR is the interrupt handler routine for the USB.////****************************************************************************voidUSBISR(void){    unsigned long ulIntStatus, ulTransactionStatus;    unsigned short usLength;    unsigned char *pucChar;    //    // Read the PDIUSBD12 interrupt register.    //    USBWriteCommand(USB_COMMAND_READ_INTERRUPT);    ulIntStatus = USBReadData();    ulIntStatus |= USBReadData() << 8;    //    // Do nothing if there was a bus reset.    //    if(ulIntStatus & USB_INT1_BUS_RESET)    {        return;    }    //    // Handle an interrupt on the bulk out endpoint.    //    if(ulIntStatus & USB_INT1_ENDPOINT2_OUT)    {        //        // Read the status of the last transaction on the bulk out endpoint.        //        USBWriteCommand(USB_COMMAND_READ_LAST_XACTION_STATUS +                        USB_ENDPOINT_BULK_OUT);        USBReadData();        //        // Loop while there is data in the bulk out buffers.        //        while(USBWriteCommand(USB_COMMAND_GET_ENDPOINT_STATUS +                              USB_ENDPOINT_BULK_OUT),              (USBReadData() & (USB_ENDPOINT_BUFFER0_FULL |                                USB_ENDPOINT_BUFFER1_FULL)))        {            //            // Read the next data packet.            //            USBReadEndpoint(USB_ENDPOINT_BULK_OUT, &sUSB.pucBulkOut,                            &sUSB.usBulkOutCount);        }    }    //    // Handle an interrupt on the control out endpoint.    //    if(ulIntStatus & USB_INT1_CONTROL_OUT)    {        //        // Read the status of the last transaction on the control out endpoint.        //        USBWriteCommand(USB_COMMAND_READ_LAST_XACTION_STATUS +                        USB_ENDPOINT_CONTROL_OUT);        ulTransactionStatus = USBReadData();        //        // Was a setup packet received?        //        if(ulTransactionStatus & USB_XACTION_STATUS_SETUP_PACKET)        {            //            // Read the packet.            //            pucChar = (unsigned char *)&sUSB.sControlOut;            usLength = sizeof(ControlTransfer);            if(USBReadEndpoint(USB_ENDPOINT_CONTROL_OUT, &pucChar,                               &usLength) != sizeof(ControlTransfer))            {                //                // The size of the setup packet is incorrect, so stall both of                // the control endpoints.                //                USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1);                USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1);            }            else            {                //                // Acknowledge both the control in and control out endpoints,                // and send a clear buffer command to the control out endpoint.                //                USBWriteCommand(USB_COMMAND_SELECT_ENDPOINT +                                USB_ENDPOINT_CONTROL_OUT);                USBWriteCommand(USB_COMMAND_ACKNOWLEDGE_ENDPOINT);                USBWriteCommand(USB_COMMAND_CLEAR_BUFFER);                USBWriteCommand(USB_COMMAND_SELECT_ENDPOINT +                                USB_ENDPOINT_CONTROL_IN);                USBWriteCommand(USB_COMMAND_ACKNOWLEDGE_ENDPOINT);                //                // Process the command in the setup packet.                //                if(((sUSB.sControlOut.bmRequestType & USB_RT_TYPE_MASK) ==                    USB_RT_TYPE_STANDARD) && (sUSB.sControlOut.bRequest < 16))                {                    //                    // This is a standard request, so call the appropriate                    // routine.                    //                    (*USBStandardDeviceRequest[sUSB.sControlOut.bRequest])();                }                else if((sUSB.sControlOut.bmRequestType & USB_RT_TYPE_MASK) ==                        USB_RT_TYPE_VENDOR)                {                    //                    // This is a vendor specific request.                    //                    USBVendor();                }                else                {                    //                    // All other requests are treated as reserved requests.                    //                    USBReserved();                }            }        }        else        {            //            // The packet was not a setup packet, so ignore it.  Send a clear            // buffer command to the control out endpoint so it will receive            // new packets.            //            USBWriteCommand(USB_COMMAND_SELECT_ENDPOINT +                            USB_ENDPOINT_CONTROL_OUT);            USBWriteCommand(USB_COMMAND_CLEAR_BUFFER);            //            // Acknowledge both the control in and control out endpoints.            //            USBWriteCommand(USB_COMMAND_ACKNOWLEDGE_ENDPOINT);            USBWriteCommand(USB_COMMAND_SELECT_ENDPOINT +                            USB_ENDPOINT_CONTROL_IN);            USBWriteCommand(USB_COMMAND_ACKNOWLEDGE_ENDPOINT);        }    }    //    // Handle an interrupt on the control in endpoint.    //    if(ulIntStatus & USB_INT1_CONTROL_IN)    {        //        // Read the status of the last transaction on the control in endpoint.        //        USBWriteCommand(USB_COMMAND_READ_LAST_XACTION_STATUS +                        USB_ENDPOINT_CONTROL_IN);        USBReadData();        //        // Was this the last block of data to be sent back to the host?        //        if(sUSB.usControlInCount != 0)        {            //            // Send the next packet of data to the host.            //            USBWriteEndpoint(USB_ENDPOINT_CONTROL_IN, &sUSB.pucControlIn,                             &sUSB.usControlInCount);        }        else        {            //            // There is no more data to send, so send an empty packet.            //            usLength = 0;            USBWriteEndpoint(USB_ENDPOINT_CONTROL_IN, 0, &usLength);        }    }    //    // Handle an interrupt on the bulk in endpoint.    //    if(ulIntStatus & USB_INT1_ENDPOINT2_IN)    {        //        // Read the status of the last transaction on the bulk in endpoint.        //        USBWriteCommand(USB_COMMAND_READ_LAST_XACTION_STATUS +                        USB_ENDPOINT_BULK_IN);        USBReadData();        //        // Was this the last block of data to be sent back to the host?        //        if(sUSB.usBulkInCount != 0)        {            //            // Send the next packet of data to the host.            //            USBWriteEndpoint(USB_ENDPOINT_BULK_IN, &sUSB.pucBulkIn,                             &sUSB.usBulkInCount);        }    }}//****************************************************************************//// USBCheck determines if the USB cable has been connected or removed and// configures the USB controller as appropriate.////****************************************************************************#ifdef HwRegUSBCablevoidUSBCheck(void){    volatile unsigned long *pulPtr = (unsigned long *)HwBaseAddress;    unsigned long bConnected;    //    // Determine if the USB cable is connected or removed.    //    bConnected =        ((pulPtr[HwRegUSBCable >> 2] & HwBitUSBCable) == HwUSBCableConnected);    //    // If the cable is connected and we have not configured the USB controller,    // then do so now.    //    if(bConnected && !(sUSB.ucFlags & 2))    {        //        // Enable the USB controller.        //        USBEnable();    }    //    // Otherwise, if the cable is not connected and we have not unconfigured    // the USB controller, then do so now.    //    else if(!bConnected && (sUSB.ucFlags & 2))    {        //        // Disable the USB controller.        //        USBDisable();    }}#endif//****************************************************************************//// USBDownload communicates with the host via the USB to update the content// stored in the Internet audio player (upload content, download content,// remove content, etc.).////****************************************************************************voidUSBDownload(unsigned char *pucData){    unsigned char ucFlags = 0;    unsigned long ulResult[2];    tFile sFile;    tDir sDir;    //    // Use the first 512 bytes of the scratch space for the write buffer needed    // by the file system.    //    FSSetWriteScratch(pucData);    pucData += 512;    //    // Setup a buffer to receive data from the bulk endpoint.    //    USBReceiveBulk(pucData, 16384);    //    // Loop forever.  This loop will be explicitly broken out of when the host    // "disconnects" from the player (i.e. sends the USB_Vendor_Disconnect    // message).    //

⌨️ 快捷键说明

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