wrap up hid device refactor

This commit is contained in:
hathach 2019-04-18 22:31:35 +07:00
parent a30461b078
commit 307ba23046
No known key found for this signature in database
GPG Key ID: 2FA891220FBFD581
11 changed files with 206 additions and 214 deletions

View File

@ -126,6 +126,14 @@ void tud_cdc_rx_cb(uint8_t itf)
// USB HID // USB HID
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#if CFG_TUD_HID #if CFG_TUD_HID
// Must match with ID declared by HID Report Descriptor, better to be in header file
enum
{
REPORT_ID_KEYBOARD = 1,
REPORT_ID_MOUSE
};
void usb_hid_task(void) void usb_hid_task(void)
{ {
// Poll every 10ms // Poll every 10ms
@ -137,6 +145,7 @@ void usb_hid_task(void)
uint32_t const btn = board_button_read(); uint32_t const btn = board_button_read();
// Remote wakeup
if ( tud_suspended() && btn ) if ( tud_suspended() && btn )
{ {
// Wake up host if we are in suspend mode // Wake up host if we are in suspend mode
@ -144,35 +153,40 @@ void usb_hid_task(void)
tud_remote_wakeup(); tud_remote_wakeup();
} }
#if 1 /*------------- Mouse -------------*/
if ( tud_hid_ready() )
{
if ( btn )
{
int8_t const delta = 5;
tud_hid_mouse_move(REPORT_ID_MOUSE, delta, delta); // right + down
// delay a bit before attempt to send keyboard report
board_delay(2);
}
}
/*------------- Keyboard -------------*/ /*------------- Keyboard -------------*/
if ( tud_hid_ready() ) if ( tud_hid_ready() )
{ {
// use to avoid send multiple consecutive zero report for keyboard
static bool has_key = false;
if ( btn ) if ( btn )
{ {
uint8_t keycode[6] = { 0 }; uint8_t keycode[6] = { 0 };
keycode[0] = HID_KEY_A; keycode[0] = HID_KEY_A;
tud_hid_keyboard_report(0, 0, keycode); tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
has_key = true;
}else }else
{ {
tud_hid_keyboard_key_release(0); // send empty key report if previously has key pressed
if (has_key) tud_hid_keyboard_key_release(REPORT_ID_KEYBOARD);
has_key = false;
} }
} }
#endif
#if 0
/*------------- Mouse -------------*/
if ( tud_hid_mouse_ready() )
{
enum { DELTA = 5 };
if ( btn & 0x01 ) tud_hid_mouse_move(-DELTA, 0); // left
if ( btn & 0x02 ) tud_hid_mouse_move( DELTA, 0); // right
if ( btn & 0x04 ) tud_hid_mouse_move( 0 , -DELTA); // up
if ( btn & 0x08 ) tud_hid_mouse_move( 0 , DELTA); // down
}
#endif
} }
uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)

View File

@ -76,7 +76,7 @@
* *
* Note: All CFG_TUD_DESC_* are relevant only if CFG_TUD_DESC_AUTO is enabled * Note: All CFG_TUD_DESC_* are relevant only if CFG_TUD_DESC_AUTO is enabled
*/ */
#define CFG_TUD_DESC_AUTO 1 #define CFG_TUD_DESC_AUTO 0
// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
// Therefore we need to force endpoint number to correct type on lpc17xx // Therefore we need to force endpoint number to correct type on lpc17xx
@ -91,19 +91,11 @@
//------------- CLASS -------------// //------------- CLASS -------------//
#define CFG_TUD_CDC 1 #define CFG_TUD_CDC 1
#define CFG_TUD_MSC 1 #define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_MIDI 0 #define CFG_TUD_MIDI 0
#define CFG_TUD_CUSTOM_CLASS 0 #define CFG_TUD_CUSTOM_CLASS 0
#define CFG_TUD_HID 1
#define CFG_TUD_HID_KEYBOARD 1
#define CFG_TUD_HID_MOUSE 0
/* Use Boot Protocol for Keyboard, Mouse. Enable this will create separated HID interface
* require more IN endpoints. If disabled, they they are all packed into a single
* multiple report interface called "Generic". */
#define CFG_TUD_HID_KEYBOARD_BOOT 1
#define CFG_TUD_HID_MOUSE_BOOT 0
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// CDC // CDC
//-------------------------------------------------------------------- //--------------------------------------------------------------------

View File

@ -26,19 +26,14 @@
#include "tusb.h" #include "tusb.h"
// If HID Generic interface is generated
#define AUTO_DESC_HID_GENERIC (CFG_TUD_HID && ((CFG_TUD_HID_KEYBOARD && !CFG_TUD_HID_KEYBOARD_BOOT) || \
(CFG_TUD_HID_MOUSE && !CFG_TUD_HID_MOUSE_BOOT)) )
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug. /* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC. * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
* *
* Auto ProductID layout's Bitmap: * Auto ProductID layout's Bitmap:
* [MSB] HID Generic | Boot Mouse | Boot Keyboard | MSC | CDC [LSB] * [MSB] HID | MSC | CDC [LSB]
*/ */
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | \ #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) )
_PID_MAP(HID_KEYBOARD, 2) | _PID_MAP(HID_MOUSE, 3) | (AUTO_DESC_HID_GENERIC << 4) )
//------------- Device Descriptors -------------// //------------- Device Descriptors -------------//
tusb_desc_device_t const desc_device = tusb_desc_device_t const desc_device =
@ -72,6 +67,59 @@ tusb_desc_device_t const desc_device =
.bNumConfigurations = 0x01 .bNumConfigurations = 0x01
}; };
//------------- HID Report Descriptor -------------//
enum
{
REPORT_ID_KEYBOARD = 1,
REPORT_ID_MOUSE
};
uint8_t const desc_hid_report[] =
{
HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD), ),
HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE), )
};
//------------- Configuration Descriptor -------------//
enum {
#if CFG_TUD_CDC
ITF_NUM_CDC = 0,
ITF_NUM_CDC_DATA,
#endif
#if CFG_TUD_MSC
ITF_NUM_MSC,
#endif
#if CFG_TUD_HID
ITF_NUM_HID,
#endif
ITF_NUM_TOTAL
};
enum {
CONFIG_DESC_LEN = sizeof(tusb_desc_configuration_t) + CFG_TUD_CDC*TUD_CDC_DESC_LEN + CFG_TUD_MSC*TUD_MSC_DESC_LEN + CFG_TUD_HID*TUD_HID_DESC_LEN
};
uint8_t const desc_configuration[] =
{
// Config: self-powered with remote wakeup support, max power up to 100 mA
TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
#if CFG_TUD_CDC
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x81, 8, 0x02, 0x82, 64),
#endif
#if CFG_TUD_MSC
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, 0x03, 0x83, 64), // highspeed 512
#endif
#if CFG_TUD_HID
TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_PROTOCOL_KEYBOARD, sizeof(desc_hid_report), 0x84, 16, 10)
#endif
};
//------------- String Descriptors -------------// //------------- String Descriptors -------------//
// array of pointer to string descriptors // array of pointer to string descriptors
uint16_t const * const string_desc_arr [] = uint16_t const * const string_desc_arr [] =
@ -88,26 +136,14 @@ uint16_t const * const string_desc_arr [] =
// 3: Serials, should use chip ID // 3: Serials, should use chip ID
TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'), TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
#if CFG_TUD_CDC
// 4: CDC Interface // 4: CDC Interface
TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'), TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'),
#endif
#if CFG_TUD_MSC
// 5: MSC Interface // 5: MSC Interface
TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'), TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'),
#endif
#if CFG_TUD_HID_KEYBOARD
// 6: Keyboard
TUD_DESC_STRCONV('t','u','s','b',' ','k','e','y','b','o','a','r','d'),
#endif
#if CFG_TUD_HID_MOUSE
// 7: Mouse
TUD_DESC_STRCONV('t','u','s','b',' ','m', 'o','u','s','e'),
#endif
// 6: HID
TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d')
}; };
// tud_desc_set is required by tinyusb stack // tud_desc_set is required by tinyusb stack
@ -115,10 +151,10 @@ uint16_t const * const string_desc_arr [] =
tud_desc_set_t tud_desc_set = tud_desc_set_t tud_desc_set =
{ {
.device = &desc_device, .device = &desc_device,
.config = NULL, .config = desc_configuration,
.string_arr = (uint8_t const **) string_desc_arr, .string_arr = (uint8_t const **) string_desc_arr,
.string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
.hid_report = NULL, .hid_report = desc_hid_report,
}; };

View File

@ -95,6 +95,12 @@ static inline void board_led_off(void)
board_led_write(false); board_led_write(false);
} }
static inline void board_delay(uint32_t ms)
{
uint32_t start_ms = board_millis();
while( board_millis() < start_ms + ms) {}
}
static inline int8_t board_uart_getchar(void) static inline int8_t board_uart_getchar(void)
{ {
uint8_t c; uint8_t c;

View File

@ -94,17 +94,6 @@ ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts); ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts);
ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding); ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding);
//--------------------------------------------------------------------+
// INTERNAL USBD-CLASS DRIVER API
//--------------------------------------------------------------------+
void cdcd_init (void);
bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length);
bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request);
bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request);
bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
void cdcd_reset (uint8_t rhport);
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Interface Descriptor Template // Interface Descriptor Template
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -116,31 +105,42 @@ void cdcd_reset (uint8_t rhport);
// interface number, string index, EP notification address and size, EP data address (out,in) and size. // interface number, string index, EP notification address and size, EP data address (out,in) and size.
#define TUD_CDC_DESCRIPTOR(_itfnum, _stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize) \ #define TUD_CDC_DESCRIPTOR(_itfnum, _stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize) \
/* Interface Associate */\ /* Interface Associate */\
0x08, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 0x02, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, 0x00,\ 8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, 0,\
/* CDC Control Interface */\ /* CDC Control Interface */\
0x09, TUSB_DESC_INTERFACE, _itfnum, 0x00, 0x01, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, _stridx,\ 9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, _stridx,\
/* CDC Header */\ /* CDC Header */\
0x05, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\ 5, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\
/* CDC Call */\ /* CDC Call */\
0x05, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_CALL_MANAGEMENT, 0x00, (_itfnum) + 1,\ 5, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_CALL_MANAGEMENT, 0, (_itfnum) + 1,\
/* CDC ACM: support line request */\ /* CDC ACM: support line request */\
0x04, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 0x02,\ 4, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 2,\
/* CDC Union */\ /* CDC Union */\
0x05, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_UNION, _itfnum, (_itfnum) + 1,\ 5, TUSB_DESC_CLASS_SPECIFIC, CDC_FUNC_DESC_UNION, _itfnum, (_itfnum) + 1,\
/* Endpoint Notification */\ /* Endpoint Notification */\
0x07, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 0x10,\ 7, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 16,\
/* CDC Data Interface */\ /* CDC Data Interface */\
0x09, TUSB_DESC_INTERFACE, (_itfnum)+1, 0x00, 0x02, TUSB_CLASS_CDC_DATA, 0x00, 0x00, 0x00,\ 9, TUSB_DESC_INTERFACE, (_itfnum)+1, 0, 2, TUSB_CLASS_CDC_DATA, 0, 0, 0,\
/* Endpoint Out */\ /* Endpoint Out */\
0x07, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00,\ 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
/* Endpoint In */\ /* Endpoint In */\
0x07, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
/** @} */
/** @} */
//--------------------------------------------------------------------+
// INTERNAL USBD-CLASS DRIVER API
//--------------------------------------------------------------------+
void cdcd_init (void);
bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length);
bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request);
bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request);
bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
void cdcd_reset (uint8_t rhport);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* _TUSB_CDC_DEVICE_H_ */ #endif /* _TUSB_CDC_DEVICE_H_ */
/** @} */
/** @} */

View File

