Skip to content

ports/zephyr: Add machine lightsleep. #16864

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/zephyr/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,36 @@ Hardware SPI is accessed via the :ref:`machine.SPI <machine.SPI>` class::
spi.write_readinto(b'abcd', buf) # write to MOSI and read from MISO into the buffer
spi.write_readinto(buf, buf) # write buf to MOSI and read back into the buf

Light-sleep mode
----------------

The following code can be used to sleep, reducing power consumption::

import machine

# put the device to sleep for 10 seconds
machine.lightsleep(10000)

Notes:

* Calling ``lightsleep()`` suspends the micropython thread, allowing Zephyr power management to reduce power consumption.

Deep-sleep mode
----------------

The following code can be used to power down the system::

import machine

# power down the system
machine.depsleep()

Notes:

* Calling ``deepsleep()`` performs a complete system power off.
* A reset or interrupt is required to restart the system.


Disk Access
-----------

Expand Down
4 changes: 4 additions & 0 deletions ports/zephyr/boards/beagleconnect_freedom.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ CONFIG_SPI=y
# Flash drivers
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y

# Disable PM
CONFIG_PM=n
CONFIG_PM_DEVICE=n
54 changes: 54 additions & 0 deletions ports/zephyr/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

#include <stdio.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/pm/pm.h>
#include <zephyr/pm/device.h>
#include <zephyr/sys/poweroff.h>

#include "modmachine.h"

Expand Down Expand Up @@ -62,3 +65,54 @@ MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);
static void mp_machine_idle(void) {
k_yield();
}

#ifdef CONFIG_PM_DEVICE
static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
// Check if no argument is provided
if (n_args == 0) {
mp_raise_ValueError(MP_ERROR_TEXT("value must be provided"));
}
mp_int_t milliseconds = mp_obj_get_int(args[0]);
// Get the UART device
const struct device *console = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
// Set UART device to low power state
pm_device_action_run(console, PM_DEVICE_ACTION_SUSPEND);
k_sleep(K_MSEC(milliseconds));
// Set UART device back to active state
pm_device_action_run(console, PM_DEVICE_ACTION_RESUME);
}
#else
static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
}
#endif

#if defined(CONFIG_PM_DEVICE) && defined(CONFIG_SYS_POWER_OFF)
static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) {
// Check if argument is provided
if (n_args > 0) {
mp_raise_ValueError(MP_ERROR_TEXT("timeout not supported"));
}
// Get the UART device
const struct device *console = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
// Set UART device to low power state
pm_device_action_run(console, PM_DEVICE_ACTION_SUSPEND);
sys_poweroff();
}
#else
static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) {
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
}
#endif

static mp_obj_t mp_machine_get_freq(void) {
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
}

static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
}

static mp_obj_t mp_machine_unique_id(void) {
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
}
1 change: 1 addition & 0 deletions ports/zephyr/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/zephyr/modmachine.c"
#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1)
#define MICROPY_PY_MACHINE_I2C (1)
#define MICROPY_PY_MACHINE_SPI (1)
#define MICROPY_PY_MACHINE_SPI_MSB (SPI_TRANSFER_MSB)
Expand Down
3 changes: 3 additions & 0 deletions ports/zephyr/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ CONFIG_FPU=y
CONFIG_MAIN_STACK_SIZE=4736
CONFIG_POLL=y

CONFIG_PM=y
CONFIG_PM_DEVICE=y
CONFIG_POWEROFF=y
CONFIG_DEVICE_DT_METADATA=y

# Enable sensor subsystem (doesn't add code if not used).
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy