Skip to content
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

alarm.sleep_memory + alarm.wake_alarm #3816

Merged
merged 7 commits into from
Dec 15, 2020
Merged

Conversation

dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Dec 11, 2020

  • Added alarm.sleep_memory, which is a bytearray-like object (similar to microcontroller.nvm) that can store state between deep sleeps. Implemented only on ESP32-S2, in the RTC slow memory.
  • Added setting alarm.wake_alarm both for real and simulated deep sleep. An object has to be created, so creation has to be delayed until the VM is up after restart.

alarm.SleepMemory, the class for the singleton alarm.sleep_memory, includes subscript and slice access code that was simply copied from nvm. There are some other bytearray-like classes that have similar code. It's not a huge amount of code, It would be nice to unify all that into something generic that was passed the appropriate common_hal_* routines, though it's kind of messy to do in C. I am not going to do that now.

Test program (tested both plugged in and on battery power):

import alarm
import microcontroller
import time
# check for compatibility with loaded wifi, even if not used.
import wifi
from adafruit_magtag.magtag import MagTag


magtag = MagTag()

magtag.add_text(
    text_scale=2,
    text_wrap=25,
    text_maxlen=300,
    text_position=(10, 10),
    text_anchor_point=(0, 0),
)

print(alarm.wake_alarm)

if not alarm.wake_alarm:
    alarm.sleep_memory[5] = 0

alarm.sleep_memory[5] = alarm.sleep_memory[5] + 1

magtag.set_text(
    "battery: {}V    count: {}".format(
        magtag.peripherals.battery, alarm.sleep_memory[5]
    )
)

magtag.refresh()

al = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10)
alarm.exit_and_deep_sleep_until_alarms(al)

@dhalbert dhalbert requested a review from tannewt December 11, 2020 13:19
@dhalbert
Copy link
Collaborator Author

GitHub Actions was failing trying to fetch an Ubuntu package. Added sudo apt-get update before the apt-get install fixed the problem. Noticed simultaneously by @microdev1.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Just a few comments/questions. Thanks!

ports/esp32s2/common-hal/alarm/SleepMemory.c Outdated Show resolved Hide resolved
shared-bindings/alarm/SleepMemory.c Outdated Show resolved Hide resolved
shared-bindings/alarm/SleepMemory.h Outdated Show resolved Hide resolved
shared-bindings/alarm/SleepMemory.c Show resolved Hide resolved
@dhalbert dhalbert mentioned this pull request Dec 11, 2020
@dhalbert
Copy link
Collaborator Author

Pasting helpful discussion from discord with @igrr about using RTC memory:

igrr Yesterday at 1:31 PM
That's risky, i think. I would suggest defining a static array of the size you need, and giving it RTC_DATA_ATTR. It will be automatically placed by the linker, and in case there is some other stuff placed into RTC_SLOW, the linker will check if everything fits.

danh Yesterday at 1:32 PM
so then we should turn off CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM
[1:32 PM]
i guess

igrr Yesterday at 1:32 PM
That config will redirect RTC_DATA_ATTR labelled variables into RTC_FAST.

danh Yesterday at 1:32 PM
or can i force using the slow section?(edited)

igrr Yesterday at 1:33 PM
If you don't enable CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM, then RTC_DATA_ATTR labelled variables will be in RTC_SLOW memory.

danh Yesterday at 1:33 PM
if we want to use the ULP, then we need a slow static array that starts at the beginning of RTC_SLOW, is that right?(edited)

@danh
or can i force using the slow section?(edited)

igrr Yesterday at 1:33 PM
There's also RTC_SLOW_ATTR if you prefer: https://github.com/espressif/esp-idf/blob/master/components/xtensa/include/esp_attr.h#L80
However, if you don't specifically care if it is in RTC_FAST or RTC_SLOW, it is better to use RTC_DATA_ATTR since it's more generic.
Not all future chips will have both RTC_FAST and RTC_SLOW, but they will have at least some kind of RTC memory. So RTC_DATA_ATTR will keep working.(edited)

@danh
if we want to use the ULP, then we need a slow static array that starts at the beginning of RTC_SLOW, is that right?(edited)

igrr Yesterday at 1:36 PM
If you use the ULP, you can set CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM to a non-zero value, which will reserve some part of RTC_SLOW memory for it.
[1:36 PM]
this memory will be automatically used by ulp_load function.

danh Yesterday at 1:37 PM
ah good; would I also need to turn off CONFIG_ESP32_ALLOW_RTC_FAST_MEM_AS_HEAP?

igrr Yesterday at 1:38 PM
not necessarily. All the RTC Fast memory which is not statically allocated for code and data (using RTC_FAST_ATTR, RTC_IRAM_ATTR, etc) will be added as a pool to the heap allocator. As long as you never dereference random raw addresses into the RTC memory, it should be completely transparent. Just some part of heap will reside in RTC_FAST memory.
[1:39 PM]
Since it is 8kB, it might be enough for stack of 1 or 2 system tasks, like esp_timer or sys_evt, freeing up "internal" RAM for other things.(edited)

danh Yesterday at 1:39 PM
v good; the allocation and linker map mechanism is more sophisticated than I realized(edited)
[1:39 PM]
this is extremely helpful!
[1:40 PM]
thanks!

@dhalbert
Copy link
Collaborator Author

e0afa32:

  • Use RTC_DATA_ATTR for sleep_memory array. Right now it's in RTC slow mem, could be changed to fast mem later with config option CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM, but not necessary now.
  • Remove superfluous bool() operator.
  • Rely on ESP-IDF to do power control (per conversation with @ igrr).
  • Normalize argument order for set_bytes and get_bytes.

@dhalbert dhalbert requested a review from tannewt December 14, 2020 16:41
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thank you!

@tannewt tannewt merged commit d076296 into adafruit:main Dec 15, 2020
@dhalbert dhalbert deleted the sleepmemory branch February 25, 2021 02:27
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