Skip to content

Add adjust_millis_forward() API to help with deep sleep processing #243

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

Closed
wants to merge 1 commit into from

Conversation

terrillmoore
Copy link

This patch fixes mcci-catena#1. During a deep sleep, systick must be off. But some libraries use millis() to get an idea of real time. (In particular, this is true for https://github.com/mcci-catena/arduino-lmic -- otherwise, it loses track of the schedule.) Since _ulTickCount is local to delay.c, the appropriate correction is to add a new API adjust_millis_forward().

The submitted patch disables interrupts briefly. I have a different, tested, patch that instead causes the systick interrupt processing routine to do the adjustment. But I think the submitted approach is more clear, and is certainly more efficient. It would be great if this could be added to the mainline distribution. (Of course, this might also imply a similar change to the AVR core, and there are portability issues related to the function of the real-time clock which I have not addressed.)

With this patch, I have successfully entered deep sleep with an Adafruit Feather M0 for the roughly six-minute sleep between LoRaWAN transmissions in my application. Use of deep sleep doubles the battery life.

Sample (somewhat clumsy use):

// Set an alarm in the future.
gRtc.SetAlarm(SLEEP_TIME_IN_SECONDS);

// Deep sleep until the alarm
gRtc.SleepForAlarm(gRtc.MATCH_HHMMSS, gRtc.SleepMode:IdleCpuAhbApb);

// At this point, approximately 1000 * SLEEP_TIME_IN_SECONDS ms have passed without interrupts. 
// Advance the value of millis() accordingly.
// (A more refined approach would take into account fractional seconds.)
adjust_millis_forward(SLEEP_TIME_IN_SECONDS * 1000);

@terrillmoore
Copy link
Author

given no response, we just created our own BSP for our boards at https://github.com/mcci-catena/arduino-boards. Closing this.

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.

Add atomic way of adjusting the time.
1 participant