fix racing issue for SAMD when executing WFI#2956
Merged
tannewt merged 1 commit intoadafruit:masterfrom May 26, 2020
Merged
Conversation
dhalbert
reviewed
May 26, 2020
| #endif | ||
| __WFI(); | ||
| common_hal_mcu_disable_interrupts(); | ||
| if (!tud_task_event_ready()) { |
Collaborator
There was a problem hiding this comment.
@tannewt Are there more cases here when we wouldn't want to go into WFI? Other pending tasks? This relates to trying to redo the background task stuff to add flags to indicate pending work.
But I think this is a good change for now to get things working better.
Member
There was a problem hiding this comment.
Not that I can think of immediately. We should rethink it with the background work.
tannewt
approved these changes
May 26, 2020
Member
tannewt
left a comment
There was a problem hiding this comment.
Thank you for the fix! We should make sure and update other ports too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix #2912 and/or other "slow responses" with USB after low power PR. It is basically the same as #2868 for nRF. There is lots of discussion there, I will sum it up here. Following is the racing.
tud_task()isn't called yet to response, there is no more USB event. CPU has a good full sleep ofNseconds.It is the racing between usb isr -> event queue -> tud_task() handling and WFI.
Note: this is NOT what we want to do, we don't want to sleep while there are USB events in the queue and host is waiting --> therefore we must make sure all the usb events in the queue task are processed before executing WFI. That's way when there is a new request (IN/OUT) from host, we could wake up and handle.
Following is reference to other platform implementation of WFI, disabling interrupt and DSB is mostly required.
Yeah, I did a bit of research it is advisable prat to disable the interrupt before WFI() to avoid race condition. WFI() doesn't require interrupt enabled to wake CPU. It is best practice to also all DSB() before WFI() as well according to ARM docs. Here is the implementation of zephyr
https://github.com/zephyrproject-rtos/zephyr/blob/master/arch/arm/core/aarch32/cpu_idle.S#L75
another reference from freeRTOS tickless mode, which do the same as this function
https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/master/portable/GCC/ARM_CM4F/port.c#L510
There is a other useful doc from mbed as well.
https://os.mbed.com/media/uploads/pateshian/wfi_wake_up_from_sleep_11_17_2016_.pdf (D_SLEEP_VERSION == 2)
Originally posted by @hathach in https://github.com/_render_node/MDIzOlB1bGxSZXF1ZXN0UmV2aWV3VGhyZWFkMjY2MTQxNzg4OnYy/pull_request_review_threads/discussion