Просмотр исходного кода

add tuh_mount_cb/tuh_umount_cb

hathach 7 лет назад
Родитель
Сommit
4e7596ca9c

+ 2 - 2
examples/host/cdc_msc_hid/src/main.c

@@ -84,7 +84,7 @@ int main(void)
 #if CFG_TUH_CDC
 CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 };
 
-void tuh_cdc_mounted_cb(uint8_t dev_addr)
+void tuh_mount_cb(uint8_t dev_addr)
 {
   // application set-up
   printf("\na CDC device  (address %d) is mounted\n", dev_addr);
@@ -92,7 +92,7 @@ void tuh_cdc_mounted_cb(uint8_t dev_addr)
   tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // schedule first transfer
 }
 
-void tuh_cdc_unmounted_cb(uint8_t dev_addr)
+void tuh_umount_cb(uint8_t dev_addr)
 {
   // application tear-down
   printf("\na CDC device (address %d) is unmounted \n", dev_addr);

+ 1 - 7
src/class/cdc/cdc_host.c

@@ -136,7 +136,7 @@ void cdch_init(void)
   tu_memclr(cdch_data, sizeof(cdch_data_t)*CFG_TUSB_HOST_DEVICE_MAX);
 }
 
-bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length)
+bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t *p_length)
 {
   // Only support ACM
   TU_VERIFY( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass);
@@ -217,9 +217,6 @@ bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c
     }
   }
 
-  // FIXME mounted class flag is not set yet
-  tuh_cdc_mounted_cb(dev_addr);
-
   // FIXME move to seperate API : connect
   tusb_control_request_t request =
   {
@@ -249,9 +246,6 @@ void cdch_close(uint8_t dev_addr)
   hcd_pipe_close(TUH_OPT_RHPORT, dev_addr, p_cdc->pipe_out);
 
   tu_memclr(p_cdc, sizeof(cdch_data_t));
-
-  tuh_cdc_unmounted_cb(dev_addr);
-
 }
 
 #endif

+ 1 - 12
src/class/cdc/cdc_host.h

@@ -104,17 +104,6 @@ tusb_error_t tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length,
 //--------------------------------------------------------------------+
 // CDC APPLICATION CALLBACKS
 //--------------------------------------------------------------------+
-/** \brief 			Callback function that will be invoked when a device with CDC Abstract Control Model interface is mounted
- * \param[in]	  dev_addr Address of newly mounted device
- * \note        This callback should be used by Application to set-up interface-related data
- */
-void tuh_cdc_mounted_cb(uint8_t dev_addr);
-
-/** \brief 			Callback function that will be invoked when a device with CDC Abstract Control Model interface is unmounted
- * \param[in] 	dev_addr Address of newly unmounted device
- * \note        This callback should be used by Application to tear-down interface-related data
- */
-void tuh_cdc_unmounted_cb(uint8_t dev_addr);
 
 /** \brief      Callback function that is invoked when an transferring event occurred
  * \param[in]		dev_addr	Address of device
@@ -138,7 +127,7 @@ void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_i
 #ifdef _TINY_USB_SOURCE_FILE_
 
 void cdch_init(void);
-bool cdch_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length);
+bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length);
 void cdch_isr(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
 void cdch_close(uint8_t dev_addr);
 

+ 3 - 2
src/device/usbd.c

@@ -272,7 +272,8 @@ static void usbd_task_body(void)
         // TODO remove since if task is too slow, we could clear the event of the new attached
         osal_queue_reset(_usbd_q);
 
-        tud_umount_cb();    // invoke callback
+        // invoke callback
+        if (tud_umount_cb) tud_umount_cb();
       break;
 
       case DCD_EVENT_SOF:
@@ -467,7 +468,7 @@ static bool process_set_config(uint8_t rhport)
   }
 
   // invoke callback
-  tud_mount_cb();
+  if (tud_mount_cb) tud_mount_cb();
 
   return TUSB_ERROR_NONE;
 }

+ 5 - 6
src/host/usbh.c

@@ -71,7 +71,7 @@ static host_class_driver_t const usbh_class_drivers[] =
     {
       .class_code   = TUSB_CLASS_CDC,
       .init         = cdch_init,
-      .open_subtask = cdch_open_subtask,
+      .open_subtask = cdch_open,
       .isr          = cdch_isr,
       .close        = cdch_close
     },
@@ -218,10 +218,6 @@ bool usbh_control_xfer (uint8_t dev_addr, tusb_control_request_t* request, uint8
   if ( XFER_RESULT_STALLED == dev->control.pipe_status ) return false;
   if ( XFER_RESULT_FAILED == dev->control.pipe_status ) return false;
 
-//  STASK_ASSERT_HDLR(TUSB_ERROR_NONE == error &&
-//                              XFER_RESULT_SUCCESS == dev->control.pipe_status,
-//                              tuh_device_mount_failed_cb(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL) );
-
   return true;
 }
 
@@ -336,6 +332,9 @@ static void usbh_device_unplugged(uint8_t hostid, uint8_t hub_addr, uint8_t hub_
         (hub_port == 0 || dev->hub_port == hub_port) &&
         dev->state    != TUSB_DEVICE_STATE_UNPLUG)
     {
+      // Invoke callback before close driver
+      if (tuh_umount_cb) tuh_umount_cb(dev_addr);
+
       // TODO Hub multiple level
       // 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);
@@ -624,7 +623,7 @@ bool enum_task(hcd_event_t* event)
     }
   }
 
-  tuh_device_mount_succeed_cb(new_addr);
+  if (tuh_mount_cb) tuh_mount_cb(new_addr);
 
   return true;
 }

+ 6 - 2
src/host/usbh.h

@@ -90,8 +90,12 @@ static inline bool tuh_device_is_configured(uint8_t dev_addr)
 // APPLICATION CALLBACK
 //--------------------------------------------------------------------+
 ATTR_WEAK uint8_t tuh_device_attached_cb (tusb_desc_device_t const *p_desc_device)  ATTR_WARN_UNUSED_RESULT;
-ATTR_WEAK void    tuh_device_mount_succeed_cb (uint8_t dev_addr);
-ATTR_WEAK void    tuh_device_mount_failed_cb(tusb_error_t error, tusb_desc_device_t const *p_desc_device); // TODO refractor remove desc_device
+
+/** Callback invoked when device is mounted (configured) */
+ATTR_WEAK void tuh_mount_cb (uint8_t dev_addr);
+
+/** Callback invoked when device is unmounted (bus reset/unplugged) */
+ATTR_WEAK void tuh_umount_cb(uint8_t dev_addr);
 
 //--------------------------------------------------------------------+
 // CLASS-USBH & INTERNAL API

