Skip to content

Commit 195e7df

Browse files
committed
rp2/modmachine: Implement additional functions incl unique_id and idle.
Added functions in the machine module are: - unique_id (returns 8 bytes) - soft_reset - idle - lightsleep, deepsleep (not power saving at the moment) - disable_irq, enable_irq - time_pulse_us Signed-off-by: Damien George <[email protected]>
1 parent 81a4d96 commit 195e7df

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ target_link_libraries(${MICROPYTHON_TARGET}
143143
pico_multicore
144144
pico_stdlib_headers
145145
pico_stdlib
146+
pico_unique_id
146147
tinyusb_device
147148
)
148149

ports/rp2/main.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,15 @@ int main(int argc, char **argv) {
110110
pyexec_frozen_module("_boot.py");
111111

112112
// Execute user scripts.
113-
pyexec_file_if_exists("boot.py");
113+
int ret = pyexec_file_if_exists("boot.py");
114+
if (ret & PYEXEC_FORCED_EXIT) {
115+
goto soft_reset_exit;
116+
}
114117
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
115-
pyexec_file_if_exists("main.py");
118+
ret = pyexec_file_if_exists("main.py");
119+
if (ret & PYEXEC_FORCED_EXIT) {
120+
goto soft_reset_exit;
121+
}
116122
}
117123

118124
for (;;) {
@@ -127,6 +133,7 @@ int main(int argc, char **argv) {
127133
}
128134
}
129135

136+
soft_reset_exit:
130137
mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n");
131138
rp2_pio_deinit();
132139
machine_pin_deinit();

ports/rp2/modmachine.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,34 @@
2626

2727
#include "py/runtime.h"
2828
#include "py/mphal.h"
29+
#include "lib/utils/pyexec.h"
2930
#include "extmod/machine_i2c.h"
3031
#include "extmod/machine_mem.h"
32+
#include "extmod/machine_pulse.h"
3133
#include "extmod/machine_spi.h"
3234

3335
#include "modmachine.h"
3436
#include "hardware/clocks.h"
3537
#include "hardware/watchdog.h"
3638
#include "pico/bootrom.h"
39+
#include "pico/unique_id.h"
3740

3841
#define RP2_RESET_PWRON (1)
3942
#define RP2_RESET_WDT (3)
4043

44+
STATIC mp_obj_t machine_unique_id(void) {
45+
pico_unique_board_id_t id;
46+
pico_get_unique_board_id(&id);
47+
return mp_obj_new_bytes(id.id, sizeof(id.id));
48+
}
49+
MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);
50+
51+
STATIC mp_obj_t machine_soft_reset(void) {
52+
pyexec_system_exit = PYEXEC_FORCED_EXIT;
53+
mp_raise_type(&mp_type_SystemExit);
54+
}
55+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
56+
4157
STATIC mp_obj_t machine_reset(void) {
4258
watchdog_reboot(0, SRAM_END, 0);
4359
for (;;) {
@@ -69,12 +85,61 @@ STATIC mp_obj_t machine_freq(void) {
6985
}
7086
MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq);
7187

88+
STATIC mp_obj_t machine_idle(void) {
89+
best_effort_wfe_or_timeout(make_timeout_time_ms(1));
90+
return mp_const_none;
91+
}
92+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle);
93+
94+
STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) {
95+
if (n_args == 0) {
96+
for (;;) {
97+
MICROPY_EVENT_POLL_HOOK
98+
}
99+
} else {
100+
mp_hal_delay_ms(mp_obj_get_int(args[0]));
101+
}
102+
return mp_const_none;
103+
}
104+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine_lightsleep);
105+
106+
STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) {
107+
machine_lightsleep(n_args, args);
108+
return machine_reset();
109+
}
110+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep);
111+
112+
STATIC mp_obj_t machine_disable_irq(void) {
113+
uint32_t state = MICROPY_BEGIN_ATOMIC_SECTION();
114+
return mp_obj_new_int(state);
115+
}
116+
MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq);
117+
118+
STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) {
119+
uint32_t state = mp_obj_get_int(state_in);
120+
MICROPY_END_ATOMIC_SECTION(state);
121+
return mp_const_none;
122+
}
123+
MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq);
124+
72125
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
73126
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
127+
{ MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
128+
{ MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
74129
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
75130
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
76131
{ MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) },
77132
{ MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
133+
134+
{ MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) },
135+
{ MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) },
136+
{ MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
137+
138+
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
139+
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
140+
141+
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
142+
78143
{ MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },
79144
{ MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
80145
{ MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },

ports/rp2/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
#define MICROPY_PY_USELECT (1)
110110
#define MICROPY_PY_MACHINE (1)
111111
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
112+
#define MICROPY_PY_MACHINE_PULSE (1)
112113
#define MICROPY_PY_MACHINE_I2C (1)
113114
#define MICROPY_PY_MACHINE_SPI (1)
114115
#define MICROPY_PY_MACHINE_SPI_MSB (SPI_MSB_FIRST)

0 commit comments

Comments
 (0)