minor update to video capture example

This commit is contained in:
hathach
2024-01-25 17:25:48 +07:00
parent de1266699d
commit 0b8b8af83d
5 changed files with 92 additions and 154 deletions

View File

@@ -131,8 +131,7 @@ static struct {
static uint8_t frame_buffer[FRAME_WIDTH * FRAME_HEIGHT * 16 / 8]; static uint8_t frame_buffer[FRAME_WIDTH * FRAME_HEIGHT * 16 / 8];
static void fill_color_bar(uint8_t* buffer, unsigned start_position) { static void fill_color_bar(uint8_t* buffer, unsigned start_position) {
/* EBU color bars /* EBU color bars: https://stackoverflow.com/questions/6939422 */
* See also https://stackoverflow.com/questions/6939422 */
static uint8_t const bar_color[8][4] = { static uint8_t const bar_color[8][4] = {
/* Y, U, Y, V */ /* Y, U, Y, V */
{ 235, 128, 235, 128}, /* 100% White */ { 235, 128, 235, 128}, /* 100% White */

View File

@@ -97,7 +97,7 @@
// The number of video streaming interfaces // The number of video streaming interfaces
#define CFG_TUD_VIDEO_STREAMING 1 #define CFG_TUD_VIDEO_STREAMING 1
// video streaming endpoint size // video streaming endpoint buffer size
#define CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE 256 #define CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE 256
// use bulk endpoint for streaming interface // use bulk endpoint for streaming interface

View File

@@ -127,22 +127,16 @@ enum {
TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \ TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \
/* Video control 0 */ \ /* Video control 0 */ \
TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \ TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \
TUD_VIDEO_DESC_CS_VC( /* UVC 1.5*/ 0x0150, \ /* Header: UVC 1.5, length of followed descs, clock (deprecated), streaming interfaces */ \
/* wTotalLength - bLength */ \ TUD_VIDEO_DESC_CS_VC(0x0150, TUD_VIDEO_DESC_CAMERA_TERM_LEN + TUD_VIDEO_DESC_OUTPUT_TERM_LEN, UVC_CLOCK_FREQUENCY, ITF_NUM_VIDEO_STREAMING), \
TUD_VIDEO_DESC_CAMERA_TERM_LEN + TUD_VIDEO_DESC_OUTPUT_TERM_LEN, \ /* Camera Terminal: ID, bAssocTerminal, iTerminal, focal min, max, length, bmControl */ \
UVC_CLOCK_FREQUENCY, ITF_NUM_VIDEO_STREAMING), \ TUD_VIDEO_DESC_CAMERA_TERM(UVC_ENTITY_CAP_INPUT_TERMINAL, 0, 0, 0, 0, 0, 0), \
TUD_VIDEO_DESC_CAMERA_TERM(UVC_ENTITY_CAP_INPUT_TERMINAL, 0, 0,\
/*wObjectiveFocalLengthMin*/0, /*wObjectiveFocalLengthMax*/0,\
/*wObjectiveFocalLength*/0, /*bmControls*/0), \
TUD_VIDEO_DESC_OUTPUT_TERM(UVC_ENTITY_CAP_OUTPUT_TERMINAL, VIDEO_TT_STREAMING, 0, 1, 0), \ TUD_VIDEO_DESC_OUTPUT_TERM(UVC_ENTITY_CAP_OUTPUT_TERMINAL, VIDEO_TT_STREAMING, 0, 1, 0), \
/* Video stream alt. 0 */ \ /* Video stream alt. 0 */ \
TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 0, 0, _stridx), \ TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 0, 0, _stridx), \
/* Video stream header for without still image capture */ \ /* Video stream header for without still image capture */ \
TUD_VIDEO_DESC_CS_VS_INPUT( /*bNumFormats*/1, \ TUD_VIDEO_DESC_CS_VS_INPUT( /*bNumFormats*/1, \
/*wTotalLength - bLength */\ /*wTotalLength - bLength */ TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR_LEN + TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT_LEN + TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN,\
TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR_LEN\
+ TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT_LEN\
+ TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN,\
_epin, /*bmInfo*/0, /*bTerminalLink*/UVC_ENTITY_CAP_OUTPUT_TERMINAL, \ _epin, /*bmInfo*/0, /*bTerminalLink*/UVC_ENTITY_CAP_OUTPUT_TERMINAL, \
/*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \ /*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \
/*bmaControls(1)*/0), \ /*bmaControls(1)*/0), \
@@ -164,22 +158,16 @@ enum {
TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \ TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \
/* Video control 0 */ \ /* Video control 0 */ \
TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \ TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \
TUD_VIDEO_DESC_CS_VC( /* UVC 1.5*/ 0x0150, \ /* Header: UVC 1.5, length of followed descs, clock (deprecated), streaming interfaces */ \
/* wTotalLength - bLength */ \ TUD_VIDEO_DESC_CS_VC(0x0150, TUD_VIDEO_DESC_CAMERA_TERM_LEN + TUD_VIDEO_DESC_OUTPUT_TERM_LEN, UVC_CLOCK_FREQUENCY, ITF_NUM_VIDEO_STREAMING), \
TUD_VIDEO_DESC_CAMERA_TERM_LEN + TUD_VIDEO_DESC_OUTPUT_TERM_LEN, \ /* Camera Terminal: ID, bAssocTerminal, iTerminal, focal min, max, length, bmControl */ \
UVC_CLOCK_FREQUENCY, ITF_NUM_VIDEO_STREAMING), \ TUD_VIDEO_DESC_CAMERA_TERM(UVC_ENTITY_CAP_INPUT_TERMINAL, 0, 0, 0, 0, 0, 0), \
TUD_VIDEO_DESC_CAMERA_TERM(UVC_ENTITY_CAP_INPUT_TERMINAL, 0, 0,\
/*wObjectiveFocalLengthMin*/0, /*wObjectiveFocalLengthMax*/0,\
/*wObjectiveFocalLength*/0, /*bmControls*/0), \
TUD_VIDEO_DESC_OUTPUT_TERM(UVC_ENTITY_CAP_OUTPUT_TERMINAL, VIDEO_TT_STREAMING, 0, 1, 0), \ TUD_VIDEO_DESC_OUTPUT_TERM(UVC_ENTITY_CAP_OUTPUT_TERMINAL, VIDEO_TT_STREAMING, 0, 1, 0), \
/* Video stream alt. 0 */ \ /* Video stream alt. 0 */ \
TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 0, 0, _stridx), \ TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 0, 0, _stridx), \
/* Video stream header for without still image capture */ \ /* Video stream header for without still image capture */ \
TUD_VIDEO_DESC_CS_VS_INPUT( /*bNumFormats*/1, \ TUD_VIDEO_DESC_CS_VS_INPUT( /*bNumFormats*/1, \
/*wTotalLength - bLength */\ /*wTotalLength - bLength */ TUD_VIDEO_DESC_CS_VS_FMT_MJPEG_LEN + TUD_VIDEO_DESC_CS_VS_FRM_MJPEG_CONT_LEN + TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN,\
TUD_VIDEO_DESC_CS_VS_FMT_MJPEG_LEN\
+ TUD_VIDEO_DESC_CS_VS_FRM_MJPEG_CONT_LEN\
+ TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN,\
_epin, /*bmInfo*/0, /*bTerminalLink*/UVC_ENTITY_CAP_OUTPUT_TERMINAL, \ _epin, /*bmInfo*/0, /*bTerminalLink*/UVC_ENTITY_CAP_OUTPUT_TERMINAL, \
/*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \ /*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \
/*bmaControls(1)*/0), \ /*bmaControls(1)*/0), \
@@ -202,22 +190,17 @@ enum {
TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \ TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \
/* Video control 0 */ \ /* Video control 0 */ \
TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \ TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \
TUD_VIDEO_DESC_CS_VC( /* UVC 1.5*/ 0x0150, \ /* Header: UVC 1.5, length of followed descs, clock (deprecated), streaming interfaces */ \
/* wTotalLength - bLength */ \ TUD_VIDEO_DESC_CS_VC(0x0150, TUD_VIDEO_DESC_CAMERA_TERM_LEN + TUD_VIDEO_DESC_OUTPUT_TERM_LEN, UVC_CLOCK_FREQUENCY, ITF_NUM_VIDEO_STREAMING), \
TUD_VIDEO_DESC_CAMERA_TERM_LEN + TUD_VIDEO_DESC_OUTPUT_TERM_LEN, \ /* Camera Terminal: ID, bAssocTerminal, iTerminal, focal min, max, length, bmControl */ \
UVC_CLOCK_FREQUENCY, ITF_NUM_VIDEO_STREAMING), \ TUD_VIDEO_DESC_CAMERA_TERM(UVC_ENTITY_CAP_INPUT_TERMINAL, 0, 0, 0, 0, 0, 0), \
TUD_VIDEO_DESC_CAMERA_TERM(UVC_ENTITY_CAP_INPUT_TERMINAL, 0, 0,\
/*wObjectiveFocalLengthMin*/0, /*wObjectiveFocalLengthMax*/0,\
/*wObjectiveFocalLength*/0, /*bmControls*/0), \
TUD_VIDEO_DESC_OUTPUT_TERM(UVC_ENTITY_CAP_OUTPUT_TERMINAL, VIDEO_TT_STREAMING, 0, 1, 0), \ TUD_VIDEO_DESC_OUTPUT_TERM(UVC_ENTITY_CAP_OUTPUT_TERMINAL, VIDEO_TT_STREAMING, 0, 1, 0), \
/* Video stream alt. 0 */ \ /* Video stream alt. 0 */ \
TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 0, 1, _stridx), \ TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 0, 1, _stridx), \
/* Video stream header for without still image capture */ \ /* Video stream header for without still image capture */ \
TUD_VIDEO_DESC_CS_VS_INPUT( /*bNumFormats*/1, \ TUD_VIDEO_DESC_CS_VS_INPUT( /*bNumFormats*/1, \
/*wTotalLength - bLength */\ /*wTotalLength - bLength */\
TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR_LEN\ TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR_LEN + TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT_LEN + TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN,\
+ TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT_LEN\
+ TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN,\
_epin, /*bmInfo*/0, /*bTerminalLink*/UVC_ENTITY_CAP_OUTPUT_TERMINAL, \ _epin, /*bmInfo*/0, /*bTerminalLink*/UVC_ENTITY_CAP_OUTPUT_TERMINAL, \
/*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \ /*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \
/*bmaControls(1)*/0), \ /*bmaControls(1)*/0), \
@@ -236,22 +219,16 @@ enum {
TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \ TUD_VIDEO_DESC_IAD(ITF_NUM_VIDEO_CONTROL, /* 2 Interfaces */ 0x02, _stridx), \
/* Video control 0 */ \ /* Video control 0 */ \
TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \ TUD_VIDEO_DESC_STD_VC(ITF_NUM_VIDEO_CONTROL, 0, _stridx), \
TUD_VIDEO_DESC_CS_VC( /* UVC 1.5*/ 0x0150, \ /* Header: UVC 1.5, length of followed descs, clock (deprecated), streaming interfaces */ \
/* wTotalLength - bLength */ \ TUD_VIDEO_DESC_CS_VC(0x0150, TUD_VIDEO_DESC_CAMERA_TERM_LEN + TUD_VIDEO_DESC_OUTPUT_TERM_LEN, UVC_CLOCK_FREQUENCY, ITF_NUM_VIDEO_STREAMING), \
TUD_VIDEO_DESC_CAMERA_TERM_LEN + TUD_VIDEO_DESC_OUTPUT_TERM_LEN, \ /* Camera Terminal: ID, bAssocTerminal, iTerminal, focal min, max, length, bmControl */ \
UVC_CLOCK_FREQUENCY, ITF_NUM_VIDEO_STREAMING), \ TUD_VIDEO_DESC_CAMERA_TERM(UVC_ENTITY_CAP_INPUT_TERMINAL, 0, 0, 0, 0, 0, 0), \
TUD_VIDEO_DESC_CAMERA_TERM(UVC_ENTITY_CAP_INPUT_TERMINAL, 0, 0,\
/*wObjectiveFocalLengthMin*/0, /*wObjectiveFocalLengthMax*/0,\
/*wObjectiveFocalLength*/0, /*bmControls*/0), \
TUD_VIDEO_DESC_OUTPUT_TERM(UVC_ENTITY_CAP_OUTPUT_TERMINAL, VIDEO_TT_STREAMING, 0, 1, 0), \ TUD_VIDEO_DESC_OUTPUT_TERM(UVC_ENTITY_CAP_OUTPUT_TERMINAL, VIDEO_TT_STREAMING, 0, 1, 0), \
/* Video stream alt. 0 */ \ /* Video stream alt. 0 */ \
TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 0, 1, _stridx), \ TUD_VIDEO_DESC_STD_VS(ITF_NUM_VIDEO_STREAMING, 0, 1, _stridx), \
/* Video stream header for without still image capture */ \ /* Video stream header for without still image capture */ \
TUD_VIDEO_DESC_CS_VS_INPUT( /*bNumFormats*/1, \ TUD_VIDEO_DESC_CS_VS_INPUT( /*bNumFormats*/1, \
/*wTotalLength - bLength */\ /*wTotalLength - bLength */ TUD_VIDEO_DESC_CS_VS_FMT_MJPEG_LEN + TUD_VIDEO_DESC_CS_VS_FRM_MJPEG_CONT_LEN + TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN,\
TUD_VIDEO_DESC_CS_VS_FMT_MJPEG_LEN\
+ TUD_VIDEO_DESC_CS_VS_FRM_MJPEG_CONT_LEN\
+ TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN,\
_epin, /*bmInfo*/0, /*bTerminalLink*/UVC_ENTITY_CAP_OUTPUT_TERMINAL, \ _epin, /*bmInfo*/0, /*bTerminalLink*/UVC_ENTITY_CAP_OUTPUT_TERMINAL, \
/*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \ /*bStillCaptureMethod*/0, /*bTriggerSupport*/0, /*bTriggerUsage*/0, \
/*bmaControls(1)*/0), \ /*bmaControls(1)*/0), \

View File

@@ -434,8 +434,9 @@ static bool _update_streaming_parameters(videod_streaming_interface_t const *stm
uint_fast32_t interval_ms = interval / 10000; uint_fast32_t interval_ms = interval / 10000;
TU_ASSERT(interval_ms); TU_ASSERT(interval_ms);
uint_fast32_t payload_size = (frame_size + interval_ms - 1) / interval_ms + 2; uint_fast32_t payload_size = (frame_size + interval_ms - 1) / interval_ms + 2;
if (CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE < payload_size) if (CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE < payload_size) {
payload_size = CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE; payload_size = CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE;
}
param->dwMaxPayloadTransferSize = payload_size; param->dwMaxPayloadTransferSize = payload_size;
return true; return true;
} }
@@ -577,8 +578,9 @@ static bool _negotiate_streaming_parameters(videod_streaming_interface_t const *
} else { } else {
payload_size = (frame_size + interval_ms - 1) / interval_ms + 2; payload_size = (frame_size + interval_ms - 1) / interval_ms + 2;
} }
if (CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE < payload_size) if (CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE < payload_size) {
payload_size = CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE; payload_size = CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE;
}
param->dwMaxPayloadTransferSize = payload_size; param->dwMaxPayloadTransferSize = payload_size;
} }
return true; return true;

View File

@@ -44,8 +44,7 @@
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
/// defined base on EHCI specs value for Endpoint Speed /// defined base on EHCI specs value for Endpoint Speed
typedef enum typedef enum {
{
TUSB_SPEED_FULL = 0, TUSB_SPEED_FULL = 0,
TUSB_SPEED_LOW = 1, TUSB_SPEED_LOW = 1,
TUSB_SPEED_HIGH = 2, TUSB_SPEED_HIGH = 2,
@@ -53,24 +52,21 @@ typedef enum
} tusb_speed_t; } tusb_speed_t;
/// defined base on USB Specs Endpoint's bmAttributes /// defined base on USB Specs Endpoint's bmAttributes
typedef enum typedef enum {
{
TUSB_XFER_CONTROL = 0 , TUSB_XFER_CONTROL = 0 ,
TUSB_XFER_ISOCHRONOUS , TUSB_XFER_ISOCHRONOUS ,
TUSB_XFER_BULK , TUSB_XFER_BULK ,
TUSB_XFER_INTERRUPT TUSB_XFER_INTERRUPT
} tusb_xfer_type_t; } tusb_xfer_type_t;
typedef enum typedef enum {
{
TUSB_DIR_OUT = 0, TUSB_DIR_OUT = 0,
TUSB_DIR_IN = 1, TUSB_DIR_IN = 1,
TUSB_DIR_IN_MASK = 0x80 TUSB_DIR_IN_MASK = 0x80
} tusb_dir_t; } tusb_dir_t;
enum enum {
{
TUSB_EPSIZE_BULK_FS = 64, TUSB_EPSIZE_BULK_FS = 64,
TUSB_EPSIZE_BULK_HS = 512, TUSB_EPSIZE_BULK_HS = 512,
@@ -78,9 +74,8 @@ enum
TUSB_EPSIZE_ISO_HS_MAX = 1024, TUSB_EPSIZE_ISO_HS_MAX = 1024,
}; };
/// Isochronous End Point Attributes /// Isochronous Endpoint Attributes
typedef enum typedef enum {
{
TUSB_ISO_EP_ATT_NO_SYNC = 0x00, TUSB_ISO_EP_ATT_NO_SYNC = 0x00,
TUSB_ISO_EP_ATT_ASYNCHRONOUS = 0x04, TUSB_ISO_EP_ATT_ASYNCHRONOUS = 0x04,
TUSB_ISO_EP_ATT_ADAPTIVE = 0x08, TUSB_ISO_EP_ATT_ADAPTIVE = 0x08,
@@ -91,8 +86,7 @@ typedef enum
} tusb_iso_ep_attribute_t; } tusb_iso_ep_attribute_t;
/// USB Descriptor Types /// USB Descriptor Types
typedef enum typedef enum {
{
TUSB_DESC_DEVICE = 0x01, TUSB_DESC_DEVICE = 0x01,
TUSB_DESC_CONFIGURATION = 0x02, TUSB_DESC_CONFIGURATION = 0x02,
TUSB_DESC_STRING = 0x03, TUSB_DESC_STRING = 0x03,
@@ -121,8 +115,7 @@ typedef enum
TUSB_DESC_SUPERSPEED_ISO_ENDPOINT_COMPANION = 0x31 TUSB_DESC_SUPERSPEED_ISO_ENDPOINT_COMPANION = 0x31
} tusb_desc_type_t; } tusb_desc_type_t;
typedef enum typedef enum {
{
TUSB_REQ_GET_STATUS = 0 , TUSB_REQ_GET_STATUS = 0 ,
TUSB_REQ_CLEAR_FEATURE = 1 , TUSB_REQ_CLEAR_FEATURE = 1 ,
TUSB_REQ_RESERVED = 2 , TUSB_REQ_RESERVED = 2 ,
@@ -138,23 +131,20 @@ typedef enum
TUSB_REQ_SYNCH_FRAME = 12 TUSB_REQ_SYNCH_FRAME = 12
} tusb_request_code_t; } tusb_request_code_t;
typedef enum typedef enum {
{
TUSB_REQ_FEATURE_EDPT_HALT = 0, TUSB_REQ_FEATURE_EDPT_HALT = 0,
TUSB_REQ_FEATURE_REMOTE_WAKEUP = 1, TUSB_REQ_FEATURE_REMOTE_WAKEUP = 1,
TUSB_REQ_FEATURE_TEST_MODE = 2 TUSB_REQ_FEATURE_TEST_MODE = 2
} tusb_request_feature_selector_t; } tusb_request_feature_selector_t;
typedef enum typedef enum {
{
TUSB_REQ_TYPE_STANDARD = 0, TUSB_REQ_TYPE_STANDARD = 0,
TUSB_REQ_TYPE_CLASS, TUSB_REQ_TYPE_CLASS,
TUSB_REQ_TYPE_VENDOR, TUSB_REQ_TYPE_VENDOR,
TUSB_REQ_TYPE_INVALID TUSB_REQ_TYPE_INVALID
} tusb_request_type_t; } tusb_request_type_t;
typedef enum typedef enum {
{
TUSB_REQ_RCPT_DEVICE =0, TUSB_REQ_RCPT_DEVICE =0,
TUSB_REQ_RCPT_INTERFACE, TUSB_REQ_RCPT_INTERFACE,
TUSB_REQ_RCPT_ENDPOINT, TUSB_REQ_RCPT_ENDPOINT,
@@ -162,8 +152,7 @@ typedef enum
} tusb_request_recipient_t; } tusb_request_recipient_t;
// https://www.usb.org/defined-class-codes // https://www.usb.org/defined-class-codes
typedef enum typedef enum {
{
TUSB_CLASS_UNSPECIFIED = 0 , TUSB_CLASS_UNSPECIFIED = 0 ,
TUSB_CLASS_AUDIO = 1 , TUSB_CLASS_AUDIO = 1 ,
TUSB_CLASS_CDC = 2 , TUSB_CLASS_CDC = 2 ,
@@ -194,19 +183,16 @@ typedef enum
MISC_SUBCLASS_COMMON = 2 MISC_SUBCLASS_COMMON = 2
}misc_subclass_type_t; }misc_subclass_type_t;
typedef enum typedef enum {
{
MISC_PROTOCOL_IAD = 1 MISC_PROTOCOL_IAD = 1
} misc_protocol_type_t; } misc_protocol_type_t;
typedef enum typedef enum {
{
APP_SUBCLASS_USBTMC = 0x03, APP_SUBCLASS_USBTMC = 0x03,
APP_SUBCLASS_DFU_RUNTIME = 0x01 APP_SUBCLASS_DFU_RUNTIME = 0x01
} app_subclass_type_t; } app_subclass_type_t;
typedef enum typedef enum {
{
DEVICE_CAPABILITY_WIRELESS_USB = 0x01, DEVICE_CAPABILITY_WIRELESS_USB = 0x01,
DEVICE_CAPABILITY_USB20_EXTENSION = 0x02, DEVICE_CAPABILITY_USB20_EXTENSION = 0x02,
DEVICE_CAPABILITY_SUPERSPEED_USB = 0x03, DEVICE_CAPABILITY_SUPERSPEED_USB = 0x03,
@@ -235,8 +221,7 @@ enum {
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// //
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
typedef enum typedef enum {
{
XFER_RESULT_SUCCESS = 0, XFER_RESULT_SUCCESS = 0,
XFER_RESULT_FAILED, XFER_RESULT_FAILED,
XFER_RESULT_STALLED, XFER_RESULT_STALLED,
@@ -244,19 +229,17 @@ typedef enum
XFER_RESULT_INVALID XFER_RESULT_INVALID
} xfer_result_t; } xfer_result_t;
enum // TODO remove // TODO remove
{ enum {
DESC_OFFSET_LEN = 0, DESC_OFFSET_LEN = 0,
DESC_OFFSET_TYPE = 1 DESC_OFFSET_TYPE = 1
}; };
enum enum {
{
INTERFACE_INVALID_NUMBER = 0xff INTERFACE_INVALID_NUMBER = 0xff
}; };
typedef enum typedef enum {
{
MS_OS_20_SET_HEADER_DESCRIPTOR = 0x00, MS_OS_20_SET_HEADER_DESCRIPTOR = 0x00,
MS_OS_20_SUBSET_HEADER_CONFIGURATION = 0x01, MS_OS_20_SUBSET_HEADER_CONFIGURATION = 0x01,
MS_OS_20_SUBSET_HEADER_FUNCTION = 0x02, MS_OS_20_SUBSET_HEADER_FUNCTION = 0x02,
@@ -268,16 +251,14 @@ typedef enum
MS_OS_20_FEATURE_VENDOR_REVISION = 0x08 MS_OS_20_FEATURE_VENDOR_REVISION = 0x08
} microsoft_os_20_type_t; } microsoft_os_20_type_t;
enum enum {
{
CONTROL_STAGE_IDLE, CONTROL_STAGE_IDLE,
CONTROL_STAGE_SETUP, CONTROL_STAGE_SETUP,
CONTROL_STAGE_DATA, CONTROL_STAGE_DATA,
CONTROL_STAGE_ACK CONTROL_STAGE_ACK
}; };
enum enum {
{
TUSB_INDEX_INVALID_8 = 0xFFu TUSB_INDEX_INVALID_8 = 0xFFu
}; };
@@ -290,8 +271,7 @@ TU_ATTR_PACKED_BEGIN
TU_ATTR_BIT_FIELD_ORDER_BEGIN TU_ATTR_BIT_FIELD_ORDER_BEGIN
/// USB Device Descriptor /// USB Device Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength ; ///< Size of this descriptor in bytes. uint8_t bLength ; ///< Size of this descriptor in bytes.
uint8_t bDescriptorType ; ///< DEVICE Descriptor Type. uint8_t bDescriptorType ; ///< DEVICE Descriptor Type.
uint16_t bcdUSB ; ///< BUSB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). This field identifies the release of the USB Specification with which the device and its descriptors are compliant. uint16_t bcdUSB ; ///< BUSB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). This field identifies the release of the USB Specification with which the device and its descriptors are compliant.
@@ -314,8 +294,7 @@ typedef struct TU_ATTR_PACKED
TU_VERIFY_STATIC( sizeof(tusb_desc_device_t) == 18, "size is not correct"); TU_VERIFY_STATIC( sizeof(tusb_desc_device_t) == 18, "size is not correct");
// USB Binary Device Object Store (BOS) Descriptor // USB Binary Device Object Store (BOS) Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength ; ///< Size of this descriptor in bytes uint8_t bLength ; ///< Size of this descriptor in bytes
uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type
uint16_t wTotalLength ; ///< Total length of data returned for this descriptor uint16_t wTotalLength ; ///< Total length of data returned for this descriptor
@@ -325,8 +304,7 @@ typedef struct TU_ATTR_PACKED
TU_VERIFY_STATIC( sizeof(tusb_desc_bos_t) == 5, "size is not correct"); TU_VERIFY_STATIC( sizeof(tusb_desc_bos_t) == 5, "size is not correct");
/// USB Configuration Descriptor /// USB Configuration Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength ; ///< Size of this descriptor in bytes uint8_t bLength ; ///< Size of this descriptor in bytes
uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type
uint16_t wTotalLength ; ///< Total length of data returned for this configuration. Includes the combined length of all descriptors (configuration, interface, endpoint, and class- or vendor-specific) returned for this configuration. uint16_t wTotalLength ; ///< Total length of data returned for this configuration. Includes the combined length of all descriptors (configuration, interface, endpoint, and class- or vendor-specific) returned for this configuration.
@@ -341,8 +319,7 @@ typedef struct TU_ATTR_PACKED
TU_VERIFY_STATIC( sizeof(tusb_desc_configuration_t) == 9, "size is not correct"); TU_VERIFY_STATIC( sizeof(tusb_desc_configuration_t) == 9, "size is not correct");
/// USB Interface Descriptor /// USB Interface Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength ; ///< Size of this descriptor in bytes uint8_t bLength ; ///< Size of this descriptor in bytes
uint8_t bDescriptorType ; ///< INTERFACE Descriptor Type uint8_t bDescriptorType ; ///< INTERFACE Descriptor Type
@@ -358,8 +335,7 @@ typedef struct TU_ATTR_PACKED
TU_VERIFY_STATIC( sizeof(tusb_desc_interface_t) == 9, "size is not correct"); TU_VERIFY_STATIC( sizeof(tusb_desc_interface_t) == 9, "size is not correct");
/// USB Endpoint Descriptor /// USB Endpoint Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength ; // Size of this descriptor in bytes uint8_t bLength ; // Size of this descriptor in bytes
uint8_t bDescriptorType ; // ENDPOINT Descriptor Type uint8_t bDescriptorType ; // ENDPOINT Descriptor Type
@@ -379,8 +355,7 @@ typedef struct TU_ATTR_PACKED
TU_VERIFY_STATIC( sizeof(tusb_desc_endpoint_t) == 7, "size is not correct"); TU_VERIFY_STATIC( sizeof(tusb_desc_endpoint_t) == 7, "size is not correct");
/// USB Other Speed Configuration Descriptor /// USB Other Speed Configuration Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength ; ///< Size of descriptor uint8_t bLength ; ///< Size of descriptor
uint8_t bDescriptorType ; ///< Other_speed_Configuration Type uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
uint16_t wTotalLength ; ///< Total length of data returned uint16_t wTotalLength ; ///< Total length of data returned
@@ -393,8 +368,7 @@ typedef struct TU_ATTR_PACKED
} tusb_desc_other_speed_t; } tusb_desc_other_speed_t;
/// USB Device Qualifier Descriptor /// USB Device Qualifier Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength ; ///< Size of descriptor uint8_t bLength ; ///< Size of descriptor
uint8_t bDescriptorType ; ///< Device Qualifier Type uint8_t bDescriptorType ; ///< Device Qualifier Type
uint16_t bcdUSB ; ///< USB specification version number (e.g., 0200H for V2.00) uint16_t bcdUSB ; ///< USB specification version number (e.g., 0200H for V2.00)
@@ -411,8 +385,7 @@ typedef struct TU_ATTR_PACKED
TU_VERIFY_STATIC( sizeof(tusb_desc_device_qualifier_t) == 10, "size is not correct"); TU_VERIFY_STATIC( sizeof(tusb_desc_device_qualifier_t) == 10, "size is not correct");
/// USB Interface Association Descriptor (IAD ECN) /// USB Interface Association Descriptor (IAD ECN)
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength ; ///< Size of descriptor uint8_t bLength ; ///< Size of descriptor
uint8_t bDescriptorType ; ///< Other_speed_Configuration Type uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
@@ -427,16 +400,14 @@ typedef struct TU_ATTR_PACKED
} tusb_desc_interface_assoc_t; } tusb_desc_interface_assoc_t;
// USB String Descriptor // USB String Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength ; ///< Size of this descriptor in bytes uint8_t bLength ; ///< Size of this descriptor in bytes
uint8_t bDescriptorType ; ///< Descriptor Type uint8_t bDescriptorType ; ///< Descriptor Type
uint16_t unicode_string[]; uint16_t unicode_string[];
} tusb_desc_string_t; } tusb_desc_string_t;
// USB Binary Device Object Store (BOS) // USB Binary Device Object Store (BOS)
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength; uint8_t bLength;
uint8_t bDescriptorType ; uint8_t bDescriptorType ;
uint8_t bDevCapabilityType; uint8_t bDevCapabilityType;
@@ -445,9 +416,8 @@ typedef struct TU_ATTR_PACKED
uint8_t CapabilityData[]; uint8_t CapabilityData[];
} tusb_desc_bos_platform_t; } tusb_desc_bos_platform_t;
// USB WebuSB URL Descriptor // USB WebUSB URL Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength; uint8_t bLength;
uint8_t bDescriptorType; uint8_t bDescriptorType;
uint8_t bScheme; uint8_t bScheme;
@@ -455,8 +425,7 @@ typedef struct TU_ATTR_PACKED
} tusb_desc_webusb_url_t; } tusb_desc_webusb_url_t;
// DFU Functional Descriptor // DFU Functional Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED {
{
uint8_t bLength; uint8_t bLength;
uint8_t bDescriptorType; uint8_t bDescriptorType;
@@ -509,36 +478,30 @@ TU_ATTR_BIT_FIELD_ORDER_END
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Get direction from Endpoint address // Get direction from Endpoint address
TU_ATTR_ALWAYS_INLINE static inline tusb_dir_t tu_edpt_dir(uint8_t addr) TU_ATTR_ALWAYS_INLINE static inline tusb_dir_t tu_edpt_dir(uint8_t addr) {
{
return (addr & TUSB_DIR_IN_MASK) ? TUSB_DIR_IN : TUSB_DIR_OUT; return (addr & TUSB_DIR_IN_MASK) ? TUSB_DIR_IN : TUSB_DIR_OUT;
} }
// Get Endpoint number from address // Get Endpoint number from address
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_number(uint8_t addr) TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_number(uint8_t addr) {
{
return (uint8_t)(addr & (~TUSB_DIR_IN_MASK)); return (uint8_t)(addr & (~TUSB_DIR_IN_MASK));
} }
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_addr(uint8_t num, uint8_t dir) TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_addr(uint8_t num, uint8_t dir) {
{
return (uint8_t)(num | (dir ? TUSB_DIR_IN_MASK : 0)); return (uint8_t)(num | (dir ? TUSB_DIR_IN_MASK : 0));
} }
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_edpt_packet_size(tusb_desc_endpoint_t const* desc_ep) TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_edpt_packet_size(tusb_desc_endpoint_t const* desc_ep) {
{
return tu_le16toh(desc_ep->wMaxPacketSize) & TU_GENMASK(10, 0); return tu_le16toh(desc_ep->wMaxPacketSize) & TU_GENMASK(10, 0);
} }
#if CFG_TUSB_DEBUG #if CFG_TUSB_DEBUG
TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_dir_str(tusb_dir_t dir) TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_dir_str(tusb_dir_t dir) {
{
tu_static const char *str[] = {"out", "in"}; tu_static const char *str[] = {"out", "in"};
return str[dir]; return str[dir];
} }
TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_type_str(tusb_xfer_type_t t) TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_type_str(tusb_xfer_type_t t) {
{
tu_static const char *str[] = {"control", "isochronous", "bulk", "interrupt"}; tu_static const char *str[] = {"control", "isochronous", "bulk", "interrupt"};
return str[t]; return str[t];
} }
@@ -549,21 +512,18 @@ TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_type_str(tusb_xfer_type_
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// return next descriptor // return next descriptor
TU_ATTR_ALWAYS_INLINE static inline uint8_t const * tu_desc_next(void const* desc) TU_ATTR_ALWAYS_INLINE static inline uint8_t const * tu_desc_next(void const* desc) {
{
uint8_t const* desc8 = (uint8_t const*) desc; uint8_t const* desc8 = (uint8_t const*) desc;
return desc8 + desc8[DESC_OFFSET_LEN]; return desc8 + desc8[DESC_OFFSET_LEN];
} }
// get descriptor type // get descriptor type
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_type(void const* desc) TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_type(void const* desc) {
{
return ((uint8_t const*) desc)[DESC_OFFSET_TYPE]; return ((uint8_t const*) desc)[DESC_OFFSET_TYPE];
} }
// get descriptor length // get descriptor length
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_len(void const* desc) TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_len(void const* desc) {
{
return ((uint8_t const*) desc)[DESC_OFFSET_LEN]; return ((uint8_t const*) desc)[DESC_OFFSET_LEN];
} }