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

Thread: [Feature request] Reliably detect Host PC suspending USB port

  1. #1
    Junior Member FeuerSturm's Avatar
    Join Date
    Jan 2023
    Posts
    15

    [Feature request] Reliably detect Host PC suspending USB port

    Hi,

    I have ported my project from using TinyUSB stack and FastLED on a generic Raspberry Pi Pico to
    a Teensy 4.X with using your amazing OctoWS2811 library, I've got almost everything to work perfect,
    but there's one thing that nags me: the lack of being able to reliably detect the PC suspending the USB ports

    Using TinyUSB allowed me to simply check for the state of the USB port with
    Code:
    bool portSuspended = TinyUSBDevice.suspended();
    and I desperatly need such a feature.
    I asked for help on the forums already and receivedd a reply with a possible solution reading the suspend flag in the PORTSC1,
    unfortunately that's not really working for me at all, using this test sketch showed that it seems to be totally random when the
    onboard led turns on or off:

    Code:
    unsigned long lastCheck;
    
    void setup()
    {
    	pinMode(LED_BUILTIN, OUTPUT);
    }
    
    void loop()
    {
    	unsigned long currentMillis = millis();
    	if(currentMillis - lastCheck >= 1000)
    	{
    		lastCheck = currentMillis;
    		if(!bitRead(USB1_PORTSC1,7))
    		{
    			digitalWrite(LED_BUILTIN, HIGH);
    		} 
    		else
    		{
    			digitalWrite(LED_BUILTIN, LOW);
    		}
    	}
    }
    I have then checked the i.MX RT1060 Processor Reference Manual, Rev. 2, 12/2019 and tried everything that I could find searching for "suspend" inside it, no success at all.

    I'd really love to see a callback or function to reliably check if the PC the Teensy is connected to is on or off (port suspended or not).

    Thanks!

  2. #2
    Senior Member
    Join Date
    Mar 2017
    Location
    Oakland, CA, USA
    Posts
    693
    Does `if (!Serial)` work?

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    17,109
    Once if(Serial) goes TRUE is stays that way.

    Not finding it but post last year suggested a register to check can identify if Host connect is active.

  4. #4
    Junior Member FeuerSturm's Avatar
    Join Date
    Jan 2023
    Posts
    15
    Quote Originally Posted by shawn View Post
    Does `if (!Serial)` work?
    Unfortunately it's exactly like defragster said, it just stays TRUE no matter what.

    Quote Originally Posted by defragster View Post
    Once if(Serial) goes TRUE is stays that way.

    Not finding it but post last year suggested a register to check can identify if Host connect is active.
    I've been searching the forums for hours literally and tried everything that people suggested additionally to searching
    through the Processor Reference Manual and trying everything that could even lightly be related, no success at all unfortunately.

    If TinyUSB can do that with a Pi Pico, I am pretty sure there's a way with a Teensy 4.X as well.

    Thanks for your replies so far, gentlemen.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    17,109
    Wasn't tried here - but there was a post and it seems I dug it up some weeks back in reference for another and re-linked ...

  6. #6
    Junior Member FeuerSturm's Avatar
    Join Date
    Jan 2023
    Posts
    15
    Quote Originally Posted by defragster View Post
    Wasn't tried here - but there was a post and it seems I dug it up some weeks back in reference for another and re-linked ...
    That might have been my post in Project Guidance, you digged out that "USB1_PORTSC1" suggestion, that unfortunately doesn't work the way I need it, as it flags the device as suspended after 3ms without activity on the bus
    which doesn't help me with my use case.

  7. #7
    Junior Member FeuerSturm's Avatar
    Join Date
    Jan 2023
    Posts
    15
    I cannot believe that I am the only one that would be interested in such a vital feature.

    Can I pretty please at least get some feedback if this is being considered at all?

  8. #8
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    1,092
    Does this thread, msg#4, help you?

    Paul

  9. #9
    Junior Member FeuerSturm's Avatar
    Join Date
    Jan 2023
    Posts
    15
    Quote Originally Posted by PaulS View Post
    Does this thread, msg#4, help you?

    Paul
    Nope, already tried that (see first post) and any other registers I found in the processor docs.
    That just reports "suspended" when there's no activity on the bus for 3ms,
    that's not what I need.

  10. #10
    Try checking USB1_USBSTS for USB_USBSTS_SLI ("if (USB1_USBSTS & USB_USBSTS_SLI) ...")

    If it works, you can enable the interrupt for that bit (USB_USBINTR_SLE) to be automatically notified when it changes.

  11. #11
    Senior Member
    Join Date
    Feb 2018
    Location
    Corvallis, OR
    Posts
    440
    Quote Originally Posted by FeuerSturm View Post
    Hi,

    I have ported my project from using TinyUSB stack and FastLED on a generic Raspberry Pi Pico to
    a Teensy 4.X with using your amazing OctoWS2811 library, I've got almost everything to work perfect,
    but there's one thing that nags me: the lack of being able to reliably detect the PC suspending the USB ports

    Using TinyUSB allowed me to simply check for the state of the USB port with
    Code:
    bool portSuspended = TinyUSBDevice.suspended();
    and I desperatly need such a feature.
    I asked for help on the forums already and receivedd a reply with a possible solution reading the suspend flag in the PORTSC1,
    unfortunately that's not really working for me at all, using this test sketch showed that it seems to be totally random when the
    onboard led turns on or off:

    Code:
    unsigned long lastCheck;
    
    void setup()
    {
    	pinMode(LED_BUILTIN, OUTPUT);
    }
    
    void loop()
    {
    	unsigned long currentMillis = millis();
    	if(currentMillis - lastCheck >= 1000)
    	{
    		lastCheck = currentMillis;
    		if(!bitRead(USB1_PORTSC1,7))
    		{
    			digitalWrite(LED_BUILTIN, HIGH);
    		} 
    		else
    		{
    			digitalWrite(LED_BUILTIN, LOW);
    		}
    	}
    }
    I have then checked the i.MX RT1060 Processor Reference Manual, Rev. 2, 12/2019 and tried everything that I could find searching for "suspend" inside it, no success at all.

    I'd really love to see a callback or function to reliably check if the PC the Teensy is connected to is on or off (port suspended or not).

    Thanks!
    If the code you posted is actually the code you tested, I'm not surprised that your results are random. The bit definition notes on page 2476 of the Rev. 3 reference manual seem to indicate that this bit may not respond as you expect if a lot of conditions are not set up properly. I'm just getting started using the host port, but I assume that there's more to using it than simply reading a register without some (missing) port setup functions.

Posting Permissions

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