@ -147,6 +147,7 @@ bool tud_hid_keyboard_key_press(uint8_t report_id, char ch)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan) bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan)
{ {
(void) pan;
hid_mouse_report_t report = hid_mouse_report_t report =
{ {
.buttons = buttons, .buttons = buttons,

View File

@ -42,55 +42,31 @@
#define CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP 0 #define CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP 0
#endif #endif
#if !CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT
#error CFG_TUD_HID_KEYBOARD must be enabled
#endif
#if !CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT
#error CFG_TUD_HID_MOUSE must be enabled
#endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Application API // Application API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
/** Check if the interface is ready to use // Check if the interface is ready to use
* \returns true if ready, otherwise interface may not be mounted or still busy transferring data
* \note Application must not perform any action if the interface is not ready
*/
bool tud_hid_ready(void); bool tud_hid_ready(void);
bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len);
// Check if current mode is Boot (true) or Report (false) // Check if current mode is Boot (true) or Report (false)
bool tud_hid_boot_mode(void); bool tud_hid_boot_mode(void);
// Send report to host
bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len);
/*------------- Callbacks (Weak is optional) -------------*/ /*------------- Callbacks (Weak is optional) -------------*/
/** Callback invoked when USB host request \ref HID_REQ_CONTROL_GET_REPORT. // Invoked when receiving GET_REPORT control request
* \param[in] report_type specify which report (INPUT, OUTPUT, FEATURE) that host requests // Application must fill buffer report's content and return its length.
* \param[out] buffer data that application need to update, value must be accessible by USB controller (see \ref CFG_TUSB_MEM_SECTION) // Return zero will cause the stack to STALL request
* \param[in] reqlen number of bytes that host requested
* \retval non-zero Actual number of bytes in the response's buffer.
* \retval zero indicates the current request is not supported. Tinyusb device stack will reject the request by
* sending STALL in the data phase.
* \note After this callback, the request is silently executed by the tinyusb stack, thus
* the completion of this control request will not be reported to application.
* For Keyboard, USB host often uses this to turn on/off the LED for CAPLOCKS, NUMLOCK (\ref hid_keyboard_led_bm_t)
*/
uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen); uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen);
/** Callback invoked when USB host request \ref HID_REQ_CONTROL_SET_REPORT. // Invoked when receiving SET_REPORT control request
* \param[in] report_type specify which report (INPUT, OUTPUT, FEATURE) that host requests
* \param[in] buffer containing the report's data
* \param[in] bufsize number of bytes in the \a buffer
* \note By the time this callback is invoked, the USB control transfer is already completed in the hardware side.
* Application are free to handle data at its own will.
*/
void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize); void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize);
ATTR_WEAK void tud_hid_mode_changed_cb(uint8_t boot_mode); // Invoked when host switch mode Boot <-> Report via SET_PROTOCOL request
void tud_hid_mode_changed_cb(uint8_t boot_mode) ATTR_WEAK;
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// KEYBOARD API // KEYBOARD API
@ -136,20 +112,38 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// HID Report Descriptor Template // Interface Descriptor Template
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
/* These template should be used as follow
* - Only 1 report : no parameter #define TUD_HID_DESC_LEN (9 + 9 + 7)
* uint8_t report_desc[] = { ID_REPORT_DESC_KEYBOARD() };
#define TUD_HID_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epin, _epsize, _ep_interval) \
/* Interface */\
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_HID, (_boot_protocol) ? HID_SUBCLASS_BOOT : 0, _boot_protocol, _stridx,\
/* HID descriptor */\
9, HID_DESC_TYPE_HID, U16_TO_U8S_LE(0x0111), 0, 1, HID_DESC_TYPE_REPORT, U16_TO_U8S_LE(_report_desc_len),\
/* Endpoint descriptor */\
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval
/* --------------------------------------------------------------------+
* HID Report Descriptor Template
*
* Convenient for declaring popular HID device (keyboard, mouse, consumer,
* gamepad etc...). Templates take "HID_REPORT_ID(n)," as input, leave
* empty if multiple reports is not used
*
* - Only 1 report: no parameter
* uint8_t const report_desc[] = { HID_REPORT_DESC_KEYBOARD() };
* *
* - Multiple Reports: "HID_REPORT_ID(ID)," must be passed to template * - Multiple Reports: "HID_REPORT_ID(ID)," must be passed to template
* uint8_t report_desc[] = { * uint8_t const report_desc[] =
* ID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1) ,) , * {
* HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2) ,) * HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1), ) ,
* HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2), )
* }; * };
*/ *--------------------------------------------------------------------*/
/*------------- Keyboard Descriptor Template -------------*/ // Keyboard Report Descriptor Template
#define HID_REPORT_DESC_KEYBOARD(...) \ #define HID_REPORT_DESC_KEYBOARD(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_KEYBOARD ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_KEYBOARD ) ,\
@ -190,7 +184,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
HID_OUTPUT ( HID_CONSTANT ) ,\ HID_OUTPUT ( HID_CONSTANT ) ,\
HID_COLLECTION_END \ HID_COLLECTION_END \
/*------------- Mouse Descriptor Template -------------*/ // Mouse Report Descriptor Template
#define HID_REPORT_DESC_MOUSE(...) \ #define HID_REPORT_DESC_MOUSE(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\
@ -230,7 +224,7 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
HID_COLLECTION_END ,\ HID_COLLECTION_END ,\
HID_COLLECTION_END \ HID_COLLECTION_END \
//------------- Consumer Control Report Template -------------// // Consumer Control Report Descriptor Template
#define HID_REPORT_DESC_CONSUMER(...) \ #define HID_REPORT_DESC_CONSUMER(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\ HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\
HID_USAGE ( HID_USAGE_CONSUMER_CONTROL ) ,\ HID_USAGE ( HID_USAGE_CONSUMER_CONTROL ) ,\
@ -245,8 +239,8 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\ HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\
HID_COLLECTION_END \ HID_COLLECTION_END \
//------------- System Control Report Template -------------// /* System Control Report Descriptor Template
/* 0x00 - do nothing * 0x00 - do nothing
* 0x01 - Power Off * 0x01 - Power Off
* 0x02 - Standby * 0x02 - Standby
* 0x04 - Wake Host * 0x04 - Wake Host
@ -271,8 +265,8 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
HID_INPUT ( HID_CONSTANT ) ,\ HID_INPUT ( HID_CONSTANT ) ,\
HID_COLLECTION_END \ HID_COLLECTION_END \
//------------- Gamepad Report Template -------------// // Gamepad Report Descriptor Template
// Gamepad with 16 buttons and 2 joysticks // with 16 buttons and 2 joysticks with following layout
// | Button Map (2 bytes) | X | Y | Z | Rz // | Button Map (2 bytes) | X | Y | Z | Rz
#define HID_REPORT_DESC_GAMEPAD(...) \ #define HID_REPORT_DESC_GAMEPAD(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\

View File

@ -62,24 +62,6 @@ TU_VERIFY_STATIC(CFG_TUD_MSC_BUFSIZE < UINT16_MAX, "Size is not correct");
#error CFG_TUD_MSC_PRODUCT_REV 4-byte string must be defined #error CFG_TUD_MSC_PRODUCT_REV 4-byte string must be defined
#endif #endif
//--------------------------------------------------------------------+
// Interface Descriptor Template
//--------------------------------------------------------------------+
// Length of template descriptor: 23 bytes
#define TUD_MSC_DESC_LEN (9 + 7 + 7)
// Interface Number, EP Out & EP In address
#define TUD_MSC_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \
/* Interface */\
0x09, TUSB_DESC_INTERFACE, _itfnum, 0x00, 0x02, TUSB_CLASS_MSC, MSC_SUBCLASS_SCSI, MSC_PROTOCOL_BOT, _stridx,\
/* Endpoint Out */\
0x07, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00,\
/* Endpoint In */\
0x07, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0x00
/** \addtogroup ClassDriver_MSC /** \addtogroup ClassDriver_MSC
* @{ * @{
* \defgroup MSC_Device Device * \defgroup MSC_Device Device
@ -150,7 +132,7 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz
*/ */
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize); int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize);
/*------------- Optional callbacks : Could be used by application to free up resources -------------*/ /*------------- Optional callbacks -------------*/
// Invoked when Read10 command is complete // Invoked when Read10 command is complete
ATTR_WEAK void tud_msc_read10_complete_cb(uint8_t lun); ATTR_WEAK void tud_msc_read10_complete_cb(uint8_t lun);
@ -164,6 +146,23 @@ ATTR_WEAK void tud_msc_scsi_complete_cb(uint8_t lun, uint8_t const scsi_cmd[16])
// Hook to make a mass storage device read-only. TODO remove // Hook to make a mass storage device read-only. TODO remove
ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun); ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun);
//--------------------------------------------------------------------+
// Interface Descriptor Template
//--------------------------------------------------------------------+
// Length of template descriptor: 23 bytes
#define TUD_MSC_DESC_LEN (9 + 7 + 7)
// Interface Number, EP Out & EP In address
#define TUD_MSC_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \
/* Interface */\
9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_MSC, MSC_SUBCLASS_SCSI, MSC_PROTOCOL_BOT, _stridx,\
/* Endpoint Out */\
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
/* Endpoint In */\
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
/** @} */ /** @} */
/** @} */ /** @} */

