fix racing issue for SAMD when executing WFI #2956
Merged
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 ofN
seconds.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