implement tuh_edpt_xfer() for non-control

This commit is contained in:
hathach 2022-03-18 22:22:21 +07:00
parent 9ae0304b1e
commit ba1185bf28
No known key found for this signature in database
GPG Key ID: 2FA891220FBFD581
5 changed files with 201 additions and 126 deletions

View File

@ -40,6 +40,8 @@
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void led_blinking_task(void); void led_blinking_task(void);
static void print_utf16(uint16_t *temp_buf, size_t buf_len);
/*------------- MAIN -------------*/ /*------------- MAIN -------------*/
int main(void) int main(void)
{ {
@ -65,10 +67,108 @@ int main(void)
// English // English
#define LANGUAGE_ID 0x0409 #define LANGUAGE_ID 0x0409
//uint8_t usb_buf[256] TU_ATTR_ALIGNED(4);
TU_ATTR_ALIGNED(4)
tusb_desc_device_t desc_device; tusb_desc_device_t desc_device;
//void parse_config_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg)
//{
// uint8_t const* desc_end = ((uint8_t const*) desc_cfg) + tu_le16toh(desc_cfg->wTotalLength);
// uint8_t const* p_desc = tu_desc_next(desc_cfg);
//}
void print_device_descriptor(tuh_xfer_t* xfer)
{
if ( XFER_RESULT_SUCCESS != xfer->result )
{
printf("Failed to get device descriptor\r\n");
return;
}
uint8_t const daddr = xfer->daddr;
printf("Device %u: ID %04x:%04x\r\n", daddr, desc_device.idVendor, desc_device.idProduct);
printf("Device Descriptor:\r\n");
printf(" bLength %u\r\n" , desc_device.bLength);
printf(" bDescriptorType %u\r\n" , desc_device.bDescriptorType);
printf(" bcdUSB %04x\r\n" , desc_device.bcdUSB);
printf(" bDeviceClass %u\r\n" , desc_device.bDeviceClass);
printf(" bDeviceSubClass %u\r\n" , desc_device.bDeviceSubClass);
printf(" bDeviceProtocol %u\r\n" , desc_device.bDeviceProtocol);
printf(" bMaxPacketSize0 %u\r\n" , desc_device.bMaxPacketSize0);
printf(" idVendor 0x%04x\r\n" , desc_device.idVendor);
printf(" idProduct 0x%04x\r\n" , desc_device.idProduct);
printf(" bcdDevice %04x\r\n" , desc_device.bcdDevice);
// Get String descriptor using Sync API
uint16_t temp_buf[128];
printf(" iManufacturer %u " , desc_device.iManufacturer);
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf)) )
{
print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf));
}
printf("\r\n");
printf(" iProduct %u " , desc_device.iProduct);
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf)))
{
print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf));
}
printf("\r\n");
printf(" iSerialNumber %u " , desc_device.iSerialNumber);
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf)))
{
print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf));
}
printf("\r\n");
printf(" bNumConfigurations %u\r\n" , desc_device.bNumConfigurations);
// Get configuration descriptor with sync API
// if (XFER_RESULT_SUCCESS == tuh_descriptor_get_configuration_sync(daddr, 0, temp_buf, sizeof(temp_buf)) )
// {
// parse_config_descriptor(daddr, (tusb_desc_configuration_t*) temp_buf);
// }
}
// Invoked when device is mounted (configured)
void tuh_mount_cb (uint8_t daddr)
{
printf("Device attached, address = %d\r\n", daddr);
// Get Device Descriptor sync API
// TODO: invoking control trannsfer now has issue with mounting hub with multiple devices attached, fix later
tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor, 0);
}
/// Invoked when device is unmounted (bus reset/unplugged)
void tuh_umount_cb(uint8_t daddr)
{
printf("Device removed, address = %d\r\n", daddr);
}
//--------------------------------------------------------------------+
// Blinking Task
//--------------------------------------------------------------------+
void led_blinking_task(void)
{
const uint32_t interval_ms = 1000;
static uint32_t start_ms = 0;
static bool led_state = false;
// Blink every interval ms
if ( board_millis() - start_ms < interval_ms) return; // not enough time
start_ms += interval_ms;
board_led_write(led_state);
led_state = 1 - led_state; // toggle
}
//--------------------------------------------------------------------+
// Helper
//--------------------------------------------------------------------+
static void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) { static void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) {
// TODO: Check for runover. // TODO: Check for runover.
(void)utf8_len; (void)utf8_len;
@ -108,94 +208,11 @@ static int _count_utf8_bytes(const uint16_t *buf, size_t len) {
return total_bytes; return total_bytes;
} }
static void utf16_to_utf8(uint16_t *temp_buf, size_t buf_len) { static void print_utf16(uint16_t *temp_buf, size_t buf_len) {
size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t); size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t);
size_t utf8_len = _count_utf8_bytes(temp_buf + 1, utf16_len); size_t utf8_len = _count_utf8_bytes(temp_buf + 1, utf16_len);
_convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len); _convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len);
((uint8_t*) temp_buf)[utf8_len] = '\0'; ((uint8_t*) temp_buf)[utf8_len] = '\0';
}
printf((char*)temp_buf);
void print_device_descriptor(tuh_xfer_t* xfer)
{
if ( XFER_RESULT_SUCCESS != xfer->result )
{
printf("Failed to get device descriptor\r\n");
return;
}
uint8_t const daddr = xfer->daddr;
printf("Device %u: ID %04x:%04x\r\n", daddr, desc_device.idVendor, desc_device.idProduct);
printf("Device Descriptor:\r\n");
printf(" bLength %u\r\n" , desc_device.bLength);
printf(" bDescriptorType %u\r\n" , desc_device.bDescriptorType);
printf(" bcdUSB %04x\r\n" , desc_device.bcdUSB);
printf(" bDeviceClass %u\r\n" , desc_device.bDeviceClass);
printf(" bDeviceSubClass %u\r\n" , desc_device.bDeviceSubClass);
printf(" bDeviceProtocol %u\r\n" , desc_device.bDeviceProtocol);
printf(" bMaxPacketSize0 %u\r\n" , desc_device.bMaxPacketSize0);
printf(" idVendor 0x%04x\r\n" , desc_device.idVendor);
printf(" idProduct 0x%04x\r\n" , desc_device.idProduct);
printf(" bcdDevice %04x\r\n" , desc_device.bcdDevice);
uint16_t temp_buf[128];
printf(" iManufacturer %u " , desc_device.iManufacturer);
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf)) )
{
utf16_to_utf8(temp_buf, TU_ARRAY_SIZE(temp_buf));
printf((const char*) temp_buf);
}
printf("\r\n");
printf(" iProduct %u " , desc_device.iProduct);
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf)))
{
utf16_to_utf8(temp_buf, TU_ARRAY_SIZE(temp_buf));
printf((const char*) temp_buf);
}
printf("\r\n");
printf(" iSerialNumber %u " , desc_device.iSerialNumber);
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf)))
{
utf16_to_utf8(temp_buf, TU_ARRAY_SIZE(temp_buf));
printf((const char*) temp_buf);
}
printf("\r\n");
printf(" bNumConfigurations %u\r\n" , desc_device.bNumConfigurations);
}
// Invoked when device is mounted (configured)
void tuh_mount_cb (uint8_t daddr)
{
printf("Device attached, address = %d\r\n", daddr);
// Get Device Descriptor using asynchronous API
tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor, 0);
}
/// Invoked when device is unmounted (bus reset/unplugged)
void tuh_umount_cb(uint8_t daddr)
{
printf("Device removed, address = %d\r\n", daddr);
}
//--------------------------------------------------------------------+
// Blinking Task
//--------------------------------------------------------------------+
void led_blinking_task(void)
{
const uint32_t interval_ms = 1000;
static uint32_t start_ms = 0;
static bool led_state = false;
// Blink every interval ms
if ( board_millis() - start_ms < interval_ms) return; // not enough time
start_ms += interval_ms;
board_led_write(led_state);
led_state = 1 - led_state; // toggle
} }