View File

@ -91,6 +91,13 @@ ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en);
// Invoked when usb bus is resumed // Invoked when usb bus is resumed
ATTR_WEAK void tud_resume_cb(void); ATTR_WEAK void tud_resume_cb(void);
//--------------------------------------------------------------------+
// Interface Descriptor Template
//--------------------------------------------------------------------+
#define TUD_CONFIG_DESCRIPTOR(_itfcount, _stridx, _total_len, _attribute, _power_ma) \
9, TUSB_DESC_CONFIGURATION, U16_TO_U8S_LE(_total_len), _itfcount, 1, _stridx, TU_BIT(7) | _attribute, (_power_ma)/2
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -473,47 +473,6 @@ desc_auto_cfg_t const _desc_auto_config_struct =
}, },
#endif // boot keyboard #endif // boot keyboard
//------------- HID Mouse -------------//
#if CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT
.hid_mse_boot =
{
.itf =
{
.bLength = sizeof(tusb_desc_interface_t),
.bDescriptorType = TUSB_DESC_INTERFACE,
.bInterfaceNumber = ITF_NUM_HID_BOOT_MSE,
.bAlternateSetting = 0x00,
.bNumEndpoints = 1,
.bInterfaceClass = TUSB_CLASS_HID,
.bInterfaceSubClass = HID_SUBCLASS_BOOT,
.bInterfaceProtocol = HID_PROTOCOL_MOUSE,
.iInterface = 0 // 4 + CFG_TUD_CDC + CFG_TUD_MSC + CFG_TUD_HID_KEYBOARD
},
.hid_desc =
{
.bLength = sizeof(tusb_hid_descriptor_hid_t),
.bDescriptorType = HID_DESC_TYPE_HID,
.bcdHID = 0x0111,
.bCountryCode = HID_Local_NotSupported,
.bNumDescriptors = 1,
.bReportType = HID_DESC_TYPE_REPORT,
.wReportLength = sizeof(_desc_auto_hid_boot_mse_report)
},
.ep_in =
{
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = EP_HID_MSE_BOOT,
.bmAttributes = { .xfer = TUSB_XFER_INTERRUPT },
.wMaxPacketSize = { .size = EP_HID_MSE_BOOT_SZ },
.bInterval = 0x0A
},
},
#endif // boot mouse
#if AUTO_DESC_HID_GENERIC #if AUTO_DESC_HID_GENERIC
//------------- HID Generic Multiple report -------------// //------------- HID Generic Multiple report -------------//
.hid_generic = .hid_generic =

View File

@ -169,22 +169,6 @@
#define CFG_TUD_MSC 0 #define CFG_TUD_MSC 0
#endif #endif
#ifndef CFG_TUD_HID_KEYBOARD
#define CFG_TUD_HID_KEYBOARD 0
#endif
#ifndef CFG_TUD_HID_MOUSE
#define CFG_TUD_HID_MOUSE 0
#endif
#ifndef CFG_TUD_HID_KEYBOARD_BOOT
#define CFG_TUD_HID_KEYBOARD_BOOT 0
#endif
#ifndef CFG_TUD_HID_MOUSE_BOOT
#define CFG_TUD_HID_MOUSE_BOOT 0
#endif
#endif // TUSB_OPT_DEVICE_ENABLED #endif // TUSB_OPT_DEVICE_ENABLED
//-------------------------------------------------------------------- //--------------------------------------------------------------------