wrap up hid device refactor

This commit is contained in:
hathach
2019-04-18 22:31:35 +07:00
parent a30461b078
commit 307ba23046
11 changed files with 206 additions and 214 deletions

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_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
//--------------------------------------------------------------------+
@@ -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.
#define TUD_CDC_DESCRIPTOR(_itfnum, _stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize) \
/* 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 */\
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 */\
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 */\
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 */\
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 */\
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 */\
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 */\
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 */\
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 */\
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
}
#endif
#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)
{
(void) pan;
hid_mouse_report_t report =
{
.buttons = buttons,

View File

@@ -42,55 +42,31 @@
#define CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP 0
#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
//--------------------------------------------------------------------+
/** 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
*/
// Check if the interface is ready to use
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)
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) -------------*/
/** Callback invoked when USB host request \ref HID_REQ_CONTROL_GET_REPORT.
* \param[in] report_type specify which report (INPUT, OUTPUT, FEATURE) that host requests
* \param[out] buffer data that application need to update, value must be accessible by USB controller (see \ref CFG_TUSB_MEM_SECTION)
* \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)
*/
// Invoked when receiving GET_REPORT control request
// Application must fill buffer report's content and return its length.
// Return zero will cause the stack to STALL request
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.
* \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);
ATTR_WEAK void tud_hid_mode_changed_cb(uint8_t boot_mode);
// Invoked when receiving SET_REPORT control request
void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize);
// 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
@@ -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
* uint8_t report_desc[] = { ID_REPORT_DESC_KEYBOARD() };
#define TUD_HID_DESC_LEN (9 + 9 + 7)
#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
* uint8_t report_desc[] = {
* ID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1) ,) ,
* HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(2) ,)
* uint8_t const report_desc[] =
* {
* 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(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
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_COLLECTION_END \
/*------------- Mouse Descriptor Template -------------*/
// Mouse Report Descriptor Template
#define HID_REPORT_DESC_MOUSE(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
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 \
//------------- Consumer Control Report Template -------------//
// Consumer Control Report Descriptor Template
#define HID_REPORT_DESC_CONSUMER(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\
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_COLLECTION_END \
//------------- System Control Report Template -------------//
/* 0x00 - do nothing
/* System Control Report Descriptor Template
* 0x00 - do nothing
* 0x01 - Power Off
* 0x02 - Standby
* 0x04 - Wake Host
@@ -271,8 +265,8 @@ static inline bool tud_hid_mouse_button_release(uint8_t report_id)
HID_INPUT ( HID_CONSTANT ) ,\
HID_COLLECTION_END \
//------------- Gamepad Report Template -------------//
// Gamepad with 16 buttons and 2 joysticks
// Gamepad Report Descriptor Template
// with 16 buttons and 2 joysticks with following layout
// | Button Map (2 bytes) | X | Y | Z | Rz
#define HID_REPORT_DESC_GAMEPAD(...) \
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
#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
* @{
* \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);
/*------------- Optional callbacks : Could be used by application to free up resources -------------*/
/*------------- Optional callbacks -------------*/
// Invoked when Read10 command is complete
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
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
/** @} */
/** @} */