-
Notifications
You must be signed in to change notification settings - Fork 1.3k
turn on ticks during USB writes #4075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The general outlines of this make sense, in retrospect. No testing performed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced this is the correct fix. It's not clear to me that we've gotten to the point of understanding what we aren't doing but should be.
This actually has two main changes and we should only need one.
- It always runs
usb_background
in supervisor_background_tasks. This circumvents the callback queue that @jepler added. - It enables ticks during writes which limits sleep times to a tick period which then runs
supervisor_background_tasks
more frequently when sleeping. Is this problem only present whentime.sleep
is used? Is the problem fixed whenwfi
isn't used?
tud_task only needs to be called if an event has been queued up there is no magic "call once a ms" requirement. (It returns quickly when the queue is empty: https://github.com/hathach/tinyusb/blob/045674745afa59028fbeed6dac5cb5a9c4a6033e/src/device/usbd.c#L457 ) If we're not calling it when we should, that means something was queued without our detection. I assume that all events are queued by an interrupt (which should trigger our background task as-is) but they could be queued by tud_task itself as well.
Instead of calling usb_background
in background_tasks try calling tud_task_event_ready
to see if there is a task queued up and then also check the background task queue to see if usb is queued. The event that is queued in tinyusb but not circuitpython is your culprit.
If the event changes, maybe there is a usb interrupt between when the usb background task completes and when it's removed from the task queue.
I don't think we actually disagree about this. We don't understand the root cause. I am fixing the symptom, and expect we can fix it more properly later. My goal is to make things work better for now, because this seems like a big support headache and regression otherwise. Then we can debug the root cause at our leisure. If you think this is not as serious a problem as I think it is, I can turn this to draft or close it for now. It just seemed kind of bad to me.
That was an easy addition so I could get
It can happen when absolutely nothing is going on: no
I think that assumption may not be true. It appears that
Right, I agree, I think the "culprit" is some work that's queued that has no corresponding interrupt, so we have to poll for it. I don't know whether that's an actual bug or not.
I could just set a flag in the usb interrupt routine and check for this to detect this race condition. The fundamental question is whether we're missing an interrupt, or whether the queued work has no relation to an interrupt. I am assuming the latter for now, and that's what this (probably temporary) fix is based on. |
Closed in favor of #4122. |
Fixes #3986.
Before 6.0.0, there was alway a 1ms tick mechanism that ran a background task. The task include a call to
tud_task()
. Starting in 6.0.0, we turned off 1ms ticks all the time, and now we currently calltud_task()
only when there's an interrupt.It appears we need to call it more often than that when USB writes happen, because some writes are not triggered by an interrupt.
So, this PR adds
usb_background()
back to the supervisor tick background task. And, when a write starts, we enable the 1ms ticks sotud_task()
will be called on every tick. When the write completes, we turn off ticks for us. (If someone else needs ticks, they stay on, due totick_enable_count
.)@hathach thinks there may be some other reason writes are not being scheduled properly, but this appears to solve the problem for now. This fix could be removed or modified when more analysis is done in TinyUSB about this issue.