AnsweredAssumed Answered

USB cable detection

Question asked by qwer.asdf on Jul 1, 2015
Latest reply on Jul 8, 2015 by qwer.asdf
I use a custom STM32F407 board.

I want to detect USB FS cable presence (no matter to pc or charger).
Here is what I did. First, the VBUS pin is initialized in usb_bsp.c USB_OTG_BSP_Init function, I didn't change anything here:

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);


I don't know why an input pin's OType is configured as open drain, but whatever.

Next, in my code, I used to detect whether the cable is plugged using this function:

int is_usb_vbus_present(void) {
    return (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_9) == Bit_SET) ? 1 : 0;
}

When I just plug and unplug the cable, it's working, I get 1 from that function when the cable is plugged and 0 when it's unplugged. But when I initialize the device as USB device (e.g. VCP) then I keep getting 1 from that function even after unplugging the cable.
What am I doing wrong? Or are there other, better ways of detecting USB cable (to turn on/off the battery charger).

Thank you.

Outcomes