Shinra team

USB disk and SD card file management control chip CH376

Part 2: Auxiliary and low-level USB transmission commands

Version: 1

http://wch.cn

Note: this page was translated mostly using Google translate from the file 'CH376DS2.pdf', of which there seem to be no official english translation available.

1. Additional Commands

Code Command name CMD_ Input data Output Data Command Purpose
04H SET_USB_SPEED Bus Speed USB bus speed setting
0AH GET_DEV_RATE Data 07H Data rate type Get the data rate of the USB device type
0AH READ_VAR8 Variable Address data Read the specified system variable (8-bit)
0BH SET_RETRY Data 25H Set the number of retries USB transaction operations
Retry count
0BH WRITE_VAR8 Variable Address Set the specified system variable (8-bit)
data
0CH READ_VAR32 Variable Address Data (4 bytes) Read the specified 32-bit system variable
0DH WRITE_VAR32 Variable Address Sets the specified 32-bit system variable
Data (4 bytes)
0FH DELAY_100US Delay Status Delay, 100uS
13H SET_USB_ADDR Address value Setting USB Address
16H TEST_CONNECT (delay 2uS)
Connection Status
USB device connection status check
17H ABORT_NAK Abandon the current Retry NAK
1CH SET_ENDP6 Working mode (delay 3uS) Set Receiver USB host endpoint
1DH SET_ENDP7 Working mode (delay 3uS) Setting Transmitter USB host endpoint
25H DIRTY_BUFFER Clear internal disk and file buffers
41H CLR_STALL Endpoint number Interrupt Control Transfer: Clear endpoint error
45H SET_ADDRESS Address value Interrupt Control Transfer: Sets the USB address
46H GET_DESCR Descriptor type Interrupt Control Transfer: Get Descriptor
49H SET_CONFIG Configuration value Interrupt Control Transfer: USB configuration settings
4DH AUTO_SETUP Interrupt Automatic configuration USB device
4EH ISSUE_TKN_X Sync flag Interrupt Issue synchronization tokens, the enforcement branch
Transaction attributes
51H DISK_INIT Interrupt Initialize USB memory
52H DISK_RESET Interrupt Reset USB storage device
53H DISK_SIZE Interrupt Obtain capacity USB memory
58H DISK_INQUIRY Interrupt Discover USB Memory Features
59H DISK_READY Interrupt Check the USB memory ready
5AH DISK_R_SENSE Interrupt Check the USB memory error
5BH RD_DISK_SEC Interrupt Read disk sector data to the internal buffer
5CH WR_DISK_SEC Interrupt The internal buffer data is written to disk sector
5DH DISK_MAX_LUN Interrupt Get from USB storage device the maximum unit number

1.1 Command CMD_SET_USB_SPEED

This command is used to set the USB bus speed.

The command needs to input a data for selecting USB bus speed,

The USB bus speed defaults to 12Mbps full speed mode, and after performing CMD_SET_USB_MODE command to set USB work mode, it also automatically reverts to full speed 12Mbps mode.

1.2 Command CMD_GET_DEV_RATE

This command is used to retrieve data rate type of USB device currently connected.

The command needs to input a data 07H, output number according to the rate type, which is: a bit 4 is 1.5Mbps low-speed USB device, otherwise it is 12Mbps full-speed USB device.

This command can only be used in USB Mode 5 (USB host mode enabled, no SOF packet).

1.3 Command CMD_READ_VAR8

This command is used to read the specified 8-bit (single byte) file system variables.

The command needs the address to read as input, and outputs the current value for the requested variable.

1.4 Command CMD_SET_RETRY

This command sets the number of retries USB transaction operations.

The command needs to input two data, namely data 25H and retry count.

Retries bits 7 and 6 designated CH376 NAK response when receiving treatment, the bit 7 to 1 and bit 6 to 0 unlimited weight

