Skip to content

Instantly share code, notes, and snippets.

@leopck
Last active October 4, 2016 07:15
Show Gist options
  • Save leopck/0d3d1e38a11f3742f0a86bdcb3d88740 to your computer and use it in GitHub Desktop.
Save leopck/0d3d1e38a11f3742f0a86bdcb3d88740 to your computer and use it in GitHub Desktop.
usb_config.h for espusb low speed CDC (Still work in progress; doesn't work)
#ifndef _USB_CONFIG_H
#define _USB_CONFIG_H
//Defines the number of endpoints for this device. (Always add one for EP0)
#define ENDPOINTS 3
//Defines a pin that is useful for debugging USB on a logic analyzer
//#define DEBUGPIN 2
//DPLUS and DMINUS are not actually used except for setting things up.
#define DPLUS 5
#define DMINUS 4
//This is what's used in the assembly
#define DMINUSBASE DMINUS //Must be D- first, then D+ second.
#define PERIPHSDPLUS PERIPHS_IO_MUX_GPIO5_U
#define PERIPHSDMINUS PERIPHS_IO_MUX_GPIO4_U
#define FUNCDPLUS FUNC_GPIO5
#define FUNCDMINUS FUNC_GPIO4
#endif
#ifdef INSTANCE_DESCRIPTORS
//Taken from http://www.usbmadesimple.co.uk/ums_ms_desc_dev.htm
static const uint8_t device_descriptor[] = {
0x09, //Length
1, //Type (Device)
0x10, 0x01, //Spec
0x02, //Device Class
0x02, //Device Subclass
0x0, //Device Protocol (000 = use config descriptor)
0x08, //Max packet size for EP0 (This has to be 8 because of the USB Low-Speed Standard)
0xcd, 0xab, //ID Vendor //TODO: register this in http://pid.codes/howto/ or somewhere.
0x66, 0x82, //ID Product
0x02, 0x00, //ID Rev
1, //Manufacturer string
2, //Product string
0, //Serial string
1, //Max number of configurations
};
static const uint8_t config_descriptor[] = { //Mostly stolen from a USB mouse I found.
// configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
9, // bLength;
2, // bDescriptorType;
67, 0x00, // wTotalLength
//34, 0x00, //for just the one descriptor
0x02, // bNumInterfaces (Normally 1)
0x01, // bConfigurationValue
0x00, // iConfiguration
0x80, // bmAttributes (was 0xa0)
0x64, // bMaxPower (200mA)
// Communication interface descriptor (http://janaxelson.com/usb_virtual_com_port.htm)
0x09, // Descriptor size in bytes
0x04, // INTERFACE descriptor type
0x00, // Interface number
0x00, // Alternate setting number
0x01, // Number of endpoints
0x02, // Class: CDC communication
0x02, // Subclass: abstract control model
0x00, // Protocol: V.25ter (AT commands)
0x00, // Interface string index
/* CDC Class-Specific descriptor (from low speed cdc232 avr)*/
5, /* sizeof(usbDescrCDC_HeaderFn): length of descriptor in bytes */
0x24, /* descriptor type */
0, /* header functional descriptor */
0x10, 0x01,
4, /* sizeof(usbDescrCDC_AcmFn): length of descriptor in bytes */
0x24, /* descriptor type */
2, /* abstract control management functional descriptor */
0x02, /* SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE */
5, /* sizeof(usbDescrCDC_UnionFn): length of descriptor in bytes */
0x24, /* descriptor type */
6, /* union functional descriptor */
0, /* CDC_COMM_INTF_ID */
1, /* CDC_DATA_INTF_ID */
5, /* sizeof(usbDescrCDC_CallMgtFn): length of descriptor in bytes */
0x24, /* descriptor type */
1, /* call management functional descriptor */
3, /* allow management on data interface, handles call management by itself */
1, /* CDC_DATA_INTF_ID */
/* Endpoint Descriptor */
7, /* sizeof(usbDescrEndpoint) */
5, /* descriptor type = endpoint */
0x80|3, /* IN endpoint number */
0x03, /* attrib: Interrupt endpoint */
8, 0, /* maximum packet size */
255, /* in ms */
/* Interface Descriptor */
9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
4, /* descriptor type */
1, /* index of this interface */
0, /* alternate setting for this interface */
2, /* endpoints excl 0: number of endpoint descriptors to follow */
0x0A, /* Data Interface Class Codes */
0,
0, /* Data Interface Class Protocol Codes */
0, /* string index for interface */
/* Endpoint Descriptor */
7, /* sizeof(usbDescrEndpoint) */
5, /* descriptor type = endpoint */
0x01, /* OUT endpoint number 1 */
//0x02, /* attrib: Bulk endpoint */ (BUG: https://github.com/digistump/DigistumpArduino/issues/16)
0x03, /* attrib: Interrupt endpoint */
8, 0, /* maximum packet size */
255, /* in ms */
/* Endpoint Descriptor */
7, /* sizeof(usbDescrEndpoint) */
5, /* descriptor type = endpoint */
0x81, /* IN endpoint number 1 */
//0x02, /* attrib: Bulk endpoint */ (BUG: https://github.com/digistump/DigistumpArduino/issues/16)
0x03, /* attrib: Interrupt endpoint */
8, 0, /* maximum packet size */
255, /* in ms */
};
static const uint8_t mouse_hid_desc[52] = { //From http://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x02, // USAGE (Mouse)
0xa1, 0x01, // COLLECTION (Application)
0x09, 0x01, // USAGE (Pointer)
0xa1, 0x00, // COLLECTION (Physical)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x03, // USAGE_MAXIMUM (Button 3)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x95, 0x03, // REPORT_COUNT (3)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x05, // REPORT_SIZE (5)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x38, // USAGE (Y)
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7f, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x03, // REPORT_COUNT (3)
0x81, 0x06, // INPUT (Data,Var,Rel)
0xc0, // END_COLLECTION
0xc0 // END_COLLECTIONs
};
//From http://codeandlife.com/2012/06/18/usb-hid-keyboard-with-v-usb/
static const uint8_t keyboard_hid_desc[63] = { /* USB report descriptor */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x05, 0x07, // USAGE_PAGE (Keyboard)(Key Codes)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)(224)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)(231)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x81, 0x02, // INPUT (Data,Var,Abs) ; Modifier byte
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x03, // INPUT (Cnst,Var,Abs) ; Reserved byte
0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x05, 0x08, // USAGE_PAGE (LEDs)
0x19, 0x01, // USAGE_MINIMUM (Num Lock)
0x29, 0x05, // USAGE_MAXIMUM (Kana)
0x91, 0x02, // OUTPUT (Data,Var,Abs) ; LED report
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x03, // REPORT_SIZE (3)
0x91, 0x03, // OUTPUT (Cnst,Var,Abs) ; LED report padding
0x95, 0x06, // REPORT_COUNT (6)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 167, // LOGICAL_MAXIMUM (167) (Normally would be 101, but we want volume buttons)
0x05, 0x07, // USAGE_PAGE (Keyboard)(Key Codes)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))(0)
0x29, 167, // USAGE_MAXIMUM (Keyboard Application)(101) (Now 167)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
//Ever wonder how you have more than 6 keys down at the same time on a USB keyboard? It's easy. Enumerate two keyboards!
#define STR_MANUFACTURER L"CNLohr"
#define STR_PRODUCT L"ESPUSB2"
#define STR_SERIAL L"000"
struct usb_string_descriptor_struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wString[];
};
const static struct usb_string_descriptor_struct string0 = {
4,
3,
{0x0409}
};
const static struct usb_string_descriptor_struct string1 = {
sizeof(STR_MANUFACTURER),
3,
STR_MANUFACTURER
};
const static struct usb_string_descriptor_struct string2 = {
sizeof(STR_PRODUCT),
3,
STR_PRODUCT
};
const static struct usb_string_descriptor_struct string3 = {
sizeof(STR_SERIAL),
3,
STR_SERIAL
};
// This table defines which descriptor data is sent for each specific
// request from the host (in wValue and wIndex).
const static struct descriptor_list_struct {
uint16_t wValue;
uint16_t wIndex;
const uint8_t *addr;
uint8_t length;
} descriptor_list[] = {
{0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
{0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
// {0x2200, 0x0000, mouse_hid_desc, sizeof(mouse_hid_desc)},
// {0x2200, 0x0001, keyboard_hid_desc, sizeof(keyboard_hid_desc)},
{0x0300, 0x0000, (const uint8_t *)&string0, 4},
{0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
{0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
{0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)}
};
#define DESCRIPTOR_LIST_ENTRIES ((sizeof(descriptor_list))/(sizeof(struct descriptor_list_struct)) )
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment