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

📄 usb.c

📁 S3C44B0——ARM7的音频测试程序
💻 C
📖 第 1 页 / 共 3 页
字号:
    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);
    }
}

void Usb_Control_IN(void)
{
	unsigned long ulLength;
	
    // 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);
    }
}

void Usb_Ep1_OUT(void)
{
	unsigned long ulTransactionStatus;

    // Read the status of the last transaction on the bulk out endpoint.
    USBWriteCommand(USB_COMMAND_READ_LAST_XACTION_STATUS +
                    USB_ENDPOINT_COMMAND_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_COMMAND_OUT, &sUSB.pucCommandOut, &sUSB.ulCommandOutCount);
    }
}

void Usb_Ep1_IN(void)
{
}

void Usb_Ep2_OUT(void)
{
	unsigned long ulTransactionStatus;

    // Read the status of the last transaction on the control in endpoint.
    USBWriteCommand(USB_COMMAND_READ_LAST_XACTION_STATUS + USB_ENDPOINT_BULK_OUT);
    ulTransactionStatus = USBReadData();
    if(ulTransactionStatus & USB_XACTION_STATUS_DATA_RX_TX_SUCCESS)
    {
    	USBReadEndpoint(USB_ENDPOINT_BULK_OUT, &sUSB.pucBulkOut, &sUSB.ulBulkOutCount);
    }
}

void Usb_Ep2_IN(void)
{
    // 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);
    }
}

//****************************************************************************
//
// USBISR is the interrupt handler routine for the USB.
//
//****************************************************************************
void __irq USBISR(void)
{
    unsigned long ulIntStatus;
    
    // Read the PDIUSBD12 interrupt register.
    rI_ISPC=BIT_EINT0;	//clear pending_bit
    USBWriteCommand(USB_COMMAND_READ_INTERRUPT);
    ulIntStatus = USBReadData();
    ulIntStatus |= USBReadData() << 8;

    // Do nothing if there was a bus reset.
    if(ulIntStatus & USB_INT1_BUS_RESET)		Usb_BusReset();
    if(ulIntStatus & USB_INT1_CONTROL_OUT)		Usb_Control_OUT();
    if(ulIntStatus & USB_INT1_CONTROL_IN)		Usb_Control_IN();
    if(ulIntStatus & USB_INT1_ENDPOINT1_IN)		Usb_Ep1_IN();
    if(ulIntStatus & USB_INT1_ENDPOINT1_OUT)	Usb_Ep1_OUT();
    if(ulIntStatus & USB_INT1_ENDPOINT2_IN)		Usb_Ep2_IN();
    if(ulIntStatus & USB_INT1_ENDPOINT2_OUT)	Usb_Ep2_OUT();
}

//****************************************************************************
//
// USBWriteEndpoint writes data to the specified endpoint.
//
//****************************************************************************
void USBWriteEndpoint(unsigned long ulEndpoint, const unsigned char **ppucData,
                 unsigned long *pulLength)
{
    U32 ulIdx, ulLength;

    // Determine the size of the packet to be sent based on the endpoint.
    if(ulEndpoint == USB_ENDPOINT_CONTROL_IN)
    {
        // The maximum packet size for the control endpoint is 16.
        ulLength = (*pulLength > 16) ? 16 : *pulLength;
    }
    else
    {
        // The maxmium packet size for the bulk endpoint is 64.
         ulLength = (*pulLength > 64) ? 64 : *pulLength;
    }
    // Select the appropriate endpoint.
    USBWriteCommand(USB_COMMAND_SELECT_ENDPOINT + ulEndpoint);

    // Send the write buffer command.
    USBWriteCommand(USB_COMMAND_WRITE_BUFFER);

    // Write the reserved byte to the buffer.
    USBWriteData(0);

    // Write the length of the data packet.
    USBWriteData(ulLength);

    // Write the data into the transmit buffer.
    for(ulIdx = 0; ulIdx < ulLength; ulIdx++)
    {
        USBWriteData(*(*ppucData)++);
    }

    //
    // Decrement the count of bytes to write.
    *pulLength -= ulLength;


    // Send the validate buffer command so that the endpoint will transmit the packet.
    USBWriteCommand(USB_COMMAND_VALIDATE_BUFFER);
}

//****************************************************************************
//
// USBReadEndpoint reads data from the specified endpoint.
//
//****************************************************************************
unsigned long
USBReadEndpoint(unsigned long ulEndpoint, unsigned char **ppucData,unsigned long *pulLength)
{
    U32 ulIdx, ulLength;
     
    // Select the appropriate endpoint.
    USBWriteCommand(USB_COMMAND_SELECT_ENDPOINT + ulEndpoint);

    // Is there buffer space to fill with this data or should we throw the data away?
    if(*pulLength)
    {    // Send the read buffer command.
       USBWriteCommand(USB_COMMAND_READ_BUFFER);
        // Throw away the reserved byte from the beginning of the buffer.

        USBReadData();
        // Read the length of the data buffer.
        ulLength = USBReadData();
        // Read the data into the receive buffer.
        for(ulIdx = 0; (ulIdx < ulLength) && (ulIdx < *pulLength); ulIdx++)
        {
            *(*ppucData)++ = USBReadData();
        }

        // Decrement the count of bytes to read.
        *pulLength -= ulIdx;
    }  
    // Send the clear buffer command so that the endpoint can receive another packet.
    USBWriteCommand(USB_COMMAND_CLEAR_BUFFER);

    // Return the size of the packet received.
    return(ulLength);
}

//****************************************************************************
//
// USBReserved handles device requests which are not supported by this USB
// device implementation.
//
//****************************************************************************
void USBReserved(void)
{
    // Stall both control endpoints.
    USBStallEndpoint(USB_ENDPOINT_CONTROL_OUT, 1);
    USBStallEndpoint(USB_ENDPOINT_CONTROL_IN, 1);
}

//****************************************************************************
//
// USBStallEndpoint stalls or un-stalls the specified endpoint.
//
//****************************************************************************
void USBStallEndpoint(unsigned long ulEndpoint, unsigned long bStall)
{
    //
    // Send the appropriate set endpoint status command to the PDIUSBD12.
    //
    USBWriteCommand(USB_COMMAND_SET_ENDPOINT_STATUS + ulEndpoint);
    USBWriteData(bStall ? 1 : 0);
}

//****************************************************************************
//
// USBGetStatus implements the USB Get_Status device request.
//
//****************************************************************************
void USBGetStatus(void)
{
    U8 ucStatus[2];
    U32 ulEndpoint;
    // Determine how to handle this request based on the recipient.

    switch(sUSB.sControlOut.bmRequestType & USB_RT_RECIPIENT_MASK)
    {
        // If the recipient is a device, return the state of the device's
        // 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++;
            }

            // Read the endpoint status.
            USBWriteCommand(USB_COMMAND_SELECT_ENDPOINT);
            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.
//
//****************************************************************************
void USBClearFeature(void)
{
    U32 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++;
        }

        // 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.
//
//****************************************************************************
void USBSetFeature(void)
{
    U32 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++;
        }

        // 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.
//
//****************************************************************************
void USBSetAddress(void)
{
    // Configure our UBS controller for the USB address assigned by the host.
    USBWriteCommand(USB_COMMAND_SET_ADDRESS_ENABLE);
    USBWriteData(sUSB.sControlOut.wValue | 0x80);

⌨️ 快捷键说明

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