This post was last edited by yang_alex on 2018-5-19 00:44 Qinheng provides the following two code files for USB HOST application: 1. USBHOST.C: CH554 USB host control transmission function definition, mainly device enumeration part, control transmission part 2. USBHostHUB_KM.C: USB host application example, initialize and enumerate the devices connected to the USB port, support the first-level external HUB, can operate USB keyboard and mouse and HUB, printer, including HID class command processing First, create the project file yourself (refer to my previous evaluation post for the process), compile and burn. Well, everything is normal. First use a wireless mouse receiver to test, and it turns out that after the evaluation board is powered on first, the result is as shown below.
Then connect the wireless mouse receiver to the USB female socket of the CH554 evaluation board, everything is normal, and the result is as shown below.
When the evaluation board is powered on, unplug the wireless mouse receiver, and "USB dev out" will be output on the debug serial port.
But if you first connect the wireless mouse receiver to the USB female socket of the CH554 evaluation board. It turns out that the D1 power indicator lights up before the evaluation board is powered on. When the evaluation board is powered on again, there is no output on the serial port. Even if it is reset or powered on again, there is no output on the serial port.
After unplugging the wireless mouse receiver, there is no output on the serial port. But after the evaluation board is reset or powered on again, the serial port output is as follows (the same as when the evaluation board is powered on without the wireless mouse receiver).
Try to insert a small-capacity USB disk for testing. After the evaluation board is powered on, connect the small-capacity USB disk to the USB female socket of the CH554 evaluation board, and it will be recognized normally. The result is as shown below.
Change the USB keyboard test, after the evaluation board is powered on, connect the USB keyboard to the USB female socket of the CH554 evaluation board, the result is as shown below.
Change the USB game remote control test, after the evaluation board is powered on, connect the USB game remote control to the USB female socket of the CH554 evaluation board, the result is as shown below.
Analyze the information printed out by the serial port, and finally find the return error status code in USBHOST.H:
- // Each subroutine returns a status code #define ERR_SUCCESS 0x00 // Operation successful #define ERR_USB_CONNECT 0x15 /* USB device connection event detected, connected*/ #define ERR_USB_DISCON 0x16 /* USB device disconnection event detected, disconnected*/ #define ERR_USB_BUF_OVER 0x17 /* USB transmitted data is incorrect or the buffer overflows due to too much data*/ #define ERR_USB_DISK_ERR 0x1F /* USB storage operation failed. The USB storage may not be supported during initialization. The disk may be damaged or disconnected during read and write operations*/ #define ERR_USB_TRANSFER 0x20 /* NAK/STALL and other more error codes are in 0x20~0x2F */ #define ERR_USB_UNSUPPORT 0xFB /*Unsupported USB device*/ #define ERR_USB_UNKNOWN 0xFE /*Device operation error*/
复制代码#define ERR_USB_BUF_OVER 0x17 /* USB transmitted data is incorrect or the buffer overflows due to too much data*/ #define ERR_USB_TRANSFER 0x20 /* NAK/STALL and other error codes are in the range of 0x20~0x2F */ It can be seen that the evaluation board encountered problems in obtaining the device description and initializing the device. The debug serial port outputs error number 17. By tracing the output information of the debugging serial port (printing information multiple times to determine the error location), the problem is located in the CtrlGetDeviceDescr() function, and the following sentence: UsbDevEndp0Size = ( (PXUSB_DEV_DESCR)TxBuffer ) -> bMaxPacketSize0; // Endpoint 0 maximum packet length, this is a simplified processing, normally you should get the first 8 bytes first and then update UsbDevEndp0Size immediately before continuing
- /*********************************************************************************** * Function Name : CtrlGetDeviceDescr * Description : Get device descriptor and return it in TxBuffer * Input : None * Output : None * Return : ERR_USB_BUF_OVER Descriptor length error ERR_SUCCESS Success Others *************************************************************************************/ UINT8 CtrlGetDeviceDescr( void ) { UINT8 s; UINT8 len; UsbDevEndp0Size = DEFAULT_ENDP0_SIZE; CopySetupReqPkg( SetupGetDevDescr ); s = HostCtrlTransfer( TxBuffer, (PUINT8)&len ); // Execute control transfer if ( s != ERR_SUCCESS ) { return( s ); } #if DE_PRINTF printf( "CtrlGetConfigDescr3\n" ); #endif UsbDevEndp0Size = ( (PXUSB_DEV_DESCR)TxBuffer ) -> bMaxPacketSize0; // Endpoint 0 maximum packet length, this is simplified processing,Normally, you should get the first 8 bytes first and then update UsbDevEndp0Size immediately before continuing if ( len < ( (PUSB_SETUP_REQ)SetupGetDevDescr ) -> wLengthL ) { return( ERR_USB_BUF_OVER ); // Descriptor length error } #if DE_PRINTF printf( "CtrlGetConfigDescr4\n" ); #endif return( ERR_SUCCESS ); }
复制代码It can be seen that Qinheng's code is simplified and not processed according to the USB specification. So some unexpected situations will go wrong. The correct way should be as mentioned in the previous post: Note (6), (7), and (8) steps. The host first obtains the device descriptor of part of the slave device through address 0, obtains the maximum data packet length, and then resets the bus to notify the slave device to switch the communication address. Next, use the new address to obtain the complete device descriptor of the slave device. Please ask Qinheng's technical support to help modify the specific code. This content is created by EEWORLD forum user
yang_alex, if you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source Analyze the information printed by the serial port, and finally find the error status code returned in USBHOST.H: