usb0 host on mcb1800 work with fullspeed mode.

use usbh_edpt_open() to correctly map ep2drv[]
This commit is contained in:
hathach
2020-05-19 00:55:43 +07:00
parent 905a80d1b2
commit 58cedf4c06
18 changed files with 115 additions and 66 deletions

View File

@@ -116,7 +116,6 @@ CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED(4) static uint8_t _usbh_ctrl_buf[CFG_TUSB_H
//------------- Helper Function Prototypes -------------//
static inline uint8_t get_new_address(void);
static inline uint8_t get_configure_number_for_device(tusb_desc_device_t* dev_desc);
static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
//--------------------------------------------------------------------+
// PUBLIC API (Parameter Verification is required)
@@ -225,6 +224,30 @@ tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size)
return TUSB_ERROR_NONE;
}
bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc)
{
bool ret = hcd_edpt_open(rhport, dev_addr, ep_desc);
if (ret)
{
usbh_device_t* dev = &_usbh_devices[dev_addr];
// new endpoints belongs to latest interface (last valid value)
uint8_t drvid = 0xff;
for(uint8_t i=0; i < sizeof(dev->itf2drv); i++)
{
if ( dev->itf2drv[i] == 0xff ) break;
drvid = dev->itf2drv[i];
}
TU_ASSERT(drvid < USBH_CLASS_DRIVER_COUNT);
uint8_t const ep_addr = ep_desc->bEndpointAddress;
dev->ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)] = drvid;
}
return ret;
}
//--------------------------------------------------------------------+
// USBH-HCD ISR/Callback API
//--------------------------------------------------------------------+
@@ -358,6 +381,8 @@ bool enum_task(hcd_event_t* event)
{
if( hcd_port_connect_status(dev0->rhport) )
{
TU_LOG2("Connect \r\n");
// connection event
osal_task_delay(POWER_STABLE_DELAY); // wait until device is stable. Increase this if the first 8 bytes is failed to get
@@ -371,6 +396,8 @@ bool enum_task(hcd_event_t* event)
}
else
{
TU_LOG2("Disconnect \r\n");
// disconnection event
usbh_device_unplugged(dev0->rhport, 0, 0);
return true; // restart task
@@ -424,6 +451,7 @@ bool enum_task(hcd_event_t* event)
TU_ASSERT_ERR( usbh_pipe_control_open(0, 8) );
//------------- Get first 8 bytes of device descriptor to get Control Endpoint Size -------------//
TU_LOG2("Get 8 byte of Device Descriptor \r\n");
request = (tusb_control_request_t ) {
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN },
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
@@ -434,12 +462,16 @@ bool enum_task(hcd_event_t* event)
bool is_ok = usbh_control_xfer(0, &request, _usbh_ctrl_buf);
//------------- Reset device again before Set Address -------------//
TU_LOG2("Port reset \r\n");
if (dev0->hub_addr == 0)
{
// connected directly to roothub
TU_ASSERT(is_ok); // TODO some slow device is observed to fail the very fist controller xfer, can try more times
hcd_port_reset( dev0->rhport ); // reset port after 8 byte descriptor
osal_task_delay(RESET_DELAY);
// hcd_port_reset_end(dev0->rhport);
// osal_task_delay(RESET_DELAY);
}
#if CFG_TUH_HUB
else
@@ -458,6 +490,7 @@ bool enum_task(hcd_event_t* event)
#endif
//------------- Set new address -------------//
TU_LOG2("Set Address \r\n");
uint8_t const new_addr = get_new_address();
TU_ASSERT(new_addr <= CFG_TUSB_HOST_DEVICE_MAX); // TODO notify application we reach max devices
@@ -484,6 +517,7 @@ bool enum_task(hcd_event_t* event)
TU_ASSERT_ERR ( usbh_pipe_control_open(new_addr, ((tusb_desc_device_t*) _usbh_ctrl_buf)->bMaxPacketSize0 ) );
//------------- Get full device descriptor -------------//
TU_LOG2("Get Device Descriptor \r\n");
request = (tusb_control_request_t ) {
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN },
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
@@ -502,6 +536,7 @@ bool enum_task(hcd_event_t* event)
TU_ASSERT(configure_selected <= new_dev->configure_count); // TODO notify application when invalid configuration
//------------- Get 9 bytes of configuration descriptor -------------//
TU_LOG2("Get 9 bytes of Configuration Descriptor \r\n");
request = (tusb_control_request_t ) {
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN },
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
@@ -515,6 +550,7 @@ bool enum_task(hcd_event_t* event)
TU_ASSERT( CFG_TUSB_HOST_ENUM_BUFFER_SIZE >= ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength );
//------------- Get full configuration descriptor -------------//
TU_LOG2("Get full Configuration Descriptor \r\n");
request.wLength = ((tusb_desc_configuration_t*)_usbh_ctrl_buf)->wTotalLength; // full length
TU_ASSERT( usbh_control_xfer( new_addr, &request, _usbh_ctrl_buf ) );
@@ -522,6 +558,7 @@ bool enum_task(hcd_event_t* event)
new_dev->interface_count = ((tusb_desc_configuration_t*) _usbh_ctrl_buf)->bNumInterfaces;
//------------- Set Configure -------------//
TU_LOG2("Set Configuration Descriptor \r\n");
request = (tusb_control_request_t ) {
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_OUT },
.bRequest = TUSB_REQ_SET_CONFIGURATION,
@@ -577,11 +614,7 @@ bool enum_task(hcd_event_t* event)
{
uint16_t itf_len = 0;
if ( usbh_class_drivers[drv_id].open(new_dev->rhport, new_addr, desc_itf, &itf_len) )
{
mark_interface_endpoint(new_dev->ep2drv, p_desc, itf_len, drv_id);
}
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) );
p_desc += itf_len;
}
@@ -597,7 +630,7 @@ bool enum_task(hcd_event_t* event)
/* USB Host Driver task
* This top level thread manages all host controller event and delegates events to class-specific drivers.
* This should be called periodically within the mainloop or rtos thread.
*
*_usbh_devices[dev_addr].
@code
int main(void)
{
@@ -661,25 +694,4 @@ static inline uint8_t get_configure_number_for_device(tusb_desc_device_t* dev_de
return config_num;
}
// Helper marking endpoint of interface belongs to class driver
// TODO merge with usbd
static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id)
{
uint16_t len = 0;
while( len < desc_len )
{
if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
{
uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress;
ep2drv[ tu_edpt_number(ep_addr) ][ tu_edpt_dir(ep_addr) ] = driver_id;
}
len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
}
}
#endif