📄 usb.c
字号:
// // Write the value to the data register. // ucRet = *pucReg; // // Delay ~500ns to comply with the timing specification of the PDIUSBD12. // for(iIdx = 0; iIdx < 24; iIdx++) { } // // De-assert the USB chip select if it is controllable. //#ifdef HwPortABCD_USB_CS pulPtr[HwPortABCD >> 2] |= HwPortABCD_USB_CS;#endif // // Return the value read. // return(ucRet);}//****************************************************************************//// USBForceConfiguration forces the USB state into the specified// configuration.////****************************************************************************voidUSBForceConfiguration(unsigned long bConfigured){ // // See if we should set the state to configured. // if(bConfigured) { // // Indicate that we are configured. // sUSB.ucFlags |= 1; } else { // // Indicate that we are not configured. // sUSB.ucFlags &= ~1; }}//****************************************************************************//// USBStallEndpoint stalls or un-stalls the specified endpoint.////****************************************************************************static voidUSBStallEndpoint(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);}//****************************************************************************//// USBReadEndpoint reads data from the specified endpoint.////****************************************************************************static unsigned longUSBReadEndpoint(unsigned long ulEndpoint, unsigned char **ppucData, unsigned short *pusLength){#ifdef HwPortABCD_USB_CS volatile unsigned long *pulPtr = (unsigned long *)HwBaseAddress;#endif volatile unsigned char *pucReg = (unsigned char *)(HwUSBAddress + HwUSBData); unsigned long ulIdx, ulLength = 0; // // 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(*pusLength) { // // 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(); // // Assert the USB chip select if it is controllable. //#ifdef HwPortABCD_USB_CS pulPtr[HwPortABCD >> 2] &= ~HwPortABCD_USB_CS;#endif // // Read the data into the receive buffer. // for(ulIdx = 0; (ulIdx < ulLength) && (ulIdx < *pusLength); ulIdx++) { // // Read the next byte from the data buffer. // *(*ppucData)++ = *pucReg; // // Delay ~500ns to comply with the timing specification of the // PDIUSBD12. // NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; } // // De-assert the USB chip select if it is controllable. //#ifdef HwPortABCD_USB_CS pulPtr[HwPortABCD >> 2] |= HwPortABCD_USB_CS;#endif // // Decrement the count of bytes to read. // *pusLength -= 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);}//****************************************************************************//// USBWriteEndpoint writes data to the specified endpoint.////****************************************************************************static voidUSBWriteEndpoint(unsigned long ulEndpoint, const unsigned char **ppucData, unsigned short *pusLength){#ifdef HwPortABCD_USB_CS volatile unsigned long *pulPtr = (unsigned long *)HwBaseAddress;#endif volatile unsigned char *pucReg = (unsigned char *)(HwUSBAddress + HwUSBData); unsigned long 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 = (*pusLength > 16) ? 16 : *pusLength; } else { // // The maxmium packet size for the bulk endpoint is 64. // ulLength = (*pusLength > 64) ? 64 : *pusLength; } // // 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); // // Assert the USB chip select if it is controllable. //#ifdef HwPortABCD_USB_CS pulPtr[HwPortABCD >> 2] &= ~HwPortABCD_USB_CS;#endif // // Write the data into the transmit buffer. // for(ulIdx = 0; ulIdx < ulLength; ulIdx++) { // // Write the next byte of the data packet. // *pucReg = *(*ppucData)++; // // Delay ~500ns to comply with the timing specification of the // PDIUSBD12. // NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; NOP; } // // De-assert the USB chip select if it is controllable. //#ifdef HwPortABCD_USB_CS pulPtr[HwPortABCD >> 2] |= HwPortABCD_USB_CS;#endif // // Decrement the count of bytes to write. // *pusLength -= ulLength; // // Send the validate buffer command so that the endpoint will transmit the // packet. // USBWriteCommand(USB_COMMAND_VALIDATE_BUFFER);}//****************************************************************************//// USBEnable configures the PDIUSBD12 device.////****************************************************************************voidUSBEnable(void){ volatile unsigned long *pulPtr = (unsigned long *)HwBaseAddress; // // Enable the interrupts for the bulk endpoints. // USBWriteCommand(USB_COMMAND_SET_DMA); USBWriteData(USB_DMA_ENDP4_INT_ENABLE | USB_DMA_ENDP5_INT_ENABLE); // // Configure the PDIUSBD12 and enable the SoftConnect pull-up. // USBWriteCommand(USB_COMMAND_SET_MODE); USBWriteData(USB_CONFIG1_SOFT_CONNECT | USB_CONFIG1_NONISO_MODE); USBWriteData(USB_CONFIG2_SET_TO_ONE | USB_CONFIG2_CLOCK_4M); // // Enable the USB interrupt. // pulPtr[HwIntMask >> 2] |= HwIrqUSB; // // Indicate that the SoftConnect pull-up is active and that we are not // configured. // sUSB.ucFlags = 2;}//****************************************************************************//// USBDisable de-configures the PDIUSBD12 device.////****************************************************************************voidUSBDisable(void){ volatile unsigned long *pulPtr = (unsigned long *)HwBaseAddress; // // Disable the interrupts for the bulk endpoints. // USBWriteCommand(USB_COMMAND_SET_DMA); USBWriteData(0); // // Disable the SoftConnect pull-up. // USBWriteCommand(USB_COMMAND_SET_MODE); USBWriteData(USB_CONFIG1_NONISO_MODE); USBWriteData(USB_CONFIG2_SET_TO_ONE | USB_CONFIG2_CLOCK_4M); // // Disable the USB interrupt. // pulPtr[HwIntMask >> 2] &= ~HwIrqUSB; // // Indicate that the SoftConnect pull-up is no longer active. // sUSB.ucFlags &= ~2;}//****************************************************************************//// USBSendControl transmits a block of data back to the host via the control// endpoint.////****************************************************************************static unsigned longUSBSendControl(const unsigned char *pucData, unsigned long ulLength){ // // If a block is already being transmitted, then return a failure. // if(sUSB.usControlInCount) { return(0); } // // Prepare to transmit this block back to the host. // sUSB.pucControlIn = pucData; sUSB.usControlInCount = ulLength; // // Send the first packet of this block back to the host. // USBWriteEndpoint(USB_ENDPOINT_CONTROL_IN, &sUSB.pucControlIn, &sUSB.usControlInCount); // // Success. // return(1);}//****************************************************************************//// USBSendBulk transmits a block of data back to the host via the bulk// endpoint.////****************************************************************************static unsigned longUSBSendBulk(const unsigned char *pucData, unsigned long ulLength){ // // If a block is already being transmitted, then return a failure. // if(sUSB.usBulkInCount) { return(0); } // // Prepare to transmit this block back to the host. // sUSB.pucBulkIn = pucData; sUSB.usBulkInCount = ulLength; // // Send the first packet of this block back to the host. // DisableIRQ(); USBWriteEndpoint(USB_ENDPOINT_BULK_IN, &sUSB.pucBulkIn, &sUSB.usBulkInCount); EnableIRQ(); // // Success. // return(1);}//****************************************************************************//// USBReceiveBulk reads a block of data from the host via the bulk endpoint.////****************************************************************************static unsigned longUSBReceiveBulk(unsigned char *pucData, unsigned long ulLength){ // // Prepare to read data from the host into this buffer. // DisableIRQ(); sUSB.pucBulkOut = pucData; sUSB.usBulkOutCount = ulLength; EnableIRQ(); // // Success. // return(1);}//****************************************************************************//// USBGetStatus implements the USB Get_Status device request.////****************************************************************************static voidUSBGetStatus(void){ unsigned char ucStatus[2]; unsigned long 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -