cancel
Showing results for 
Search instead for 
Did you mean: 

USBX, Detect cable disconnected.

Henrik76
Associate

I am running the "Ux_Device_HID_CDC_ACM" example project on a NUCLEO-H563ZI board.

The example runs without issues, i can attach and remove the usb cable and the mouse connection appears and disappear.

The issue is that the USB-stack does not seem to detect that the usb-cable is removed. When I hit the button to move the mouse pointer when the cable is removed the mouse movements que up in the stack and when i attach the cable again the mouse pointer moves the queued movements. This also happen in VCP part of the example. Messages queue up and are transmitted when the cable is attached again.

This is not ok. My real system needs to know when the cable is removed so that actions do not queue up in the stack.

The actual cable disconnection is not the problem, i can think of several ways to detect this. 

 

The question:

How do I bring down the USB stack in a proper way so that messages are not queued up in the stack?

 

1 ACCEPTED SOLUTION

Accepted Solutions
FBL
ST Employee

Hi @Henrik76 

You can handle disconnection event in 

void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
{
    // Stop USB device
    USBD_Stop(&hUsbDeviceFS);
    // Deinitialize USB device
    USBD_DeInit(&hUsbDeviceFS);
}

Otherwise, you can use USBPD stack with no Power Delivery, under usbpd_dpm_core.c, callback reporting events on a specified port from CAD layer.

void USBPD_DPM_CADCallback(uint8_t PortNum, USBPD_CAD_EVENT State, CCxPin_TypeDef Cc)
{
#ifdef _TRACE
  USBPD_TRACE_Add(USBPD_TRACE_CADEVENT, PortNum, (uint8_t)State, NULL, 0);
#endif /* _TRACE */

  switch (State)
  {
#if defined(USBPDCORE_VPD)
    case USPPD_CAD_EVENT_VPD :
#endif /* USBPDCORE_VPD */
    case USBPD_CAD_EVENT_ATTEMC :
    case USBPD_CAD_EVENT_ATTACHED :
    {
      USBPD_DPM_UserCableDetection(PortNum, State);
      USBPD_DPM_Notification(PortNum, USBPD_NOTIFY_USBSTACK_START);
      break;
    }
    case USBPD_CAD_EVENT_DETACHED :
    case USBPD_CAD_EVENT_EMC :
    {
      USBPD_DPM_UserCableDetection(PortNum, State);
      USBPD_DPM_Notification(PortNum, USBPD_NOTIFY_USBSTACK_STOP);
      break;
    }
    default :
      /* nothing to do */
      break;
  }
}

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


I'm out of offce with limited access to my emails.
Happy New Year!

View solution in original post

1 REPLY 1
FBL
ST Employee

Hi @Henrik76 

You can handle disconnection event in 

void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
{
    // Stop USB device
    USBD_Stop(&hUsbDeviceFS);
    // Deinitialize USB device
    USBD_DeInit(&hUsbDeviceFS);
}

Otherwise, you can use USBPD stack with no Power Delivery, under usbpd_dpm_core.c, callback reporting events on a specified port from CAD layer.

void USBPD_DPM_CADCallback(uint8_t PortNum, USBPD_CAD_EVENT State, CCxPin_TypeDef Cc)
{
#ifdef _TRACE
  USBPD_TRACE_Add(USBPD_TRACE_CADEVENT, PortNum, (uint8_t)State, NULL, 0);
#endif /* _TRACE */

  switch (State)
  {
#if defined(USBPDCORE_VPD)
    case USPPD_CAD_EVENT_VPD :
#endif /* USBPDCORE_VPD */
    case USBPD_CAD_EVENT_ATTEMC :
    case USBPD_CAD_EVENT_ATTACHED :
    {
      USBPD_DPM_UserCableDetection(PortNum, State);
      USBPD_DPM_Notification(PortNum, USBPD_NOTIFY_USBSTACK_START);
      break;
    }
    case USBPD_CAD_EVENT_DETACHED :
    case USBPD_CAD_EVENT_EMC :
    {
      USBPD_DPM_UserCableDetection(PortNum, State);
      USBPD_DPM_Notification(PortNum, USBPD_NOTIFY_USBSTACK_STOP);
      break;
    }
    default :
      /* nothing to do */
      break;
  }
}

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.


I'm out of offce with limited access to my emails.
Happy New Year!