Commit 195e7dfa authored by Damien George's avatar Damien George

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: default avatarDamien George <damien@micropython.org>
parent 81a4d96a
...@@ -143,6 +143,7 @@ target_link_libraries(${MICROPYTHON_TARGET} ...@@ -143,6 +143,7 @@ target_link_libraries(${MICROPYTHON_TARGET}
pico_multicore pico_multicore
pico_stdlib_headers pico_stdlib_headers
pico_stdlib pico_stdlib
pico_unique_id
tinyusb_device tinyusb_device
) )
......
...@@ -110,9 +110,15 @@ int main(int argc, char **argv) { ...@@ -110,9 +110,15 @@ int main(int argc, char **argv) {
pyexec_frozen_module("_boot.py"); pyexec_frozen_module("_boot.py");
// Execute user scripts. // Execute user scripts.
pyexec_file_if_exists("boot.py"); int ret = pyexec_file_if_exists("boot.py");
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
pyexec_file_if_exists("main.py"); ret = pyexec_file_if_exists("main.py");
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
} }
for (;;) { for (;;) {
...@@ -127,6 +133,7 @@ int main(int argc, char **argv) { ...@@ -127,6 +133,7 @@ int main(int argc, char **argv) {
} }
} }
soft_reset_exit:
mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n");
rp2_pio_deinit(); rp2_pio_deinit();
machine_pin_deinit(); machine_pin_deinit();
......
...@@ -26,18 +26,34 @@ ...@@ -26,18 +26,34 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "lib/utils/pyexec.h"
#include "extmod/machine_i2c.h" #include "extmod/machine_i2c.h"
#include "extmod/machine_mem.h" #include "extmod/machine_mem.h"
#include "extmod/machine_pulse.h"
#include "extmod/machine_spi.h" #include "extmod/machine_spi.h"
#include "modmachine.h" #include "modmachine.h"
#include "hardware/clocks.h" #include "hardware/clocks.h"
#include "hardware/watchdog.h" #include "hardware/watchdog.h"
#include "pico/bootrom.h" #include "pico/bootrom.h"
#include "pico/unique_id.h"
#define RP2_RESET_PWRON (1) #define RP2_RESET_PWRON (1)
#define RP2_RESET_WDT (3) #define RP2_RESET_WDT (3)
STATIC mp_obj_t machine_unique_id(void) {
pico_unique_board_id_t id;
pico_get_unique_board_id(&id);
return mp_obj_new_bytes(id.id, sizeof(id.id));
}
MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);
STATIC mp_obj_t machine_soft_reset(void) {
pyexec_system_exit = PYEXEC_FORCED_EXIT;
mp_raise_type(&mp_type_SystemExit);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
STATIC mp_obj_t machine_reset(void) { STATIC mp_obj_t machine_reset(void) {
watchdog_reboot(0, SRAM_END, 0); watchdog_reboot(0, SRAM_END, 0);
for (;;) { for (;;) {
...@@ -69,12 +85,61 @@ STATIC mp_obj_t machine_freq(void) { ...@@ -69,12 +85,61 @@ STATIC mp_obj_t machine_freq(void) {
} }
MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq);
STATIC mp_obj_t machine_idle(void) {
best_effort_wfe_or_timeout(make_timeout_time_ms(1));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle);
STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
for (;;) {
MICROPY_EVENT_POLL_HOOK
}
} else {
mp_hal_delay_ms(mp_obj_get_int(args[0]));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine_lightsleep);
STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) {
machine_lightsleep(n_args, args);
return machine_reset();
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep);
STATIC mp_obj_t machine_disable_irq(void) {
uint32_t state = MICROPY_BEGIN_ATOMIC_SECTION();
return mp_obj_new_int(state);
}
MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq);
STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) {
uint32_t state = mp_obj_get_int(state_in);
MICROPY_END_ATOMIC_SECTION(state);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq);
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
{ MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
{ MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
{ MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) }, { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) },
{ MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
{ MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) },
{ MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) },
{ MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
{ MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },
{ MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
{ MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },
......
...@@ -109,6 +109,7 @@ ...@@ -109,6 +109,7 @@
#define MICROPY_PY_USELECT (1) #define MICROPY_PY_USELECT (1)
#define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
#define MICROPY_PY_MACHINE_PULSE (1)
#define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_I2C (1)
#define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI (1)
#define MICROPY_PY_MACHINE_SPI_MSB (SPI_MSB_FIRST) #define MICROPY_PY_MACHINE_SPI_MSB (SPI_MSB_FIRST)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment