Skip to content

Commit

Permalink
Merge pull request #7 from WootingKb/feature/TinyUSB-improvements
Browse files Browse the repository at this point in the history
Fix and improvement
  • Loading branch information
RockyZeroFour authored Sep 19, 2024
2 parents ac3c2c5 + 0915bac commit b868293
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/class/hid/hid_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,59 @@ typedef struct

CFG_TUSB_MEM_SECTION static hidd_interface_t _hidd_itf[CFG_TUD_HID];

/*------------- Helpers -------------*/
static inline uint8_t get_index_by_itfnum(uint8_t itf_num)
//--------------------------------------------------------------------+
// APPLICATION API
//--------------------------------------------------------------------+
uint8_t tud_hid_get_instance(uint8_t itf_num)
{
for (uint8_t i=0; i < CFG_TUD_HID; i++ )
// Search for the interface
for (uint8_t i=0; i < CFG_TUD_HID; i++ )
{
if ( itf_num == _hidd_itf[i].itf_num ) return i;
// Skip current instance if that isn't assigned yet
if (0 == _hidd_itf[i].ep_in && 0 == _hidd_itf[i].ep_out)
{
continue;
}

if (itf_num == _hidd_itf[i].itf_num)
{
return i;
}
}

return 0xFF;
}

//--------------------------------------------------------------------+
// APPLICATION API
//--------------------------------------------------------------------+
uint8_t tud_hid_get_itf_num(uint8_t instance)
{
// Abort if an invalid instance number was given
if (CFG_TUD_HID <= instance)
{
return 0xFF;
}

// Abort if the instance hasn't a valid interface assigned yet
if (0 == _hidd_itf[instance].ep_in && 0 == _hidd_itf[instance].ep_out)
{
return 0xFF;
}

// Return the interface number
return _hidd_itf[instance].itf_num;
}

bool tud_hid_n_ready(uint8_t instance)
{
TU_VERIFY( CFG_TUD_HID > instance );

uint8_t const ep_in = _hidd_itf[instance].ep_in;
return tud_ready() && (ep_in != 0) && !usbd_edpt_busy(TUD_OPT_RHPORT, ep_in);
}

bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, uint16_t len)
{
TU_VERIFY( CFG_TUD_HID > instance );

uint8_t const rhport = 0;
hidd_interface_t * p_hid = &_hidd_itf[instance];

Expand All @@ -108,11 +139,15 @@ bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, u

uint8_t tud_hid_n_interface_protocol(uint8_t instance)
{
TU_VERIFY( CFG_TUD_HID > instance, 0xFF );

return _hidd_itf[instance].itf_protocol;
}

uint8_t tud_hid_n_get_protocol(uint8_t instance)
{
TU_VERIFY( CFG_TUD_HID > instance, 0xFF );

return _hidd_itf[instance].protocol_mode;
}

Expand Down Expand Up @@ -241,7 +276,7 @@ bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
{
TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);

uint8_t const hid_itf = get_index_by_itfnum((uint8_t) request->wIndex);
uint8_t const hid_itf = tud_hid_get_instance((uint8_t) request->wIndex);
TU_VERIFY(hid_itf < CFG_TUD_HID);

hidd_interface_t* p_hid = &_hidd_itf[hid_itf];
Expand Down
6 changes: 6 additions & 0 deletions src/class/hid/hid_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
// CFG_TUD_HID > 1
//--------------------------------------------------------------------+

// Returns the instance of the given interface or 0xFF if not available
uint8_t tud_hid_get_instance(uint8_t itf_num);

// Returns the interface number of the given instance or 0xFF if not available
uint8_t tud_hid_get_itf_num(uint8_t instance);

// Check if the interface is ready to use
bool tud_hid_n_ready(uint8_t instance);

Expand Down
3 changes: 3 additions & 0 deletions src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,9 @@ bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr)
// TU_VERIFY(tud_ready());

uint8_t const epnum = tu_edpt_number(ep_addr);

TU_ASSERT( 0 < epnum && CFG_TUD_ENDPPOINT_MAX > epnum );

uint8_t const dir = tu_edpt_dir(ep_addr);
tu_edpt_state_t* ep_state = &_usbd_dev.ep_status[epnum][dir];

Expand Down

0 comments on commit b868293

Please sign in to comment.