Author Topic: Detecting USB connection [solutions]  (Read 147 times)

mharizanov

  • Newbie
  • *
  • Posts: 10
Detecting USB connection [solutions]
« on: October 15, 2020, 09:14:05 AM »
Hi,
I am now a proud owner of a Moteino M0 and just starting to experiment with it. One of the use cases I tested is the possibility to emulate USB mass storage device using the SPI flash as storage. The idea is to expose configuration JSON and possibly logs while the Moteino is plugged in a computer. That worked without issues with Adafruit's branch of TunyUSB. Now I would like to detect USB connection and only invoke the mass storage device emulation if plugged on USB - otherwise run in low power mode.
I have done something similar with Atmega32u4 ages ago: https://harizanov.com/2013/03/detecting-usb-connection-on-the-funky-v2/
Looking into achieving something similar with SAMD21..
Hints?

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6357
  • Country: us
    • LowPowerLab
Re: How to detect USB connection?
« Reply #1 on: October 15, 2020, 12:40:05 PM »
Glad you like the MoteinoM0  ;D
I have not tried this but its an interesting challenge.
So did you mean you achieved what you're looking for by using a Adafruit target or TinyUSB while programming the MoteinoM0?
If you pick MoteinoM0 as target, there is a TinyUSB option under Tools>USBStack, did you see that?

mharizanov

  • Newbie
  • *
  • Posts: 10
Re: How to detect USB connection?
« Reply #2 on: October 15, 2020, 01:22:21 PM »
The Adafruit msc_external_flash example did not compile for board "Moteino M0"+USB stack = "Tiny USB", so I changed the board to "Adafruit Feather M0" and modified the code to use Moteino's FLASH SS pin instead. Worked right away.

So now I am stuck on figuring out how to detect running on USB..

Cheers!

mharizanov

  • Newbie
  • *
  • Posts: 10
Re: How to detect USB connection?
« Reply #3 on: October 17, 2020, 12:32:26 PM »
I think this will work OK-ish:

Code: [Select]
//Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial && millis() < 5000) {
    ; // wait for serial port to connect. Needed for native USB
  }

  if (!Serial) {
    ; // USB not detected within 5 seconds, most probably we are running on battery power
  } else {
    ; // USB was detected
  }

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6357
  • Country: us
    • LowPowerLab
Re: How to detect USB connection?
« Reply #4 on: October 19, 2020, 11:25:09 AM »
You could also detect USB activity by counting the USB frames.

Code: [Select]
  uint32_t now = millis();
  uint16_t frameNum = USB->DEVICE.FNUM.reg;
 
  while (millis()-now <1000)
  {
    if (USB->DEVICE.FNUM.reg != frameNum)
    {
      Serial1.print("USB detected delay:");
      Serial1.println(millis()-now);
      break;
    }
  }

This could work after waking up from sleep (USB disabled):

Code: [Select]
void standbySleep() {
  USBDevice.detach();
  SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; //Standby - lowest power sleep mode
  __DSB();
  __WFI(); // Wait For Interrupt call
  //Sleep until woken by interrupt
  USBDevice.attach(); //enble USB
}

mharizanov

  • Newbie
  • *
  • Posts: 10
Re: How to detect USB connection?
« Reply #5 on: October 20, 2020, 04:28:02 AM »
Thanks - your suggestion is better and more flexible. I consider the topic closed.