Skip to content

Unification of wait functions #2274

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

Merged
merged 2 commits into from
Aug 9, 2016
Merged

Unification of wait functions #2274

merged 2 commits into from
Aug 9, 2016

Conversation

bogdanm
Copy link
Contributor

@bogdanm bogdanm commented Jul 27, 2016

This commit adds two implementations for the mbed wait functions (wait,
wait_ms, wait_us):

  • with the RTOS present, the wait functions will use Thread::wait for
    millisecond delays and a busy wait loop for sub-millisecond delays.
  • with the RTOS not present, the wait functions will always use a busy
    wait loop.

This commit adds two implementations for the mbed wait functions (wait,
wait_ms, wait_us):

- with the RTOS present, the wait functions will use `Thread::wait` for
  millisecond delays and a busy wait loop for sub-millisecond delays.
- with the RTOS not present, the wait functions will always use a busy
  wait loop.
@bogdanm
Copy link
Contributor Author

bogdanm commented Jul 27, 2016

@sg- @0xc0170 @geky

Possible issues with this PR:

  • wait_us will likely not have the same accuracy with RTOS and without RTOS.
  • the mbed wait function takes a float that specifies seconds as argument, as opposed to Thread::wait that takes a uint32_t that specifies milliseconds. This can be potentially confusing.

@geky
Copy link
Contributor

geky commented Jul 27, 2016

+1 This looks great to me!

Now that I think about it, won't the previous wait_us have the same accuracy issue with the rtos? Unless interrupts are disabled the busy loop could be prempted at any time.

@bogdanm
Copy link
Contributor Author

bogdanm commented Jul 27, 2016

You're right, any blocking function that relies on its blocking behaviour for timing will have a problem in a preemptive RTOS environment.
Another potential improvement would be to check if wait_us is called from an interrupt context and switch to blocking behaviour if it is (it might make sense to allow blocking wait behaviour in an interrupt context for short wait times).

@geky
Copy link
Contributor

geky commented Jul 27, 2016

I think this may actually handle interrupt context correctly based on the implementation of core_util_are_interrupts_enabled:
https://github.com/mbedmicro/mbed/blob/5fea6e69ec1aec4c56852f2e959002dc815eb480/hal/common/mbed_critical.c#L28

}

void wait_ms(int ms) {
wait_us(ms * 1000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might consider having wait and wait_ms use Thread:wait exclusively, since the RTOS will get you very close to ms resoultion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd mean adding core_utils_are_interrupts_enabled checks to each wait function. Not sure this is needed.

@sg-
Copy link
Contributor

sg- commented Jul 27, 2016

LGTM 👍

@bogdanm
Copy link
Contributor Author

bogdanm commented Aug 8, 2016

Added a new commit that potentially improves the accuracy of wait_us, as suggested by @c1728p9

@bogdanm bogdanm merged commit 2f127cb into master Aug 9, 2016
@bogdanm bogdanm deleted the unified_wait branch August 9, 2016 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants