Post History
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...
Answer
#3: Post edited
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
- 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
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.