Skip to content

Added function to get the timestamp of the next us_ticker event. #931

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
Feb 26, 2015

Conversation

jeremybrodt
Copy link
Contributor

This function will allow us to have a tickless wakeup from deepstop using a low-speed RTC timer.

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 24, 2015

Can you elaborate, how this would allow you wakeup? can provide a code snippet.

@jeremybrodt
Copy link
Contributor Author

To minimize power in deepsleep, the high frequency clock to the us_ticker can be disabled. The RTC is clocked from a slower, low-power clock.

When deepsleep is called, the next event timestamp is retrieved from the us_ticker and an RTC alarm is set to wakeup at the next event time. Upon wakeup, the us_ticker is updated an reenabled. This allows you to have high resolution timing during run mode, and low power consumption during deepsleep.

@Sissors
Copy link
Contributor

Sissors commented Feb 25, 2015

I guess this would do the same as just using a set time for deepsleep such as WakeUp library does, only it allows you to check when the next ticker event is going to happen. What do you do with all the events in the queue though? Because if you go into deepsleep for lets say the 2 seconds until the next ticker event happens (then automatically the high frequency clocks will be disabled), and you wake up again, then the other events are still in the queue, with their original timestamps, and the us_ticker timer is still at its old time, two seconds in the past.

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 25, 2015

@jeremybrodt how do you update the ticker and reenable? There's no function which would allow to do it.

@Sissors A good question, what to do with us ticker event's queue if the system is about to enter the deep sleep ? Fire all events right before the entering the deep_sleep()?

@jeremybrodt
Copy link
Contributor Author

The HAL implementation that we are working on supports these functions. Here is a code snippet from deepsleep().

// Get the next mbed timer expiration
sleepDurationUs = us_ticker_get_next_timestamp() - sleepStartTickerUs;

if (sleepDurationUs < MIN_DEEP_SLEEP_US) {
    /* The next wakeup is too soon. */
    return;
}

// Disable the us_ticker. It won't be clocked in DeepSleep
us_ticker_deinit();

// Prepare to wakeup from the RTC
rtc_set_wakeup(sleepStartRtcUs + sleepDurationUs);

// Deep sleep for ARM core
SCB->SCR = SCB_SCR_SLEEPDEEP_Msk;
__WFE();
// Wakeup

// Get the elapsed time from the RTC. Wakeup could have been from some other event.
elapsedUs = sleepEndRtcUs - sleepStartRtcUs;

// Update the us_ticker. It was not clocked during DeepSleep
us_ticker_init();
us_ticker_set(sleepStartTickerUs + elapsedUs);
us_ticker_set_interrupt(us_ticker_get_next_timestamp());

@jeremybrodt
Copy link
Contributor Author

@0xc0170 All other events in the queue are later than the event that would be returned by us_ticker_get_next_timestamp(). There is no need to fire them all. They do not occur until after the next scheduled wakeup.

@jeremybrodt
Copy link
Contributor Author

@Sissors What is the WakeUp library?

@Sissors
Copy link
Contributor

Sissors commented Feb 25, 2015

Library of mine for timed waking from deepsleep: https://developer.mbed.org/users/Sissors/code/WakeUp/

@jeremybrodt
Copy link
Contributor Author

Ah. That looks like a useful library. Without my proposed function, how do you know how long you can go to sleep without missing an event?

@Sissors
Copy link
Contributor

Sissors commented Feb 25, 2015

That is up for the user to set. But generally it is intended for for example sensor applications, where you want to sample a sensor roughly every minute, and sleep in the meantime. Then you don't have other tickers running.

But you are correct that something like this would be a useful addition for systems with multiple different tickers with different periods where it may sleep in between. Of course it is still then up to the user to decide if it should go to sleep during that time. Just because no tickers are active does not mean no other code is active ;).

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 25, 2015

@jeremybrodt the snippet you shared looks like a small scheduler. It's more clear now the intention of the usage of this functionality, thanks.

We might consider deinit(), reinit(arguments required to us ticker to function again). A new issue can be created for this, a new functionality. Something to think about.

0xc0170 added a commit that referenced this pull request Feb 26, 2015
Us ticker - add function to get the timestamp of the next us_ticker event.
@0xc0170 0xc0170 merged commit 252d06b into ARMmbed:master Feb 26, 2015
@0xc0170
Copy link
Contributor

0xc0170 commented Feb 26, 2015

@jeremybrodt Added an issue, regarding this new addition.

@jeremybrodt jeremybrodt deleted the nexttimestamp branch February 26, 2015 14:40
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.

3 participants