Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

60%
+1 −0
Q&A STM32. What should be done in firmware for VBUS detection through a GPIO pin?

I've found a solution, and tested it. What @Olin wrote earlier holds. This is to post my code for STM32. Does the VBUS detection have to be done in an interrupt? I've asked the ST's technic...

posted 3mo ago by Nick Alexeev‭  ·  edited 3mo ago by Nick Alexeev‭

Answer
#3: Post edited by user avatar Nick Alexeev‭ · 2024-07-14T14:54:01Z (3 months ago)
  • What [@Olin wrote earlier](https://electrical.codidact.com/posts/291709/291716#answer-291716) holds. This is to post code for STM32.
  • > Does the VBUS detection have to be done in an interrupt?
  • I posted this question to ST's technical support, and they said that it should be done in an interrupt. Here's my ISR code.
  • /**
  • * @brief EXTI line detection callback
  • * @param GPIO_Pin: Specifies the pins connected EXTI line
  • */
  • void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  • {
  • if (GPIO_Pin == VBUS_DETECT_Pin)
  • {
  • if (HAL_GPIO_ReadPin(VBUS_DETECT_GPIO_Port, VBUS_DETECT_Pin) == GPIO_PIN_SET)
  • {
  • MX_USB_Device_Init(); // reinitialize USB
  • }
  • else
  • {
  • USB_Device_DeInit(); // deinitialize USB
  • }
  • }
  • }
  • The `USB_Device_DeInit()` function is in the `usb_device.c`.
  • /* USER CODE BEGIN 1 */
  • void USB_Device_DeInit(void)
  • {
  • if (USBD_Stop(&hUsbDeviceFS) != USBD_OK)
  • {
  • Error_Handler();
  • }
  • if (USBD_DeInit(&hUsbDeviceFS) != USBD_OK)
  • {
  • Error_Handler();
  • }
  • }
  • /* USER CODE END 1 */
  • `MX_USB_Device_Init()` remained unchanged just like CubeMX generated it.
  • I ran these snippets by the ST's support, and got a "looks good to me" verdict.
  • I've found a solution, and tested it. What [@Olin wrote earlier](https://electrical.codidact.com/posts/291709/291716#answer-291716) holds. This is to post my code for STM32.
  • > Does the VBUS detection have to be done in an interrupt?
  • I've asked the ST's technical support, and they replied that it should be done in the interrupt. Here's my ISR code.
  • /**
  • * @brief EXTI line detection callback
  • * @param GPIO_Pin: Specifies the pins connected EXTI line
  • */
  • void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  • {
  • if (GPIO_Pin == VBUS_DETECT_Pin)
  • {
  • if (HAL_GPIO_ReadPin(VBUS_DETECT_GPIO_Port, VBUS_DETECT_Pin) == GPIO_PIN_SET)
  • {
  • MX_USB_Device_Init(); // reinitialize USB
  • }
  • else
  • {
  • USB_Device_DeInit(); // deinitialize USB
  • }
  • }
  • }
  • The `USB_Device_DeInit()` function is in the `usb_device.c`.
  • /* USER CODE BEGIN 1 */
  • void USB_Device_DeInit(void)
  • {
  • if (USBD_Stop(&hUsbDeviceFS) != USBD_OK)
  • {
  • Error_Handler();
  • }
  • if (USBD_DeInit(&hUsbDeviceFS) != USBD_OK)
  • {
  • Error_Handler();
  • }
  • }
  • /* USER CODE END 1 */
  • `MX_USB_Device_Init()` remained unchanged just like CubeMX generated it.
  • I ran these snippets by the ST's support, and got a "looks good to me" verdict.
#2: Post edited by user avatar Nick Alexeev‭ · 2024-07-14T14:28:51Z (3 months ago)
  • What [@Olin wrote earlier](https://electrical.codidact.com/posts/291709/291716#answer-291716) holds. This is to post code for STM32.
  • > Does the VBUS detection have to be done in an interrupt?
  • I posted this question to ST's technical support, and they said that it should be done in an interrupt. Here's my ISR code.
  • /**
  • * @brief EXTI line detection callback
  • * @param GPIO_Pin: Specifies the pins connected EXTI line
  • */
  • void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  • {
  • if (GPIO_Pin == VBUS_DETECT_Pin)
  • {
  • if (HAL_GPIO_ReadPin(VBUS_DETECT_GPIO_Port, VBUS_DETECT_Pin) == GPIO_PIN_SET)
  • {
  • MX_USB_Device_Init(); // reinitialize USB
  • }
  • else
  • {
  • USB_Device_DeInit(); // deinitialize USB
  • }
  • }
  • }
  • The USB_Device_DeInit() function is in the `usb_device.c`.
  • /* USER CODE BEGIN 1 */
  • void USB_Device_DeInit(void)
  • {
  • if (USBD_Stop(&hUsbDeviceFS) != USBD_OK)
  • {
  • Error_Handler();
  • }
  • if (USBD_DeInit(&hUsbDeviceFS) != USBD_OK)
  • {
  • Error_Handler();
  • }
  • }
  • /* USER CODE END 1 */
  • `MX_USB_Device_Init()` remained unchanged just like CubeMX generated it.
  • I ran these snippets by the ST's support, and got a "looks good to me" verdict.
  • What [@Olin wrote earlier](https://electrical.codidact.com/posts/291709/291716#answer-291716) holds. This is to post code for STM32.
  • > Does the VBUS detection have to be done in an interrupt?
  • I posted this question to ST's technical support, and they said that it should be done in an interrupt. Here's my ISR code.
  • /**
  • * @brief EXTI line detection callback
  • * @param GPIO_Pin: Specifies the pins connected EXTI line
  • */
  • void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  • {
  • if (GPIO_Pin == VBUS_DETECT_Pin)
  • {
  • if (HAL_GPIO_ReadPin(VBUS_DETECT_GPIO_Port, VBUS_DETECT_Pin) == GPIO_PIN_SET)
  • {
  • MX_USB_Device_Init(); // reinitialize USB
  • }
  • else
  • {
  • USB_Device_DeInit(); // deinitialize USB
  • }
  • }
  • }
  • The `USB_Device_DeInit()` function is in the `usb_device.c`.
  • /* USER CODE BEGIN 1 */
  • void USB_Device_DeInit(void)
  • {
  • if (USBD_Stop(&hUsbDeviceFS) != USBD_OK)
  • {
  • Error_Handler();
  • }
  • if (USBD_DeInit(&hUsbDeviceFS) != USBD_OK)
  • {
  • Error_Handler();
  • }
  • }
  • /* USER CODE END 1 */
  • `MX_USB_Device_Init()` remained unchanged just like CubeMX generated it.
  • I ran these snippets by the ST's support, and got a "looks good to me" verdict.
#1: Initial revision by user avatar Nick Alexeev‭ · 2024-07-14T14:27:44Z (3 months ago)
What [@Olin wrote earlier](https://electrical.codidact.com/posts/291709/291716#answer-291716) holds.  This is to post code for STM32.

> Does the VBUS detection have to be done in an interrupt?

I posted this question to ST's technical support, and they said that it should be done in an interrupt.  Here's my ISR code.

    /**
      * @brief EXTI line detection callback
      * @param GPIO_Pin: Specifies the pins connected EXTI line
      */
    void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
    {
      if (GPIO_Pin == VBUS_DETECT_Pin)
      {
        if (HAL_GPIO_ReadPin(VBUS_DETECT_GPIO_Port, VBUS_DETECT_Pin) == GPIO_PIN_SET)
        {
          MX_USB_Device_Init(); // reinitialize USB
        }
        else
        {
          USB_Device_DeInit(); // deinitialize USB
        }
      }
    }

The USB_Device_DeInit() function is in the `usb_device.c`.

    /* USER CODE BEGIN 1 */
     
    void USB_Device_DeInit(void)
    {
      if (USBD_Stop(&hUsbDeviceFS) != USBD_OK) 
      {
        Error_Handler();
      }
      if (USBD_DeInit(&hUsbDeviceFS) != USBD_OK) 
      {
        Error_Handler();
      }
    }
    /* USER CODE END 1 */

`MX_USB_Device_Init()` remained unchanged just like CubeMX generated it.

I ran these snippets by the ST's support, and got a "looks good to me" verdict.