add tud_descriptor_device_cb()/tud_descriptor_configuration_cb()
remove the use of tud_desc_set_t tud_desc_set
This commit is contained in:
6 files changed
+85
-79
No files matched your search
@@ -36,58 +36,6 @@
|
||||
|
||||
extern uint8_t load_serial_number(uint16_t* serial_str);
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
// tud_desc_set is required by tinyusb stack
|
||||
tud_desc_set_t tud_desc_set =
|
||||
{
|
||||
.device = NULL, // update later
|
||||
.config = NULL, // update later
|
||||
};
|
||||
|
||||
// Invoked when received GET_STRING_DESC request
|
||||
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
|
||||
// Return number of characters. Note usb string is in 16-bits unicode format
|
||||
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
// language = English
|
||||
desc[0] = 0x0409;
|
||||
return 1;
|
||||
|
||||
case 1: // Manufacturer
|
||||
case 2: // Product
|
||||
{
|
||||
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
|
||||
|
||||
// cap at max char
|
||||
uint8_t count = strlen(str);
|
||||
if ( count > max_char ) count = max_char;
|
||||
|
||||
for(uint8_t i=0; i<count; i++)
|
||||
{
|
||||
*desc++ = str[i];
|
||||
}
|
||||
return count;
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
// serial Number
|
||||
return load_serial_number(desc);
|
||||
|
||||
default: return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
} // extern C
|
||||
|
||||
Adafruit_USBD_Device USBDevice;
|
||||
|
||||
Adafruit_USBD_Device::Adafruit_USBD_Device(void)
|
||||
@@ -98,17 +46,11 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
|
||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||
.bcdUSB = 0x0200,
|
||||
|
||||
#if CFG_TUD_CDC
|
||||
// Use Interface Association Descriptor (IAD) for CDC
|
||||
// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
|
||||
.bDeviceClass = TUSB_CLASS_MISC,
|
||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
|
||||
.bDeviceProtocol = MISC_PROTOCOL_IAD,
|
||||
#else
|
||||
.bDeviceClass = 0x00,
|
||||
.bDeviceSubClass = 0x00,
|
||||
.bDeviceProtocol = 0x00,
|
||||
#endif
|
||||
|
||||
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
|
||||
|
||||
@@ -145,9 +87,6 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
|
||||
_desc_cfglen = sizeof(tusb_desc_configuration_t);
|
||||
_itf_count = 0;
|
||||
_epin_count = _epout_count = 1;
|
||||
|
||||
tud_desc_set.config = _desc_cfg;
|
||||
tud_desc_set.device = &_desc_device;
|
||||
}
|
||||
|
||||
// Add interface descriptor
|
||||
@@ -207,4 +146,63 @@ bool Adafruit_USBD_Device::begin(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
// Invoked when received GET DEVICE DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
uint8_t const * tud_descriptor_device_cb(void)
|
||||
{
|
||||
return (uint8_t const *) &USBDevice._desc_device;
|
||||
}
|
||||
|
||||
// Invoked when received GET CONFIGURATION DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
// Descriptor contents must exist long enough for transfer to complete
|
||||
uint8_t const * tud_descriptor_configuration_cb(void)
|
||||
{
|
||||
return USBDevice._desc_cfg;
|
||||
}
|
||||
|
||||
// Invoked when received GET_STRING_DESC request
|
||||
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
|
||||
// Return number of characters. Note usb string is in UTF-16 format
|
||||
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
// language = English
|
||||
desc[0] = 0x0409;
|
||||
return 1;
|
||||
|
||||
case 1: // Manufacturer
|
||||
case 2: // Product
|
||||
{
|
||||
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
|
||||
|
||||
// cap at max char
|
||||
uint8_t count = strlen(str);
|
||||
if ( count > max_char ) count = max_char;
|
||||
|
||||
for(uint8_t i=0; i<count; i++)
|
||||
{
|
||||
*desc++ = str[i];
|
||||
}
|
||||
return count;
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
// serial Number
|
||||
return load_serial_number(desc);
|
||||
|
||||
default: return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // extern C
|
||||
|
||||
#endif // USE_TINYUSB
|
||||
@@ -58,6 +58,9 @@ class Adafruit_USBD_Device
|
||||
bool suspended(void) { return tud_suspended(); }
|
||||
bool ready(void) { return tud_ready(); }
|
||||
bool remoteWakeup(void) { return tud_remote_wakeup(); }
|
||||
|
||||
friend uint8_t const * tud_descriptor_device_cb(void);
|
||||
friend uint8_t const * tud_descriptor_configuration_cb(void);
|
||||
};
|
||||
|
||||
extern Adafruit_USBD_Device USBDevice;
|
||||
|
||||
@@ -68,9 +68,9 @@ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y
|
||||
// Callbacks (Weak is optional)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when received GET REPORT DESCRIPTOR
|
||||
// Application return pointer to report descriptor.
|
||||
// descriptor contents must exist long enough for transfer to complete
|
||||
// Invoked when received GET HID REPORT DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
// Descriptor contents must exist long enough for transfer to complete
|
||||
ATTR_WEAK uint8_t const * tud_hid_descriptor_report_cb(void);
|
||||
|
||||
// Invoked when received GET_REPORT control request
|
||||
|
||||
@@ -479,7 +479,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
|
||||
// This function parse configuration descriptor & open drivers accordingly
|
||||
static bool process_set_config(uint8_t rhport)
|
||||
{
|
||||
tusb_desc_configuration_t const * desc_cfg = (tusb_desc_configuration_t const *) tud_desc_set.config;
|
||||
tusb_desc_configuration_t const * desc_cfg = (tusb_desc_configuration_t const *) tud_descriptor_configuration_cb();
|
||||
TU_ASSERT(desc_cfg != NULL && desc_cfg->bDescriptorType == TUSB_DESC_CONFIGURATION);
|
||||
|
||||
// Parse configuration descriptor
|
||||
@@ -558,11 +558,14 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
|
||||
switch(desc_type)
|
||||
{
|
||||
case TUSB_DESC_DEVICE:
|
||||
return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.device, sizeof(tusb_desc_device_t));
|
||||
return usbd_control_xfer(rhport, p_request, (void*) tud_descriptor_device_cb(), sizeof(tusb_desc_device_t));
|
||||
break;
|
||||
|
||||
case TUSB_DESC_CONFIGURATION:
|
||||
return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.config, ((tusb_desc_configuration_t const*) tud_desc_set.config)->wTotalLength);
|
||||
{
|
||||
tusb_desc_configuration_t const* desc_config = (tusb_desc_configuration_t const*) tud_descriptor_configuration_cb();
|
||||
return usbd_control_xfer(rhport, p_request, (void*) desc_config, desc_config->wTotalLength);
|
||||
}
|
||||
break;
|
||||
|
||||
case TUSB_DESC_STRING:
|
||||
|
||||
@@ -37,15 +37,6 @@
|
||||
#include "common/tusb_common.h"
|
||||
#include "device/dcd.h"
|
||||
|
||||
/// \brief Descriptor pointer collector to all the needed.
|
||||
typedef struct {
|
||||
void const * device; ///< pointer to device descriptor \ref tusb_desc_device_t
|
||||
void const * config; ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
|
||||
}tud_desc_set_t;
|
||||
|
||||
// Descriptor collection set, must be defined by application
|
||||
extern tud_desc_set_t tud_desc_set;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -72,9 +63,18 @@ bool tud_remote_wakeup(void);
|
||||
// Application Callbacks (WEAK is optional)
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// Invoked when received GET_STRING_DESC request
|
||||
// Invoked when received GET DEVICE DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
uint8_t const * tud_descriptor_device_cb(void);
|
||||
|
||||
// Invoked when received GET CONFIGURATION DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
// Descriptor contents must exist long enough for transfer to complete
|
||||
uint8_t const * tud_descriptor_configuration_cb(void);
|
||||
|
||||
// Invoked when received GET STRING DESC request
|
||||
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
|
||||
// Return number of characters. Note usb string is in 16-bits unicode format
|
||||
// Return number of characters. Note usb string is in UTF-16 format
|
||||
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char);
|
||||
|
||||
// Invoked when device is mounted (configured)
|
||||
|
||||
@@ -93,8 +93,10 @@ bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, v
|
||||
_control_state.total_len = tu_min16(len, request->wLength);
|
||||
_control_state.total_transferred = 0;
|
||||
|
||||
if ( (buffer != NULL) && len )
|
||||
if ( len )
|
||||
{
|
||||
TU_ASSERT(buffer);
|
||||
|
||||
// Data stage
|
||||
TU_ASSERT( start_control_data_xact(rhport) );
|
||||
}else
|
||||
|
||||
Reference in new issue
Block a user