View File

@ -256,7 +256,13 @@ bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t instance)
// claim endpoint // claim endpoint
TU_VERIFY( usbh_edpt_claim(dev_addr, hid_itf->ep_in) ); TU_VERIFY( usbh_edpt_claim(dev_addr, hid_itf->ep_in) );
return usbh_edpt_xfer(dev_addr, hid_itf->ep_in, hid_itf->epin_buf, hid_itf->epin_size); if ( !usbh_edpt_xfer(dev_addr, hid_itf->ep_in, hid_itf->epin_buf, hid_itf->epin_size) )
{
usbh_edpt_claim(dev_addr, hid_itf->ep_in);
return false;
}
return true;
} }
//bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance) //bool tuh_n_hid_n_ready(uint8_t dev_addr, uint8_t instance)

View File

@ -82,8 +82,7 @@ typedef struct {
uint8_t speed; uint8_t speed;
// Device State // Device State
struct TU_ATTR_PACKED struct TU_ATTR_PACKED {
{
volatile uint8_t connected : 1; volatile uint8_t connected : 1;
volatile uint8_t addressed : 1; volatile uint8_t addressed : 1;
volatile uint8_t configured : 1; volatile uint8_t configured : 1;
@ -110,7 +109,11 @@ typedef struct {
tu_edpt_state_t ep_status[CFG_TUH_ENDPOINT_MAX][2]; tu_edpt_state_t ep_status[CFG_TUH_ENDPOINT_MAX][2];
#if CFG_TUH_API_EDPT_XFER #if CFG_TUH_API_EDPT_XFER
// ep_xfer[]; // TODO array can be CFG_TUH_ENDPOINT_MAX-1
struct {
tuh_xfer_cb_t complete_cb;
uintptr_t user_data;
}ep_callback[CFG_TUH_ENDPOINT_MAX][2];
#endif #endif
} usbh_device_t; } usbh_device_t;
@ -227,6 +230,8 @@ TU_ATTR_ALWAYS_INLINE static inline void usbh_unlock(void)
#else #else
#define _usbh_mutex NULL
#define usbh_lock() #define usbh_lock()
#define usbh_unlock() #define usbh_unlock()
@ -449,10 +454,38 @@ void tuh_task(void)
}else }else
{ {
uint8_t drv_id = dev->ep2drv[epnum][ep_dir]; uint8_t drv_id = dev->ep2drv[epnum][ep_dir];
TU_ASSERT(drv_id < USBH_CLASS_DRIVER_COUNT, ); if(drv_id < USBH_CLASS_DRIVER_COUNT)
{
TU_LOG2("%s xfer callback\r\n", usbh_class_drivers[drv_id].name);
usbh_class_drivers[drv_id].xfer_cb(event.dev_addr, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
}
else
{
#if CFG_TUH_API_EDPT_XFER
tuh_xfer_cb_t complete_cb = dev->ep_callback[epnum][ep_dir].complete_cb;
if ( complete_cb )
{
tuh_xfer_t xfer =
{
.daddr = event.dev_addr,
.ep_addr = ep_addr,
.result = event.xfer_complete.result,
.actual_len = event.xfer_complete.len,
.buflen = 0, // not available
.buffer = NULL, // not available
.complete_cb = complete_cb,
.user_data = dev->ep_callback[epnum][ep_dir].user_data
};
TU_LOG2("%s xfer callback\r\n", usbh_class_drivers[drv_id].name); complete_cb(&xfer);
usbh_class_drivers[drv_id].xfer_cb(event.dev_addr, ep_addr, event.xfer_complete.result, event.xfer_complete.len); }else
#endif
{
// no driver/callback responsible for this transfer
TU_ASSERT(false, );
}
}
} }
} }
} }
@ -638,9 +671,18 @@ static bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result
bool tuh_edpt_xfer(tuh_xfer_t* xfer) bool tuh_edpt_xfer(tuh_xfer_t* xfer)
{ {
TU_VERIFY(xfer->daddr && xfer->ep_addr); uint8_t const daddr = xfer->daddr;
uint8_t const ep_addr = xfer->ep_addr;
TU_VERIFY(daddr && ep_addr);
TU_VERIFY(usbh_edpt_claim(daddr, ep_addr));
if ( !usbh_edpt_xfer_with_callback(daddr, ep_addr, xfer->buffer, xfer->buflen, xfer->complete_cb, xfer->user_data) )
{
usbh_edpt_release(daddr, ep_addr);
return false;
}
return true; return true;
} }
@ -687,50 +729,50 @@ bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr)
uint8_t const epnum = tu_edpt_number(ep_addr); uint8_t const epnum = tu_edpt_number(ep_addr);
uint8_t const dir = tu_edpt_dir(ep_addr); uint8_t const dir = tu_edpt_dir(ep_addr);
tu_edpt_state_t* ep_state = &dev->ep_status[epnum][dir]; return tu_edpt_claim(&dev->ep_status[epnum][dir], _usbh_mutex);
#if TUSB_OPT_MUTEX
return tu_edpt_claim(ep_state, _usbh_mutex);
#else
return tu_edpt_claim(ep_state, NULL);
#endif
} }
// TODO has some duplication code with device, refactor later // TODO has some duplication code with device, refactor later
bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr) bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr)
{ {
// addr0 is always available usbh_device_t* dev = get_device(dev_addr);
if (dev_addr == 0) return true;
usbh_device_t* dev = get_device(dev_addr); // addr0 only use tuh_control_xfer
uint8_t const epnum = tu_edpt_number(ep_addr); TU_ASSERT(dev);
uint8_t const dir = tu_edpt_dir(ep_addr);
tu_edpt_state_t* ep_state = &dev->ep_status[epnum][dir];
#if TUSB_OPT_MUTEX uint8_t const epnum = tu_edpt_number(ep_addr);
return tu_edpt_release(ep_state, _usbh_mutex); uint8_t const dir = tu_edpt_dir(ep_addr);
#else
return tu_edpt_release(ep_state, NULL); return tu_edpt_release(&dev->ep_status[epnum][dir], _usbh_mutex);
#endif
} }
// TODO has some duplication code with device, refactor later // TODO has some duplication code with device, refactor later
bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes,
tuh_xfer_cb_t complete_cb, uintptr_t user_data)
{ {
(void) complete_cb;
(void) user_data;
usbh_device_t* dev = get_device(dev_addr); usbh_device_t* dev = get_device(dev_addr);
TU_VERIFY(dev); TU_VERIFY(dev);
uint8_t const epnum = tu_edpt_number(ep_addr); uint8_t const epnum = tu_edpt_number(ep_addr);
uint8_t const dir = tu_edpt_dir(ep_addr); uint8_t const dir = tu_edpt_dir(ep_addr);
tu_edpt_state_t* ep_state = &dev->ep_status[epnum][dir];
TU_LOG2(" Queue EP %02X with %u bytes ... ", ep_addr, total_bytes); TU_LOG2(" Queue EP %02X with %u bytes ... ", ep_addr, total_bytes);
// Attempt to transfer on a busy endpoint, sound like an race condition ! // Attempt to transfer on a busy endpoint, sound like an race condition !
TU_ASSERT(dev->ep_status[epnum][dir].busy == 0); TU_ASSERT(ep_state->busy == 0);
// Set busy first since the actual transfer can be complete before hcd_edpt_xfer() // Set busy first since the actual transfer can be complete before hcd_edpt_xfer()
// could return and USBH task can preempt and clear the busy // could return and USBH task can preempt and clear the busy
dev->ep_status[epnum][dir].busy = true; ep_state->busy = 1;
#if CFG_TUH_API_EDPT_XFER
dev->ep_callback[epnum][dir].complete_cb = complete_cb;
dev->ep_callback[epnum][dir].user_data = user_data;
#endif
if ( hcd_edpt_xfer(dev->rhport, dev_addr, ep_addr, buffer, total_bytes) ) if ( hcd_edpt_xfer(dev->rhport, dev_addr, ep_addr, buffer, total_bytes) )
{ {
@ -739,9 +781,9 @@ bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_
}else }else
{ {
// HCD error, mark endpoint as ready to allow next transfer // HCD error, mark endpoint as ready to allow next transfer
dev->ep_status[epnum][dir].busy = false; ep_state->busy = 0;
dev->ep_status[epnum][dir].claimed = 0; ep_state->claimed = 0;
TU_LOG1("failed\r\n"); TU_LOG1("Failed\r\n");
TU_BREAKPOINT(); TU_BREAKPOINT();
return false; return false;
} }

View File

@ -44,25 +44,27 @@ typedef struct tuh_xfer_s tuh_xfer_t;
typedef void (*tuh_xfer_cb_t)(tuh_xfer_t* xfer); typedef void (*tuh_xfer_cb_t)(tuh_xfer_t* xfer);
// Note: layout and order of this will be changed in near future
// it is advised to initialize it using member name
struct tuh_xfer_s struct tuh_xfer_s
{ {
uint8_t daddr; uint8_t daddr;
uint8_t ep_addr; uint8_t ep_addr;
xfer_result_t result; xfer_result_t result;
uint32_t actual_len; // excluding setup packet uint32_t actual_len; // excluding setup packet
union union
{ {
tusb_control_request_t const* setup; // setup packet pointer if control transfer tusb_control_request_t const* setup; // setup packet pointer if control transfer
uint32_t buflen; // length if not control transfer uint32_t buflen; // expected length if not control transfer (not available in callback)
}; };
uint8_t* buffer; uint8_t* buffer; // not available in callback if not control transfer
tuh_xfer_cb_t complete_cb; tuh_xfer_cb_t complete_cb;
uintptr_t user_data; uintptr_t user_data;
uint32_t timeout_ms; // place holder, not supported yet // uint32_t timeout_ms; // place holder, not supported yet
}; };
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@ -66,8 +66,16 @@ void usbh_int_set(bool enabled);
// Open an endpoint // Open an endpoint
bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep); bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep);
// Submit a usb transfer // Submit a usb transfer with callback support, require CFG_TUH_API_EDPT_XFER
bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes,
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
TU_ATTR_ALWAYS_INLINE
static inline bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
{
return usbh_edpt_xfer_with_callback(dev_addr, ep_addr, buffer, total_bytes, NULL, 0);
}
// Claim an endpoint before submitting a transfer. // Claim an endpoint before submitting a transfer.
// If caller does not make any transfer, it must release endpoint for others. // If caller does not make any transfer, it must release endpoint for others.