Skip to content

Commit 075fcdc

Browse files
committed
ports/zephyr: Add machine lightsleep.
Signed-off-by: Patrick Joy <[email protected]>
1 parent b4cf82b commit 075fcdc

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

docs/zephyr/quickref.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ Hardware SPI is accessed via the :ref:`machine.SPI <machine.SPI>` class::
104104
spi.write_readinto(b'abcd', buf) # write to MOSI and read from MISO into the buffer
105105
spi.write_readinto(buf, buf) # write buf to MOSI and read back into the buf
106106

107+
Light-sleep mode
108+
----------------
109+
110+
The following code can be used to sleep, reducing power consumption::
111+
112+
import machine
113+
114+
# put the device to sleep for 10 seconds
115+
machine.lightsleep(10000)
116+
117+
Notes:
118+
119+
* Calling ``lightsleep()`` suspends the micropython thread, allowing Zephyr power management to reduce power consumption.
120+
121+
107122
Disk Access
108123
-----------
109124

ports/zephyr/boards/beagleconnect_freedom.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ CONFIG_SPI=y
55
# Flash drivers
66
CONFIG_FLASH=y
77
CONFIG_FLASH_MAP=y
8+
9+
# Disable PM
10+
CONFIG_PM=n
11+
CONFIG_PM_DEVICE=n

ports/zephyr/modmachine.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#include <stdio.h>
3333
#include <zephyr/sys/reboot.h>
34+
#include <zephyr/pm/pm.h>
35+
#include <zephyr/pm/device.h>
3436

3537
#include "modmachine.h"
3638

@@ -61,3 +63,40 @@ MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);
6163
static void mp_machine_idle(void) {
6264
k_yield();
6365
}
66+
67+
#ifdef CONFIG_PM_DEVICE
68+
static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
69+
// Check if no argument is provided
70+
if (n_args == 0) {
71+
mp_raise_ValueError(MP_ERROR_TEXT("value must be provided"));
72+
}
73+
mp_int_t milliseconds = mp_obj_get_int(args[0]);
74+
// Get the UART device
75+
const struct device *uart0 = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
76+
// Set UART device to low power state
77+
pm_device_action_run(uart0, PM_DEVICE_ACTION_SUSPEND);
78+
k_sleep(K_MSEC(milliseconds));
79+
// Set UART device back to active state
80+
pm_device_action_run(uart0, PM_DEVICE_ACTION_RESUME);
81+
}
82+
#else
83+
static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
84+
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
85+
}
86+
#endif
87+
88+
static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) {
89+
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
90+
}
91+
92+
static mp_obj_t mp_machine_get_freq(void) {
93+
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
94+
}
95+
96+
static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
97+
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
98+
}
99+
100+
static mp_obj_t mp_machine_unique_id(void) {
101+
mp_raise_ValueError(MP_ERROR_TEXT("not implemented"));
102+
}

ports/zephyr/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
6262
#define MICROPY_PY_MACHINE (1)
6363
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/zephyr/modmachine.c"
64+
#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1)
6465
#define MICROPY_PY_MACHINE_I2C (1)
6566
#define MICROPY_PY_MACHINE_SPI (1)
6667
#define MICROPY_PY_MACHINE_SPI_MSB (SPI_TRANSFER_MSB)

ports/zephyr/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ CONFIG_FPU=y
1616
CONFIG_MAIN_STACK_SIZE=4736
1717
CONFIG_POLL=y
1818

19+
CONFIG_PM=y
20+
CONFIG_PM_DEVICE=y
1921
CONFIG_DEVICE_DT_METADATA=y
2022

2123
# Enable sensor subsystem (doesn't add code if not used).

0 commit comments

Comments
 (0)