Skip to content

ESP32: add support to light sleep#1789

Merged
bettio merged 1 commit intoatomvm:release-0.6from
bettio:add-support-to-esp32-light-sleep
Jan 25, 2026
Merged

ESP32: add support to light sleep#1789
bettio merged 1 commit intoatomvm:release-0.6from
bettio:add-support-to-esp32-light-sleep

Conversation

@bettio
Copy link
Collaborator

@bettio bettio commented Aug 8, 2025

Allow putting ESP32 to light sleep, and wake it up using a GPIO or a timer.

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@bettio bettio force-pushed the add-support-to-esp32-light-sleep branch from 778ca20 to 0cb4308 Compare August 8, 2025 14:39
}
#endif

static term nif_esp_gpio_wakeup_enable(Context *ctx, int argc, term argv[])
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would call this gpio:wakeup_enable/2 to match with the esp-idf function name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this means we are leaking more ESP32 specific stuff into GPIO module. I think they can be made somehow generic, but I'm not sure how.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

@bettio
Copy link
Collaborator Author

bettio commented Aug 10, 2025

There is something that I'd like to here the opinion about:

Should we use the avm_pubsub for pushing power manager events when returning from sleep?
Such as different 3rd party drivers might need to check if any IRQ pin has been raised during sleep, or any other process might need to know that everything was suspend for a certain amount of time.
So we have either 2 options: leave the caller handle this when returning from sleep, or standardizing some kind of mechanism for informing drivers and anything else that there was a power management related event.

@pguyot
Copy link
Collaborator

pguyot commented Aug 10, 2025

There is something that I'd like to here the opinion about:

Should we use the avm_pubsub for pushing power manager events when returning from sleep? Such as different 3rd party drivers might need to check if any IRQ pin has been raised during sleep, or any other process might need to know that everything was suspend for a certain amount of time. So we have either 2 options: leave the caller handle this when returning from sleep, or standardizing some kind of mechanism for informing drivers and anything else that there was a power management related event.

We could use monitor/2 to get the event.

BEAM documentation reads:

The monitor functionality is expected to be extended. That is, other Types and Items are expected to be supported in a future release.

@bettio bettio force-pushed the add-support-to-esp32-light-sleep branch from 0cb4308 to 4d62044 Compare August 10, 2025 15:01
@UncleGrumpy
Copy link
Collaborator

We could use monitor/2 to get the event.

This is an interesting suggestion, and might be useful to expose an API for pushing other “monitored” events, like acquiring or loosing a network connection.

I think some standardized notification system would be good, but it should be opt in, so extra resources are not consumed if they are not used.

@petermm
Copy link
Contributor

petermm commented Jan 11, 2026

lgtm, there is also some docs over here:

"Usage of light sleep is untested, and no support for controlling light sleep is currently implemented. Reach out if you do any experiments and measurements."

#### Sleep mode - Light sleep

that needs updating

@bettio bettio force-pushed the add-support-to-esp32-light-sleep branch from 4d62044 to a07cef1 Compare January 22, 2026 14:06
if (n <= 64) {
union maybe_unsigned_int64 value;
bool status = bitstring_extract_integer(
((term) bin_ptr) | TERM_PRIMARY_BOXED, offset, n, bs_flags, &value);

Check failure

Code scanning / CodeQL

Passing a non-term to a function expecting a term

Passing a non-term to a function expecting a term, without an explicit cast
bool status;
switch (n) {
case 16:
status = bitstring_extract_f16(((term) bin_ptr) | TERM_PRIMARY_BOXED, offset, n, bs_flags, &value);

Check failure

Code scanning / CodeQL

Passing a non-term to a function expecting a term

Passing a non-term to a function expecting a term, without an explicit cast
status = bitstring_extract_f16(((term) bin_ptr) | TERM_PRIMARY_BOXED, offset, n, bs_flags, &value);
break;
case 32:
status = bitstring_extract_f32(((term) bin_ptr) | TERM_PRIMARY_BOXED, offset, n, bs_flags, &value);

Check failure

Code scanning / CodeQL

Passing a non-term to a function expecting a term

Passing a non-term to a function expecting a term, without an explicit cast
status = bitstring_extract_f32(((term) bin_ptr) | TERM_PRIMARY_BOXED, offset, n, bs_flags, &value);
break;
case 64:
status = bitstring_extract_f64(((term) bin_ptr) | TERM_PRIMARY_BOXED, offset, n, bs_flags, &value);

Check failure

Code scanning / CodeQL

Passing a non-term to a function expecting a term

Passing a non-term to a function expecting a term, without an explicit cast
#### Sleep mode - Light sleep

Usage of light sleep is untested, and no support for controlling light sleep is currently implemented. Reach out if you do any experiments and measurements.
Light sleep is supported as well and can be enabled on ESP32 using the releated functions. When using light sleep memory is retained, and execution is resumed as soon as wakeup is triggered from a wakeup source.
Copy link
Contributor

Choose a reason for hiding this comment

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

releated -> related

@bettio bettio force-pushed the add-support-to-esp32-light-sleep branch from a07cef1 to 28023c0 Compare January 24, 2026 13:57
Allow putting ESP32 to light sleep, and wake it up using a GPIO or a
timer.

Signed-off-by: Davide Bettio <davide@uninstall.it>
@bettio bettio force-pushed the add-support-to-esp32-light-sleep branch from 28023c0 to 183c398 Compare January 24, 2026 14:23
Copy link
Contributor

@petermm petermm left a comment

Choose a reason for hiding this comment

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

LGTM - we just need to remember build_instructions.md when forwarding it to main..

@bettio bettio merged commit bcb9d81 into atomvm:release-0.6 Jan 25, 2026
87 of 105 checks passed
bettio added a commit that referenced this pull request Feb 1, 2026
Merge ESP32 light sleep support (#1789) from release-0.6 branch.
bettio added a commit to bettio/AtomVM-fork that referenced this pull request Feb 5, 2026
After atomvm#1789 (and the subsequent forward port from release-0.6), light
sleep has been introduced in main.
Update newer documentation accordingly.

Signed-off-by: Davide Bettio <davide@uninstall.it>
bettio added a commit to bettio/AtomVM-fork that referenced this pull request Feb 6, 2026
After atomvm#1789 (and the subsequent forward port from release-0.6), light
sleep has been merged in main.
Update newer documentation accordingly.

Signed-off-by: Davide Bettio <davide@uninstall.it>
bettio added a commit to bettio/AtomVM-fork that referenced this pull request Feb 6, 2026
After atomvm#1789 (and the subsequent forward port from release-0.6), light
sleep has been merged in main.
Update newer documentation accordingly.

Signed-off-by: Davide Bettio <davide@uninstall.it>
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