mirror of
https://github.com/hathach/tinyusb.git
synced 2025-04-25 03:02:26 +00:00
add tud_set_descriptors
This commit is contained in:
parent
29071c10b1
commit
6f7c4346c3
@ -60,7 +60,7 @@
|
||||
|
||||
/// Application MUST define this variable and initialize its pointers's member to all required USB descriptors including
|
||||
/// Device Descriptor, Configuration Descriptor, String Descriptors, HID Report Descriptors etc ...
|
||||
tusbd_descriptor_pointer_t tusbd_descriptor_pointers;
|
||||
tud_desc_init_t tusbd_descriptor_pointers;
|
||||
|
||||
/** \defgroup config_device_class Class Driver
|
||||
* \brief For each Class Driver a value of 1 means enable, value of 0 mean disable
|
||||
|
@ -45,6 +45,7 @@
|
||||
|
||||
#include "bsp/board.h"
|
||||
#include "tusb.h"
|
||||
#include "tusb_descriptors.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
@ -64,6 +65,7 @@ int main(void)
|
||||
print_greeting();
|
||||
|
||||
tusb_init();
|
||||
tud_set_descriptors(&usb_desc_init);
|
||||
|
||||
while (1)
|
||||
{
|
||||
|
@ -245,12 +245,11 @@ uint16_t const * const string_descriptor_arr [] =
|
||||
}
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TINYUSB Descriptors Pointer (this variable is required by the stack)
|
||||
//--------------------------------------------------------------------+
|
||||
tusbd_descriptor_pointer_t tusbd_descriptor_pointers =
|
||||
|
||||
/*------------- Variable used by tud_set_descriptors -------------*/
|
||||
tud_desc_init_t usb_desc_init =
|
||||
{
|
||||
.p_device = (uint8_t const * ) &desc_device,
|
||||
.p_configuration = (uint8_t const * ) &desc_configuration,
|
||||
.p_string_arr = (uint8_t const **) string_descriptor_arr,
|
||||
.device = (uint8_t const * ) &desc_device,
|
||||
.configuration = (uint8_t const * ) &desc_configuration,
|
||||
.string_arr = (uint8_t const **) string_descriptor_arr,
|
||||
};
|
||||
|
@ -98,4 +98,8 @@ typedef struct ATTR_PACKED
|
||||
|
||||
} app_descriptor_configuration_t;
|
||||
|
||||
|
||||
|
||||
extern tud_desc_init_t usb_desc_init;
|
||||
|
||||
#endif
|
||||
|
@ -45,6 +45,7 @@
|
||||
|
||||
#include "bsp/board.h"
|
||||
#include "tusb.h"
|
||||
#include "tusb_descriptors.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
@ -64,6 +65,7 @@ int main(void)
|
||||
print_greeting();
|
||||
|
||||
tusb_init();
|
||||
tud_set_descriptors(&usb_desc_init);
|
||||
|
||||
while (1)
|
||||
{
|
||||
|
@ -284,12 +284,11 @@ uint16_t const * const string_descriptor_arr [] =
|
||||
}
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TINYUSB Descriptors Pointer (this variable is required by the stack)
|
||||
//--------------------------------------------------------------------+
|
||||
tusbd_descriptor_pointer_t tusbd_descriptor_pointers =
|
||||
|
||||
/*------------- Variable used by tud_set_descriptors -------------*/
|
||||
tud_desc_init_t usb_desc_init =
|
||||
{
|
||||
.p_device = (uint8_t const * ) &desc_device,
|
||||
.p_configuration = (uint8_t const * ) &desc_configuration,
|
||||
.p_string_arr = (uint8_t const **) string_descriptor_arr,
|
||||
.device = (uint8_t const * ) &desc_device,
|
||||
.configuration = (uint8_t const * ) &desc_configuration,
|
||||
.string_arr = (uint8_t const **) string_descriptor_arr,
|
||||
};
|
||||
|
@ -113,4 +113,8 @@ typedef struct ATTR_PACKED
|
||||
|
||||
} app_descriptor_configuration_t;
|
||||
|
||||
|
||||
|
||||
extern tud_desc_init_t usb_desc_init;
|
||||
|
||||
#endif
|
||||
|
@ -46,6 +46,7 @@
|
||||
#include "bsp/board.h"
|
||||
#include "app_os_prio.h"
|
||||
#include "tusb.h"
|
||||
#include "tusb_descriptors.h"
|
||||
|
||||
#include "msc_device_app.h"
|
||||
#include "keyboard_device_app.h"
|
||||
@ -87,6 +88,7 @@ int main(void)
|
||||
print_greeting();
|
||||
|
||||
tusb_init();
|
||||
tud_set_descriptors(&usb_desc_init);
|
||||
|
||||
//------------- application task init -------------//
|
||||
led_blinking_init();
|
||||
|
@ -456,14 +456,13 @@ uint16_t const * const string_descriptor_arr [] =
|
||||
}
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// TINYUSB Descriptors Pointer (this variable is required by the stack)
|
||||
//--------------------------------------------------------------------+
|
||||
tusbd_descriptor_pointer_t tusbd_descriptor_pointers =
|
||||
|
||||
/*------------- Variable used by tud_set_descriptors -------------*/
|
||||
tud_desc_init_t usb_desc_init =
|
||||
{
|
||||
.p_device = (uint8_t const * ) &desc_device,
|
||||
.p_configuration = (uint8_t const * ) &desc_configuration,
|
||||
.p_string_arr = (uint8_t const **) string_descriptor_arr,
|
||||
.device = (uint8_t const * ) &desc_device,
|
||||
.configuration = (uint8_t const * ) &desc_configuration,
|
||||
.string_arr = (uint8_t const **) string_descriptor_arr,
|
||||
|
||||
#if CFG_TUD_HID_KEYBOARD
|
||||
.p_hid_keyboard_report = (uint8_t const *) desc_keyboard_report,
|
||||
|
@ -175,4 +175,8 @@ typedef struct ATTR_PACKED
|
||||
|
||||
} app_descriptor_configuration_t;
|
||||
|
||||
|
||||
|
||||
extern tud_desc_init_t usb_desc_init;
|
||||
|
||||
#endif
|
||||
|
@ -106,13 +106,10 @@
|
||||
// INLINE FUNCTION
|
||||
//--------------------------------------------------------------------+
|
||||
#define memclr_(buffer, size) memset((buffer), 0, (size))
|
||||
#define varclr_(_var) memclr_(_var, sizeof(*(_var)))
|
||||
#define arrclr_(_arr) memclr_(_arr, sizeof(_arr))
|
||||
|
||||
|
||||
#define memclr(buffer, size) memset(buffer, 0, size)
|
||||
#define varclr(_var) memclr(_var, sizeof(*(_var)))
|
||||
#define arrclr(_arr) memclr(_arr, sizeof(_arr))
|
||||
|
||||
#define arrcount(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
|
||||
#define arrcount_(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
|
||||
|
||||
static inline uint8_t const * descriptor_next(uint8_t const p_desc[])
|
||||
{
|
||||
|
@ -71,14 +71,11 @@
|
||||
// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7
|
||||
#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__)
|
||||
|
||||
static inline void verify_breakpoint(void)
|
||||
{
|
||||
// Cortex M CoreDebug->DHCSR
|
||||
volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL);
|
||||
|
||||
// Only halt mcu if debugger is attached
|
||||
if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n");
|
||||
}
|
||||
#define verify_breakpoint() \
|
||||
do {\
|
||||
volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \
|
||||
if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */\
|
||||
} while(0)
|
||||
|
||||
#else
|
||||
#define verify_breakpoint()
|
||||
|
@ -42,14 +42,13 @@
|
||||
|
||||
#define _TINY_USB_SOURCE_FILE_
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
#include "tusb.h"
|
||||
#include "usbd.h"
|
||||
#include "device/usbd_pvt.h"
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct {
|
||||
void (* init) (void);
|
||||
tusb_error_t (* open)(uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length);
|
||||
@ -72,11 +71,15 @@ typedef struct {
|
||||
}usbd_device_info_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO CONSTANT TYPEDEF
|
||||
// INTERNAL VARIABLE
|
||||
//--------------------------------------------------------------------+
|
||||
usbd_device_info_t usbd_devices[CONTROLLER_DEVICE_NUMBER];
|
||||
CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN uint8_t usbd_enum_buffer[CFG_TUD_ENUM_BUFFER_SIZE];
|
||||
|
||||
tud_desc_init_t _usbd_descs[CONTROLLER_DEVICE_NUMBER];
|
||||
usbd_device_info_t usbd_devices[CONTROLLER_DEVICE_NUMBER];
|
||||
|
||||
|
||||
|
||||
static usbd_class_driver_t const usbd_class_drivers[] =
|
||||
{
|
||||
#if DEVICE_CLASS_HID
|
||||
@ -130,19 +133,26 @@ enum { USBD_CLASS_DRIVER_COUNT = sizeof(usbd_class_drivers) / sizeof(usbd_class_
|
||||
//};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
// INTERNAL FUNCTION
|
||||
//--------------------------------------------------------------------+
|
||||
static tusb_error_t proc_set_config_req(uint8_t rhport, uint8_t config_number);
|
||||
static uint16_t get_descriptor(uint8_t rhport, tusb_control_request_t const * const p_request, uint8_t const ** pp_buffer);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION INTERFACE
|
||||
// APPLICATION API
|
||||
//--------------------------------------------------------------------+
|
||||
bool tud_n_mounted(uint8_t rhport)
|
||||
{
|
||||
return usbd_devices[rhport].state == TUSB_DEVICE_STATE_CONFIGURED;
|
||||
return usbd_devices[rhport].state == TUSB_DEVICE_STATE_CONFIGURED;
|
||||
}
|
||||
|
||||
bool tud_n_set_descriptors(uint8_t rhport, tud_desc_init_t const* desc_cfg)
|
||||
{
|
||||
_usbd_descs[rhport] = *desc_cfg;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// IMPLEMENTATION
|
||||
//--------------------------------------------------------------------+
|
||||
@ -213,8 +223,8 @@ tusb_error_t usbd_init (void)
|
||||
|
||||
osal_task_create(usbd_task, "usbd", CFG_TUD_TASK_STACKSIZE, NULL, CFG_TUD_TASK_PRIO);
|
||||
|
||||
//------------- Descriptor Check -------------//
|
||||
TU_ASSERT(tusbd_descriptor_pointers.p_device != NULL && tusbd_descriptor_pointers.p_configuration != NULL, TUSB_ERROR_DESCRIPTOR_CORRUPTED);
|
||||
//------------- Core init -------------//
|
||||
arrclr_( _usbd_descs );
|
||||
|
||||
//------------- class init -------------//
|
||||
for (uint8_t class_code = TUSB_CLASS_AUDIO; class_code < USBD_CLASS_DRIVER_COUNT; class_code++)
|
||||
@ -390,12 +400,14 @@ static tusb_error_t proc_set_config_req(uint8_t rhport, uint8_t config_number)
|
||||
usbd_devices[rhport].config_num = config_number;
|
||||
|
||||
//------------- parse configuration & open drivers -------------//
|
||||
uint8_t const * p_desc_config = tusbd_descriptor_pointers.p_configuration;
|
||||
uint8_t const * p_desc_config = _usbd_descs[rhport].configuration;
|
||||
TU_ASSERT(p_desc_config != NULL, TUSB_ERROR_DESCRIPTOR_CORRUPTED);
|
||||
|
||||
uint8_t const * p_desc = p_desc_config + sizeof(tusb_desc_configuration_t);
|
||||
|
||||
uint16_t const config_total_length = ((tusb_desc_configuration_t*)p_desc_config)->wTotalLength;
|
||||
uint16_t const config_len = ((tusb_desc_configuration_t*)p_desc_config)->wTotalLength;
|
||||
|
||||
while( p_desc < p_desc_config + config_total_length )
|
||||
while( p_desc < p_desc_config + config_len )
|
||||
{
|
||||
if ( TUSB_DESC_INTERFACE_ASSOCIATION == p_desc[DESCRIPTOR_OFFSET_TYPE])
|
||||
{
|
||||
@ -436,23 +448,26 @@ static uint16_t get_descriptor(uint8_t rhport, tusb_control_request_t const * co
|
||||
uint8_t const * desc_data = NULL ;
|
||||
uint16_t len = 0;
|
||||
|
||||
//------------- Descriptor Check -------------//
|
||||
tud_desc_init_t const* descs = &_usbd_descs[rhport];
|
||||
|
||||
switch(desc_type)
|
||||
{
|
||||
case TUSB_DESC_DEVICE:
|
||||
desc_data = tusbd_descriptor_pointers.p_device;
|
||||
desc_data = descs->device;
|
||||
len = sizeof(tusb_desc_device_t);
|
||||
break;
|
||||
|
||||
case TUSB_DESC_CONFIGURATION:
|
||||
desc_data = tusbd_descriptor_pointers.p_configuration;
|
||||
len = ((tusb_desc_configuration_t*)tusbd_descriptor_pointers.p_configuration)->wTotalLength;
|
||||
desc_data = descs->configuration;
|
||||
len = ((tusb_desc_configuration_t*)descs->configuration)->wTotalLength;
|
||||
break;
|
||||
|
||||
case TUSB_DESC_STRING:
|
||||
// windows sometimes ask for string at index 238 !!!
|
||||
if ( !(desc_index < 100) ) return 0;
|
||||
|
||||
desc_data = tusbd_descriptor_pointers.p_string_arr[desc_index];
|
||||
desc_data = descs->string_arr[desc_index];
|
||||
VERIFY( desc_data != NULL, 0 );
|
||||
|
||||
len = desc_data[0]; // first byte of descriptor is its size
|
||||
@ -467,10 +482,13 @@ static uint16_t get_descriptor(uint8_t rhport, tusb_control_request_t const * co
|
||||
default: return 0;
|
||||
}
|
||||
|
||||
TU_ASSERT( desc_data != NULL, 0);
|
||||
|
||||
// up to Host's length
|
||||
len = min16_of(p_request->wLength, len );
|
||||
TU_ASSERT( len <= CFG_TUD_ENUM_BUFFER_SIZE, 0);
|
||||
|
||||
// FIXME copy data to enum buffer
|
||||
memcpy(usbd_enum_buffer, desc_data, len);
|
||||
(*pp_buffer) = usbd_enum_buffer;
|
||||
|
||||
|
@ -59,29 +59,38 @@
|
||||
|
||||
/// \brief Descriptor pointer collector to all the needed.
|
||||
typedef struct {
|
||||
uint8_t const * p_device; ///< pointer to device descritpor \ref tusb_desc_device_t
|
||||
uint8_t const * p_configuration; ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
|
||||
uint8_t const** p_string_arr; ///< a array of pointers to string descriptors
|
||||
uint8_t const * device; ///< pointer to device descriptor \ref tusb_desc_device_t
|
||||
uint8_t const * configuration; ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
|
||||
uint8_t const** string_arr; ///< a array of pointers to string descriptors
|
||||
|
||||
uint8_t const * p_hid_keyboard_report; ///< pointer to HID report descriptor of Keybaord interface. Only needed if CFG_TUD_HID_KEYBOARD is enabled
|
||||
uint8_t const * p_hid_keyboard_report; ///< pointer to HID report descriptor of Keyboard interface. Only needed if CFG_TUD_HID_KEYBOARD is enabled
|
||||
uint8_t const * p_hid_mouse_report; ///< pointer to HID report descriptor of Mouse interface. Only needed if CFG_TUD_HID_MOUSE is enabled
|
||||
}tusbd_descriptor_pointer_t;
|
||||
|
||||
// define by application
|
||||
extern tusbd_descriptor_pointer_t tusbd_descriptor_pointers;
|
||||
}tud_desc_init_t;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API
|
||||
// APPLICATION API (Multiple Root Ports)
|
||||
// Should be used only with MCU that support more than 1 ports
|
||||
//--------------------------------------------------------------------+
|
||||
bool tud_n_mounted(uint8_t rhport);
|
||||
bool tud_n_set_descriptors(uint8_t rhport, tud_desc_init_t const* desc_cfg);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION API (Single Port)
|
||||
// Should be used with MCU supporting only 1 USB port for code simplicity
|
||||
//--------------------------------------------------------------------+
|
||||
static inline bool tud_mounted(void)
|
||||
{
|
||||
return tud_n_mounted(0);
|
||||
}
|
||||
|
||||
static inline bool tud_set_descriptors(tud_desc_init_t const* desc_cfg)
|
||||
{
|
||||
return tud_n_set_descriptors(0, desc_cfg);
|
||||
}
|
||||
|
||||
/*------------- Callback -------------*/
|
||||
//--------------------------------------------------------------------+
|
||||
// APPLICATION CALLBACK
|
||||
//--------------------------------------------------------------------+
|
||||
/** \brief Callback function that will be invoked device is mounted (configured) by USB host
|
||||
* \param[in] rhport USB Controller ID of the interface
|
||||
* \note This callback should be used by Application to \b set-up application data
|
||||
|
@ -103,7 +103,7 @@ void bus_reset(void)
|
||||
NRF_USBD->TASKS_STARTISOIN = 0;
|
||||
NRF_USBD->TASKS_STARTISOOUT = 0;
|
||||
|
||||
varclr(&_dcd);
|
||||
varclr_(&_dcd);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
|
@ -113,6 +113,8 @@ tusb_error_t tusb_init(void);
|
||||
your_init_code();
|
||||
tusb_init();
|
||||
|
||||
// other config code
|
||||
|
||||
while(1) // the mainloop
|
||||
{
|
||||
your_application_code();
|
||||
|
Loading…
x
Reference in New Issue
Block a user