Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: Teensy LC: Detect if a usb connection has been established

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    Cologne, germany
    Posts
    41

    Teensy LC: Detect if a usb connection has been established

    How is it possible to detect (via code) if a Teensy LC has established a usb-connection?
    I need to know this, because I have created a program that sets the Teensy LC to hibernate mode and only wakes up every x minutes
    to check if a certain voltage at a certain analog input has been reached, and if not it immediately goes back to sleep.
    To program the Teensy when the cpu is in hibernate mode it is necessary to press the program-button to upload the code, but the Teensy is placed in a housing where the reset button is hardly reachable, so I need a solution that when the Teensy awakes from hibernate mode it first will check if a usb-connection has been established and if so it will not go back to hibernate mode, so that programming is possible without pressing the program-button.
    Thanks for the answers in advance!

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,441
    Typically : if (Serial) {} - will indicate that the USB is online. Try that and then look for input characters and echo them back when present and see if that works accurately.

    Using Snooze there are some things that are understood to be less than perfect (if I read comments right) - when I looked at a sample last week the USB even when awake (after a snooze) would not respond to the request to go to HID mode for programming and I had to use the button.

    You might want to put a part in your code to read and parse input from the USB port to "reset the Teensy" and then in setup() before calling any SNOOZE sleep modes - watch for "if (Serial)" and perhaps USB input and stay there because at that time it will respond to request to program from what I saw.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    Cologne, germany
    Posts
    41
    Quote Originally Posted by defragster View Post
    Typically : if (Serial) {} - will indicate that the USB is online. Try that and then look for input characters and echo them back when present and see if that works accurately.
    "If (Serial)" needs an open serial monitor to return TRUE!
    Isn't there a (direct) way like on the Atmega32u4 ( "USBCON |= 1<<OTGPADE; if ((USBSTA & (1<<VBUS))){}" )?
    Thanks!
    Last edited by Joegi; 01-04-2018 at 09:36 PM.

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,441
    Teensy USB is 'on the processor' and it ready when compiled with USB Serial code. But until connected that doesn't mean anything. I've only seen the if (Serial) show indeed when a SerMon has connected - after the needed hardware interface is complete.

    About Arduino it seems they have two families - not sure which posted sample applies to: Those with offchip USB ( Uno) that may report when that is ready and connected using TTL serial port, and the latter version ( Leonardo?) that had on chip USB. Teensy would be more like the latter - but not sure how far that similarity goes.

    As posted - to program it - it has to be connected. When connected it could be arranged to exit snooze influenced setup and restart and then wait when connected to allow programming. The problem it seems is the effect of Snooze taking the processor to low power states and getting USB faithfully restored.

  5. #5
    Senior Member duff's Avatar
    Join Date
    Jan 2013
    Location
    Las Vegas
    Posts
    904
    Quote Originally Posted by Joegi View Post
    "If (Serial)" needs an open serial monitor to return TRUE!
    Isn't there a (direct) way like on the Atmega32u4 ( "USBCON |= 1<<OTGPADE; if ((USBSTA & (1<<VBUS)) && Serial.dtr()){}" )?
    Thanks!
    There is USBSerial driver for Snooze that might help too. Current Serial USB code doesn't reset the dtr when USB has been disconnected (what happens in all sleep modes for Snooze) while powered from an external source, Snooze USBSerial driver resets the dtr driver for you. There is example too. As far as waking up the Teensy in Hibernate with a usb connection you can monitor V_USB pin with the proper voltage divider or such to a digital pin to wake it up when it sees a voltage from plugging in the USB.
    Last edited by duff; 01-04-2018 at 09:55 PM.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,607
    "If (Serial)" actually checks for 2 things.

    1: The configure device command was received, meaning it has completed reading descriptors to identify the device

    2: The set baud+parameters command was received with DTR asserted, meaning a program running on the PC has opened the serial device.

  7. #7
    Senior Member duff's Avatar
    Join Date
    Jan 2013
    Location
    Las Vegas
    Posts
    904
    Quote Originally Posted by PaulStoffregen View Post
    "If (Serial)" actually checks for 2 things.

    1: The configure device command was received, meaning it has completed reading descriptors to identify the device

    2: The set baud+parameters command was received with DTR asserted, meaning a program running on the PC has opened the serial device.
    So what happens with current USB Serial code if I disconnect the USB but keep the Teensy powered and running and plug it back in? Will "while(!Serial)" work? From what I could tell the "usb_cdc_line_rtsdtr_millis" is not updated in this scenario so it doesn't wait.

    Also would you know what the proper way to shut down the USB connection through software? I currently put the usb in a suspend state(USB0_USBCTRL |= USB_USBCTRL_SUSP) and then disbale the usb(USB0_CTL &= ~USB_CTL_USBENSOFEN) and lastly turn off its clock(SIM_SCGC4 &= ~SIM_SCGC4_USBOTG) and pretty much reverse that when waking back up.

  8. #8
    Member
    Join Date
    Jan 2014
    Location
    Cologne, germany
    Posts
    41
    Quote Originally Posted by duff View Post
    There is USBSerial driver for Snooze that might help too. Current Serial USB code doesn't reset the dtr when USB has been disconnected (what happens in all sleep modes for Snooze) while powered from an external source, Snooze USBSerial driver resets the dtr driver for you. There is example too. As far as waking up the Teensy in Hibernate with a usb connection you can monitor V_USB pin with the proper voltage divider or such to a digital pin to wake it up when it sees a voltage from plugging in the USB.
    Sorry, but I forgot to remove the "&& Serial.dtr())" from the copied Atmega example code which might have caused confusion. The thing is that I just want to check if an usb-connection has been established! It's a good idea to check if "V_USB" has 5V, but the Teensy already is soldered onto a pcb which is placed in a tight housing so that adding things would be complicated! I might have to add that disconnecting the Teensy from the external power source is practically impossible!

  9. #9
    Senior Member duff's Avatar
    Join Date
    Jan 2013
    Location
    Las Vegas
    Posts
    904
    Quote Originally Posted by Joegi View Post
    The thing is that I just want to check if an usb-connection has been established!
    With software, (USB Serial)? If so then what I asked paul is relevent becasue the dtr timer check in "if (!Serial);" will already be timed out and return true, so after if you disconnect the usb cable and plug it back in that dtr timer check is not reset as when you power off the Teensy and start it back up again.
    Quote Originally Posted by Joegi View Post
    It's a good idea to check if "V_USB" has 5V, but the Teensy already is soldered onto a pcb which is placed in a tight housing so that adding things would be complicated! I might have to add that disconnecting the Teensy from the external power source is practically impossible!
    This would be the only way to wakeup from Hibernate with inserting USB cable. There is no wakeup event for a USB connect or disconnect for the Teensy in Device mode. You could solder a through hole resistor divider to a wakeup pin from VUSB if you have access to the top of the Teensy. If you don't have access not sure what you are going to do it.

  10. #10
    Member
    Join Date
    Jan 2014
    Location
    Cologne, germany
    Posts
    41
    Quote Originally Posted by duff View Post
    There is no wakeup event for a USB connect or disconnect for the Teensy in Device mode.
    I don't need a wakeup event, I would wait until the Teensy wakes up. After the Teensy has woken up it would check for a usb connection and then switch on a led (and not go back to hibernate mode for a certain time) so that I would know that I'm able to upload code! Thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •