Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 30 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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typo here. Should be deepsleep()


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 4 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 54 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();
Comment thread
dpgeorge marked this conversation as resolved.
}
#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 1 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 3 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.