From 97c436a16ee6de7852c5cb77fa4359a5f00f4c46 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Mar 2013 11:20:02 +0700 Subject: [PATCH] add usbh_pipe_control_close (in conjunction with usbh pipe control open) close control pipe when unplugged --- tests/test/host/test_usbh.c | 14 +++++++++----- tinyusb/core/tusb_types.h | 7 ++++--- tinyusb/host/ehci/ehci.c | 1 + tinyusb/host/usbh.c | 17 ++++++++++++++++- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/tests/test/host/test_usbh.c b/tests/test/host/test_usbh.c index f69e32a5f..fa35a22e7 100644 --- a/tests/test/host/test_usbh.c +++ b/tests/test/host/test_usbh.c @@ -164,14 +164,18 @@ void class_close_expect(void) void test_usbh_device_unplugged_isr(void) { - usbh_device_info_pool[1].status = TUSB_DEVICE_STATUS_READY; - usbh_device_info_pool[1].core_id = 0; - usbh_device_info_pool[1].hub_addr = 0; - usbh_device_info_pool[1].hub_port = 0; + uint8_t dev_addr = 1; + + usbh_device_info_pool[dev_addr].status = TUSB_DEVICE_STATUS_READY; + usbh_device_info_pool[dev_addr].core_id = 0; + usbh_device_info_pool[dev_addr].hub_addr = 0; + usbh_device_info_pool[dev_addr].hub_port = 0; class_close_expect(); + hcd_pipe_control_close_ExpectAndReturn(dev_addr, TUSB_ERROR_NONE); + //------------- Code Under Test -------------// usbh_device_unplugged_isr(0); - TEST_ASSERT_EQUAL(TUSB_DEVICE_STATUS_REMOVING, usbh_device_info_pool[1].status); + TEST_ASSERT_EQUAL(TUSB_DEVICE_STATUS_REMOVING, usbh_device_info_pool[dev_addr].status); } diff --git a/tinyusb/core/tusb_types.h b/tinyusb/core/tusb_types.h index 45c6fbeac..1d69b9326 100644 --- a/tinyusb/core/tusb_types.h +++ b/tinyusb/core/tusb_types.h @@ -146,12 +146,13 @@ typedef enum { }tusb_std_class_code_t; enum { - TUSB_DESC_CONFIG_ATT_BUS_POWER = BIT_(7), - TUSB_DESC_CONFIG_ATT_SELF_POWER = BIT_(6), - TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP = BIT_(5) + TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP = BIT_(5), + TUSB_DESC_CONFIG_ATT_SELF_POWER = BIT_(6), + TUSB_DESC_CONFIG_ATT_BUS_POWER = BIT_(7) }; #define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2) + #ifdef __cplusplus } #endif diff --git a/tinyusb/host/ehci/ehci.c b/tinyusb/host/ehci/ehci.c index 2696033f3..f79b478e6 100644 --- a/tinyusb/host/ehci/ehci.c +++ b/tinyusb/host/ehci/ehci.c @@ -67,6 +67,7 @@ STATIC_ASSERT( ALIGN_OF(period_frame_list0) == 4096, "Period Framelist must be 4 #if CONTROLLER_HOST_NUMBER > 1 STATIC_ASSERT( ALIGN_OF(period_frame_list1) == 4096, "Period Framelist must be 4k alginment"); #endif +// TODO static assert for memory placement on some known MCU such as lpc43xx //--------------------------------------------------------------------+ // IMPLEMENTATION diff --git a/tinyusb/host/usbh.c b/tinyusb/host/usbh.c index 5f679facf..0e78a315b 100644 --- a/tinyusb/host/usbh.c +++ b/tinyusb/host/usbh.c @@ -151,6 +151,7 @@ tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, tusb_std_request_t cons OSAL_SUBTASK_END } +tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) ATTR_ALWAYS_INLINE; tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) { osal_semaphore_reset( usbh_device_info_pool[dev_addr].sem_hdl ); @@ -160,6 +161,14 @@ tusb_error_t usbh_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size) return TUSB_ERROR_NONE; } +static inline tusb_error_t usbh_pipe_control_close(uint8_t dev_addr) ATTR_ALWAYS_INLINE; +static inline tusb_error_t usbh_pipe_control_close(uint8_t dev_addr) +{ + ASSERT_STATUS( hcd_pipe_control_close(dev_addr) ); + + return TUSB_ERROR_NONE; +} + pipe_status_t usbh_pipe_status_get(pipe_handle_t pipe_hdl) { return PIPE_STATUS_BUSY; @@ -192,6 +201,7 @@ void usbh_device_plugged_isr(uint8_t hostid, tusb_speed_t speed) void usbh_device_unplugged_isr(uint8_t hostid) { + //------------- find the device address that is unplugged -------------// uint8_t dev_addr=1; while ( dev_addr <= TUSB_CFG_HOST_DEVICE_MAX && ! (usbh_device_info_pool[dev_addr].core_id == hostid && usbh_device_info_pool[dev_addr].hub_addr == 0 && @@ -202,12 +212,17 @@ void usbh_device_unplugged_isr(uint8_t hostid) ASSERT(dev_addr <= TUSB_CFG_HOST_DEVICE_MAX, (void) 0 ); + // if device unplugged is not a hub TODO handle hub unplugged for (uint8_t class_code = 1; class_code < TUSB_CLASS_MAX_CONSEC_NUMBER; class_code++) { if (usbh_class_drivers[class_code].close) usbh_class_drivers[class_code].close(dev_addr); } + usbh_pipe_control_close(dev_addr); + + // set to REMOVING to prevent allocate to other new device + // need to set to UNPLUGGED by HCD after freeing all resources usbh_device_info_pool[dev_addr].status = TUSB_DEVICE_STATUS_REMOVING; } @@ -276,7 +291,7 @@ OSAL_TASK_DECLARE(usbh_enumeration_task) usbh_device_info_pool[new_addr].hub_port = usbh_device_info_pool[0].hub_port; usbh_device_info_pool[new_addr].speed = usbh_device_info_pool[0].speed; usbh_device_info_pool[new_addr].status = TUSB_DEVICE_STATUS_ADDRESSED; - hcd_pipe_control_close(0); + usbh_pipe_control_close(0); // hcd_port_reset( usbh_device_info_pool[new_addr].core_id ); TODO verified