You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
USBDevice.connected() should return true if and only if the device is connected to a host.
Its actual return value does not reflect the connection state. If called repeatedly in a tight loop in connected state it shows periodic behavior. Every 2 seconds it returns false for 0.25s and true for the remaining time.
Error 1: FNUM is a 11bit number. Writing it to uint8_t variable discards the upper three bits and makes the comparison in the return statement invalid if FNUM is >= 256.
Error 2: Due to the commented out delay call, the two FNUM reads result in the same value with high probability, if I understand Wikipedia correctly, because it changes only every millisecond.
The errors explain the observed behavior. In connected state FNUM changes every millisecond. For 0 <= FNUM < 256 the return value is false. For 256 <= FNUM < 2048 the return value is true. In disconnected state FNUM does not change. Depending on its (random) value the function returns either true or false as described.
The following code fixes the issues for me.
boolUSBDeviceClass::connected()
{
// Count frame numbersuint16_tf=USB->DEVICE.FNUM.bit.FNUM;
delay(1); // wait for next SOFreturnf!=USB->DEVICE.FNUM.bit.FNUM;
}
Happy to raise a PR.
The text was updated successfully, but these errors were encountered:
pillilz
added a commit
to pillilz/ArduinoCore-samd
that referenced
this issue
Feb 18, 2022
USBDevice.connected()
should returntrue
if and only if the device is connected to a host.Its actual return value does not reflect the connection state. If called repeatedly in a tight loop in connected state it shows periodic behavior. Every 2 seconds it returns
false
for 0.25s andtrue
for the remaining time.This is cause by two errors in the code:
Error 1: FNUM is a 11bit number. Writing it to
uint8_t
variable discards the upper three bits and makes the comparison in thereturn
statement invalid if FNUM is >= 256.Error 2: Due to the commented out
delay
call, the twoFNUM
reads result in the same value with high probability, if I understand Wikipedia correctly, because it changes only every millisecond.The errors explain the observed behavior. In connected state
FNUM
changes every millisecond. For0 <= FNUM < 256
the return value isfalse
. For256 <= FNUM < 2048
the return value istrue
. In disconnected stateFNUM
does not change. Depending on its (random) value the function returns eithertrue
orfalse
as described.The following code fixes the issues for me.
Happy to raise a PR.
The text was updated successfully, but these errors were encountered: