ASF / Detect USB connection with PC (MSD)

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi All,

 

I have an application where I log data to an SD card. This works fine. I also have the microcontroller configured as a USB mass storage device to access the data from the SD card, this works (standalone) also fine. I'd like know to switch between those two modes (logging and USB file access). But for that I need to know when a PC is connected to the USB port. I'm using a SAMD21/ASF3.4 and don't have VBUS voltage. This wouldn't be useful anyway as I'm also charging a battery through USB and when using an USB charger this shouldn't be recognized as PC connection. Pseudo code:

 

while(1)
{
    if (PCUSBConnection)
    {
        udi_msc_process_trans();
    }
    else
    {
        logData();
    }
}

I tried to use the callback functions UDI_MSC_ENABLE_EXT and UDI_MSC_DISABLE_EXT but the disable function is also called when plugging the USB in. Call list says that it is because the _usb_on_bus_reset function is called.

 

Any ideas how to figure out whether a PC is connected or not? Any status bit or likewise somewhere deeply hidden?

 

Thanks!

Oliver

Last Edited: Wed. Sep 12, 2018 - 12:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Monitor the Frame Number in the USB registers. It should be zero when disconnected and non-zero/increasing when connected.