Merge branch 'master' into add-stm-hs
This commit is contained in:
@@ -88,13 +88,13 @@ typedef struct
|
||||
char const* name;
|
||||
#endif
|
||||
|
||||
void (* init ) (void);
|
||||
void (* reset ) (uint8_t rhport);
|
||||
bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length);
|
||||
bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request);
|
||||
bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request);
|
||||
bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
void (* sof ) (uint8_t rhport); /* optional */
|
||||
void (* init ) (void);
|
||||
void (* reset ) (uint8_t rhport);
|
||||
uint16_t (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t max_len);
|
||||
bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request);
|
||||
bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request);
|
||||
bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
|
||||
void (* sof ) (uint8_t rhport); /* optional */
|
||||
} usbd_class_driver_t;
|
||||
|
||||
static usbd_class_driver_t const _usbd_driver[] =
|
||||
@@ -202,6 +202,19 @@ static usbd_class_driver_t const _usbd_driver[] =
|
||||
.sof = NULL,
|
||||
},
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_BTH
|
||||
{
|
||||
DRIVER_NAME("BTH")
|
||||
.init = btd_init,
|
||||
.reset = btd_reset,
|
||||
.open = btd_open,
|
||||
.control_request = btd_control_request,
|
||||
.control_complete = btd_control_complete,
|
||||
.xfer_cb = btd_xfer_cb,
|
||||
.sof = NULL
|
||||
},
|
||||
#endif
|
||||
};
|
||||
|
||||
enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) };
|
||||
@@ -432,7 +445,7 @@ void tud_task (void)
|
||||
|
||||
if ( 0 == epnum )
|
||||
{
|
||||
usbd_control_xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
|
||||
usbd_control_xfer_cb(event.rhport, ep_addr, (xfer_result_t)event.xfer_complete.result, event.xfer_complete.len);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -440,7 +453,7 @@ void tud_task (void)
|
||||
TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,);
|
||||
|
||||
TU_LOG2(" %s xfer callback\r\n", _usbd_driver[drv_id].name);
|
||||
_usbd_driver[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
|
||||
_usbd_driver[drv_id].xfer_cb(event.rhport, ep_addr, (xfer_result_t)event.xfer_complete.result, event.xfer_complete.len);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -519,6 +532,18 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
|
||||
{
|
||||
//------------- Device Requests e.g in enumeration -------------//
|
||||
case TUSB_REQ_RCPT_DEVICE:
|
||||
if ( TUSB_REQ_TYPE_CLASS == p_request->bmRequestType_bit.type )
|
||||
{
|
||||
uint8_t const itf = tu_u16_low(p_request->wIndex);
|
||||
TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv));
|
||||
|
||||
uint8_t const drvid = _usbd_dev.itf2drv[itf];
|
||||
TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT);
|
||||
|
||||
// forward to class driver: "non-STD request to Interface"
|
||||
TU_VERIFY(invoke_class_control(rhport, drvid, p_request));
|
||||
return true;
|
||||
}
|
||||
if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type )
|
||||
{
|
||||
// Non standard request is not supported
|
||||
@@ -725,6 +750,8 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
|
||||
TU_ASSERT( TUSB_DESC_INTERFACE == tu_desc_type(p_desc) );
|
||||
|
||||
tusb_desc_interface_t const * desc_itf = (tusb_desc_interface_t const*) p_desc;
|
||||
uint16_t const remaining_len = desc_end-p_desc;
|
||||
|
||||
uint8_t drv_id;
|
||||
uint16_t drv_len;
|
||||
|
||||
@@ -732,23 +759,25 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
|
||||
{
|
||||
usbd_class_driver_t const *driver = &_usbd_driver[drv_id];
|
||||
|
||||
drv_len = 0;
|
||||
if ( driver->open(rhport, desc_itf, &drv_len) )
|
||||
drv_len = driver->open(rhport, desc_itf, remaining_len);
|
||||
|
||||
if ( drv_len > 0 )
|
||||
{
|
||||
// Open successfully, check if length is correct
|
||||
TU_ASSERT( sizeof(tusb_desc_interface_t) <= drv_len && drv_len <= remaining_len);
|
||||
|
||||
// Interface number must not be used already
|
||||
TU_ASSERT( DRVID_INVALID == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] );
|
||||
|
||||
TU_LOG2(" %s opened\r\n", _usbd_driver[drv_id].name);
|
||||
TU_LOG2(" %s opened\r\n", driver->name);
|
||||
_usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id;
|
||||
|
||||
// If IAD exist, assign all interfaces to the same driver
|
||||
if (desc_itf_assoc)
|
||||
{
|
||||
// IAD's first interface number and class/subclass/protocol should match with opened interface
|
||||
TU_ASSERT(desc_itf_assoc->bFirstInterface == desc_itf->bInterfaceNumber &&
|
||||
desc_itf_assoc->bFunctionClass == desc_itf->bInterfaceClass &&
|
||||
desc_itf_assoc->bFunctionSubClass == desc_itf->bInterfaceSubClass &&
|
||||
desc_itf_assoc->bFunctionProtocol == desc_itf->bInterfaceProtocol);
|
||||
// IAD's first interface number and class should match with opened interface
|
||||
TU_ASSERT(desc_itf_assoc->bFirstInterface == desc_itf->bInterfaceNumber &&
|
||||
desc_itf_assoc->bFunctionClass == desc_itf->bInterfaceClass);
|
||||
|
||||
for(uint8_t i=1; i<desc_itf_assoc->bInterfaceCount; i++)
|
||||
{
|
||||
@@ -760,8 +789,8 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
|
||||
}
|
||||
}
|
||||
|
||||
// Assert if cannot find supported driver
|
||||
TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT && drv_len >= sizeof(tusb_desc_interface_t) );
|
||||
// Failed if cannot find supported driver
|
||||
TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT);
|
||||
|
||||
mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, drv_len, drv_id); // TODO refactor
|
||||
|
||||
@@ -829,8 +858,10 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
|
||||
if (!tud_descriptor_bos_cb) return false;
|
||||
|
||||
tusb_desc_bos_t const* desc_bos = (tusb_desc_bos_t const*) tud_descriptor_bos_cb();
|
||||
|
||||
uint16_t total_len;
|
||||
memcpy(&total_len, &desc_bos->wTotalLength, 2); // possibly mis-aligned memory
|
||||
// Use offsetof to avoid pointer to the odd/misaligned address
|
||||
memcpy(&total_len, (uint8_t*) desc_bos + offsetof(tusb_desc_bos_t, wTotalLength), 2);
|
||||
|
||||
return tud_control_xfer(rhport, p_request, (void*) desc_bos, total_len);
|
||||
}
|
||||
@@ -844,7 +875,8 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
|
||||
TU_ASSERT(desc_config);
|
||||
|
||||
uint16_t total_len;
|
||||
memcpy(&total_len, &desc_config->wTotalLength, 2); // possibly mis-aligned memory
|
||||
// Use offsetof to avoid pointer to the odd/misaligned address
|
||||
memcpy(&total_len, (uint8_t*) desc_config + offsetof(tusb_desc_configuration_t, wTotalLength), 2);
|
||||
|
||||
return tud_control_xfer(rhport, p_request, (void*) desc_config, total_len);
|
||||
}
|
||||
@@ -873,15 +905,31 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
|
||||
case TUSB_DESC_DEVICE_QUALIFIER:
|
||||
TU_LOG2(" Device Qualifier\r\n");
|
||||
|
||||
// TODO If not highspeed capable stall this request otherwise
|
||||
// return the descriptor that could work in highspeed
|
||||
return false;
|
||||
// Host sends this request to ask why our device with USB BCD from 2.0
|
||||
// but is running at Full/Low Speed. If not highspeed capable stall this request,
|
||||
// otherwise return the descriptor that could work in highspeed mode
|
||||
if ( tud_descriptor_device_qualifier_cb )
|
||||
{
|
||||
uint8_t const* desc_qualifier = tud_descriptor_device_qualifier_cb();
|
||||
TU_ASSERT(desc_qualifier);
|
||||
|
||||
// first byte of descriptor is its size
|
||||
return tud_control_xfer(rhport, p_request, (void*) desc_qualifier, desc_qualifier[0]);
|
||||
}else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case TUSB_DESC_OTHER_SPEED_CONFIG:
|
||||
TU_LOG2(" Other Speed Configuration\r\n");
|
||||
|
||||
// After Device Qualifier descriptor is received host will ask for this descriptor
|
||||
return false; // not supported
|
||||
break;
|
||||
|
||||
default: return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
@@ -68,6 +68,8 @@ static inline bool tud_ready(void)
|
||||
// Remote wake up host, only if suspended and enabled by host
|
||||
bool tud_remote_wakeup(void);
|
||||
|
||||
// Enable pull-up resistor on D+ D-
|
||||
// Return false on unsupported MCUs
|
||||
static inline bool tud_disconnect(void)
|
||||
{
|
||||
TU_VERIFY(dcd_disconnect);
|
||||
@@ -75,6 +77,8 @@ static inline bool tud_disconnect(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
// Disable pull-up resistor on D+ D-
|
||||
// Return false on unsupported MCUs
|
||||
static inline bool tud_connect(void)
|
||||
{
|
||||
TU_VERIFY(dcd_connect);
|
||||
@@ -110,6 +114,10 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index);
|
||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid);
|
||||
|
||||
// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
|
||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||
TU_ATTR_WEAK uint8_t const* tud_descriptor_device_qualifier_cb(void);
|
||||
|
||||
// Invoked when device is mounted (configured)
|
||||
TU_ATTR_WEAK void tud_mount_cb(void);
|
||||
|
||||
@@ -125,6 +133,8 @@ TU_ATTR_WEAK void tud_resume_cb(void);
|
||||
|
||||
// Invoked when received control request with VENDOR TYPE
|
||||
TU_ATTR_WEAK bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const * request);
|
||||
|
||||
// Invoked when vendor control request is complete
|
||||
TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request);
|
||||
|
||||
|
||||
@@ -438,6 +448,60 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re
|
||||
/* Endpoint Out */\
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
|
||||
|
||||
//------------- BT Radio -------------//
|
||||
#define TUD_BT_APP_CLASS (TUSB_CLASS_WIRELESS_CONTROLLER)
|
||||
#define TUD_BT_APP_SUBCLASS 0x01
|
||||
#define TUD_BT_PROTOCOL_PRIMARY_CONTROLLER 0x01
|
||||
#define TUD_BT_PROTOCOL_AMP_CONTROLLER 0x02
|
||||
|
||||
#ifndef CFG_TUD_BTH_ISO_ALT_COUNT
|
||||
#define CFG_TUD_BTH_ISO_ALT_COUNT 0
|
||||
#endif
|
||||
|
||||
// Length of template descriptor: 30 bytes + number of ISO alternatives * 23
|
||||
#define TUD_BTH_DESC_LEN (9 + 7 + 7 + 7 + (CFG_TUD_BTH_ISO_ALT_COUNT) * (9 + 7 + 7))
|
||||
|
||||
/* Primary Interface */
|
||||
#define TUD_BTH_PRI_ITF(_itfnum, _stridx, _ep_evt, _ep_evt_size, _ep_evt_interval, _ep_in, _ep_out, _ep_size) \
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, _stridx, 3, TUD_BT_APP_CLASS, TUD_BT_APP_SUBCLASS, TUD_BT_PROTOCOL_PRIMARY_CONTROLLER, 0, \
|
||||
/* Endpoint In for events */ \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_evt, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_evt_size), _ep_evt_interval, \
|
||||
/* Endpoint In for ACL data */ \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_in, TUSB_XFER_BULK, U16_TO_U8S_LE(_ep_size), 1, \
|
||||
/* Endpoint Out for ACL data */ \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_out, TUSB_XFER_BULK, U16_TO_U8S_LE(_ep_size), 1
|
||||
|
||||
#define TUD_BTH_ISO_ITF(_itfnum, _alt, _ep_in, _ep_out, _n) ,\
|
||||
/* Interface with 2 endpoints */ \
|
||||
9, TUSB_DESC_INTERFACE, _itfnum, _alt, 2, TUD_BT_APP_CLASS, TUD_BT_APP_SUBCLASS, TUD_BT_PROTOCOL_PRIMARY_CONTROLLER, 0, \
|
||||
/* Isochronous endpoints */ \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_in, TUSB_XFER_ISOCHRONOUS, U16_TO_U8S_LE(_n), 1, \
|
||||
7, TUSB_DESC_ENDPOINT, _ep_out, TUSB_XFER_ISOCHRONOUS, U16_TO_U8S_LE(_n), 1
|
||||
|
||||
#define _FIRST(a, ...) a
|
||||
#define _REST(a, ...) __VA_ARGS__
|
||||
|
||||
#define TUD_BTH_ISO_ITF_0(_itfnum, ...)
|
||||
#define TUD_BTH_ISO_ITF_1(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 1, _ep_in, _ep_out, _FIRST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_2(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 2, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_1(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_3(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 3, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_2(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_4(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 4, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_3(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_5(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 5, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_4(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
#define TUD_BTH_ISO_ITF_6(_itfnum, _ep_in, _ep_out, ...) TUD_BTH_ISO_ITF(_itfnum, (CFG_TUD_BTH_ISO_ALT_COUNT) - 6, _ep_in, _ep_out, _FIRST(__VA_ARGS__)) \
|
||||
TUD_BTH_ISO_ITF_5(_itfnum, _ep_in, _ep_out, _REST(__VA_ARGS__))
|
||||
|
||||
#define TUD_BTH_ISO_ITFS(_itfnum, _ep_in, _ep_out, ...) \
|
||||
TU_XSTRCAT(TUD_BTH_ISO_ITF_, CFG_TUD_BTH_ISO_ALT_COUNT)(_itfnum, _ep_in, _ep_out, __VA_ARGS__)
|
||||
|
||||
// BT Primary controller descriptor
|
||||
// Interface number, string index, attributes, event endpoint, event endpoint size, interval, data in, data out, data endpoint size, iso endpoint sizes
|
||||
#define TUD_BTH_DESCRIPTOR(_itfnum, _stridx, _ep_evt, _ep_evt_size, _ep_evt_interval, _ep_in, _ep_out, _ep_size,...) \
|
||||
TUD_BTH_PRI_ITF(_itfnum, _stridx, _ep_evt, _ep_evt_size, _ep_evt_interval, _ep_in, _ep_out, _ep_size) \
|
||||
TUD_BTH_ISO_ITFS(_itfnum + 1, _ep_in + 1, _ep_out + 1, __VA_ARGS__)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user