Skip to content

Use LP tickers for waiting in no RTOS builds when available #9960

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 1 commit into from
May 21, 2019
Merged

Use LP tickers for waiting in no RTOS builds when available #9960

merged 1 commit into from
May 21, 2019

Conversation

marcuschangarm
Copy link
Contributor

@marcuschangarm marcuschangarm commented Mar 6, 2019

Description

For bare metal builds, use the lp_ticker for calls to wait_ms.

Pull request type

[x] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

Reviewers

Release Notes

For bare metal builds, use the lp_ticker for calls to wait_ms instead of us ticker if lp_ticker is available.

@ciarmcom
Copy link
Member

ciarmcom commented Mar 6, 2019

@marcuschangarm, thank you for your changes.
@ARMmbed/mbed-os-core @ARMmbed/mbed-os-maintainers please review.

@ciarmcom ciarmcom requested review from a team March 6, 2019 16:00
@deepikabhavnani
Copy link

@marcuschangarm - This will enable both lpticker and usticker by default.
Also using lpticker will change the functional behavior of wait - as precision will be in msec and not usec, will be considered breaking change.

Faced similar challenges with RTOS version - #8189

@kjbracey-arm @bulislaw - Can provide more input on this for bare metal version.

@marcuschangarm
Copy link
Contributor Author

This will enable both lpticker and usticker by default.

Only if the user actually calls wait_us. Also, presumably the cost of enabling the lp ticker is negligible if you already have the us ticker running.

Also using lpticker will change the functional behavior of wait - as precision will be in msec and not usec, will be considered breaking change.

We can keep the float version as-is. I'm more concerned about having to use the us ticker by default for wait_ms on systems that supports lp ticker.

@deepikabhavnani
Copy link

deepikabhavnani commented Mar 6, 2019

Only if the user actually calls wait_us

wait_us is called from multiple places in mbed-os code base

We can keep the float version as-is.

Limitation is not of float, lpticker precision is in msec

@bulislaw
Copy link
Member

bulislaw commented Mar 6, 2019

This will enable both lpticker and usticker by default.

Only if the user actually calls wait_us. Also, presumably the cost of enabling the lp ticker is negligible if you already have the us ticker running.

Could we check what's the difference?

Also using lpticker will change the functional behavior of wait - as precision will be in msec and not usec, will be considered breaking change.

We can keep the float version as-is. I'm more concerned about having to use the us ticker by default for wait_ms on systems that supports lp ticker.

I'm also afraid we are going further and further apart between RTOS and non-RTOS implementations, wait_ms with RTOS will use RTX osDelay. The way it works is a bit weird and timing is absolutely not guaranteed (I think it's something like osDelay(5) for 1ms tick will sleep for 4-5ms). It's good idea to use LPTicker rather than usticker whenever possible, but we should clean up the "wait" calls and make them behave in a predictable way between both OS variants.

@marcuschangarm
Copy link
Contributor Author

Limitation is not of float, lpticker precision is in msec

Exactly, I would expect msec precision from an API that takes msec as input and usec precision from an API that takes usec as input.

@deepikabhavnani
Copy link

deepikabhavnani commented Mar 6, 2019

Exactly, I would expect msec precision from an API that takes msec as input and usec precision from an API that takes usec as input.