Test (you can use CMD_ABORT_NAK command to temporarily abandon the current retry), bit 7 to 1 and bit 6 is a finite retry up to three seconds.

So, bit 7 to 0 as a result of the notification will NAK microcontroller or as error handling.

Retries bits 5 to 0 to specify when the USB device answers retries after a timeout CH376 for 0 no retries after a timeout.

The default number of retries after a chip reset or re-set the USB mode is 8FH, it will NAK response received unlimited retries

USB devices after the timeout retries 15 times.

1.5. Command CMD_WRITE_VAR8

This command sets the specified 8-bit (single byte) system variable.

The command needs two input bytes, respectively, to specify the variable Address and specify variable value.

1.6 Command CMD_READ_VAR32

This command is used to read the specified 32-bit (4-byte) system variable.

The command needs the variable address as input.

The commands reply with the 4 byte value, LSB first.

1.7 Command CMD_WRITE_VAR32

This command sets the specified 32-bit (4-byte) system variable.

The command needs 5 input bytes, first the address of the variable, then the 4 bytes of the value, LSB first.

1.8 Command CMD_DELAY_100US

This command is used to delay the 100uS, does not support the serial port.

During the delay, any read from the device will return 0, after the end of the delay, non-zero data is returned (Usually a chip version number). The host microcontroller can poll the data port to detect the end of the delay.

1.9 Command CMD_SET_USB_ADDR

This command sets the USB device address.

The command needs to input a data for selecting the USB device to be operated address.

After reset or USB device is connected or disconnected, USB device address is always 00H, which is the address used for USB device configuration from the microcontroller.

If the microcontroller is provided through a standard USB requests USB device address, then you must also set the same USB device through the command to allow access to the new address by CH376 USB device communication.

1.10. Command CMD_TEST_CONNECT

This command is used to query the current connection status of the USB device in the USB host mode.

Typically, the command completes in about 2uS.

After completing the command outputs one of the three states USB_INT_CONNECT, USB_INT_DISCONNECT or USB_INT_USB_READY three.

outputs 0 indicating that the command has not been completed, you can then read later state.

1.11. Command CMD_ABORT_NAK

This command is used to abort the current retry the NAK.

When CH376 works in USB host mode, the default, CH376 USB receipt when the device returns NAK state, will keep retrying until it returns success or error.

This command can be forced to terminate CH376 retry, and then try another operation

See also SET_RETRY command to set whether to ban NAK retries.

1.12. Command CMD_SET_ENDP6

This command sets the USB host endpoint or endpoint 2 receiver (device mode Endpoint OUT / IN 2 of the host endpoint).

The command needs one input byte, specifying a new working mode.

For example, if you perform a transaction and would like to receive DATA0 IN abandoned DATA1, you must set the host endpoint via the command receiver synchronization trigger flag is 0, the corresponding work mode byte is 80H.

The command usually completes within 3uS.

For USB endpoint transceiver working modes, refer to the following table.

Mode byte Name Bit analysis shows ways of working
Bit 7 to bit 6 Synchronous trigger flag If bit 7 is a 6-bit synchronous trigger for the new logo:
  • 00 or 01 = holding current synchronous trigger flag unchanged
  • 10 = synchronous trigger flag 0
  • 11 = synchronous trigger flag is set to 1
Bits 5 to 4 (Reserved bits) (Undefined, must be 0)
Bit 3 ~ Bit 0 Transaction Response Information Must be 0000

1.13. Command CMD_SET_ENDP7

This command sets the USB host endpoint or endpoint 2 (device mode endpoint IN / OUT 2 of host endpoints).

The command needs one input byte, specifying the new mode.

For example, if you perform SETUP or OUT transaction and want to send DATA0, that must be set by the host endpoint command transmitter synchronization trigger flag is 0, the corresponding work mode byte is 80H.

If you want to send to DATA1, the work mode byte is C0H.

Under normal circumstances, the command completes within 3uS of time.

1.14. Command CMD_DIRTY_BUFFER

This command is used at the host file mode clears the internal disk and the file buffer.

After entering the host file mode, CH376 always store some frequently used data to the internal disk buffer, but some commands (for example CMD_RD_DISK_SEC or CMD_WR_DISK_SEC, etc.) also use the internal buffer, and invalidate its data. After such commands are finished executing, the CH376 must be notified that it should refresh the internal buffer variables.

1.15. Command CMD_CLR_STALL

The command is to remove endpoint error control transfer commands.

The command needs one input byte, which is the identifier of the stalled USB device endpoint to clear. For OUT endpoint, the effective address is 01H to 0FH, for IN endpoints, the effective address is 81H to 8FH.

To simplify the standard USB requests CLEAR_FEATURE, CH376 complete command after executing an interrupt request to the MCU, if the interrupt status is USB_INT_SUCCESS, then the command is executed successfully.

1.16. Command CMD_SET_ADDRESS

The command is to set the control transfer command USB address.

The command needs to input one data to appoint new USB device address, there is effective address is 00H ~ 7FH.

This command is used to simplify the standard USB requests SET_ADDRESS, CH376 execute the command after the completion of the microcontroller request interrupt, if the interrupt status is USB_INT_SUCCESS, then the command is executed successfully.

1.17. Command CMD_GET_DESCR

The command is to obtain the descriptor control transfer commands.

The command needs to input one data to appoint will be acquired descriptor type, effective type is 1 or 2, corresponding to the DEVICE CONFIGURATION device descriptor and configuration descriptor, wherein the configuration descriptor also includes an interface descriptor and endpoint descriptors.

This command is used to simplify the standard USB requests GET_DESCRIPTOR

On command completion, the CH376 triggers an interrupt request to the MCU, if the interrupt status is USB_INT_SUCCESS, then the command is successful, microcontroller can use the CMD_RD_USB_DATA0 command to get descriptor data.

Because CH376 control transmission buffer is only 64 bytes, when the descriptor is longer than 64 bytes, CH376 returns to the operating state USB_INT_BUF_OVER. In that case, the control transfer must be performed manually using the CMD_ISSUE_TKN_X command.

1.18. Command CMD_SET_CONFIG

The command is to set the USB configuration control transfer commands.

The command needs to input one data to appoint new USB configuration values.

0 configuration is canceled, or should the USB device from the configuration descriptor.

This command is used to simplify the standard USB requests SET_CONFIGURATION, CH376 complete command after executing an interrupt request to the MCU, if the interrupt status is USB_INT_SUCCESS, then the command is executed successfully.

1.19. Command CMD_AUTO_SETUP

