|
|
@ -42,10 +42,17 @@
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// MACRO CONSTANT TYPEDEF
|
|
|
|
// MACRO CONSTANT TYPEDEF
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
#if CFG_TUSB_DEBUG >= 2
|
|
|
|
|
|
|
|
#define DRIVER_NAME(_name) .name = _name,
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define DRIVER_NAME(_name)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static host_class_driver_t const usbh_class_drivers[] =
|
|
|
|
static host_class_driver_t const usbh_class_drivers[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#if CFG_TUH_CDC
|
|
|
|
#if CFG_TUH_CDC
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
DRIVER_NAME("CDC")
|
|
|
|
.class_code = TUSB_CLASS_CDC,
|
|
|
|
.class_code = TUSB_CLASS_CDC,
|
|
|
|
.init = cdch_init,
|
|
|
|
.init = cdch_init,
|
|
|
|
.open = cdch_open,
|
|
|
|
.open = cdch_open,
|
|
|
@ -56,6 +63,7 @@ static host_class_driver_t const usbh_class_drivers[] =
|
|
|
|
|
|
|
|
|
|
|
|
#if CFG_TUH_MSC
|
|
|
|
#if CFG_TUH_MSC
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
DRIVER_NAME("MSC")
|
|
|
|
.class_code = TUSB_CLASS_MSC,
|
|
|
|
.class_code = TUSB_CLASS_MSC,
|
|
|
|
.init = msch_init,
|
|
|
|
.init = msch_init,
|
|
|
|
.open = msch_open,
|
|
|
|
.open = msch_open,
|
|
|
@ -66,6 +74,7 @@ static host_class_driver_t const usbh_class_drivers[] =
|
|
|
|
|
|
|
|
|
|
|
|
#if HOST_CLASS_HID
|
|
|
|
#if HOST_CLASS_HID
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
DRIVER_NAME("HID")
|
|
|
|
.class_code = TUSB_CLASS_HID,
|
|
|
|
.class_code = TUSB_CLASS_HID,
|
|
|
|
.init = hidh_init,
|
|
|
|
.init = hidh_init,
|
|
|
|
.open = hidh_open_subtask,
|
|
|
|
.open = hidh_open_subtask,
|
|
|
@ -76,6 +85,7 @@ static host_class_driver_t const usbh_class_drivers[] =
|
|
|
|
|
|
|
|
|
|
|
|
#if CFG_TUH_HUB
|
|
|
|
#if CFG_TUH_HUB
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
DRIVER_NAME("HUB")
|
|
|
|
.class_code = TUSB_CLASS_HUB,
|
|
|
|
.class_code = TUSB_CLASS_HUB,
|
|
|
|
.init = hub_init,
|
|
|
|
.init = hub_init,
|
|
|
|
.open = hub_open,
|
|
|
|
.open = hub_open,
|
|
|
@ -86,6 +96,7 @@ static host_class_driver_t const usbh_class_drivers[] =
|
|
|
|
|
|
|
|
|
|
|
|
#if CFG_TUH_VENDOR
|
|
|
|
#if CFG_TUH_VENDOR
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
DRIVER_NAME("VENDOR")
|
|
|
|
.class_code = TUSB_CLASS_VENDOR_SPECIFIC,
|
|
|
|
.class_code = TUSB_CLASS_VENDOR_SPECIFIC,
|
|
|
|
.init = cush_init,
|
|
|
|
.init = cush_init,
|
|
|
|
.open = cush_open_subtask,
|
|
|
|
.open = cush_open_subtask,
|
|
|
@ -131,7 +142,7 @@ static inline void osal_task_delay(uint32_t msec)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
(void) msec;
|
|
|
|
(void) msec;
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t start = hcd_frame_number(TUH_OPT_RHPORT);
|
|
|
|
const uint32_t start = hcd_frame_number(TUH_OPT_RHPORT);
|
|
|
|
while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {}
|
|
|
|
while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -162,7 +173,11 @@ bool usbh_init(void)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Class drivers init
|
|
|
|
// Class drivers init
|
|
|
|
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++) usbh_class_drivers[drv_id].init();
|
|
|
|
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
TU_LOG2("%s init\r\n", usbh_class_drivers[drv_id].name);
|
|
|
|
|
|
|
|
usbh_class_drivers[drv_id].init();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TU_ASSERT(hcd_init());
|
|
|
|
TU_ASSERT(hcd_init());
|
|
|
|
hcd_int_enable(TUH_OPT_RHPORT);
|
|
|
|
hcd_int_enable(TUH_OPT_RHPORT);
|
|
|
@ -269,6 +284,7 @@ void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t ev
|
|
|
|
|
|
|
|
|
|
|
|
if (usbh_class_drivers[drv_id].isr)
|
|
|
|
if (usbh_class_drivers[drv_id].isr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
TU_LOG2("%s isr\r\n", usbh_class_drivers[drv_id].name);
|
|
|
|
usbh_class_drivers[drv_id].isr(dev_addr, ep_addr, event, xferred_bytes);
|
|
|
|
usbh_class_drivers[drv_id].isr(dev_addr, ep_addr, event, xferred_bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
@ -336,7 +352,11 @@ static void usbh_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t hub_
|
|
|
|
if (tuh_umount_cb) tuh_umount_cb(dev_addr);
|
|
|
|
if (tuh_umount_cb) tuh_umount_cb(dev_addr);
|
|
|
|
|
|
|
|
|
|
|
|
// Close class driver
|
|
|
|
// Close class driver
|
|
|
|
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++) usbh_class_drivers[drv_id].close(dev_addr);
|
|
|
|
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
TU_LOG2("%s close\r\n", usbh_class_drivers[drv_id].name);
|
|
|
|
|
|
|
|
usbh_class_drivers[drv_id].close(dev_addr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
memset(dev->itf2drv, 0xff, sizeof(dev->itf2drv)); // invalid mapping
|
|
|
|
memset(dev->itf2drv, 0xff, sizeof(dev->itf2drv)); // invalid mapping
|
|
|
|
memset(dev->ep2drv , 0xff, sizeof(dev->ep2drv )); // invalid mapping
|
|
|
|
memset(dev->ep2drv , 0xff, sizeof(dev->ep2drv )); // invalid mapping
|
|
|
@ -381,7 +401,7 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if( hcd_port_connect_status(dev0->rhport) )
|
|
|
|
if( hcd_port_connect_status(dev0->rhport) )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TU_LOG2("Connect \r\n");
|
|
|
|
TU_LOG2("Device connect \r\n");
|
|
|
|
|
|
|
|
|
|
|
|
// connection event
|
|
|
|
// connection event
|
|
|
|
osal_task_delay(POWER_STABLE_DELAY); // wait until device is stable. Increase this if the first 8 bytes is failed to get
|
|
|
|
osal_task_delay(POWER_STABLE_DELAY); // wait until device is stable. Increase this if the first 8 bytes is failed to get
|
|
|
@ -396,7 +416,7 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TU_LOG2("Disconnect \r\n");
|
|
|
|
TU_LOG2("Device disconnect \r\n");
|
|
|
|
|
|
|
|
|
|
|
|
// disconnection event
|
|
|
|
// disconnection event
|
|
|
|
usbh_device_unplugged(dev0->rhport, 0, 0);
|
|
|
|
usbh_device_unplugged(dev0->rhport, 0, 0);
|
|
|
@ -451,7 +471,7 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
TU_ASSERT_ERR( usbh_pipe_control_open(0, 8) );
|
|
|
|
TU_ASSERT_ERR( usbh_pipe_control_open(0, 8) );
|
|
|
|
|
|
|
|
|
|
|
|
//------------- Get first 8 bytes of device descriptor to get Control Endpoint Size -------------//
|
|
|
|
//------------- Get first 8 bytes of device descriptor to get Control Endpoint Size -------------//
|
|
|
|
TU_LOG2("Get 8 byte of Device Descriptor \r\n");
|
|
|
|
TU_LOG2("Get 8 byte of Device Descriptor\r\n");
|
|
|
|
request = (tusb_control_request_t ) {
|
|
|
|
request = (tusb_control_request_t ) {
|
|
|
|
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN },
|
|
|
|
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN },
|
|
|
|
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
|
|
|
|
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
|
|
|
@ -536,7 +556,7 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
TU_ASSERT(configure_selected <= new_dev->configure_count); // TODO notify application when invalid configuration
|
|
|
|
TU_ASSERT(configure_selected <= new_dev->configure_count); // TODO notify application when invalid configuration
|
|
|
|
|
|
|
|
|
|
|
|
//------------- Get 9 bytes of configuration descriptor -------------//
|
|
|
|
//------------- Get 9 bytes of configuration descriptor -------------//
|
|
|
|
TU_LOG2("Get 9 bytes of Configuration Descriptor \r\n");
|
|
|
|
TU_LOG2("Get 9 bytes of Configuration Descriptor\r\n");
|
|
|
|
request = (tusb_control_request_t ) {
|
|
|
|
request = (tusb_control_request_t ) {
|
|
|
|
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN },
|
|
|
|
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN },
|
|
|
|
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
|
|
|
|
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
|
|
|
@ -550,7 +570,7 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
TU_ASSERT( CFG_TUSB_HOST_ENUM_BUFFER_SIZE >= ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength );
|
|
|
|
TU_ASSERT( CFG_TUSB_HOST_ENUM_BUFFER_SIZE >= ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength );
|
|
|
|
|
|
|
|
|
|
|
|
//------------- Get full configuration descriptor -------------//
|
|
|
|
//------------- Get full configuration descriptor -------------//
|
|
|
|
TU_LOG2("Get full Configuration Descriptor \r\n");
|
|
|
|
TU_LOG2("Get full Configuration Descriptor\r\n");
|
|
|
|
request.wLength = ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength; // full length
|
|
|
|
request.wLength = ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength; // full length
|
|
|
|
TU_ASSERT( usbh_control_xfer( new_addr, &request, _usbh_ctrl_buf ) );
|
|
|
|
TU_ASSERT( usbh_control_xfer( new_addr, &request, _usbh_ctrl_buf ) );
|
|
|
|
|
|
|
|
|
|
|
@ -558,7 +578,7 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
new_dev->interface_count = ((tusb_desc_configuration_t*) _usbh_ctrl_buf)->bNumInterfaces;
|
|
|
|
new_dev->interface_count = ((tusb_desc_configuration_t*) _usbh_ctrl_buf)->bNumInterfaces;
|
|
|
|
|
|
|
|
|
|
|
|
//------------- Set Configure -------------//
|
|
|
|
//------------- Set Configure -------------//
|
|
|
|
TU_LOG2("Set Configuration Descriptor \r\n");
|
|
|
|
TU_LOG2("Set Configuration Descriptor\r\n");
|
|
|
|
request = (tusb_control_request_t ) {
|
|
|
|
request = (tusb_control_request_t ) {
|
|
|
|
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_OUT },
|
|
|
|
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_OUT },
|
|
|
|
.bRequest = TUSB_REQ_SET_CONFIGURATION,
|
|
|
|
.bRequest = TUSB_REQ_SET_CONFIGURATION,
|
|
|
@ -568,6 +588,7 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
TU_ASSERT(usbh_control_xfer( new_addr, &request, NULL ));
|
|
|
|
TU_ASSERT(usbh_control_xfer( new_addr, &request, NULL ));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TU_LOG2("Device configured\r\n");
|
|
|
|
new_dev->state = TUSB_DEVICE_STATE_CONFIGURED;
|
|
|
|
new_dev->state = TUSB_DEVICE_STATE_CONFIGURED;
|
|
|
|
|
|
|
|
|
|
|
|
//------------- TODO Get String Descriptors -------------//
|
|
|
|
//------------- TODO Get String Descriptors -------------//
|
|
|
@ -575,6 +596,8 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
//------------- parse configuration & install drivers -------------//
|
|
|
|
//------------- parse configuration & install drivers -------------//
|
|
|
|
uint8_t const* p_desc = _usbh_ctrl_buf + sizeof(tusb_desc_configuration_t);
|
|
|
|
uint8_t const* p_desc = _usbh_ctrl_buf + sizeof(tusb_desc_configuration_t);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TU_LOG2_MEM(_usbh_ctrl_buf, ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// parse each interfaces
|
|
|
|
// parse each interfaces
|
|
|
|
while( p_desc < _usbh_ctrl_buf + ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength )
|
|
|
|
while( p_desc < _usbh_ctrl_buf + ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength )
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -584,7 +607,7 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
p_desc = tu_desc_next(p_desc); // skip the descriptor, increase by the descriptor's length
|
|
|
|
p_desc = tu_desc_next(p_desc); // skip the descriptor, increase by the descriptor's length
|
|
|
|
}else
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tusb_desc_interface_t* desc_itf = (tusb_desc_interface_t*) p_desc;
|
|
|
|
tusb_desc_interface_t const* desc_itf = (tusb_desc_interface_t const*) p_desc;
|
|
|
|
|
|
|
|
|
|
|
|
// Check if class is supported
|
|
|
|
// Check if class is supported
|
|
|
|
uint8_t drv_id;
|
|
|
|
uint8_t drv_id;
|
|
|
@ -614,6 +637,7 @@ bool enum_task(hcd_event_t* event)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
uint16_t itf_len = 0;
|
|
|
|
uint16_t itf_len = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TU_LOG2("%s open\r\n", usbh_class_drivers[drv_id].name);
|
|
|
|
TU_ASSERT( usbh_class_drivers[drv_id].open(new_dev->rhport, new_addr, desc_itf, &itf_len) );
|
|
|
|
TU_ASSERT( usbh_class_drivers[drv_id].open(new_dev->rhport, new_addr, desc_itf, &itf_len) );
|
|
|
|
TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) );
|
|
|
|
TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) );
|
|
|
|
p_desc += itf_len;
|
|
|
|
p_desc += itf_len;
|
|
|
|