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

Thread: How to detect USB state changes - suspend/resume in particular?

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

    How to detect USB state changes - suspend/resume in particular?

    Hi,

    is there a callback or function that I can use to keep
    track of USB state changes?
    I'd need to know when the USB state changes to suspended
    and resume to turn off and on attached led strips.

    I've looked through the source files but didn't see anything
    ready to use, any ideas?

    Thanks in advance!

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,874
    Seems some weeks/months back a status register (?) was indicated to track the connect state?

  3. #3
    Junior Member FeuerSturm's Avatar
    Join Date
    Jan 2023
    Posts
    15
    Quote Originally Posted by defragster View Post
    Seems some weeks/months back a status register (?) was indicated to track the connect state?
    To be honest, I have no idea what you're trying to tell me

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,874
    Quote Originally Posted by FeuerSturm View Post
    To be honest, I have no idea what you're trying to tell me
    ... with a search of forum there is a posting making note of a status register that changes when USB drops and returns IIRC.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,874
    quick search of 'usb status' found this down some dozen lines: pjrc.com/threads/70721-detecting-usb-connection-on-a-teensy-4-0

    What finally worked was to check the suspend flag in the PORTSC1 register. 1 = Suspend state, 0 = not in Suspend state.

  6. #6
    Junior Member FeuerSturm's Avatar
    Join Date
    Jan 2023
    Posts
    15
    Quote Originally Posted by defragster View Post
    quick search of 'usb status' found this down some dozen lines: pjrc.com/threads/70721-detecting-usb-connection-on-a-teensy-4-0
    Thank you, this is what I was looking for.
    I did search various different combinations of terms, but that post didn't come up.

  7. #7
    Junior Member FeuerSturm's Avatar
    Join Date
    Jan 2023
    Posts
    15
    I've tried that approach and it is working unreliable for me at most.

    I'm using this simple test code:
    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);
    		}
    	}
    }
    The led does turn on, but randomly turns off for seconds and turns on again.
    Am I doing something wrong?

    Thanks!

Posting Permissions

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