arduino / arduino/ArduinoCore-samd
Lost characters in Serial.print with many Interrupts in background
- Dominant language
- C
- Stars
- 502
- Forks
- 740
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
I'm logging to SERIAL and have a lot (~3000 per sec) of iterrupts.
When having the interrupts active, the SERIAL.print() often drops characters (return 0 bytes written). This was reproducable on MKRzero and on MKR1000.
Fix is a small change in file USBCore.cpp:
When checking for last send was finished, its first tested for IsReady, then it waitet for TransferComplete. My assuption is, that ISR is happening inbetween, so there is a bigger time difference between both lines.
Adding in the wait loop the condition for IsReady solved the issue.
Here the code:
uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
{
[...]
while (len != 0)
{
if (usbd.epBank1IsReady(ep)) {
// previous transfer is still not complete
[...]
// Wait for (previous) transfer to complete
// inspired by Paul Stoffregen's work on Teensy
// [Pae] old version : while (!usbd.epBank1IsTransferComplete(ep) && usbd.epBank1IsReady(ep)) {
while (!usbd.epBank1IsTransferComplete(ep) && usbd.epBank1IsReady(ep)) {
// [Pae] ^^^ ^^^^^^^^^^^^^^
if (LastTransmitTimedOut[ep] || timeout-- == 0) {
LastTransmitTimedOut[ep] = 1;
// set byte count to zero, so that ZLP is sent
// instead of stale data
usbd.epBank1SetByteCount(ep, 0);
return -1;
}
}
}
What are your thoughts?
Can this be fixed?
Thanks and best regards
Achim
Contributor guide
No contributing guide indexed for this repository
Research direction
The issue points to USBCore.cpp and USBDeviceClass::send; start by reviewing the IsReady and TransferComplete checks in the send loop. Reproduce on an MKRzero or MKR1000 with about 3,000 interrupts per second and serial logging, then verify that characters are no longer dropped and writes do not return zero bytes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100