-
-
Save quadcodes/671f5e30247bb9129a02c2d9ef4cfbd1 to your computer and use it in GitHub Desktop.
CH376S USB Chip with UART Communication.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the clear code for CH376S UART communication. | |
#include <SoftwareSerial.h> | |
byte USB_Byte; //used to store data coming from the USB stick | |
int LED = 13; //the LED is connected to digital pin 13 | |
int timeOut = 2000; //TimeOut is 2 seconds. This is the amount of time you wish to wait for a response from the CH376S module. | |
SoftwareSerial USB(10, 11); // Digital pin 10 on Arduino (RX) connects to TXD on the CH376S module | |
// Digital pin 11 on Arduino (TX) connects to RXD on the CH376S module | |
// GND on Arduino to GND on CH376S module | |
// 5V on Arduino to 5V on CH376S module | |
//waitForResponse=================================================================================== | |
//is used to wait for a response from USB. Returns true when bytes become available, false if it times out. | |
boolean waitForResponse(String errorMsg){ | |
boolean bytesAvailable = true; | |
int counter=0; | |
while(!USB.available()){ //wait for CH376S to verify command | |
delay(1); | |
counter++; | |
if(counter>timeOut){ | |
Serial.print("TimeOut waiting for response: Error while: "); | |
Serial.println(errorMsg); | |
bytesAvailable = false; | |
break; | |
} | |
} | |
delay(1); | |
return(bytesAvailable); | |
} | |
//getResponseFromUSB================================================================================ | |
//is used to get any error codes or messages from the CH376S module (in response to certain commands) | |
byte getResponseFromUSB(){ | |
byte response = byte(0x00); | |
if (USB.available()){ | |
response = USB.read(); | |
} | |
return(response); | |
} | |
//checkConnection================================================================================== | |
//This function is used to check for successful communication with the CH376S module. This is not dependant of the presence of a USB stick. | |
//Send any value between 0 to 255, and the CH376S module will return a number = 255 - value. | |
boolean checkConnection(){ | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x06); | |
USB.write(0x01); | |
if(waitForResponse("checking connection")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false. | |
if(getResponseFromUSB()==(255-0x01)){ | |
Serial.println(">Connection to CH376S was successful."); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} | |
//diskConnectionStatus================================================================================ | |
//Check the disk connection status | |
boolean diskConnectionStatus(){ | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x30); | |
if(waitForResponse("Connecting to USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false. | |
if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful | |
Serial.println(">Connection to USB OK"); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} | |
//set_USB_Mode===================================================================================== | |
//Make sure that the USB is inserted when using 0x06 as the value in this specific code sequence | |
boolean set_USB_Mode (){ | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x15); | |
USB.write(0x06); | |
delay(20); | |
if(USB.available()){ | |
USB_Byte=USB.read(); | |
//Check to see if the command has been successfully transmitted and acknowledged. | |
if(USB_Byte==0x51){ // If true - the CH376S has acknowledged the command. | |
USB_Byte = USB.read(); | |
//Check to see if the USB stick is connected or not. | |
if(USB_Byte==0x15){ // If true - there is a USB stick connected | |
Serial.println(">USB is present"); | |
return false; | |
} else { | |
Serial.print(">USB Not present. Error code:"); // If the USB is not connected - it should return an Error code = FFH | |
Serial.print(USB_Byte, HEX); | |
Serial.println("H"); | |
return true; | |
} | |
} else { | |
Serial.print("CH3765 error! Error code:"); | |
Serial.print(USB_Byte, HEX); | |
Serial.println("H"); | |
return true; | |
} | |
} | |
delay(20); | |
} | |
//USBdiskMount======================================================================================== | |
//initialise the USB disk and check that it is ready - this process is required if you want to find the manufacturing information of the USB disk | |
boolean USBdiskMount(){ | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x31); | |
if(waitForResponse("mounting USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false. | |
if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful | |
Serial.println(">USB Mounted - OK"); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} | |
//diskQuery=========================================================================================== | |
void diskQuery(){ | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x3F); // Disk Query 查詢儲存空間訊息 | |
if(waitForResponse("Query USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false. | |
if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful | |
Serial.println(">Query - OK!"); | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x27); // Read data from current interrupt port buffer of USB or receive buffer of host port | |
if(waitForResponse("reading data")){ //Wait for the CH376S module to return data. TimeOut will return false. If data is being transmitted, it will return true. | |
Serial.print("This USB has "); | |
unsigned long remainSize = 0; | |
uint8_t remainSize1 = 0; | |
uint16_t remainSize2 = 0; | |
uint32_t remainSize3 = 0; | |
uint32_t remainSize4 = 0; | |
int remainSizeBuf = 0; | |
remainSizeBuf = USB.read(); | |
delay(1); | |
remainSizeBuf = USB.read(); | |
delay(1); | |
remainSizeBuf = USB.read(); | |
delay(1); | |
remainSizeBuf = USB.read(); | |
delay(1); | |
remainSizeBuf = USB.read(); | |
delay(1); | |
remainSize1 = USB.read(); | |
delay(1); | |
remainSize2 = USB.read(); | |
delay(1); | |
remainSize3 = USB.read(); | |
delay(1); | |
remainSize4 = USB.read(); | |
delay(1); | |
remainSize = remainSize1 | remainSize2 << 8 | remainSize3 << 16 | remainSize4 << 24; | |
remainSizeBuf = USB.read(); | |
delay(1); | |
Serial.print(remainSize/(1000000/512)); | |
Serial.println(" MB Left."); | |
delay(1); | |
} | |
} | |
else { | |
Serial.print(">Query FAILED."); | |
} | |
} | |
} | |
//============================================================================================================================================== | |
void setup() { | |
Serial.begin(9600); // Setup serial communication with the computer (using a baud rate of 9600 on serial monitor) | |
USB.begin(9600); // Setup serial communication with the CH376S module (using the default baud rate of 9600) | |
pinMode(LED, OUTPUT); // Define digital pin 13 as an OUTPUT pin - so that we can use it with an LED | |
digitalWrite(LED, LOW); // Turn off the LED | |
Serial.println("Initializing..."); | |
} | |
//================================================================================================================================================ | |
void loop() { | |
while(checkConnection()); | |
while(diskConnectionStatus()); | |
while(set_USB_Mode()); | |
while(USBdiskMount()); | |
diskQuery(); | |
while(true){ | |
if(USB.available()){ // This is here to capture any unexpected data transmitted by the CH376S module | |
int input = USB.read(); | |
Serial.print(">CH376S has just sent this code:"); | |
Serial.println(input, HEX); | |
if(input == 0x15){ | |
Serial.println("USB connected."); | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
byte computerByte; //used to store data coming from the computer | |
byte USB_Byte; //used to store data coming from the USB stick | |
int LED = 13; //the LED is connected to digital pin 13 | |
int timeOut = 2000; //TimeOut is 5 seconds. This is the amount of time you wish to wait for a response from the CH376S module. | |
int buf[64]; | |
SoftwareSerial USB(10, 11); // Digital pin 10 on Arduino (RX) connects to TXD on the CH376S module | |
// Digital pin 11 on Arduino (TX) connects to RXD on the CH376S module | |
// GND on Arduino to GND on CH376S module | |
// 5V on Arduino to 5V on CH376S module | |
//waitForResponse=================================================================================== | |
//is used to wait for a response from USB. Returns true when bytes become available, false if it times out. | |
boolean waitForResponse(String errorMsg){ | |
boolean bytesAvailable = true; | |
int counter=0; | |
while(!USB.available()){ //wait for CH376S to verify command | |
delay(1); | |
counter++; | |
if(counter>timeOut){ | |
Serial.print("TimeOut waiting for response: Error while: "); | |
Serial.println(errorMsg); | |
bytesAvailable = false; | |
break; | |
} | |
} | |
delay(1); | |
return(bytesAvailable); | |
} | |
//getResponseFromUSB================================================================================ | |
//is used to get any error codes or messages from the CH376S module (in response to certain commands) | |
byte getResponseFromUSB(){ | |
byte response = byte(0x00); | |
if (USB.available()){ | |
response = USB.read(); | |
} | |
return(response); | |
} | |
//blinkLED========================================================================================== | |
//Turn an LED on for 1 second | |
void blinkLED(){ | |
digitalWrite(LED, HIGH); | |
delay(1000); | |
digitalWrite(LED,LOW); | |
} | |
void printCommandHeader(String header){ | |
Serial.println("======================"); | |
Serial.println(""); | |
Serial.println(header); | |
Serial.println("----------------------"); | |
} | |
//checkConnection================================================================================== | |
//This function is used to check for successful communication with the CH376S module. This is not dependant of the presence of a USB stick. | |
//Send any value between 0 to 255, and the CH376S module will return a number = 255 - value. | |
void checkConnection(byte value){ | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x06); | |
USB.write(value); | |
if(waitForResponse("checking connection")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false. | |
if(getResponseFromUSB()==(255-value)){ | |
Serial.println(">Connection to CH376S was successful."); | |
blinkLED(); //blink the LED for 1 second if the connection was successful | |
} else { | |
Serial.print(">Connection to CH376S - FAILED."); | |
} | |
} | |
} | |
//diskConnectionStatus================================================================================ | |
//Check the disk connection status | |
void diskConnectionStatus(){ | |
Serial.println("Checking USB disk connection status"); | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x30); | |
if(waitForResponse("Connecting to USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false. | |
if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful | |
Serial.println(">Connection to USB OK"); | |
} else { | |
Serial.print(">Connection to USB - FAILED."); | |
} | |
} | |
} | |
//set_USB_Mode===================================================================================== | |
//Make sure that the USB is inserted when using 0x06 as the value in this specific code sequence | |
void set_USB_Mode (byte value){ | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x15); | |
USB.write(value); | |
delay(20); | |
if(USB.available()){ | |
USB_Byte=USB.read(); | |
//Check to see if the command has been successfully transmitted and acknowledged. | |
if(USB_Byte==0x51){ // If true - the CH376S has acknowledged the command. | |
Serial.println("set_USB_Mode command acknowledged"); //The CH376S will now check and monitor the USB port | |
USB_Byte = USB.read(); | |
//Check to see if the USB stick is connected or not. | |
if(USB_Byte==0x15){ // If true - there is a USB stick connected | |
Serial.println("USB is present"); | |
blinkLED(); // If the process was successful, then turn the LED on for 1 second | |
} else { | |
Serial.print("USB Not present. Error code:"); // If the USB is not connected - it should return an Error code = FFH | |
Serial.print(USB_Byte, HEX); | |
Serial.println("H"); | |
} | |
} else { | |
Serial.print("CH3765 error! Error code:"); | |
Serial.print(USB_Byte, HEX); | |
Serial.println("H"); | |
} | |
} | |
delay(20); | |
} | |
//USBdiskMount======================================================================================== | |
//initialise the USB disk and check that it is ready - this process is required if you want to find the manufacturing information of the USB disk | |
void USBdiskMount(){ | |
Serial.println("Mounting USB disk"); | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x31); | |
if(waitForResponse("mounting USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false. | |
if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful | |
Serial.println(">USB Mounted - OK"); | |
} else { | |
Serial.print(">Failed to Mount USB disk."); | |
} | |
} | |
} | |
//====================================================================================================================== | |
void diskQuery(){ | |
Serial.println("Querying USB disk"); | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x3F); // Disk Query 查詢儲存空間訊息 | |
if(waitForResponse("Query USB disk")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false. | |
if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful | |
Serial.println(">Query - OK!"); | |
USB.write(0x57); | |
USB.write(0xAB); | |
USB.write(0x27); // Read data from current interrupt port buffer of USB or receive buffer of host port | |
if(waitForResponse("reading data")){ //Wait for the CH376S module to return data. TimeOut will return false. If data is being transmitted, it will return true. | |
Serial.print("This USB has "); | |
unsigned long remainSize = 0; | |
uint8_t remainSize1 = 0; | |
uint16_t remainSize2 = 0; | |
uint32_t remainSize3 = 0; | |
uint32_t remainSize4 = 0; | |
int remainSizeBuf = 0; | |
remainSizeBuf = USB.read(); | |
remainSizeBuf = USB.read(); | |
remainSizeBuf = USB.read(); | |
remainSizeBuf = USB.read(); | |
remainSizeBuf = USB.read(); | |
remainSize1 = USB.read(); | |
remainSize2 = USB.read(); | |
remainSize3 = USB.read(); | |
remainSize4 = USB.read(); | |
remainSize = remainSize1 | remainSize2 << 8 | remainSize3 << 16 | remainSize4 << 24; | |
remainSizeBuf = USB.read(); | |
Serial.print(remainSize/(1000000/512)); | |
Serial.println(" MB Left."); | |
delay(10); | |
} | |
} | |
else { | |
Serial.print(">Query FAILED."); | |
} | |
} | |
} | |
//============================================================================================================================================== | |
void setup() { | |
Serial.begin(9600); // Setup serial communication with the computer (using a baud rate of 9600 on serial monitor) | |
USB.begin(9600); // Setup serial communication with the CH376S module (using the default baud rate of 9600) | |
pinMode(LED, OUTPUT); // Define digital pin 13 as an OUTPUT pin - so that we can use it with an LED | |
digitalWrite(LED, LOW); // Turn off the LED | |
Serial.println("Ready!"); | |
} | |
//================================================================================================================================================ | |
void loop() { | |
if(Serial.available()){ | |
computerByte = Serial.read(); | |
if(computerByte==49){ //1 | |
printCommandHeader("COMMAND1: Check connection"); | |
checkConnection(0x01); | |
} | |
if(computerByte==50){ //2 | |
printCommandHeader("COMMAND2: Check USB disk connection"); | |
diskConnectionStatus(); | |
} | |
if(computerByte==51){ //3 | |
printCommandHeader("COMMAND2: set_USB_Mode"); | |
set_USB_Mode(0x06); // Code used to enable read/write communication and monitoring of the USB stick | |
} | |
if(computerByte==52){ //4 | |
printCommandHeader("COMMAND3: Mount USB disk"); | |
USBdiskMount(); | |
} | |
if(computerByte==53){ //5 | |
printCommandHeader("COMMAND4: Disk Query"); | |
diskQuery(); | |
} | |
} | |
if(USB.available()){ // This is here to capture any unexpected data transmitted by the CH376S module | |
Serial.print("CH376S has just sent this code:"); | |
Serial.println(USB.read(), HEX); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment