Skip to content

Instantly share code, notes, and snippets.

@meriororen
Created September 24, 2018 09:16
Show Gist options
  • Save meriororen/4a5a92dd3944eee86e57e84b653ba6ce to your computer and use it in GitHub Desktop.
Save meriororen/4a5a92dd3944eee86e57e84b653ba6ce to your computer and use it in GitHub Desktop.
ch375b test code snippet
int do_once = 0;
typedef struct __attribute__((__packed__)) {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdUSB;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bMaxPacketSize;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
} USBDeviceDescriptor;
typedef struct __attribute__((__packed__)) {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wTotalLength;
uint8_t bNumInterfaces;
uint8_t bConfigurationValue;
uint8_t iConfiguration;
uint8_t bmAttributes;
uint8_t bMaxPower;
} USBConfigurationDescriptor;
typedef struct __attribute__((__packed__)) {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bNumEndpoints;
uint8_t bInterfaceClass;
uint8_t bInterfaceSubClass;
uint8_t bInterfaceProtocol;
uint8_t iInterface;
} USBInterfaceDescriptor;
typedef struct __attribute__((__packed__)) {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
} USBEndpointDescriptor;
typedef struct __attribute__((__packed__)) {
USBConfigurationDescriptor configuration;
USBInterfaceDescriptor interface;
USBEndpointDescriptor endpoints[4];
} USBConfigurationDescriptorFull;
void toggleHostEndpoint( uint8_t tog )
{
USART_PutChar(0x1D);
USART_PutChar(tog ? 0xC0 : 0x80);
USB_OTG_BSP_mDelay(2);
}
uint8_t sendToken(uint8_t targetEndpoint, uint8_t pid)
{
USB_OTG_BSP_mDelay(10);
USART_PutChar(0x4F); //send token
USART_PutChar(targetEndpoint << 4 | pid);
USB_OTG_BSP_mDelay(10);
USART_PutChar(0x22); // get status
if (USART_GetChar() != 0x14) while(1);
USB_OTG_BSP_mDelay(20);
return 0;
}
uint8_t toggleSend = 0;
uint8_t doBulkTransfer(uint8_t targetEndpoint, uint8_t *c, uint16_t len)
{
if (len > 64) return !0;
toggleHostEndpoint(toggleSend);
/* send data */
USART_PutChar(0x2B);
USART_PutChar(len & 0xff);
USART_Write(c);
if (!sendToken(targetEndpoint, 0x01)) {
toggleSend = !toggleSend;
return 0;
} else {
return !0;
}
}
uint16_t bufIndex = 0;
uint8_t bufSend[64];
void writeToUsb(uint8_t targetEndpoint, uint8_t b){
if (bufIndex == 64) {
doBulkTransfer(targetEndpoint, &bufSend[0], bufIndex);
bufIndex = 0;
}
bufSend[bufIndex++] = b;
}
void printUsb(uint8_t targetEndpoint, char *s, uint16_t len) {
while (len--) {
writeToUsb(targetEndpoint, *s++);
}
}
void test_usb_ch375b( void )
{
USBDeviceDescriptor desc;
USBConfigurationDescriptorFull descfull;
volatile uint16_t len = 0;
uint8_t outEndpointNum = 0;
if (do_once == 0) {
USART_Write("\x15\x07");
USB_OTG_BSP_mDelay(1);
if (USART_GetChar() != 0x51) return;
USART_Write("\x15\x06");
USB_OTG_BSP_mDelay(10);
if (USART_GetChar() != 0x51) return;
USART_Write("\x46\x01");
USB_OTG_BSP_mDelay(10);
USART_Write("\x28");
USB_OTG_BSP_mDelay(1);
len = USART_GetChar() & 0x00ff;
USART_GetString(len, (uint8_t *)&desc);
USB_OTG_BSP_mDelay(10);
USART_Write("\x45\x03"); // set address
USB_OTG_BSP_mDelay(5);
USART_Write("\x22"); // get status
if (USART_GetChar() != 0x14) while(1);
USB_OTG_BSP_mDelay(20);
USART_Write("\x13\x03"); // set usb address
USB_OTG_BSP_mDelay(5);
USART_Write("\x46\x02"); // get full config
USB_OTG_BSP_mDelay(10);
USART_Write("\x28");
USB_OTG_BSP_mDelay(1);
len = USART_GetChar() & 0x00ff;
USART_GetString(len, (uint8_t *)&descfull);
USB_OTG_BSP_mDelay(10);
USART_PutChar(0x49); // set config value
USART_PutChar(descfull.configuration.bConfigurationValue);
USB_OTG_BSP_mDelay(5);
USART_Write("\x22"); // get status
if (USART_GetChar() != 0x14) while(1);
outEndpointNum = descfull.endpoints[1].bEndpointAddress & 0x0f;
doBulkTransfer(outEndpointNum, "hello worldzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzyyyyyyyyyyyy!\n\n", 64);
doBulkTransfer(outEndpointNum, "hello worldzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzyyyyyyyyyyyy!\n\n", 64);
doBulkTransfer(outEndpointNum, "hello worldzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzyyyyyyyyyyyy!\n\n", 64);
doBulkTransfer(outEndpointNum, "hello worldzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzyyyyyyyyyyyy!\n\n", 64);
doBulkTransfer(outEndpointNum, "hello worldzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzyyyyyyyyyyyy!\n\n", 64);
doBulkTransfer(outEndpointNum, "hello worldzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzyyyyyyyyyyyy!\n\n", 64);
doBulkTransfer(outEndpointNum, "hello worldzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzyyyyyyyyyyyy!\n\n", 64);
doBulkTransfer(outEndpointNum, "hello worldzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzyyyyyyyyyyyy!\n\n", 64);
do_once = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment