flatUSB/usb_device_types.h
2024-04-08 19:25:47 +02:00

211 lines
5.8 KiB
C

#ifndef CORE_USB_USB_DEVICE_TYPES
#define CORE_USB_USB_DEVICE_TYPES
#include "usb_common_types.h"
// /* USB endpoint direction */
// typedef enum { USB_IN = 0,
// USB_OUT = 1 } USB_Ep_Dir;
/* ------ USB DESCRIPTORS ------- */
// USB Descriptor type codes
typedef enum {
UD_Device = 1,
UD_Configuration = 2,
UD_String = 3,
UD_Interface = 4,
UD_Endpoint = 5,
UD_DeviceQualifier = 6,
UD_OtherSpeedConfiguration = 7
// NOT FULL!
} USB_DescType;
// USB descriptor header
#define USB_DESC_HEADER \
uint8_t bLength; /* length in bytes */ \
uint8_t bDescriptorType; /* descriptor type */
#define USB_DESC_HEADER_SIZE (2) // USB descriptor header size
// USB Descriptor header
typedef struct {
USB_DESC_HEADER
} USB_DescHdr;
// USB Device descriptor
typedef struct {
USB_DESC_HEADER
uint16_t bcdUSB; // USB specification release number (BCD)
uint8_t bDeviceClass; // Class code
uint8_t bDeviceSubclass; // Subclass code
uint8_t bDeviceProtocol; // Protocol code
uint8_t bMaxPacketSize0; // Maximum packet size for endpoint 0
uint16_t idVendor; // Vendor ID
uint16_t idProduct; // Product ID
uint16_t bcdDevice; // Device release number (BCD)
uint8_t iManufacturer; // Index of string descriptor for manufacturer
uint8_t iProduct; // Index of string descriptor for the product
uint8_t iSerialNumber; // Index of string descriptor for the serial number
uint8_t bNumConfiguration; // Number of possible configurations
} USB_DeviceDesc;
// USB Device Qualifier descriptor
typedef struct {
USB_DESC_HEADER
uint16_t bcdUSB; // USB specification release number (BCD)
uint8_t bDeviceClass; // Class code
uint8_t bDeviceSubclass; // Subclass code
uint8_t bDeviceProtocol; // Protocol code
uint8_t bMaxPacketSize0; // Maximum packet size for endpoint 0
uint8_t bNumConfiguration; // Number of possible configurations
} __attribute__((packed)) USB_DeviceQualifierDesc;
#define USB_CONFIG_ATTR_SELF_POWERED (1 << 6)
#define USB_CONFIG_ATTR_REMOTE_WKUP (1 << 5)
#define USB_CONFIG_ATTR_USB1_1_FLAG (1 << 7)
// USB Configuration descriptor
typedef struct {
USB_DESC_HEADER
uint16_t wTotalLength; // The number of bytes in the configuration descriptor and all of its subordinate descriptors
uint8_t bNumInterfaces; // Number of interfaces in the configuration
uint8_t bConfigrationValue; // Identifier for Set Configuration and Get Configuration Requests
uint8_t iConfiguration; // Index of string descriptor for the configuration
uint8_t bmAttributes; // Self/bus power and remote wakeup settings
uint8_t bMaxPower; // Bus power required in units of 2 mA
} __attribute__((packed)) USB_ConfigurationDesc;
// USB Interface descriptor
typedef struct {
USB_DESC_HEADER
uint8_t bInterfaceNumber; // Number identifying this interface
uint8_t bAlternateSetting; // A number that identifies a descriptor with alternate settings for this bInterfaceNumber
uint8_t bNumEndpoints; // Number of endpoints supported not counting endpoint zero
uint8_t bInterfaceClass; // Class code
uint8_t bInterfaceSubclass; // Subclass code
uint8_t bInterfaceProtocol; // Protocol code
uint8_t iInterface; // Index of string descriptor for the interface
} __attribute__((packed)) USB_InterfaceDesc;
// USB Transfer type
typedef enum {
UT_Control = 0b00,
UT_Isochronous = 0b01,
UT_Bulk = 0b10,
UT_Interrupt = 0b11
} USB_TransferType;
// USB Endpoint descriptor
typedef struct {
USB_DESC_HEADER
uint8_t bEndpointAddress; // Endpoint number and direction
uint8_t bmAttributes; // Transfer type and supplementary information
uint16_t wMaxPacketSize; // Maximum packet size supported
uint8_t bInterval; // Service interval or NAK rate
} __attribute__((packed)) USB_EndpointDesc;
/* struct {
uint8_t n : 7; // endpoint number (four bits, but this style no "spacer" needed)
uint8_t dir; // direction
} bEndpointAddress; // Endpoint number and direction*/
// USB String descriptor
typedef struct {
USB_DESC_HEADER
} USB_StringDesc;
// USB endpoint direction
typedef enum {
USB_OUT = 0,
USB_IN = 1
} USB_EndpointDir;
// USB PIDs
typedef enum {
PID_EXT = 0,
PID_OUT = 1,
PID_ACK = 2,
PID_DATA0 = 3,
PID_PING = 4,
PID_SOF = 5,
PID_NYET = 6,
PID_DATA2 = 7,
PID_SPLIT = 8,
PID_IN = 9,
PID_NAK = 10,
PID_DATA1 = 11,
PID_PRE_ERR = 12,
PID_SETUP = 13,
PID_STALL = 14,
PID_MDATA = 15,
} USB_PID;
// USB Request types
typedef enum {
UR_Standard = 0,
UR_VendorSpec = 1,
UR_ReqDefVendorSpec = 2
} USB_RequestType;
// USB Recipients
typedef enum {
UREC_Device = 0,
UREC_SpecificInterface = 1,
UREC_Endpoint = 2,
UREC_OtherElement = 3
} USB_Recipient;
// USB Request types
typedef enum {
UREQ_GetStatus = 0x00,
UREQ_ClearFeature = 0x01,
UREQ_SetFeature = 0x03,
UREQ_SetAddress = 0x05,
UREQ_GetDescriptor = 0x06,
UREQ_SetDescriptor = 0x07,
UREQ_GetConfiguration = 0x08,
UREQ_SetConfiguration = 0x09,
UREQ_GetInterface = 0x0A,
UREQ_SetInterface = 0x0B,
UREQ_SyncFrame = 0x0C,
UREQ_SetSEL = 0x30,
UREQ_SetIsochronousDelay = 0x31
} USB_RequestCode;
// setup request structure
typedef struct {
union {
uint8_t bmRequestType;
struct {
uint8_t recipient : 5;
uint8_t type : 2;
uint8_t dir : 1;
} fields;
} bmRequestType; // request type
uint8_t bRequest; // request
uint16_t wValue; // ...
uint16_t wIndex; // ...
uint16_t wLength; // number of bytes in the data stage
} USB_SetupRequest;
#endif /* CORE_USB_USB_DEVICE_TYPES */