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

📄 usb.c

📁 一个简单的helloworld编程
💻 C
📖 第 1 页 / 共 4 页
字号:
    // Prepare to read data from the host into this buffer.
    sUSB.pucBulkOut = pucData;
    sUSB.ulBulkOutCount = ulLength;
    return(1);
}

//****************************************************************************
//
// USBReceiveBulkDone determines if the data receive from the bulk endpoint
// has completed.
//
//****************************************************************************
unsigned long
USBReceiveBulkDone(void)
{
    // Is there more data to receive from the bulk endpoint?
    if(sUSB.ulBulkOutCount)
    {
        // There's more data to receive, so indicate that we're not done.
        return(0);
    }
    else
    {
        // We've received all the data, so indicate that we're done.
        return(1);
    }
}

//****************************************************************************
//
// USBReceiveCommand reads a block of data from the host via the bulk endpoint 1.
//
//****************************************************************************
unsigned long
USBReceiveCommand(unsigned char *pucData, unsigned long ulLength)
{
    // If a block is already being read, then return a failure.
    if(sUSB.ulCommandOutCount)
    {
        return(0);
    }

    // Prepare to read data from the host into this buffer.
    sUSB.pucCommandOut = pucData;
    sUSB.ulCommandOutCount = ulLength;
    return(1);
}

//****************************************************************************
//
// USBReceiveCommandDone determines if the data receive from the bulk endpoint
// has completed.
//
//****************************************************************************
unsigned long
USBReceiveCommandDone(void)
{
    // Is there more data to receive from the bulk endpoint?
    if(sUSB.ulCommandOutCount)
    {     // There's more data to receive, so indicate that we're not done.
         return(0);
    }
    else
    {
        // We've received all the data, so indicate that we're done.
     return(1);
    }
}

//****************************************************************************
//
// USBSendACKDone determines if the data transmit to the bulk endpoint 2 has
// completed.
//
//****************************************************************************
unsigned long
USBSendACKDone(void)
{
     // Is there more data to be sent to the bulk endpoint?
    if(sUSB.ulACKInCount != 0)
    {
        // There's more data to transmit, so indicate that we're not done.
         return(0);
    }
    else
    {   // We've sent all the data, so indicate that we're done.
         return(1);
    }
}

//****************************************************************************
//
// USBSendBulk transmits a block of data back to the host via the bulk
// endpoint.
//
//****************************************************************************
unsigned long
USBSendBulk(const unsigned char *pucData, unsigned long ulLength)
{
    // If a block is already being transmitted, then return a failure.
    if(sUSB.ulBulkInCount)
    {
        return(0);
    }
    // Prepare to transmit this block back to the host.
    sUSB.pucBulkIn = pucData;
    sUSB.ulBulkInCount = ulLength;

     USBWriteEndpoint(USB_ENDPOINT_BULK_IN, &sUSB.pucBulkIn, &sUSB.ulBulkInCount);

    return(1);
}

//****************************************************************************
//
// USBSendBulkDone determines if the data transmit to the bulk endpoint has
// completed.
//
//****************************************************************************
unsigned long
USBSendBulkDone(void)
{
    // Is there more data to be sent to the bulk endpoint?
    if(sUSB.ulBulkInCount)
    {
        // There's more data to transmit, so indicate that we're not done.
        return(0);
    }
    else
    {
        // We've sent all the data, so indicate that we're done.
         return(1);
    }
}


//****************************************************************************
//
// USBSendControlDone determines if the data transmit to the control endpoint
// has completed.
//
//****************************************************************************
unsigned long
USBSendControlDone(void)
{
    // Is there more data to be sent to the control endpoint?
    if(sUSB.ulControlInCount)
    {
        // There's more data to transmit, so indicate that we're not done.
        return(0);
    }
    else
    {
        // We've sent all the data, so indicate that we're done.
        return(1);
    }
}

void USBISRPOLLING(int number)
{
    unsigned long ulIntStatus, ulTransactionStatus, ulLength;
    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)
    {
        if (verbose)
            printf("Host Send BUS-Reset Command.\n");
      
        return;
    }

    //
    // Handle an interrupt on the bulk out endpoint.
    //
    if(ulIntStatus & USB_INT1_ENDPOINT2_OUT)
    {
        if (verbose)
            printf("Occur Main-Out-Endpoint Interrupt.\n");

        //
        // Read the status of the last transaction on the bulk out endpoint.
        //
        USBWriteCommand(USB_COMMAND_READ_LAST_XACTION_STATUS +
                        USB_ENDPOINT_BULK_OUT);
        ulTransactionStatus = USBReadData();

        //
        // Was a packet of data received successfully?
        //
        if(ulTransactionStatus & USB_XACTION_STATUS_DATA_RX_TX_SUCCESS)
        {
            //
            // If there is a buffer to read this data into, then read the data
            // into that buffer.
            //
            //
            // Read the packet.
            //
            USBReadEndpoint(USB_ENDPOINT_BULK_OUT, &sUSB.pucBulkOut,&sUSB.ulBulkOutCount);
        }
    }

    //
    // Handle an interrupt on the control out endpoint.
    //
    if(ulIntStatus & USB_INT1_CONTROL_OUT)
    {
        if (verbose)
            printf("Occur Control-Out-Endpoint Interrupt.\n");

        //
        // 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 = (U8 *)&sUSB.sControlOut;
            ulLength = sizeof(ControlTransfer);
            if(USBReadEndpoint(USB_ENDPOINT_CONTROL_OUT, &pucChar,&ulLength) != 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
                {
                    //
                    // 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)
    {
        if (verbose)
            printf("Occur Control-In--Endpoint Interrupt.\n");

        //
        // 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.ulControlInCount != 0)
        {
            //
            // Send the next packet of data to the host.
            //
            USBWriteEndpoint(USB_ENDPOINT_CONTROL_IN, &sUSB.pucControlIn,
                             &sUSB.ulControlInCount);
        }
        else
        {
            //
            // There is no more data to send, so send an empty packet.
            //
            ulLength = 0;
            USBWriteEndpoint(USB_ENDPOINT_CONTROL_IN, 0, &ulLength);
        }
    }

    //
    // Handle an interrupt on the bulk in endpoint.
    //
    if(ulIntStatus & USB_INT1_ENDPOINT2_IN)
    {
        if (verbose)
            printf("Occur Main-In--Endpoint Interrupt.\n");

        //
        // 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.ulBulkInCount != 0)
        {
            //
            // Send the next packet of data to the host.
            //
            USBWriteEndpoint(USB_ENDPOINT_BULK_IN, &sUSB.pucBulkIn,
                             &sUSB.ulBulkInCount);
        }
    }
}

⌨️ 快捷键说明

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