This command is used to automatically configure the USB device (does not

This command is used to simplify common USB device initialization step, phase when in GET_DESCR, SET_ADDRESS, SET_CONFIGURATION and other command sequence.

CH376 After completion of a command to execute single request interrupt, if the interrupt status is USB_INT_SUCCESS, then the command is executed successfully.

1.20. Command CMD_ISSUE_TKN_X

This command enables the CH376 issues a synchronization token, the enforcement branch.

The command needs to input two data, respectively, and a synchronization flag affairs which sex.

Receiver synchronization flag bit 7 for the host endpoint synchronous trigger flag, bit 6 for the host endpoint transmitter synchronization trigger flag, Bits 5 to 0 must be zero.

Low transaction attribute four specified transaction token PID, high four designated purpose USB device endpoint number.

CH376 complete command after executing an interrupt request to the MCU, the MCU can read the interrupt status as the operating state of the command.

in case Operating state is USB_INT_SUCCESS, then the command is executed successfully, otherwise described command fails, the microcontroller can operate State further analyze the reasons for failure.

SETUP transaction for sending data and OUT transaction, the number ready to be sent should be written by CMD_WR_HOST_DATA command.

It is, then perform a transaction through CMD_ISSUE_TKN_X command; for receiving data IN transaction, it should first pass

CMD_ISSUE_TKN_X command is executed transaction, after successful execution, and then reads the data that has been received by CMD_RD_USB_DATA0 command.

Here is CH376 supports USB token PID.

PID byte name Explanation
0DH DEF_USB_PID_SETUP Launch control transfer, the establishment of data sent
01H DEF_USB_PID_OUT Executive OUT transaction, the transmission data
09H DEF_USB_PID_IN IN transaction execution, the received data

For example, when the transaction attribute byte is 09H, the CH376 receive data from the default endpoint 0 USB devices; transaction attribute bytes.

When 21H, the CH376 USB devices to send data to the endpoint 2; when the transaction attribute byte 29H, the CH376 from USB endpoint 2 receives data, the endpoint address is 82H.

1.21. Command CMD_DISK_INIT

This command is used to initialize the USB storage device (does not support the SD card).

For USB devices already connected, this command first reset USB Bus, and then parses the USB device descriptor, if it is able to support USB storage device, it will automatically configure the device, most After establishing a connection with the USB storage device.

CH376 complete command after executing an interrupt request to the MCU, if the USB device has been disconnected, Interrupt status may be USB_INT_DISCONNECT; if the USB device is not recognized or the USB storage device is not supported, Then the state usually is USB_INT_DISK_ERR or USB_INT_BUF_OVER; if USB storage device initialization is successful, then the state will be USB_INT_SUCCESS.

1.22. Command CMD_DISK_RESET

This command is used to reset the control transfer by USB storage device (does not support the SD card).

CH376 complete command after executing the microcontroller Request interrupt, if the interrupt status is USB_INT_SUCCESS, the command is executed successfully.

Complete the reset process comprising: This command resets the USB storage device, by CLR_STALL command to reset the Bulk-IN endpoint, By CLR_STALL command resets Bulk-OUT endpoint.

When a USB storage device errors occur, CH376 will analyze cause of the error and automatically select whether to reset the USB device as needed.

1.23. Command CMD_DISK_SIZE

This command is used to obtain the capacity of USB memory (does not support SD cards).

After successful initialization USB storage device, the command

CH376 complete command after executing an interrupt request to the MCU, if the interrupt status is USB_INT_SUCCESS, you can get the data from the CMD_RD_USB_DATA0 command, data is usually 8 bytes, the first four bytes is the total number of sectors USB storage device, double-word data after four byte consisting of the number of bytes per sector (both LSB first), the result of multiplying the two data is the total capacity in bytes of USB storage devices.

1.24. Command CMD_DISK_INQUIRY

The command queries the USB storage device characteristics (does not support SD cards).

CH376 complete command after executing the request to the MCU, if the interrupt status is USB_INT_SUCCESS, you can get data from the CMD_RD_USB_DATA0 command, data is usually 36 bytes, including the characteristics of USB storage devices as well as vendor and product identification information.

Generally, this command does not need to be used, unless Analyzing new logical unit.

1.25. Command CMD_DISK_READY

This command is used to check the USB storage device is ready (do not support the SD card).

CH376 complete command after executing an interrupt request to MCU, if the interrupt status is USB_INT_SUCCESS, then that USB storage device is already ready.

1.26. Command CMD_DISK_R_SENSE

This command is used to check for

CH376 complete command after executing an interrupt request to the MCU, under normal circumstances Interrupt status is USB_INT_SUCCESS, after the data can be obtained by the CMD_RD_USB_DATA0 command parsing errors.

1.27. Command CMD_RD_DISK_SEC

This command is used in a host file mode to read data from the disk one sector to the internal buffer.

1.28. Command CMD_WR_DISK_SEC

This command is used in a host file mode to write data to a disk sector internal buffer.

1.29. Command CMD_DISK_MAX_LUN

The command gets the largest logical unit number of USB storage device by controlling the transmission.

CH376 complete command after executing Ask microcontroller Seeking interrupt, if the interrupt status is USB_INT_SUCCESS, you can get the data from the CMD_RD_USB_DATA0 command, data communication.

2. External firmware mode

2.1 Overview

CMD_ISSUE_TKN_X command is used to perform basic USB transmission services, firmware programming is the most basic USB host mode operation.

On this basis, the external USB device can be in accordance with the requirements of the agreement, the discretion has not been directly CH376 provides a simplified command control Transmission system.

Still further, the device can be in accordance with the requirements of the USB protocol, dealing with their own particular type of equipment USB protocol, to achieve USB device control and data exchange.

CH376 built-Mass-Storage mass storage device Bulk-Only Transport Protocol on CBI transport protocol in use, or other transfer protocol USB storage device, still need external microcontroller based CMD_ISSUE_TKN_X

Command and control transfer commands themselves.

2.2. External Firmware Reference Flow

The firmware provides an external reference procedure in CH376 evaluation board, the following processes are executed by the external microcontroller controls the transmission standard

Quasi USB request GET_STATUS, get the status of USB devices, reference designs for external microcontroller firmware.

⑴ control establishment phase transfer

① issue CMD_WR_HOST_DATA command writes the requested data eight bytes of output buffer, followed by the requested data

80H, 00H, 00H, 00H, 00H, 00H, 02H, 00H, length is 8.

② issue CMD_ISSUE_TKN_X command transaction, the synchronization flag is 00H, the transaction attribute byte is 0DH, the default port endpoint 0 SETUP token issue and send DATA0.

③ wait for the transaction to complete microcontroller interrupt or wait for interrupt notification.

④ after the transaction is complete, CH376 the INT # pin is set low, the interrupt request to the microcontroller;

⑤ the device enters the interrupt service routine, or in the main program after receiving notice of the interrupt exit standby.

⑥ issue CMD_GET_STATUS command to get a break.

After ⑦ CH376 completed in CMD_GET_STATUS command INT # pin back to high, to cancel an interrupt request.

⑧ SCM Analytical results of the interrupt status, if not USB_INT_SUCCESS the operation fails, exception handling; such as If the transaction is USB_INT_SUCCESS executed successfully complete the build phase.

⑵ control data transfer phase

① issue CMD_ISSUE_TKN_X command transaction, the synchronization flag is 80H, the transaction attribute byte is 09H, the default port 0 endpoint issued IN token and receive DATA1.

② wait for the transaction to complete microcontroller; after the transaction is completed, CH376 SCM interrupt request.

③ CMD_GET_STATUS command to get the interrupt status issue, CH376 undo the interrupt request.

④ Microcontroller Analytical results of the interrupt status, if the operation fails exception handling; if USB_INT_SUCCESS The transaction is executed successfully.

⑤ issue CMD_RD_USB_DATA0 command to get the data returned USB devices, and save it as a control transfer return results.

⑥ because the control transmission requires only one IN transaction, so the data phase is completed.

⑶ control transfer status stage

① issue CMD_WR_HOST_DATA command writes the state data output zero-length buffer length of zero.

② issue CMD_ISSUE_TKN_X command transaction, the synchronization flag is 40H, the transaction attribute byte is 01H, the default port endpoint 0 OUT token issue and send DATA1.

③ wait for the transaction to complete microcontroller; after the transaction is completed, CH376 SCM interrupt request.

④ issue CMD_GET_STATUS command to get the interrupt status, CH376 undo the interrupt request.

⑤ Microcontroller Analytical results of the interrupt status, if the operation fails exception handling; if USB_INT_SUCCESS The transaction is successful, the status phase is completed.

⑷ control transfer is complete, the data phase data returned data is returned as a standard USB request GET_STATUS usually

The returned data length is 2 bytes.

Valid HTML 4.01 Strict Site fait à La Rache