CH376 Datasheet (I)
USB Flash Disk and SD Card File Management Control Chip
This datasheet is modified to remove most irrelevant content for building a USB host library. Note that we have two of these ICs on the FPGA board, one for each of the two USB ports. Both are connected via a separate SPI interface to the FPGA, and the interrupt pins are connected to two separate GPIO pins on the FPGA (which are readable via memory-mapped addresses)
1. Overview
CH376 is a file management control chip, which is used for MCU system to read and write files in USB flash disk or SD card.
CH376 supports USB device mode and USB host mode, has the basic firmware of the USB communication protocol, the firmware for processing the special communication protocol of Mass-Storage device, the SD card communication interface firmware and the management firmware of FAT16 and FAT32 and FAT12 file systems, and supports the common USB storage devices (including USB flash disk/USB hard disk, USB flash drive/USB card reader) and SD card (including standard capacity SD card and high capacity HC-SD card as well as protocol compatible MMC card and TF card).
CH376 supports three communication interfaces: 8-bit parallel port, SPI interface or asynchronous serial interface. Controllers such as DSP/MCU/MPU can control CH376 through any of the above communication interfaces, access files in USB flash disk or SD card or communicate with the computer.
The USB device mode of CH376 is fully compatible with CH372, and the USB host mode of CH376 is basically compatible with CH375.
2. Features
- Support 1.5Mbps low-speed and 12Mbps full-speed USB communication, compatible with USB V2.0.
- Support USB-HOST interface and USB-Device interface, and support dynamic mode switching between HOST and DEVICE.
- Support USB device control transmission, bulk transmission and interrupt transmission.
- Automatically detecting the connection and disconnection of USB device, and providing event notifications for device connection and disconnection.
- Built-in USB control transmission protocol processor simplifies common control transmission.
- The built-in firmware processes special communication protocols for mass storage devices, and supports BULk-Only transport protocol and USB storage devices (including USB flash disk/USB hard disk/USB flash drive/USB card reader) with SCSI, UFI, RBC or equivalent command sets.
- The management firmware with FAT16, FAT32 and FAT12 file systems supports 32GB USB flash disk and SD card.
- Provide file management functions: open, create or delete files, enumerate and search files, create subdirectories, and support long filenames.
- Provide file read-write function: read and write files in the multi-level subdirectories in bytes or sectors.
- Provide the disk management function: initialize the disk, inquire the physical capacity, inquire the free space, read and write physical sectors.
- Provide a 2MB 8-bit passive parallel interface and support the parallel data bus connected to MCU.
- Provide 24MHz SPI device interface, and support SPI serial bus connected to MCU.
- Provide the asynchronous serial interface with the maximum speed of 3Mbps, support the serial port connected to MCU, and support the dynamic adjustment of communication baud rate.
- Support supply voltage of 5V, 3.3V and 3V as well as low power mode.
- The USB device mode is fully compatible with CH372; the USB host mode is basically compatible with CH375 chip.
3. Package
Irrelevant content.
4. Pins
Only relevant pin for building a library is the interrupt pin, as this pin can be read directly by reading the FPGC memory address 0x700000E for the top USB port or 0x7000011 for the bottom USB port.
| CH376S Pin No. | CH376T Pin No. | Pin Name | Pin Type | Description |
|---|---|---|---|---|
| 1 | 1 | INT# | Output | Interrupt request output, active at low level, built-in pull-up resistor |
5. Commands
For the data in this datasheet, the suffix B is a binary number, and the suffix H is a hexadecimal number. Otherwise, it is a decimal number.
Double-word data (32 bits in total) with low bytes in the front (Little-Endian) refers to: the lowest byte (bits 7 to 0), followed by the lower byte (bits 15 to 8), followed by the higher byte (bits 23 to 16), followed by the highest byte (bits 31 to 24).
Data stream is a data block consisting of several consecutive bytes with a total length of at least 0 and at most 255.
The number in the brackets for the input data and output data in the following table is the number of bytes of the parameter. If there are no brackets, it will be 1 byte by default.
MCU referred to in this datasheet is basically applicable to DSP or MCU/MPU/SCM, etc.
USB flash disk referred to in this datasheet includes USB flash disk, USB external hard disk, USB flash drive, USB card reader, etc.
SD card referred to in this datasheet includes SD card, MMC card, HC-SD card (high-capacity SD card), TF card, etc.
This datasheet mainly provides commonly used file management control commands for USB flash disk and SD card. Refer to CH376 Datasheet (II), which is at the second half of this file, for some less commonly used auxiliary commands and commands for performing USB basic transactions and control transmission (Note: these are especially interesting for building a generic USB host library!).
CH376 contains all the functions of CH372 chip. This datasheet does not provide the description of CH376 in USB device mode. For related information, refer to the CH372 Datasheet (which is not relevant as we are interested in a USB host library).
| Code | Command name CMD_ | Input data | Output data | Command purpose |
|---|---|---|---|---|
| 01H | GET_IC_VER | Version | Get the chip and firmware versions | |
| 02H | SET_BAUDRATE | Frequency division coefficient Frequency division constant |
(Wait for 1mS) Operation status |
Set serial communication baud rate |
| 03H | ENTER_SLEEP | Enter the low-power sleep suspend state | ||
| 05H | RESET_ALL | (Wait for 35mS) | Execute hardware reset | |
| 06H | CHECK_EXIST | Any data | Bitwise NOT | Test the communication interface and operation status |
| 0BH | SET_SDO_INT | Data 16H Interrupt mode |
Set the interrupt mode for SDO pin of SPI | |
| 0CH | GET_FILE_SIZE | Data 68H | File length (4) | Get the current file length |
| 15H | SET_USB_MODE | Mode code | (Wait for 10uS) Operation status |
Set USB working mode |
| 22H | GET_STATUS | Interrupt status | Get the interrupt status and cancel the interrupt request | |
| 27H | RD_USB_DATA0 | Data Length Data stream (n) |
Read the data block from the endpoint buffer of the current USB interrupt or from the receive buffer of the host endpoint | |
| 2CH | WR_HOST_DATA | Data Length Data stream (n) |
Write the data block to the transmit buffer of the USB host endpoint | |
| 2DH | WR_REQ_DATA | Data Length Data stream (n) |
Write the requested data block to the internal specified buffer | |
| 2EH | WR_OFS_DATA | Offset address Data Length Data stream (n) |
Write the data block to the specified offset address of the internal buffer | |
| 2FH | SET_FILE_NAME | Character string (n) | Set the filename of the file to be operated | |
| 30H | DISK_CONNECT | Generate interrupt | Check whether the disk is connected | |
| 31H | DISK_MOUNT | Generate interrupt | Initialize the disk and test whether the disk is ready | |
| 32H | FILE_OPEN | Generate interrupt | Open files or directories, enumerate files and directories | |
| 33H | FILE_ENUM_GO | Generate interrupt | Continue enumerating files and directories | |
| 34H | FILE_CREATE | Generate interrupt | New File | |
| 35H | FILE_ERASE | Generate interrupt | Delete File | |
| 36H | FILE_CLOSE | Whether update is allowed | Generate interrupt | Close the currently opened file or directory |
| 37H | DIR_INFO_READ | Directory index number | Generate interrupt | Read the directory information for the file |
| 38H | DIR_INFO_SAVE | Generate interrupt | Save the directory information for the file | |
| 39H | BYTE_LOCATE | Number of offset bytes (4) | Generate interrupt | Move the current file pointer in bytes |
| 3AH | BYTE_READ | Number of bytes requested (2) | Generate interrupt | Read data blocks from the current location in bytes |
| 3BH | BYTE_RD_GO | Generate interrupt | Continue byte read | |
| 3CH | BYTE_WRITE | Number of bytes requested (2) | Generate interrupt | Write data blocks to the current location in bytes |
| 3DH | BYTE_WR_GO | Generate interrupt | Continue byte write | |
| 3EH | DISK_CAPACITY | Generate interrupt | Inquire the physical capacity of the disk | |
| 3FH | DISK_QUERY | Generate interrupt | Inquire the space information of the disk | |
| 40H | DIR_CREATE | Generate interrupt | Create a new directory and open it or open an existing directory | |
| 4AH | SEC_LOCATE | Number of offset sectors (4) | Generate interrupt | Move the current file pointer in sectors |
| 4BH | SEC_READ | Number of requested sectors | Generate interrupt | Read data blocks from the current location in sectors |
| 4CH | SEC_WRITE | Number of requested sectors | Generate interrupt | Write data blocks to the current location in sectors |
| 50H | DISK_BOC_CMD | Generate interrupt | Execute BO transport protocol commands on USB memory | |
| 54H | DISK_READ | LBA sector address (4) Number of sectors |
Generate interrupt | Read physical sectors from USB memory |
| 55H | DISK_RD_GO | Generate interrupt | Continue the physical sector read operation of the USB memory | |
| 56H | DISK_WRITE | LBA sector address (4) Number of sectors |
Generate interrupt | Write physical sectors to USB memory |
| 57H | DISK_WR_GO | Generate interrupt | Continue the physical sector write operation of the USB memory |
If the output data of the command is the operation status, refer to the following table.
| Status code | Status name | Status description |
|---|---|---|
| 51H | CMD_RET_SUCCESS | Operated Successfully |
| 5FH | CMD_RET_ABORT | Operation failure |
It usually takes time to execute the commands marked as "generate interrupt". CH376 requests an interrupt from MCU after the command is executed. MCU can read the interrupt status as the operation status of the command. If the interrupt status is USB_INT_SUCCESS, the operation will be successful. Some commands have return data (refer to CH376_CMD_DATA structure in the file CH376INC.H), and the returned data can be read through the command CMD_RD_USB_DATA0.
5.1. CMD_GET_IC_VER
This command is used to get the chip and firmware versions. One byte of data returned is the version number, the bit 7 is 0, the bit 6 is 1, and the bits 5-0 are the version number. If the returned value is 41H, remove bits 7 and 6, and the version number will be 01H.
5.2. CMD_SET_BAUDRATE (Irrelevant)
5.3. CMD_ENTER_SLEEP
This command enables CH376 in a low-power sleep suspended state. After entering the low power state, the clock of CH376 stops vibrating, thus saving power. The low power state is not quitted until one of the following two situations is detected: first, the signal is detected on the USB bus (such as the USB host starting transmission or USB device plugging event). Second, MCU writes new commands to CH376 (commands without input data, such as CMD_GET_IC_VER or CMD_ABORT_NAK). For SPI interface communication interface mode, active SCS chip selection will also cause CH376 to exit the low-power state, so MCU shall immediately disable the SCS chip selection after sending the command CMD_ENTER_SLEEP.
Typically, it takes several milliseconds for CH376 to exit the low-power state and return to normal operation. When fully restored to normal operation, CH376 will generate the event interrupt USB_INT_WAKE_UP.
5.4. CMD_RESET_ALL
This command enables CH376 to perform a hardware reset. Typically, hardware reset is completed within 35mS. In the parallel communication mode, hardware reset is generally completed within 1mS.
5.5. CMD_CHECK_EXIST
This command is used to test the communication interface and working state to check whether CH376 is working properly. This command needs to input 1 data, which can be any data. If CH376 is working properly, the output data of CH376 will be the bitwise reverse of the input data. For example, if the input data is 57H, the output data will be A8H. In addition, CH376 with parallel communication mode normally reads the data 00H from its parallel port before receiving no command after its reset.
5.6. CMD_SET_SDO_INT (Irrelevant)
5.7. CMD_GET_FILE_SIZE (Irrelevant)
5.8. CMD_SET_FILE_SIZE (Irrelevant)
5.9. CMD_SET_USB_MODE
This command is used to set USB working mode. This command needs to input 1 data, which is a mode code:
- Switch to the disabled USB device mode (default mode after power-on or reset) when the mode code is 00H;
- Switch to the enabled USB device mode and external firmware mode (serial connection mode is not supported) when the mode code is 01H;
- Switch to the enabled USB device mode and built-in firmware mode when the mode code is 02H;
- Switch to the SD card host mode to manage and access files in the SD card when the mode code is 03H;
- Switch to the disabled USB host mode when the mode code is 04H;
- Switch to the enabled USB host mode when the mode code is 05H, not generating SOF package;
- Switch to the enabled USB host mode when the mode code is 06H, automatically generating SOF package;
- Switch to the enabled USB host mode when the mode code is 07H, resetting USB bus;
Please refer to CH372 Datasheet for USB device mode. The USB device mode of CH376 is fully compatible with CH372 chip.
In USB host mode, "Not enabled" means that whether the USB device is connected is not automatically detected, so the external MCU shall detect; "Enabled" means that whether the USB device is connected is automatically detected. When the USB device is connected or disconnected, an interrupt will be generated to notify the external MCU. After switching to the mode code 06H, CH376 will automatically periodically generate a USB frame cycle SOF packet to be sent to a connected USB device. The mode code 07H is usually used to provide a USB bus reset state to a connected USB device, and the USB bus reset will not end until it is switched to other working mode. It is recommended to use mode 5 when there is no USB device. After the USB device is plugged, enter mode 7 first and then switch to mode 6.
Usually, the USB working mode is set within 10uS, and the operation status is output after completion.
5.10. CMD_GET_STATUS
This command is used to get the interrupt status of CH376 and notify CH376 to cancel the interrupt request. After CH376 requests an interrupt from MCU, MCU gets the interrupt status through the command, analyzes the interrupt cause and processes.
| Interrupt status byte | Classification of interrupt status |
|---|---|
| 00H~0FH | Please refer to CH372 Datasheet for the interrupt status in USB device mode. |
| 10H~1FH | Operation interrupt status in SD card or USB host mode |
| 20H~3FH | Communication failure status in USB host mode, used to analyze the cause of operation failure |
| 40H~4FH | File system warning codes in SD card or USB host file mode |
| 80H~BFH | File system error codes in SD card or USB host file mode |
The following table shows operation interrupt statuses in SD card or USB host mode.
| Status byte | Status name | Analysis and description of interrupt status |
|---|---|---|
| 14H | USB_INT_SUCCESS | SD card or USB transaction or transmission operation or file operation is successful |
| 15H | USB_INT_CONNECT | A USB device connection event is detected |
| 16H | USB_INT_DISCONNECT | A USB device disconnection event is detected |
| 17H | USB_INT_BUF_OVER | The transmitted data is wrong or the buffer overflows due to too many data |
| 18H | USB_INT_USB_READY | USB device has been initialized (USB address has been assigned) |
| 1DH | USB_INT_DISK_READ | Storage device performs read operation, and requests data read |
| 1EH | USB_INT_DISK_WRITE | Storage device performs write operation, and requests data write |
| 1FH | USB_INT_DISK_ERR | Storage device operation failed |
The following table shows communication failure statuses in USB host mode, which are generally used to analyze the cause of operation failure.
| Interrupt status byte | Name | Analysis and description of interrupt status |
|---|---|---|
| Bits 7-6 | (Reserved bit) | Always 00 |
| Bit 5 | (Flag bit) | Always 1, indicating that this status is the operation failure status |
| Bit 4 | IN transaction Sync flag | For IN transactions, if the bit is 0, data packets currently received are out of sync and the data may be invalid |
| Bits 3-0 | Returned value of USB device that causes operation failure | 1010 = Device returns NAK 1110 = Device returns STALL XX00 = Device returns timeout. The device did not return Other value is PID returned by the device |
The following table shows file system warning codes and error codes in SD card or USB host file mode.
Note: this table has been removed as it is not relevant for building a generic USB host library (we are not interested in the integrated file system of CH376).
5.11. CMD_RD_USB_DATA0
This command is used to read the data block from the endpoint buffer of the current USB interrupt or from the receive buffer of the host endpoint. The output data firstly read is data block length, namely, the number of bytes of subsequent data streams. The effective value of the data block length is 0 to 255. For USB underlying transport, the data block length is 0 to 64. If the length is not 0, MCU must read the subsequent data one by one from CH376.
5.12. CMD_WR_HOST_DATA
This command is used to write data blocks to the send buffer of the USB host endpoint. The input data firstly written is data block length, namely, the number of bytes of subsequent data streams. The effective value of the data block length is 0 to 64. If the length is not 0, MCU must write the subsequent data one by one to CH376.
5.13. CMD_WR_REQ_DATA
This command is used to write the data block requested by CH376 to the internal specified buffer. The output data firstly read is data block length, namely, the number of bytes of subsequent data streams that CH376 requests MCU to write. The effective value of the data block length is 0 to 255. For USB underlying transport, the data block length is 0 to 64. If the length is not 0, MCU must write the subsequent data one by one to CH376.
5.14. CMD_WR_OFS_DATA
This command is used to write the data block to the specified offset address of the internal buffer. The input data firstly written is the offset address (the internal buffer start address plus the offset address gives the start address for writing the command block), and the input data written later is the data block length, namely, the number of bytes of subsequent streams. The effective value of the data block length is 0 to 32, and the sum of offset address and data block length shall not be more than 32. If the data block length is not 0, MCU must write the subsequent data one by one to CH376.
5.15. CMD_SET_FILE_NAME (Irrelevant)
5.16. CMD_DISK_CONNECT (Irrelevant)
5.17. CMD_DISK_MOUNT (Irrelevant)
5.18. CMD_FILE_OPEN (Irrelevant)
5.19. CMD_FILE_ENUM_GO (Irrelevant)
5.20. CMD_FILE_CREATE (Irrelevant)
5.21. CMD_FILE_ERASE (Irrelevant)
5.22. CMD_FILE_CLOSE (Irrelevant)
5.23. CMD_DIR_INFO_READ (Irrelevant)
5.24. CMD_DIR_INFO_SAVE (Irrelevant)
5.25. CMD_BYTE_LOCATE (Irrelevant)
5.26. CMD_BYTE_READ (Irrelevant)
5.27. CMD_BYTE_RD_GO (Irrelevant)
5.28. CMD_BYTE_WRITE (Irrelevant)
5.29. CMD_BYTE_WR_GO (Irrelevant)
5.30. CMD_DISK_CAPACITY (Irrelevant)
5.31. CMD_DISK_QUERY (Irrelevant)
5.32. CMD_DIR_CREATE (Irrelevant)
5.33. CMD_SEC_LOCATE (Irrelevant)
5.34. CMD_SEC_READ (Irrelevant)
5.35. CMD_SEC_WRITE (Irrelevant)
5.36. CMD_DISK_BOC_CMD (Irrelevant)
5.37. CMD_DISK_READ (Irrelevant)
5.38. CMD_DISK_RD_GO (Irrelevant)
5.39. CMD_DISK_WRITE (Irrelevant)
5.40. CMD_DISK_WR_GO (Irrelevant)
6. Functional Specification
6.1. Communication Interfaces of MCU
There are two communication interfaces supported between CH376T and MCU: SPI synchronous serial interface and asynchronous serial interface. During power on reset, CH376T chip will sample the state of SPI# pin. If SPI# is at low level, SPI interface will be selected; if SPI# is at high level, the asynchronous serial interface will be selected.
The interrupt request of INT# pin output of CH376 is active at low level by default and can be connected to the interrupt input pin or ordinary input pin of MCU. MCU can get the interrupt request of CH376 in interrupt mode or query mode.
6.2. Parallel Interfaces
This section is irrelevant as we are using SPI instead of parallel interface.
6.3. SPI
This section is not relevant for building a generic USB host library, as the hardware interface is handled by the FPGA itself and works perfectly. Just note that for each of the two CH376 ICs there are three memory-mapped addresses in the FPGA:
- data address to read or write one byte of data to/from the CH376
- chip select address to activate/deactivate the CS pin of the CH376
- interrupt status address to read the INT# pin of the CH376
6.4. Asynchronous Serial Interface
This is not relevant as we are using SPI instead of UART.
6.5. Other Hardware
This section is irrelevant for building a generic USB host library.
7. Parameters
7.1. Absolute Maximum Value
Irrelevant content.
7.2. Electrical Parameters
Irrelevant content.
7.3. Timing Parameters
Test Conditions: TA=25℃, VCC=5V or VCC=3.3V, refer to the attached figure.
| Name | Parameter description | Min. | Typ. | Max. | Unit |
|---|---|---|---|---|---|
| FCLK | Input clock frequency of XI pin in USB host mode | 11.995 | 12.00 | 12.005 | MHz |
| TPR | Internal power-on reset time | 25 | 35 | 40 | mS |
| TRI | Effective signal width of external reset input | 100 | nS | ||
| TRD | Reset delay after external reset input | 25 | 32 | 35 | mS |
| TWAK | Wake-up time when exiting from low-power state | 3 | 7 | 12 | mS |
| TE1 | Execution time of command CMD_RESET_ALL | 32 | 35 | mS | |
| TE2 | Execution time of command CMD_SET_USB_MODE | 4 | 10 | uS | |
| TE3 | Execution time of command TEST_CONNECT or SET_ENDP? | 2 | 3 | uS | |
| TE4 | Execution time of command CMD_SET_BAUDRATE | 200 | 1000 | 2000 | |
| TE0 | Execution time of other commands | 1.5 | 2 | uS | |
| TSX | Interval time between command codes | 1.5 | uS | ||
| TSC | Interval time between command code and data | 1.5 | uS | ||
| TSD | Interval time between data and data | 0.6 | uS | ||
| TINT | Receive the command GET_STATUS until INT# pin undoes the interrupt | 1.5 | 2 | uS |
CH376 Datasheet (II)
USB Flash Disk and SD Card File Management Control Chip
Datasheet (II): Auxiliary Commands and USB Basic Transmission Commands
This second half of the datasheet is more interesting for building a generic USB host library, as it contains auxiliary commands and commands for performing USB basic transactions and control transmission.
1. Additional Commands
| Code | Command name CMD_ | Input data | Output data | Command purpose |
|---|---|---|---|---|
| 04H | SET_USB_SPEED | Bus speed | Set USB bus speed | |
| 0AH | GET_DEV_RATE | Data 07H | Data rate type | Get the data rate type of the USB device |
| 0AH | READ_VAR8 | Variable address | Data | Read the specified 8-bit file system variables |
| 0BH | SET_RETRY | Data 25H Number of retries |
Set the number of retries for USB transaction operations | |
| 0BH | WRITE_VAR8 | Variable address Data |
Set the specified 8-bit file system variables | |
| 0CH | READ_VAR32 | Variable address | Data (4 bytes) | Read the specified 32-bit file system variables |
| 0DH | WRITE_VAR32 | Variable address Data (4 bytes) |
Set the specified 32-bit file system variables | |
| 0FH | DELAY_100US | Delay status | Delay 100uS | |
| 13H | SET_USB_ADDR | Address value | Set USB address | |
| 16H | TEST_CONNECT | (Wait for 2uS) Connection status |
Check the connection status of USB device | |
| 17H | ABORT_NAK | Abort the retry of the current NAK. | ||
| 1CH | SET_ENDP6 | Working mode | (Wait for 3uS) | Set the receiver for USB host endpoint |
| 1DH | SET_ENDP7 | Working mode | (Wait for 3uS) | Set the transmitter for USB host endpoint |
| 25H | DIRTY_BUFFER | Clear internal disk and file buffers | ||
| 41H | CLR_STALL | Endpoint number | Generate interrupt | Control transmission: Clear endpoint error |
| 45H | SET_ADDRESS | Address value | Generate interrupt | Control transmission: Set USB address |
| 46H | GET_DESCR | Descriptor type | Generate interrupt | Control transmission: Get the descriptor |
| 49H | SET_CONFIG | Configuration Value | Generate interrupt | Control transmission: Set USB configuration |
| 4DH | AUTO_SETUP | Generate interrupt | Automatically configure USB device | |
| 4EH | ISSUE_TKN_X | Sync flag Transaction attribute |
Generate interrupt | Send synchronization token, and execute transactions |
| 51H | DISK_INIT | Generate interrupt | Initialize USB memory | |
| 52H | DISK_RESET | Generate interrupt | Reset USB storage device | |
| 53H | DISK_SIZE | Generate interrupt | Get the maximum capacity of the USB memory | |
| 58H | DISK_INQUIRY | Generate interrupt | Query USB memory features | |
| 59H | DISK_READY | Generate interrupt | Check USB memory readiness | |
| 5AH | DISK_R_SENSE | Generate interrupt | Check USB memory error | |
| 5BH | RD_DISK_SEC | Generate interrupt | Read sector data from the disk to the internal buffer | |
| 5CH | WR_DISK_SEC | Generate interrupt | Write sector data from the internal buffer to the disk | |
| 5DH | DISK_MAX_LUN | Generate interrupt | Get the maximum unit number of the USB storage device |
1.1. Command CMD_SET_USB_SPEED
This command is used to set USB bus speed. This command needs the input of 1 data to select the USB bus speed. 00H corresponds to 12Mbps full speed mode, 01H corresponds to 1.5MBps full speed mode (non-standard mode), and 02H corresponds to 1.5Mbps low speed mode. The USB bus speed of CH376 is 12Mbps full speed by default, and will be restored to 12Mbps full speed mode after the command CMD_SET_USB_MODE is executed to set USB working mode.
1.2. Command CMD_GET_DEV_RATE
This command is used to get the data rate type of the currently connected USB device. This command requires to input one data 07H, output the data rate type. If the bit 4 is 1, the device will be 1.5Mbps low speed USB device; otherwise, it will be 12Mbps full speed USB device. This command is only valid in USB mode 5 (enabled USB host mode, not generating SOF package).
1.3. Command CMD_READ_VAR8
This command is used to read the specified 8-bit (single byte) file system variables. This command requires to input 1 data specified variable address and output the data for this variable.
1.4. Command CMD_SET_RETRY
This command is used to set the number of retries for USB transaction operations. This command requires to input two data, respectively the data 25H and the number of retries.
The bits 7 and 6 of the number of retries specify the processing mode when CH376 receives NAK response. If the bit 7 is 1 and the bit 6 is 0, infinite retry will be performed (the current retry can be given up through the command CMD_ABORT_NAK); if the bit 7 is 1 and the bit 6 is 1, finite retry will be performed for 3 seconds around; if the bit 7 is 0, NAK will be notified to MCU as the result or processed as an error. Bits 5-0 of the number of retries specify the number of retries CH376 after the USB device response timeout. If the bit is 0, retry will not be performed after timeout.
The default number of retries is 8FH after chip reset or USB mode reset, so infinite retry will be performed after NAK reply, and the response is received, and 15 retries will be performed after USB device response timeout.
1.5. Command CMD_WRITE_VAR8
This command is used to set the specified 8-bit (single byte) file system variables. This command requires to input two data, respectively the specified variable address and the specified variable data.
1.6. Command CMD_READ_VAR32
This command is used to read the specified 32-bit (4 bytes) file system variables. This command requires to input 1 data specified variable address and output the data for this variable. The variable data has a total of 4 bytes, which are the lowest byte of data, the lower byte of data, the higher byte of data, and the highest byte of data.
1.7. Command CMD_WRITE_VAR32
This command is used to set the specified 32-bit (4 bytes) file system variables. This command requires to input 5 data, respectively specified variable address, the lowest byte of variable data, lower byte of data, higher byte of data and the highest byte of data.
1.8. Command CMD_DELAY_100US
This command is used to delay for 100uS and does not support serial port mode. The output data is 0 during delay, and is non-0 (usually the chip version number) at the end of the delay. MCU determines whether the delay is ended according to the read data.
1.9. Command CMD_SET_USB_ADDR
This command is used to set the USB device address. This command requires to input 1 data to select the address of the USB device being operated. After reset or after the USB device is connected or disconnected, the USB device address is always 00H, and MCU communicates with the USB device through the default address 00H. If MCU sets the address of the USB device through the standard USB request, it must set the same USB device address through the command, so that CH376 can communicate with the USB device through the new address.
1.10. Command CMD_TEST_CONNECT
This command is used to query the connection status of the current USB device in USB host mode. Typically, this command is completed within 2uS. After completion, USB_INT_CONNECT, USB_INT_DISCONNECT, or USB_INT_USB_READY is output. USB_INT_CONNECT indicates that the USB device is just connected or has been connected but has not been initialized. USB_INT_DISCONNECT indicates that the USB device has not been connected or has been disconnected. Status USB_INT_USB_READY indicates that USB device has been connected and initialized (USB address has been assigned). 0 indicates that the command is not completed and the status can be read later.
1.11. Command CMD_ABORT_NAK
This command is used to abort the retry of the current NAK. When CH376 works in the USB host mode, by default, it will keep retrying until it returns success or error when receiving the NAK status returned by the USB device. This command can force CH376 to terminate a retry in order to perform a new operation. In addition, the command SET_RETRY can be used to set whether NAK retry is disabled.
1.12. Command CMD_SET_ENDP6
This command is used to set either the USB host endpoint or the receiver of the endpoint 2 (OUT of device mode endpoint 2 / IN of host endpoint). This command needs to input 1 data to specify a new working mode. For example, if an IN transaction is executed, DATA0 is expected to be received and DATA1 is given up, the synchronous trigger flag of the receiver at the host endpoint must be set to 0 through this command. The byte in the corresponding working mode byte is 80H. Typically, this command is completed within 3uS.
For the working mode of the transceiver for USB endpoint, refer to the following table.
| Working mode byte | Name | Bit analysis description of working mode |
|---|---|---|
| Bits 7-6 | Synchronous trigger flag | If the bit 7 is 1, the bit 6 will be the new synchronous trigger flag: 00 or 01 = Keep the current synchronous trigger flag unchanged 10 = Synchronous trigger flag set to 0 11 = Synchronous trigger flag set to 1 |
| Bits 5-4 | (Reserved bit) | (Undefined, must be 0) |
| Bits 3-0 | Transaction response mode | Must be 0000 |
1.13. Command CMD_SET_ENDP7
This command is used to set either the USB host endpoint or the transmitter of the endpoint 2 (IN of device mode endpoint 2 / OUT of host endpoint). This command needs to input 1 data to specify a new working mode. For example, if SETUP or OUT transaction is executed and DATA0 is expected to be transmitted, the synchronous trigger flag of the transmitter at the host endpoint must be set to 0 through this command. The corresponding working mode byte is 80H. If DATA1 is expected to be transmitted, the working mode byte will be C0H. Typically, this command is completed within 3uS.
1.14. Command CMD_DIRTY_BUFFER
This command is used to clear internal disk and file buffers in the host file mode. After being in host file mode, CH376 always stores some frequently used data in the internal disk buffer, but when some commands (such as CMD_RD_DISK_SEC or CMD_WR_DISK_SEC, etc.) are executed, the buffer will be used, causing the buffer data to be invalid. In order to prevent CH376 from misusing the invalid data, it is necessary to notify CH376 to clear the internal buffer after other commands use the internal buffer.
1.15. Command CMD_CLR_STALL
This command is used to clear the wrong control transmission command of the endpoint. This command requires to input 1 data to specify the endpoint address of the USB device for which errors will be cleared. Valid addresses are 01H ~ 0FH for OUT endpoint and 81H ~ 8FH for IN endpoint. This command is used to simplify the standard USB request CLEAR_FEATURE. CH376 requests an interrupt from MCU after the command is executed. If the interrupt status is USB_INT_SUCCESS, the command will be executed successfully.
1.16. Command CMD_SET_ADDRESS
This command is used to set the control transmission command of the USB address. This command requires to input 1 data to specify the new address USB device, and the valid addresses are 00H-7FH. This command is used to simplify the standard USB request SET_ADDRESS. CH376 requests an interrupt from MCU after the command is executed. If the interrupt status is USB_INT_SUCCESS, the command will be executed successfully.
1.17. Command CMD_GET_DESCR
This command is used to get the control transmission command of the descriptor. This command requires to input 1 data to specify the type of descriptor to be obtained. The valid type is 1 or 2, corresponding to DEVICE descriptor and CONFIGURATION descriptor. CONFIGURATION descriptor also includes the interface descriptor and the endpoint descriptor. This command is used to simplify the standard USB request GET_DESCRIPTOR. CH376 requests an interrupt from MCU after the command is executed. If the interrupt status is USB_INT_SUCCESS, the command will be executed successfully. MCU can get the descriptor data through the command CMD_RD_USB_DATA0. As the control transmission buffer of CH376 is only 64 bytes, CH376 will return the operation status USB_INT_BUF_OVER when the length of the descriptor exceeds 64 bytes. For this USB device, MCU can process the control transmission through the command CMD_ISSUE_TKN_X.
1.18. Command CMD_SET_CONFIG
This command is used to set the control transmission command of the USB configuration. This command requires to input 1 data to specify a new USB configuration value. If the configuration value is 0, the configuration will be canceled, otherwise it shall be taken from the configuration descriptor of the USB device. This command is used to simplify the standard USB request SET_CONFIGURATION. CH376 requests an interrupt from MCU after the command is executed. If the interrupt status is USB_INT_SUCCESS, the command will be executed successfully.
1.19. Command CMD_AUTO_SETUP
This command is used to automatically configure USB device and does not support SD card. This command is used to simplify the initialization steps for the common USB device and is equivalent to the sequence of multiple commands such as GET_DESCR, SET_ADDRESS, SET_CONFIGURATION and so on. CH376 requests an interrupt from MCU after the command is executed. If the interrupt status is USB_INT_SUCCESS, the command will be executed successfully.
1.20. Command CMD_ISSUE_TKN_X
This command enables CH376 to send a synchronous token to execute the transaction. This command requires to input two data, respectively synchronization flag and transaction attribute. The bit 7 of the synchronization flag is the synchronous trigger flag of the receiver at the host endpoint, the bit 6 is the synchronous trigger flag of the transmitter at the host endpoint, and the bits 5-0 must be 0. The low 4 bits of the transaction attribute specify the token PID of the transaction, and the high 4 bits specify the destination endpoint number of the USB device. CH376 requests an interrupt from MCU after the command is executed. MCU can read the interrupt status as the operation status of the command. If the operation status is USB_INT_SUCCESS, the command will be executed successfully; otherwise, the command will be executed unsuccessfully. MCU can further analyze the cause of failure according to the operation status.
For SETUP and OUT transactions sending data, first write the data to be sent through the command CMD_WR_HOST_DATA, and then execute the transaction through the command CMD_ISSUE_TKN_X. For IN transaction receiving data, first execute the transaction through the command CMD_ISSUE_TKN_X and read the received data through the command CMD_RD_USB_DATA0 after successful execution.
The following table shows USB token PID supported by CH376.
| PID byte | Name | Description |
|---|---|---|
| 0DH | DEF_USB_PID_SETUP | Initiate control transmission, and send setup data |
| 01H | DEF_USB_PID_OUT | Execute OUT transaction, and send data |
| 09H | DEF_USB_PID_IN | Execute IN transaction, and receive data |
For example, when the transaction attribute byte is 09H, CH376 receives data from the default endpoint 0 of the USB device; when the transaction attribute byte is 21H, CH376 sends data to the endpoint 2 of the USB device; when the transaction attribute byte is 29H, CH376 receives data from the endpoint 2 of the USB device, with address of 82H.
1.21. Command CMD_DISK_INIT (Irrelevant)
1.22. Command CMD_DISK_RESET (Irrelevant)
1.23. Command CMD_DISK_SIZE (Irrelevant)
1.24. Command CMD_DISK_INQUIRY (Irrelevant)
1.25. Command CMD_DISK_READY (Irrelevant)
1.26. Command CMD_DISK_R_SENSE (Irrelevant)
1.27. Command CMD_RD_DISK_SEC (Irrelevant)
1.28. Command CMD_WR_DISK_SEC (Irrelevant)
1.29. Command CMD_DISK_MAX_LUN (Irrelevant)
2. External Firmware
2.1. Overview
The command CMD_ISSUE_TKN_X is used to perform basic USB transmission transactions. It is the most basic operation in firmware programming in the USB host mode. On this basis, the external MCU can process the control transmission for which CH376 has not directly provided the simplified commands according to the requirements of USB protocol. Further, MCU can process the USB protocol of a variety of specific types of devices in accordance with the requirements of the USB protocol, to achieve the control and data exchange of USB devices. CH376 has Bulk-Only transport protocol for Mass-Storage devices. For USB Storage devices using CBI transport protocol or other transport protocols, the external MCU still needs to process based on the command CMD_ISSUE_TKN_X and the control transmission command.
2.2. External Firmware Reference Flow
The external firmware reference program is provided in the CH376 evaluation board data. The following process is that the external MCU executes the standard USB request GET_STATUS through the control transmission to get the status of USB device for reference when the external MCU designs the firmware program.
(1) Setup stage of control transmission
- Send the command CMD_WR_HOST_DATA to write 8 bytes of request data to the output buffer. The request data is 80H, 00H, 00H, 00H, 00H, 02H and 00H in order, and the length is 8.
- Send the command CMD_ISSUE_TKN_X to execute a transaction with the synchronization flag of 00H and the transaction attribute byte of 0DH, send a SETUP token to the default endpoint 0, and send DATA0.
- MCU waits for the completion of transaction interrupt or wait for the interrupt notification.
- After the transaction is completed, CH376 sets INT# pin to low level and requests an interrupt from MCU.
- MCU enters the interrupt service program, or exits after receiving the interrupt notification in the main program.
- Send the command CMD_GET_STATUS to get the interrupt status.
- CH376 restores INT# pin to a high level and cancels the interrupt request after the command CMD_GET_STATUS is completed..
- MCU analyzes the obtained interrupt status. If it is not USB_INT_SUCCESS, the operation will fail and exception will be handled; if it is USB_INT_SUCCESS, the transaction will be executed successfully and the setup stage will be completed.
(2) Data stage of control transmission
- Send the command CMD_ISSUE_TKN_X to execute a transaction with the synchronization flag of 80H and the transaction attribute byte of 09H, send an IN token to the default endpoint 0, and send DATA1.
- MCU waits for the transaction to be completed; after the transaction is completed, CH376 requests an interrupt from MCU.
- Send the command CMD_GET_STATUS to get the interrupt status. CH376 cancels the interrupt request.
- MCU analyzes the interrupt status. If the operation failed, exception will be handled; if it is USB_INT_SUCCESS, the transaction will be executed successfully.
- Send the command CMD_RD_USB_DATA0 to get the data returned by the USB device and save it as the return result of the control transmission.
- Because this control transmission requires only one IN transaction, the data stage is completed.
(3) Status stage of control transmission
- Send the command CMD_WR_HOST_DATA to write the status data of the length 0 to the output buffer. The length is 0.
- Send the command CMD_ISSUE_TKN_X to execute a transaction with the synchronization flag of 40H and the transaction attribute byte of 01H, send an OUT token to the default endpoint 0 and send DATA1.
- MCU waits for the transaction to be completed; after the transaction is completed, CH376 requests an interrupt from MCU.
- Send the command CMD_GET_STATUS to get the interrupt status. CH376 cancels the interrupt request.
- MCU analyzes the obtained interrupt status. If the operation failed, exception will be handled; if it is USB_INT_SUCCESS, the transaction will be executed successfully and the status stage will be completed.
(4) The control transmission is completed. The data returned in the data stage is returned as the standard USB request GET_STATUS. Typically, the length of returned data is 2 bytes.
END OF DATASHEETS