Skip to main content
Explorer II
May 1, 2024
Solved

USB_HS CONNECTION AND DICONNECTION CABLE

  • May 1, 2024
  • 2 replies
  • 1450 views

I AM USING IN USB_HS DEVICE MODE ,WHEN I CONNECT USB CABLE TO THE PC THEN I WANT  TO KNOWE WHERE IT IS DETECT AND AFTER THAT I REMOVE USB CABLE THEN WHRE IT IS DETECT GIVE ME ANY REGISTER OR CALL BACK FUNCTION ONLY TELL ME IN DEVICE MODE

    This topic has been closed for replies.
    Best answer by Saket_Om

    Hello, 

    To monitor the connection and disconnection events of a USB device, you can implement the callback functions HAL_PCD_ConnectCallback() and HAL_PCD_DisconnectCallback(). These functions are triggered when the USB cable is connected and disconnected, respectively, allowing you to detect these events in device mode.

    Please refer to the snipped code below to implement this function: 

    #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
    static void PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
    #else
    void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
    #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
    {
     /* USER CODE BEGIN HAL_PCD_ConnectCallback_PreTreatment */
    
     /* USER CODE END HAL_PCD_ConnectCallback_PreTreatment */
    
     /* USER CODE BEGIN HAL_PCD_ConnectCallback_PostTreatment */
    
     /* USER CODE END HAL_PCD_ConnectCallback_PostTreatment */
    }

     

    #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
    static void PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
    #else
    void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
    #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
    {
     /* USER CODE BEGIN HAL_PCD_DisconnectCallback_PreTreatment */
    
     /* USER CODE END HAL_PCD_DisconnectCallback_PreTreatment */
    
     /* USER CODE BEGIN HAL_PCD_DisconnectCallback_PostTreatment */
    
     /* USER CODE END HAL_PCD_DisconnectCallback_PostTreatment */
    }

     You can refer also to the example provided in STM32Cube firmware H7 in this link

    2 replies

    Technical Moderator
    May 8, 2024

    Hello @SITARAM 

     

    Could you please provide additional details about the specific MCU product and the version of the Cube firmware that you are currently utilizing?

     

    SITARAMAuthor
    Explorer II
    May 9, 2024

     

    HERE I AM USING STM32CubeMX 5.4.0 VERSION AND STM32H743ZI PRODUCT USE

    SITARAMAuthor
    Explorer II
    May 9, 2024

    PC0 - USB_OTG_HS_ULPI_STP
    PC2_C - USB_OTG_HS_ULPI_DIR
    PC3_C - USB_OTG_HS_ULPI_NXT
    PA3 - USB_OTG_HS_ULPI_D0
    PA5 - USB_OTG_HS_ULPI_CK
    PB0 - USB_OTG_HS_ULPI_D1
    PB1 - USB_OTG_HS_ULPI_D2
    PB10 -USB_OTG_HS_ULPI_D3
    PB11 - USB_OTG_HS_ULPI_D4
    PB12 - USB_OTG_HS_ULPI_D5
    PB13 -USB_OTG_HS_ULPI_D6
    PB5 - USB_OTG_HS_ULPI_D7

    AND STM32H743ZI  AND STM32CubeMX 5.4.0 VERSION USE

    Saket_OmAnswer
    Technical Moderator
    May 9, 2024

    Hello, 

    To monitor the connection and disconnection events of a USB device, you can implement the callback functions HAL_PCD_ConnectCallback() and HAL_PCD_DisconnectCallback(). These functions are triggered when the USB cable is connected and disconnected, respectively, allowing you to detect these events in device mode.

    Please refer to the snipped code below to implement this function: 

    #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
    static void PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
    #else
    void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
    #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
    {
     /* USER CODE BEGIN HAL_PCD_ConnectCallback_PreTreatment */
    
     /* USER CODE END HAL_PCD_ConnectCallback_PreTreatment */
    
     /* USER CODE BEGIN HAL_PCD_ConnectCallback_PostTreatment */
    
     /* USER CODE END HAL_PCD_ConnectCallback_PostTreatment */
    }

     

    #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
    static void PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
    #else
    void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
    #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
    {
     /* USER CODE BEGIN HAL_PCD_DisconnectCallback_PreTreatment */
    
     /* USER CODE END HAL_PCD_DisconnectCallback_PreTreatment */
    
     /* USER CODE BEGIN HAL_PCD_DisconnectCallback_PostTreatment */
    
     /* USER CODE END HAL_PCD_DisconnectCallback_PostTreatment */
    }

     You can refer also to the example provided in STM32Cube firmware H7 in this link

    Technical Moderator
    May 15, 2024

    Hello @SITARAM 

     

    Did my answer solve your issue? If yes, please mark my answer as “Solution" by clicking on the “Accept as Solution" button, this can be helpful for Community users to find this solution faster.