CH376 USB Host Interface Module Flash Disk Drive RW Module Documentation
The CH376 USB Host Interface Module Flash Disk Drive RW Module is a USB interface module that allows microcontrollers to read and write data to USB flash drives. This module is based on the CH376 chip, which is a USB host controller that supports USB 1.1 and 2.0 specifications.
The module has the following pinouts:
VCC: Power supply (3.3V or 5V)
GND: Ground
CS: Chip select (active low)
SCK: Clock signal
MOSI: Master out, slave in (data transmission)
MISO: Master in, slave out (data reception)
INT: Interrupt signal
D+: USB differential signal
D-: USB differential signal
The CH376 module communicates with the microcontroller using a SPI protocol. The module acts as a slave device, and the microcontroller acts as the master device.
Example 1: Reading a File from a USB Flash Drive using Arduino
This example demonstrates how to use the CH376 module to read a file from a USB flash drive using an Arduino board.
#define CH376_CS 10 // Chip select pin
#define CH376_INT 2 // Interrupt pin
void setup() {
Serial.begin(9600);
pinMode(CH376_CS, OUTPUT);
pinMode(CH376_INT, INPUT);
SPI.begin();
}
void loop() {
// Initialize the CH376 module
digitalWrite(CH376_CS, LOW);
SPI.transfer(0x57);
SPI.transfer(0x03);
digitalWrite(CH376_CS, HIGH);
// Wait for the USB flash drive to be recognized
while (digitalRead(CH376_INT) == LOW) {
delay(1);
}
// Open a file on the USB flash drive
char filename[] = "example.txt";
uint8_t buffer[256];
int bytesRead = 0;
digitalWrite(CH376_CS, LOW);
SPI.transfer(0x57);
SPI.transfer(0x04);
digitalWrite(CH376_CS, HIGH);
while (bytesRead < 256) {
digitalWrite(CH376_CS, LOW);
SPI.transfer(0x57);
SPI.transfer(0x01);
for (int i = 0; i < 256; i++) {
buffer[i] = SPI.transfer(0x00);
}
digitalWrite(CH376_CS, HIGH);
bytesRead += 256;
}
// Print the file contents
for (int i = 0; i < bytesRead; i++) {
Serial.print(buffer[i], HEX);
Serial.print(" ");
}
Serial.println();
}
```
Example 2: Writing Data to a USB Flash Drive using Raspberry Pi (Python)
This example demonstrates how to use the CH376 module to write data to a USB flash drive using a Raspberry Pi board.
```python
import spidev
import RPi.GPIO as GPIO
# Initialize the SPI interface
bus = 0
device = 0
spi = spidev.SpiDev()
spi.open(bus, device)
spi.max_speed_hz = 1000000
# Initialize the GPIO interface
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT) # Chip select pin
GPIO.setup(23, GPIO.IN) # Interrupt pin
# Initialize the CH376 module
GPIO.output(17, GPIO.LOW)
spi.xfer2([0x57, 0x03])
GPIO.output(17, GPIO.HIGH)
# Wait for the USB flash drive to be recognized
while GPIO.input(23) == GPIO.LOW:
time.sleep(0.1)
# Open a file on the USB flash drive
filename = "example.txt"
data = bytearray("Hello, World!", 'utf-8')
# Write data to the file
GPIO.output(17, GPIO.LOW)
spi.xfer2([0x57, 0x04])
for byte in data:
spi.xfer2([0x57, 0x01, byte])
GPIO.output(17, GPIO.HIGH)
```
Example 3: Enumerating USB Devices using ESP32 (C++)
This example demonstrates how to use the CH376 module to enumerate USB devices using an ESP32 board.
```c++
#include <WiFi.h>
#include <SPI.h>
#define CH376_CS 5 // Chip select pin
#define CH376_INT 18 // Interrupt pin
void setup() {
Serial.begin(115200);
pinMode(CH376_CS, OUTPUT);
pinMode(CH376_INT, INPUT);
spi = new SPIClass(VSPI);
spi->begin();
spi->setFrequency(1000000);
}
void loop() {
// Initialize the CH376 module
digitalWrite(CH376_CS, LOW);
spi->transfer(0x57);
spi->transfer(0x03);
digitalWrite(CH376_CS, HIGH);
// Wait for the USB device to be enumerated
while (digitalRead(CH376_INT) == LOW) {
delay(1);
}
// Get the device count
uint8_t deviceCount = 0;
digitalWrite(CH376_CS, LOW);
spi->transfer(0x57);
spi->transfer(0x05);
deviceCount = spi->transfer(0x00);
digitalWrite(CH376_CS, HIGH);
// Print the device list
for (int i = 0; i < deviceCount; i++) {
digitalWrite(CH376_CS, LOW);
spi->transfer(0x57);
spi->transfer(0x06);
uint8_t deviceInfo[8];
for (int j = 0; j < 8; j++) {
deviceInfo[j] = spi->transfer(0x00);
}
digitalWrite(CH376_CS, HIGH);
Serial.print("Device ");
Serial.print(i);
Serial.print(": ");
for (int j = 0; j < 8; j++) {
Serial.print(deviceInfo[j], HEX);
Serial.print(" ");
}
Serial.println();
}
}
```
Note: These examples are for illustration purposes only and may require modifications to work with your specific setup. Ensure that you have the correct pinouts, voltage levels, and communication protocols configured for your application.