mirror of
https://github.com/hathach/tinyusb.git
synced 2025-01-31 05:52:55 +08:00
more API update
- remove tuh_n_hid_n_get_report() - usbh auto queue get report and invoke callback when received data
This commit is contained in:
parent
dfa8b41b96
commit
93661042d9
@ -108,7 +108,6 @@ void cdc_task(void)
|
|||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// USB HID
|
// USB HID
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
CFG_TUSB_MEM_SECTION static hid_keyboard_report_t usb_keyboard_report;
|
|
||||||
uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII };
|
uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII };
|
||||||
|
|
||||||
// look up new key in previous keys
|
// look up new key in previous keys
|
||||||
@ -156,10 +155,6 @@ void tuh_hid_mounted_cb(uint8_t dev_addr, uint8_t instance)
|
|||||||
printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance);
|
printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance);
|
||||||
// printf("A Keyboard device (address %d) is mounted\r\n", dev_addr);
|
// printf("A Keyboard device (address %d) is mounted\r\n", dev_addr);
|
||||||
// printf("A Mouse device (address %d) is mounted\r\n", dev_addr);
|
// printf("A Mouse device (address %d) is mounted\r\n", dev_addr);
|
||||||
|
|
||||||
if (instance == 0) {
|
|
||||||
tuh_n_hid_n_get_report(dev_addr, instance, &usb_keyboard_report, sizeof(usb_keyboard_report));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tuh_hid_unmounted_cb(uint8_t dev_addr, uint8_t instance)
|
void tuh_hid_unmounted_cb(uint8_t dev_addr, uint8_t instance)
|
||||||
@ -169,9 +164,6 @@ void tuh_hid_unmounted_cb(uint8_t dev_addr, uint8_t instance)
|
|||||||
// printf("A Mouse device (address %d) is unmounted\r\n", dev_addr);
|
// printf("A Mouse device (address %d) is unmounted\r\n", dev_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CFG_TUSB_MEM_SECTION static hid_mouse_report_t usb_mouse_report;
|
|
||||||
|
|
||||||
void cursor_movement(int8_t x, int8_t y, int8_t wheel)
|
void cursor_movement(int8_t x, int8_t y, int8_t wheel)
|
||||||
{
|
{
|
||||||
//------------- X -------------//
|
//------------- X -------------//
|
||||||
@ -220,32 +212,13 @@ static inline void process_mouse_report(hid_mouse_report_t const * p_report)
|
|||||||
cursor_movement(p_report->x, p_report->y, p_report->wheel);
|
cursor_movement(p_report->x, p_report->y, p_report->wheel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tuh_hid_get_report_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len)
|
||||||
|
{
|
||||||
|
process_kbd_report( (hid_keyboard_report_t*) report );
|
||||||
|
}
|
||||||
|
|
||||||
void hid_task(void)
|
void hid_task(void)
|
||||||
{
|
{
|
||||||
uint8_t const daddr = 1;
|
|
||||||
uint8_t const instance = 0;
|
|
||||||
|
|
||||||
if ( tuh_n_hid_n_keyboard_mounted(daddr, instance) )
|
|
||||||
{
|
|
||||||
if ( tuh_n_hid_n_ready(daddr, instance) )
|
|
||||||
{
|
|
||||||
process_kbd_report(&usb_keyboard_report);
|
|
||||||
tuh_n_hid_n_get_report(daddr, instance, &usb_keyboard_report, sizeof(usb_keyboard_report));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// #if CFG_TUH_HID_MOUSE
|
|
||||||
(void) usb_mouse_report;
|
|
||||||
// if ( tuh_n_hid_n_mouse_mounted(daddr, instance) )
|
|
||||||
// {
|
|
||||||
// if ( tuh_n_hid_n_ready(daddr, instance) )
|
|
||||||
// {
|
|
||||||
// process_mouse_report(&usb_mouse_report);
|
|
||||||
// tuh_n_hid_n_get_report(daddr, instance, &usb_mouse_report, sizeof(usb_mouse_report));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
@ -84,7 +84,7 @@
|
|||||||
|
|
||||||
//------------- HID -------------//
|
//------------- HID -------------//
|
||||||
|
|
||||||
//#define CFG_TUH_HID_EP_BUFSIZE 64
|
#define CFG_TUH_HID_EP_BUFSIZE 64
|
||||||
|
|
||||||
// Max number of reports per interface
|
// Max number of reports per interface
|
||||||
// E.g composite HID with keyboard + mouse + gamepad will have 3 reports
|
// E.g composite HID with keyboard + mouse + gamepad will have 3 reports
|
||||||
|
@ -53,7 +53,9 @@ typedef struct
|
|||||||
|
|
||||||
uint8_t report_desc_type;
|
uint8_t report_desc_type;
|
||||||
uint16_t report_desc_len;
|
uint16_t report_desc_len;
|
||||||
uint16_t ep_size;
|
|
||||||
|
uint16_t epin_size;
|
||||||
|
uint16_t epout_size;
|
||||||
|
|
||||||
uint8_t boot_protocol; // None, Keyboard, Mouse
|
uint8_t boot_protocol; // None, Keyboard, Mouse
|
||||||
bool boot_mode; // Boot or Report protocol
|
bool boot_mode; // Boot or Report protocol
|
||||||
@ -65,6 +67,9 @@ typedef struct
|
|||||||
uint8_t rid_mouse;
|
uint8_t rid_mouse;
|
||||||
uint8_t rid_gamepad;
|
uint8_t rid_gamepad;
|
||||||
uint8_t rid_consumer;
|
uint8_t rid_consumer;
|
||||||
|
|
||||||
|
uint8_t epin_buf[CFG_TUH_HID_EP_BUFSIZE];
|
||||||
|
uint8_t epout_buf[CFG_TUH_HID_EP_BUFSIZE];
|
||||||
} hidh_interface_t;
|
} hidh_interface_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
@ -82,6 +87,11 @@ static uint8_t get_instance_id_by_itfnum(uint8_t dev_addr, uint8_t itf);
|
|||||||
static hidh_interface_t* get_instance_by_itfnum(uint8_t dev_addr, uint8_t itf);
|
static hidh_interface_t* get_instance_by_itfnum(uint8_t dev_addr, uint8_t itf);
|
||||||
static uint8_t get_instance_id_by_epaddr(uint8_t dev_addr, uint8_t ep_addr);
|
static uint8_t get_instance_id_by_epaddr(uint8_t dev_addr, uint8_t ep_addr);
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline bool hidh_get_report(uint8_t dev_addr, hidh_interface_t* hid_itf)
|
||||||
|
{
|
||||||
|
return usbh_edpt_xfer(dev_addr, hid_itf->ep_in, hid_itf->epin_buf, hid_itf->epin_size);
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Application API
|
// Application API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
@ -122,19 +132,6 @@ bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance)
|
|||||||
return !hcd_edpt_busy(dev_addr, hid_itf->ep_in);
|
return !hcd_edpt_busy(dev_addr, hid_itf->ep_in);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tuh_n_hid_n_get_report(uint8_t dev_addr, uint8_t instance, void* report, uint16_t len)
|
|
||||||
{
|
|
||||||
TU_VERIFY( tuh_device_ready(dev_addr) && report && len);
|
|
||||||
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
|
|
||||||
|
|
||||||
// TODO change to claim endpoint
|
|
||||||
TU_VERIFY( !hcd_edpt_busy(dev_addr, hid_itf->ep_in) );
|
|
||||||
|
|
||||||
len = tu_min16(len, hid_itf->ep_size);
|
|
||||||
|
|
||||||
return usbh_edpt_xfer(dev_addr, hid_itf->ep_in, report, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// KEYBOARD
|
// KEYBOARD
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
@ -147,18 +144,11 @@ bool tuh_n_hid_n_keyboard_mounted(uint8_t dev_addr, uint8_t instance)
|
|||||||
return tuh_device_ready(dev_addr) && (hid_itf->ep_in != 0);
|
return tuh_device_ready(dev_addr) && (hid_itf->ep_in != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
|
||||||
// MOUSE
|
|
||||||
//--------------------------------------------------------------------+
|
|
||||||
|
|
||||||
// TODO remove
|
// TODO remove
|
||||||
static hidh_interface_t mouseh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1
|
|
||||||
|
|
||||||
//------------- Public API -------------//
|
|
||||||
bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance)
|
bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance)
|
||||||
{
|
{
|
||||||
// hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
|
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
|
||||||
return tuh_device_ready(dev_addr) && (mouseh_data[dev_addr-1].ep_in != 0);
|
return tuh_device_ready(dev_addr) && (hid_itf->ep_in != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
@ -173,10 +163,17 @@ bool hidh_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32
|
|||||||
{
|
{
|
||||||
uint8_t const dir = tu_edpt_dir(ep_addr);
|
uint8_t const dir = tu_edpt_dir(ep_addr);
|
||||||
uint8_t const instance = get_instance_id_by_epaddr(dev_addr, ep_addr);
|
uint8_t const instance = get_instance_id_by_epaddr(dev_addr, ep_addr);
|
||||||
|
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);
|
||||||
|
|
||||||
if ( dir == TUSB_DIR_IN )
|
if ( dir == TUSB_DIR_IN )
|
||||||
{
|
{
|
||||||
if (tuh_hid_get_report_complete_cb) tuh_hid_get_report_complete_cb(dev_addr, instance, xferred_bytes);
|
if (tuh_hid_get_report_cb)
|
||||||
|
{
|
||||||
|
tuh_hid_get_report_cb(dev_addr, instance, hid_itf->epin_buf, xferred_bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
// queue next report
|
||||||
|
hidh_get_report(dev_addr, hid_itf);
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
if (tuh_hid_set_report_complete_cb) tuh_hid_set_report_complete_cb(dev_addr, instance, xferred_bytes);
|
if (tuh_hid_set_report_complete_cb) tuh_hid_set_report_complete_cb(dev_addr, instance, xferred_bytes);
|
||||||
@ -233,7 +230,7 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
|
|||||||
|
|
||||||
hid_itf->itf_num = desc_itf->bInterfaceNumber;
|
hid_itf->itf_num = desc_itf->bInterfaceNumber;
|
||||||
hid_itf->ep_in = desc_ep->bEndpointAddress;
|
hid_itf->ep_in = desc_ep->bEndpointAddress;
|
||||||
hid_itf->ep_size = desc_ep->wMaxPacketSize.size;
|
hid_itf->epin_size = desc_ep->wMaxPacketSize.size;
|
||||||
|
|
||||||
// Assume bNumDescriptors = 1
|
// Assume bNumDescriptors = 1
|
||||||
hid_itf->report_desc_type = desc_hid->bReportType;
|
hid_itf->report_desc_type = desc_hid->bReportType;
|
||||||
@ -357,6 +354,9 @@ bool config_get_report_desc_complete(uint8_t dev_addr, tusb_control_request_t co
|
|||||||
// enumeration is complete
|
// enumeration is complete
|
||||||
if (tuh_hid_mounted_cb) tuh_hid_mounted_cb(dev_addr, instance);
|
if (tuh_hid_mounted_cb) tuh_hid_mounted_cb(dev_addr, instance);
|
||||||
|
|
||||||
|
// queue transfer for IN endpoint
|
||||||
|
hidh_get_report(dev_addr, hid_itf);
|
||||||
|
|
||||||
// notify usbh that driver enumeration is complete
|
// notify usbh that driver enumeration is complete
|
||||||
usbh_driver_set_config_complete(dev_addr, itf_num);
|
usbh_driver_set_config_complete(dev_addr, itf_num);
|
||||||
|
|
||||||
@ -381,13 +381,12 @@ static void parse_report_descriptor(hidh_interface_t* hid_itf, uint8_t const* de
|
|||||||
while(desc_len)
|
while(desc_len)
|
||||||
{
|
{
|
||||||
header.byte = *desc_report++;
|
header.byte = *desc_report++;
|
||||||
|
desc_len--;
|
||||||
|
|
||||||
uint8_t const tag = header.tag;
|
uint8_t const tag = header.tag;
|
||||||
uint8_t const type = header.type;
|
uint8_t const type = header.type;
|
||||||
uint8_t const size = header.size;
|
uint8_t const size = header.size;
|
||||||
|
|
||||||
desc_len--;
|
|
||||||
|
|
||||||
TU_LOG2("tag = %d, type = %d, size = %d, data = ", tag, type, size);
|
TU_LOG2("tag = %d, type = %d, size = %d, data = ", tag, type, size);
|
||||||
for(uint32_t i=0; i<size; i++) TU_LOG2("%02X ", desc_report[i]);
|
for(uint32_t i=0; i<size; i++) TU_LOG2("%02X ", desc_report[i]);
|
||||||
TU_LOG2("\r\n");
|
TU_LOG2("\r\n");
|
||||||
|
@ -46,6 +46,11 @@
|
|||||||
#define CFG_TUH_HID_REPORT_MAX 4
|
#define CFG_TUH_HID_REPORT_MAX 4
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// TODO Highspeed interrupt can be up to 512 bytes
|
||||||
|
#ifndef CFG_TUH_HID_EP_BUFSIZE
|
||||||
|
#define CFG_TUH_HID_EP_BUFSIZE 64
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t count; // number of info
|
uint8_t count; // number of info
|
||||||
@ -88,9 +93,6 @@ tuh_hid_report_info_t const* tuh_n_hid_n_get_report_info(uint8_t dev_addr, uint8
|
|||||||
// Check if the interface is ready to use
|
// Check if the interface is ready to use
|
||||||
bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance);
|
bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance);
|
||||||
|
|
||||||
// Get Report from device
|
|
||||||
bool tuh_n_hid_n_get_report(uint8_t dev_addr, uint8_t instance, void* report, uint16_t len);
|
|
||||||
|
|
||||||
// Set Report using control endpoint
|
// Set Report using control endpoint
|
||||||
//bool tuh_n_hid_n_set_report_control(uint8_t dev_addr, uint8_t instance, void* report, uint16_t len);
|
//bool tuh_n_hid_n_set_report_control(uint8_t dev_addr, uint8_t instance, void* report, uint16_t len);
|
||||||
|
|
||||||
@ -117,7 +119,7 @@ TU_ATTR_WEAK void tuh_hid_mounted_cb (uint8_t dev_addr, uint8_t instance);
|
|||||||
TU_ATTR_WEAK void tuh_hid_unmounted_cb(uint8_t dev_addr, uint8_t instance);
|
TU_ATTR_WEAK void tuh_hid_unmounted_cb(uint8_t dev_addr, uint8_t instance);
|
||||||
|
|
||||||
// Invoked when received Report from device via either regular or control endpoint
|
// Invoked when received Report from device via either regular or control endpoint
|
||||||
TU_ATTR_WEAK void tuh_hid_get_report_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t xferred_bytes);
|
TU_ATTR_WEAK void tuh_hid_get_report_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len);
|
||||||
|
|
||||||
// Invoked when Sent Report to device via either regular or control endpoint
|
// Invoked when Sent Report to device via either regular or control endpoint
|
||||||
TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t xferred_bytes);
|
TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t xferred_bytes);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user