+ 8 - 8
tests/lpc18xx_43xx/test/host/cdc/test_cdc_host.c

@@ -89,7 +89,7 @@ void test_cdch_open_failed_to_open_notification_endpoint(void)
   hcd_pipe_open_ExpectAndReturn(dev_addr, p_endpoint_notification, TUSB_CLASS_CDC, null_hdl);
 
   //------------- CUT -------------//
-  TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open_subtask(dev_addr, p_comm_interface, &length));
+  TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open(dev_addr, p_comm_interface, &length));
 
 }
 
@@ -102,7 +102,7 @@ void test_cdch_open_failed_to_open_data_endpoint_out(void)
   hcd_pipe_open_ExpectAndReturn(dev_addr, p_endpoint_out, TUSB_CLASS_CDC, null_hdl);
 
   //------------- CUT -------------//
-  TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open_subtask(dev_addr, p_comm_interface, &length));
+  TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open(dev_addr, p_comm_interface, &length));
 
 }
 
@@ -116,7 +116,7 @@ void test_cdch_open_failed_to_open_data_endpoint_in(void)
   hcd_pipe_open_ExpectAndReturn(dev_addr, p_endpoint_in, TUSB_CLASS_CDC, null_hdl);
 
   //------------- CUT -------------//
-  TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open_subtask(dev_addr, p_comm_interface, &length));
+  TEST_ASSERT_EQUAL(TUSB_ERROR_HCD_OPEN_PIPE_FAILED, cdch_open(dev_addr, p_comm_interface, &length));
 
 }
 
@@ -135,7 +135,7 @@ void test_cdch_open_length_check(void)
   tusbh_cdc_mounted_cb_Expect(dev_addr);
 
   //------------- CUT -------------//
-  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) );
+  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) );
 
   TEST_ASSERT_EQUAL(expected_length, length);
 }
@@ -147,7 +147,7 @@ void test_cdch_open_interface_number_check(void)
   tusbh_cdc_mounted_cb_Expect(dev_addr);
 
   //------------- CUT -------------//
-  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) );
+  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) );
 
   TEST_ASSERT_EQUAL(1, p_cdc->interface_number);
 
@@ -160,7 +160,7 @@ void test_cdch_open_protocol_check(void)
   tusbh_cdc_mounted_cb_Expect(dev_addr);
 
   //------------- CUT -------------//
-  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) );
+  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) );
 
   TEST_ASSERT_EQUAL(p_comm_interface->bInterfaceProtocol, p_cdc->interface_protocol);
 
@@ -173,7 +173,7 @@ void test_cdch_open_acm_capacity_check(void)
   tusbh_cdc_mounted_cb_Expect(dev_addr);
 
   //------------- CUT -------------//
-  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) );
+  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) );
 
   TEST_ASSERT_EQUAL_MEMORY(&cdc_config_descriptor.cdc_acm.bmCapabilities, &p_cdc->acm_capability, 1);
 }
@@ -192,7 +192,7 @@ void test_cdch_close_device(void)
   hcd_pipe_open_ExpectAndReturn(dev_addr, p_endpoint_in, TUSB_CLASS_CDC, pipe_int);
   tusbh_cdc_mounted_cb_Expect(dev_addr);
 
-  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open_subtask(dev_addr, p_comm_interface, &length) );
+  TEST_ASSERT_EQUAL( TUSB_ERROR_NONE, cdch_open(dev_addr, p_comm_interface, &length) );
 
   hcd_pipe_close_ExpectAndReturn(pipe_notification , TUSB_ERROR_NONE);
   hcd_pipe_close_ExpectAndReturn(pipe_int          , TUSB_ERROR_NONE);