arduino / ArduinoCore-samd

Arduino Core for SAMD21 CPU
GNU Lesser General Public License v2.1
470 stars 717 forks source link

SAMD21 USBDeviceClass::ISRHandler() called continuously. #575

Open studiohsoftware opened 3 years ago

studiohsoftware commented 3 years ago

Running the MIDIUSB library on MKR1000, I discovered with a debug statement in USBDeviceClass::ISRHandler() within USBCore.cpp, the handler is called continuously.

Specifically, adding Serial1.println("Handler Called"); to the first line of ISRHandler() exposed the problem.

I did not look deeper to find the specific flag in DEVICE.INTFLAG causing the problem, and whether or not that flag is required for enumeration. However, executing this statement after enumeration seems to solve the problem. USB->DEVICE.INTENCLR.reg = 0xFF;

matthijskooijman commented 3 years ago

What is "continuously"? I'd expect the handler might be called ever 1ms, when start-of-frame (SOF) is received, if that ISR is enabled? If so, that would be by design, I think? Or are you saying it is called really all the time, blocking the main loop completely?

studiohsoftware commented 3 years ago

It is indeed the SOF. USBDeviceClass::ISRHandler() appears to explicitly use it only to toggle LEDs.

However it must be the case that your enumeration code within the ISR depends on it implicitly because I get an error on the host side without it. Indeed it appears that we get a stall.

Perhaps you can disable it once your enumeration is done with it.

Or, on the other hand, if the hardware requires a SOF ACK from user code whenever iso or interrupt pipes are in use, maybe there could be a way to optionally disable it from the sketch when those pipes are not in use so user code is not interrupted every 1ms.