From 28a1a48a7f53e8c16b3292a243dfc80f5cebfcbf Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 12 May 2019 00:58:45 +0700 Subject: [PATCH] add tud_descriptor_device_cb()/tud_descriptor_configuration_cb() remove the use of tud_desc_set_t tud_desc_set --- .../Adafruit_USBD_Device.cpp | 120 +++++++++--------- .../Adafruit_USBD_Device.h | 3 + .../tinyusb/src/class/hid/hid_device.h | 6 +- .../tinyusb/src/device/usbd.c | 9 +- .../tinyusb/src/device/usbd.h | 22 ++-- .../tinyusb/src/device/usbd_control.c | 4 +- 6 files changed, 85 insertions(+), 79 deletions(-) diff --git a/cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp b/cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp index 6900b009..44266794 100644 --- a/cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp +++ b/cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp @@ -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 max_char ) count = max_char; + + for(uint8_t i=0; ibDescriptorType == 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: diff --git a/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd.h b/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd.h index 4de36550..f8485166 100644 --- a/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd.h +++ b/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd.h @@ -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) diff --git a/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd_control.c b/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd_control.c index 31f447c0..0b878fbc 100644 --- a/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd_control.c +++ b/cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd_control.c @@ -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