100% with you on this, just that existing implementation was not like that :-( hence functionality change.
Forgot to mention - Problem is with wait - msec or usec.

@kjbracey
Copy link
Contributor

kjbracey commented Mar 7, 2019

wait_us is called from multiple places in mbed-os code base

True, but I believe all instances in the core code are because they specifically need an "IRQ-safe busywait" rather than "sleep", not because they want precision. Not sure about stuff in targets.

This ties in with JIRA ISGDEVPREQ-1083, in which I propose fundamentally separating the wait APIs from the sleep ones - wait calls would be the same busy waits in RTOS and non-RTOS. I hadn't proposed retaining wait_ms (as opposed to sleep) as part of that, but maybe it is needed in specialised circumstances. Up to now, that call was just going to be removed, on the basis that any busy-waiting milliseconds delay is not to be encouraged.

What most normal users of wait_ms really want to be doing is using ThisThread::sleep_for(), and a non-RTOS implementation of that can stop the CPU, sleep the HAL and wait for an interrupt, achieving much better power than this busy wait. That's something we need to get on and implement.

There was recent talk about changing wait_us to have a non-DEVICE_USTICKER fallback to wait_ns for secure code, justified on not having any timers at all in a secure image. Conceivably that should be also done manually in core code like mbed_die that uses timers to avoid ever starting up or pulling in any HAL ticker code.

For a non-RTOS build, I think having at least a low power timer running is going to be almost a given - you need something to be your timebase. Emulation of rtos calls especially Kernel::get_ms_count() would also need it. I think we should concentrate on eliminating unnecessary use of the microsecond timer, and assume the low power is what will be normally active.

But more care is needed in the "always present" bits to avoid pulling any timers into bootloaders or secure libraries that otherwise need no timing at all - either don't try for blinky LED on mbed_die or use wait_ns CPU spins.

So, this PR is an incremental improvement, but if we're going to go ahead with wider changes proposed in ISGDEVPREQ-1083, both changes are redundant, as both affected calls are to be deprecated. Still, no reason to block this, so I basically approve.

@cmonr
Copy link
Contributor

cmonr commented Mar 28, 2019

so I basically approve.

👍

@cmonr cmonr requested a review from a team March 28, 2019 18:49
@0xc0170
Copy link
Contributor

0xc0170 commented Apr 12, 2019

@ARMmbed/mbed-os-core Please review

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 15, 2019

Scheduling Ci while finalizing reviews

@mbed-ci
Copy link

mbed-ci commented Apr 15, 2019

Test run: FAILED

Summary: 6 of 7 test jobs failed
Build number : 1
Build artifacts

Failed test jobs:

  • jenkins-ci/mbed-os-ci_mbed2-build-ARM
  • jenkins-ci/mbed-os-ci_mbed2-build-GCC_ARM
  • jenkins-ci/mbed-os-ci_build-IAR
  • jenkins-ci/mbed-os-ci_mbed2-build-IAR
  • jenkins-ci/mbed-os-ci_build-ARM
  • jenkins-ci/mbed-os-ci_build-GCC_ARM

uint32_t start = ticker_read(ticker);
while ((ticker_read(ticker) - start) < (uint32_t)(ms * 1000));
#else
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.

missing ; - see the build failures

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops! 😄

@adbridge
Copy link
Contributor

ci started

@mbed-ci
Copy link

mbed-ci commented Apr 25, 2019

Test run: FAILED

Summary: 1 of 11 test jobs failed
Build number : 4
Build artifacts

Failed test jobs:

  • jenkins-ci/mbed-os-ci_greentea-test

@adbridge
Copy link
Contributor

@marcuschangarm please take a look at the test failure

@0xc0170
Copy link
Contributor

0xc0170 commented May 3, 2019

I restarted the test, the latest test logs have no failure, and hook showed error that should be fixed now on master.

@adbridge
Copy link
Contributor

adbridge commented May 7, 2019

Restarted ci

@mbed-ci
Copy link

mbed-ci commented May 7, 2019

Test run: FAILED

Summary: 2 of 11 test jobs failed
Build number : 5
Build artifacts

Failed test jobs:

  • jenkins-ci/mbed-os-ci_dynamic-memory-usage
  • jenkins-ci/mbed-os-ci_greentea-test

@0xc0170
Copy link
Contributor

0xc0170 commented May 12, 2019

CI restarted

@0xc0170
Copy link
Contributor

0xc0170 commented May 15, 2019

@marcuschangarm Can you rebase this PR please? the error in dynamic memory job is configuration error that I've noticed in another Pr that is also weeks old (assuming the CI job was updated - configuration changed). Rebase should fix it

For bare metal builds, use the lp_ticker for calls to wait_ms.
@adbridge
Copy link
Contributor

CI started

@mbed-ci
Copy link

mbed-ci commented May 17, 2019

Test run: SUCCESS

Summary: 11 of 11 test jobs passed
Build number : 6
Build artifacts

@0xc0170 0xc0170 requested a review from kjbracey May 20, 2019 09:31
Copy link
Contributor

@kjbracey kjbracey left a comment

Choose a reason for hiding this comment

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

Still fine with this - I will need to rebase my #10104. (I think it does the same thing differently, will double-check).

@0xc0170 0xc0170 merged commit 5be51a5 into ARMmbed:master May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants