Detecting USB disconnection (self powered)

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
brsi_1319346
Level 1
Level 1

Hi, I'm a bit new to USB device design and have a fairly straight forward question about connection state. 

   

My application is a keyboard style piece of hardware that has custom buttons, USBFS with three HID end points for keys, mouse and volume control. It's always powered and always connected to an Embedded X86, running Windows 10 or similar. The OS will reboot many times during the users day, because Windows is like that, and each time, my device will stop working, requiring a full power cycle to bring us back.

   

I need to detect the USB disconnection by host... right now I'm using USBFS_1_bGetConfiguration, and clearly it is returning a != 0 value even after the OS has shut down. 

   

Should I be using isConfigurationChanged()? This would let me see the OS come up, then I'd reInit the USBFS, use USBFS_1_bGetConfiguration to confirm a connection and load each end point? I'm working on this now with no success yet.

   

 

   

Thanks, Brent. 

0 Likes
1 Solution
2 Replies
EvPa_264126
Level 7
Level 7
500 replies posted 250 replies posted 100 likes received
0 Likes

Regarding improvement suggestion for KBA210620:

It might be worth mentioning that some psoc usb examples have the potential to get hung

if the unplug event occurs at a special time.

so that the VBUS monitoring stuff in the main loop can not do its work anymore.

e.g. code lines like:

while(!USBFS_bGetEPAckState(KEYBOARD_ENDPOINT));

KBA210620 should give advices (or links) for this problem also:
- like calming a  watchdog only in the VBUS monitoring section
- doing VBUS monitoring in a periodic ISR

- hints how to avoid potential endless llops

My preliminary approach

void SysTickISRCallback(void)

{

    static unsigned int count = 0;

    if(count > 50)

    {

        count = 0;

        if (!USBFS_VBusPresent() && usbEnumerated)

        {

            LED_BOARD_Write(0);

            USBFS_Stop();

            CySoftwareReset();

        }

    }

    else

    {

        count++;

    }

}