[Feature request] Reliably detect Host PC suspending USB port

FeuerSturm

Member
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!
 
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.
 
Does `if (!Serial)` work?

Unfortunately it's exactly like defragster said, it just stays TRUE no matter what. :(

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.
 
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 ...
 
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.
 
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?
 
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.
 
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.
 
Back
Top