From e1ec6af654b1c5c4a973b6c6b029ee68bb92eb89 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 24 Nov 2023 18:25:30 +1100 Subject: [PATCH] extmod/modmachine: Provide common bindings for 6 bare-metal functions. Minor changes for consistency are: - nrf gains: unique_id(), freq() [they do nothing] - samd: deepsleep() now resets after calling lightsleep() - esp32: lightsleep()/deepsleep() no longer take kw arg "sleep", instead it's positional to match others. also, passing 0 here will now do a 0ms sleep instead of acting like nothing was passed. reset_cause() no longer takes any args (before it would just ignore them) - mimxrt: freq() with an argument and lightsleep() both raise NotImplementedError Signed-off-by: Damien George --- extmod/modmachine.c | 64 ++++++++++++ extmod/modmachine.h | 6 ++ ports/cc3200/mods/modmachine.c | 41 +++----- ports/cc3200/mpconfigport.h | 1 + ports/esp32/modmachine.c | 135 ++++++++++--------------- ports/esp32/mpconfigport.h | 1 + ports/esp8266/modmachine.c | 60 +++++------ ports/esp8266/mpconfigport.h | 1 + ports/mimxrt/modmachine.c | 39 ++++--- ports/mimxrt/mpconfigport.h | 1 + ports/nrf/modules/machine/modmachine.c | 37 +++---- ports/nrf/modules/machine/modmachine.h | 5 - ports/nrf/mpconfigport.h | 1 + ports/renesas-ra/modmachine.c | 42 +++----- ports/renesas-ra/modmachine.h | 12 --- ports/renesas-ra/mpconfigport.h | 1 + ports/renesas-ra/powerctrl.c | 4 +- ports/renesas-ra/powerctrl.h | 2 +- ports/rp2/modmachine.c | 64 +++++------- ports/rp2/mpconfigport.h | 1 + ports/samd/modmachine.c | 50 ++++----- ports/samd/mpconfigport.h | 1 + ports/stm32/modmachine.c | 114 +++++++++------------ ports/stm32/modmachine.h | 7 -- ports/stm32/mpconfigport.h | 1 + ports/stm32/powerctrl.c | 6 +- ports/stm32/powerctrl.h | 2 +- ports/zephyr/modmachine.c | 4 +- ports/zephyr/modmachine.h | 2 - 29 files changed, 320 insertions(+), 385 deletions(-) diff --git a/extmod/modmachine.c b/extmod/modmachine.c index 789d324d66..d56b938bb3 100644 --- a/extmod/modmachine.c +++ b/extmod/modmachine.c @@ -43,6 +43,16 @@ STATIC void mp_machine_idle(void); NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args); #endif +#if MICROPY_PY_MACHINE_BARE_METAL_FUNCS +STATIC mp_obj_t mp_machine_unique_id(void); +NORETURN STATIC void mp_machine_reset(void); +STATIC mp_int_t mp_machine_reset_cause(void); +STATIC mp_obj_t mp_machine_get_freq(void); +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args); +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args); +NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args); +#endif + // The port can provide additional machine-module implementation in this file. #ifdef MICROPY_PY_MACHINE_INCLUDEFILE #include MICROPY_PY_MACHINE_INCLUDEFILE @@ -67,6 +77,46 @@ STATIC mp_obj_t machine_idle(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle); +#if MICROPY_PY_MACHINE_BARE_METAL_FUNCS + +STATIC mp_obj_t machine_unique_id(void) { + return mp_machine_unique_id(); +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); + +NORETURN STATIC mp_obj_t machine_reset(void) { + mp_machine_reset(); +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); + +STATIC mp_obj_t machine_reset_cause(void) { + return MP_OBJ_NEW_SMALL_INT(mp_machine_reset_cause()); +} +MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); + +STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + return mp_machine_get_freq(); + } else { + mp_machine_set_freq(n_args, args); + return mp_const_none; + } +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq); + +STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { + mp_machine_lightsleep(n_args, args); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine_lightsleep); + +NORETURN STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) { + mp_machine_deepsleep(n_args, args); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep); + +#endif + STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_machine) }, @@ -75,14 +125,28 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, + // Miscellaneous functions. + #if MICROPY_PY_MACHINE_BARE_METAL_FUNCS + { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, + #endif + // Reset related functions. { MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) }, #if MICROPY_PY_MACHINE_BOOTLOADER { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) }, #endif + #if MICROPY_PY_MACHINE_BARE_METAL_FUNCS + { 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) }, + #endif // Power related functions. { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, + #if MICROPY_PY_MACHINE_BARE_METAL_FUNCS + { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_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) }, + #endif // Functions for bit protocols. #if MICROPY_PY_MACHINE_BITSTREAM diff --git a/extmod/modmachine.h b/extmod/modmachine.h index b0253bbb8c..d186ce6615 100644 --- a/extmod/modmachine.h +++ b/extmod/modmachine.h @@ -241,6 +241,12 @@ NORETURN mp_obj_t machine_bootloader(size_t n_args, const mp_obj_t *args); void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len); mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us); +MP_DECLARE_CONST_FUN_OBJ_0(machine_unique_id_obj); +MP_DECLARE_CONST_FUN_OBJ_0(machine_reset_obj); +MP_DECLARE_CONST_FUN_OBJ_0(machine_reset_cause_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bootloader_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bitstream_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj); diff --git a/ports/cc3200/mods/modmachine.c b/ports/cc3200/mods/modmachine.c index 1df11c9d28..5eb7e2ef15 100644 --- a/ports/cc3200/mods/modmachine.c +++ b/ports/cc3200/mods/modmachine.c @@ -56,16 +56,10 @@ #endif #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \ - { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, \ MICROPY_PY_MACHINE_INFO_ENTRY \ - { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, \ { MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&machine_main_obj) }, \ { MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&machine_rng_get_obj) }, \ { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_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_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, \ { MP_ROM_QSTR(MP_QSTR_wake_reason), MP_ROM_PTR(&machine_wake_reason_obj) }, \ \ { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, \ @@ -99,24 +93,20 @@ extern OsiTaskHandle svTaskHandle; extern OsiTaskHandle xSimpleLinkSpawnTaskHndl; #endif - -/// \module machine - functions related to the SoC -/// - MP_DECLARE_CONST_FUN_OBJ_0(machine_disable_irq_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_enable_irq_obj); /******************************************************************************/ // MicroPython bindings; -STATIC mp_obj_t machine_reset(void) { +NORETURN STATIC void mp_machine_reset(void) { // disable wlan wlan_stop(SL_STOP_TIMEOUT_LONG); // reset the cpu and it's peripherals MAP_PRCMMCUReset(true); - return mp_const_none; + for (;;) { + } } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); #ifdef DEBUG STATIC mp_obj_t machine_info(uint n_args, const mp_obj_t *args) { @@ -145,17 +135,19 @@ STATIC mp_obj_t machine_info(uint n_args, const mp_obj_t *args) { STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info); #endif -STATIC mp_obj_t machine_freq(void) { +STATIC mp_obj_t mp_machine_get_freq(void) { return mp_obj_new_int(HAL_FCPU_HZ); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); -STATIC mp_obj_t machine_unique_id(void) { +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { + mp_raise_NotImplementedError(NULL); +} + +STATIC mp_obj_t mp_machine_unique_id(void) { uint8_t mac[SL_BSSID_LENGTH]; wlan_get_mac (mac); return mp_obj_new_bytes(mac, SL_BSSID_LENGTH); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); STATIC mp_obj_t machine_main(mp_obj_t main) { if (mp_obj_is_str(main)) { @@ -171,22 +163,19 @@ STATIC void mp_machine_idle(void) { __WFI(); } -STATIC mp_obj_t machine_lightsleep(void) { +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { pyb_sleep_sleep(); - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_lightsleep_obj, machine_lightsleep); -STATIC mp_obj_t machine_deepsleep (void) { +NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) { pyb_sleep_deepsleep(); - return mp_const_none; + for (;;) { + } } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_deepsleep_obj, machine_deepsleep); -STATIC mp_obj_t machine_reset_cause (void) { - return mp_obj_new_int(pyb_sleep_get_reset_cause()); +STATIC mp_int_t mp_machine_reset_cause(void) { + return pyb_sleep_get_reset_cause(); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); STATIC mp_obj_t machine_wake_reason (void) { return mp_obj_new_int(pyb_sleep_get_wake_reason()); diff --git a/ports/cc3200/mpconfigport.h b/ports/cc3200/mpconfigport.h index f2dff028fc..7c52732459 100644 --- a/ports/cc3200/mpconfigport.h +++ b/ports/cc3200/mpconfigport.h @@ -122,6 +122,7 @@ #define MICROPY_PY_TIME_INCLUDEFILE "ports/cc3200/mods/modtime.c" #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/cc3200/mods/modmachine.c" +#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1) #define MICROPY_PY_MACHINE_WDT (1) #define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/cc3200/mods/machine_wdt.c" diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 18bc0965a2..0170655ba3 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -52,12 +52,7 @@ #endif #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \ - { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, \ { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_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) }, \ @@ -73,7 +68,6 @@ { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) }, \ \ /* Reset reasons */ \ - { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, \ { MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(MP_HARD_RESET) }, \ { MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(MP_PWRON_RESET) }, \ { MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(MP_WDT_RESET) }, \ @@ -103,65 +97,51 @@ STATIC bool is_soft_reset = 0; int esp_clk_cpu_freq(void); #endif -STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - // get - return mp_obj_new_int(esp_rom_get_cpu_ticks_per_us() * 1000000); - } else { - // set - mp_int_t freq = mp_obj_get_int(args[0]) / 1000000; - if (freq != 20 && freq != 40 && freq != 80 && freq != 160 - #if !CONFIG_IDF_TARGET_ESP32C3 - && freq != 240 - #endif - ) { - #if CONFIG_IDF_TARGET_ESP32C3 - mp_raise_ValueError(MP_ERROR_TEXT("frequency must be 20MHz, 40MHz, 80Mhz or 160MHz")); - #else - mp_raise_ValueError(MP_ERROR_TEXT("frequency must be 20MHz, 40MHz, 80Mhz, 160MHz or 240MHz")); - #endif - } - #if CONFIG_IDF_TARGET_ESP32 - esp_pm_config_esp32_t pm; - #elif CONFIG_IDF_TARGET_ESP32C3 - esp_pm_config_esp32c3_t pm; - #elif CONFIG_IDF_TARGET_ESP32S2 - esp_pm_config_esp32s2_t pm; - #elif CONFIG_IDF_TARGET_ESP32S3 - esp_pm_config_esp32s3_t pm; +STATIC mp_obj_t mp_machine_get_freq(void) { + return mp_obj_new_int(esp_rom_get_cpu_ticks_per_us() * 1000000); +} + +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { + mp_int_t freq = mp_obj_get_int(args[0]) / 1000000; + if (freq != 20 && freq != 40 && freq != 80 && freq != 160 + #if !CONFIG_IDF_TARGET_ESP32C3 + && freq != 240 #endif - pm.max_freq_mhz = freq; - pm.min_freq_mhz = freq; - pm.light_sleep_enable = false; - esp_err_t ret = esp_pm_configure(&pm); - if (ret != ESP_OK) { - mp_raise_ValueError(NULL); - } - while (esp_rom_get_cpu_ticks_per_us() != freq) { - vTaskDelay(1); - } - return mp_const_none; + ) { + #if CONFIG_IDF_TARGET_ESP32C3 + mp_raise_ValueError(MP_ERROR_TEXT("frequency must be 20MHz, 40MHz, 80Mhz or 160MHz")); + #else + mp_raise_ValueError(MP_ERROR_TEXT("frequency must be 20MHz, 40MHz, 80Mhz, 160MHz or 240MHz")); + #endif + } + #if CONFIG_IDF_TARGET_ESP32 + esp_pm_config_esp32_t pm; + #elif CONFIG_IDF_TARGET_ESP32C3 + esp_pm_config_esp32c3_t pm; + #elif CONFIG_IDF_TARGET_ESP32S2 + esp_pm_config_esp32s2_t pm; + #elif CONFIG_IDF_TARGET_ESP32S3 + esp_pm_config_esp32s3_t pm; + #endif + pm.max_freq_mhz = freq; + pm.min_freq_mhz = freq; + pm.light_sleep_enable = false; + esp_err_t ret = esp_pm_configure(&pm); + if (ret != ESP_OK) { + mp_raise_ValueError(NULL); + } + while (esp_rom_get_cpu_ticks_per_us() != freq) { + vTaskDelay(1); } } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq); - -STATIC mp_obj_t machine_sleep_helper(wake_type_t wake_type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - - enum {ARG_sleep_ms}; - const mp_arg_t allowed_args[] = { - { MP_QSTR_sleep_ms, MP_ARG_INT, { .u_int = 0 } }, - }; - - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - - mp_int_t expiry = args[ARG_sleep_ms].u_int; +STATIC void machine_sleep_helper(wake_type_t wake_type, size_t n_args, const mp_obj_t *args) { // First, disable any previously set wake-up source esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL); - if (expiry != 0) { + // Set the expiry time of the sleep, if given. + if (n_args != 0) { + mp_int_t expiry = mp_obj_get_int(args[0]); esp_sleep_enable_timer_wakeup(((uint64_t)expiry) * 1000); } @@ -199,53 +179,45 @@ STATIC mp_obj_t machine_sleep_helper(wake_type_t wake_type, size_t n_args, const esp_deep_sleep_start(); break; } - return mp_const_none; } -STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - return machine_sleep_helper(MACHINE_WAKE_SLEEP, n_args, pos_args, kw_args); +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { + machine_sleep_helper(MACHINE_WAKE_SLEEP, n_args, args); }; -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_lightsleep_obj, 0, machine_lightsleep); -STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - return machine_sleep_helper(MACHINE_WAKE_DEEPSLEEP, n_args, pos_args, kw_args); +NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) { + machine_sleep_helper(MACHINE_WAKE_DEEPSLEEP, n_args, args); + mp_machine_reset(); }; -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_deepsleep_obj, 0, machine_deepsleep); -STATIC mp_obj_t machine_reset_cause(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_int_t mp_machine_reset_cause(void) { if (is_soft_reset) { - return MP_OBJ_NEW_SMALL_INT(MP_SOFT_RESET); + return MP_SOFT_RESET; } switch (esp_reset_reason()) { case ESP_RST_POWERON: case ESP_RST_BROWNOUT: - return MP_OBJ_NEW_SMALL_INT(MP_PWRON_RESET); - break; + return MP_PWRON_RESET; case ESP_RST_INT_WDT: case ESP_RST_TASK_WDT: case ESP_RST_WDT: - return MP_OBJ_NEW_SMALL_INT(MP_WDT_RESET); - break; + return MP_WDT_RESET; case ESP_RST_DEEPSLEEP: - return MP_OBJ_NEW_SMALL_INT(MP_DEEPSLEEP_RESET); - break; + return MP_DEEPSLEEP_RESET; case ESP_RST_SW: case ESP_RST_PANIC: case ESP_RST_EXT: // Comment in ESP-IDF: "For ESP32, ESP_RST_EXT is never returned" - return MP_OBJ_NEW_SMALL_INT(MP_HARD_RESET); - break; + return MP_HARD_RESET; case ESP_RST_SDIO: case ESP_RST_UNKNOWN: default: - return MP_OBJ_NEW_SMALL_INT(0); - break; + return 0; } } -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_reset_cause_obj, 0, machine_reset_cause); NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { MICROPY_BOARD_ENTER_BOOTLOADER(n_args, args); @@ -267,18 +239,15 @@ STATIC mp_obj_t machine_wake_reason(size_t n_args, const mp_obj_t *pos_args, mp_ } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_wake_reason_obj, 0, machine_wake_reason); -STATIC mp_obj_t machine_reset(void) { +NORETURN STATIC void mp_machine_reset(void) { esp_restart(); - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); -STATIC mp_obj_t machine_unique_id(void) { +STATIC mp_obj_t mp_machine_unique_id(void) { uint8_t chipid[6]; esp_efuse_mac_get_default(chipid); return mp_obj_new_bytes(chipid, 6); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); STATIC void mp_machine_idle(void) { MP_THREAD_GIL_EXIT(); diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index ef55d9ae35..083f1d11cf 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -97,6 +97,7 @@ #define MICROPY_PY_OS_URANDOM (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/esp32/modmachine.c" +#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1) #define MICROPY_PY_MACHINE_BOOTLOADER (1) #define MICROPY_PY_MACHINE_ADC (1) #define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/esp32/machine_adc.c" diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index cce6b01ce7..d55a656ce0 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -41,13 +41,7 @@ #define MACHINE_WAKE_DEEPSLEEP (0x04) #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \ - { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_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_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, \ { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_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) }, \ @@ -68,45 +62,42 @@ { MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(REASON_WDT_RST) }, \ { MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(REASON_SOFT_RESTART) }, \ -STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - // get - return mp_obj_new_int(system_get_cpu_freq() * 1000000); - } else { - // set - mp_int_t freq = mp_obj_get_int(args[0]) / 1000000; - if (freq != 80 && freq != 160) { - mp_raise_ValueError(MP_ERROR_TEXT("frequency can only be either 80Mhz or 160MHz")); - } - system_update_cpu_freq(freq); - return mp_const_none; +STATIC mp_obj_t mp_machine_get_freq(void) { + return mp_obj_new_int(system_get_cpu_freq() * 1000000); +} + +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { + mp_int_t freq = mp_obj_get_int(args[0]) / 1000000; + if (freq != 80 && freq != 160) { + mp_raise_ValueError(MP_ERROR_TEXT("frequency can only be either 80Mhz or 160MHz")); + } + system_update_cpu_freq(freq); +} + +NORETURN STATIC void mp_machine_reset(void) { + system_restart(); + + // we must not return + for (;;) { + ets_loop_iter(); } } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq); -STATIC mp_obj_t machine_reset(void) { - system_restart(); - return mp_const_none; +STATIC mp_int_t mp_machine_reset_cause(void) { + return system_get_rst_info()->reason; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); -STATIC mp_obj_t machine_reset_cause(void) { - return MP_OBJ_NEW_SMALL_INT(system_get_rst_info()->reason); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); - -STATIC mp_obj_t machine_unique_id(void) { +STATIC mp_obj_t mp_machine_unique_id(void) { uint32_t id = system_get_chip_id(); return mp_obj_new_bytes((byte *)&id, sizeof(id)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); STATIC void mp_machine_idle(void) { asm ("waiti 0"); ets_event_poll(); // handle any events after possibly a long wait (eg feed WDT) } -STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { uint32_t max_us = 0xffffffff; if (n_args == 1) { mp_int_t max_ms = mp_obj_get_int(args[0]); @@ -124,11 +115,9 @@ STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { asm ("waiti 0"); } } - return mp_const_none; } -STATIC 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) { +NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) { // default to sleep forever uint32_t sleep_us = 0; @@ -171,10 +160,7 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) { // we must not return ets_loop_iter(); } - - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep); // These values are from the datasheet #define ESP_TIMER_US_MIN (100) diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index fa9acce6c7..1925fc3e72 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -65,6 +65,7 @@ #define MICROPY_PY_LWIP_SOCK_RAW (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/esp8266/modmachine.c" +#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1) #define MICROPY_PY_MACHINE_ADC (1) #define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/esp8266/machine_adc.c" #define MICROPY_PY_MACHINE_ADC_READ (1) diff --git a/ports/mimxrt/modmachine.c b/ports/mimxrt/modmachine.c index 8bcf10122c..b299112d95 100644 --- a/ports/mimxrt/modmachine.c +++ b/ports/mimxrt/modmachine.c @@ -57,18 +57,12 @@ #endif #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \ - { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_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_freq), MP_ROM_PTR(&machine_freq_obj) }, \ MICROPY_PY_MACHINE_LED_ENTRY \ { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, \ { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \ { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, \ MICROPY_PY_MACHINE_SDCARD_ENTRY \ \ - { 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) }, \ \ @@ -85,27 +79,27 @@ typedef enum { MP_SOFT_RESET } reset_reason_t; -STATIC mp_obj_t machine_unique_id(void) { +STATIC mp_obj_t mp_machine_unique_id(void) { unsigned char id[8]; mp_hal_get_unique_id(id); return mp_obj_new_bytes(id, sizeof(id)); } -MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); -STATIC mp_obj_t machine_reset(void) { +NORETURN STATIC void mp_machine_reset(void) { WDOG_TriggerSystemSoftwareReset(WDOG1); - return mp_const_none; + while (true) { + ; + } } -MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); -STATIC mp_obj_t machine_reset_cause(void) { +STATIC mp_int_t mp_machine_reset_cause(void) { #ifdef MIMXRT117x_SERIES uint32_t user_reset_flag = kSRC_M7CoreIppUserResetFlag; #else uint32_t user_reset_flag = kSRC_IppUserResetFlag; #endif if (SRC->SRSR & user_reset_flag) { - return MP_OBJ_NEW_SMALL_INT(MP_DEEPSLEEP_RESET); + return MP_DEEPSLEEP_RESET; } uint16_t reset_cause = WDOG_GetStatusFlags(WDOG1) & (kWDOG_PowerOnResetFlag | kWDOG_TimeoutResetFlag | kWDOG_SoftwareResetFlag); @@ -116,20 +110,26 @@ STATIC mp_obj_t machine_reset_cause(void) { } else { reset_cause = MP_SOFT_RESET; } - return MP_OBJ_NEW_SMALL_INT(reset_cause); + return reset_cause; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); -STATIC mp_obj_t machine_freq(void) { +STATIC mp_obj_t mp_machine_get_freq(void) { return MP_OBJ_NEW_SMALL_INT(mp_hal_get_cpu_freq()); } -MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); + +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { + mp_raise_NotImplementedError(NULL); +} STATIC void mp_machine_idle(void) { MICROPY_EVENT_POLL_HOOK; } -STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) { +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { + mp_raise_NotImplementedError(NULL); +} + +NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) { if (n_args != 0) { mp_int_t seconds = mp_obj_get_int(args[0]) / 1000; if (seconds > 0) { @@ -155,10 +155,7 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) { while (true) { ; } - - return mp_const_none; } -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(); diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index ad35bfe8c5..8150e3b1f5 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -79,6 +79,7 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_RANDOM_SEED_INIT_FUNC (trng_random_u32()) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/mimxrt/modmachine.c" +#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1) #define MICROPY_PY_MACHINE_BOOTLOADER (1) #define MICROPY_PY_MACHINE_ADC (1) #define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/mimxrt/machine_adc.c" diff --git a/ports/nrf/modules/machine/modmachine.c b/ports/nrf/modules/machine/modmachine.c index 54e45b4d0f..7cdeb78c0a 100644 --- a/ports/nrf/modules/machine/modmachine.c +++ b/ports/nrf/modules/machine/modmachine.c @@ -85,13 +85,9 @@ #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \ { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, \ { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, \ { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, \ { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_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_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, \ { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) }, \ \ MICROPY_PY_MACHINE_RTCOUNTER_ENTRY \ @@ -178,14 +174,16 @@ STATIC mp_obj_t machine_info(mp_uint_t n_args, const mp_obj_t *args) { return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info); + +STATIC mp_obj_t mp_machine_unique_id(void) { + return mp_const_empty_bytes; +} // Resets the board in a manner similar to pushing the external RESET button. -STATIC mp_obj_t machine_reset(void) { +NORETURN STATIC void mp_machine_reset(void) { NVIC_SystemReset(); - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { MICROPY_BOARD_ENTER_BOOTLOADER(n_args, args); @@ -197,22 +195,25 @@ STATIC void mp_machine_idle(void) { MICROPY_EVENT_POLL_HOOK; } -STATIC mp_obj_t machine_lightsleep(void) { +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { __WFE(); - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_0(machine_lightsleep_obj, machine_lightsleep); -STATIC mp_obj_t machine_deepsleep(void) { - __WFI(); - return mp_const_none; +NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) { + mp_machine_reset(); } -MP_DEFINE_CONST_FUN_OBJ_0(machine_deepsleep_obj, machine_deepsleep); -STATIC mp_obj_t machine_reset_cause(void) { - return MP_OBJ_NEW_SMALL_INT(reset_cause); +STATIC mp_int_t mp_machine_reset_cause(void) { + return reset_cause; +} + +STATIC mp_obj_t mp_machine_get_freq(void) { + mp_raise_NotImplementedError(NULL); +} + +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { + mp_raise_NotImplementedError(NULL); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); STATIC mp_obj_t machine_enable_irq(void) { #ifndef BLUETOOTH_SD diff --git a/ports/nrf/modules/machine/modmachine.h b/ports/nrf/modules/machine/modmachine.h index 5aab2a6029..4574dc0010 100644 --- a/ports/nrf/modules/machine/modmachine.h +++ b/ports/nrf/modules/machine/modmachine.h @@ -32,9 +32,4 @@ void machine_init(void); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj); -MP_DECLARE_CONST_FUN_OBJ_0(machine_reset_obj); -MP_DECLARE_CONST_FUN_OBJ_0(machine_lightsleep_obj); -MP_DECLARE_CONST_FUN_OBJ_0(machine_deepsleep_obj); - #endif // __MICROPY_INCLUDED_NRF5_MODMACHINE_H__ diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index f3e71f675d..264b0dfe8d 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -160,6 +160,7 @@ #define MICROPY_PY_TIME (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/nrf/modules/machine/modmachine.c" +#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1) #define MICROPY_PY_MACHINE_PULSE (0) #define MICROPY_PY_MACHINE_SOFTI2C (MICROPY_PY_MACHINE_I2C) diff --git a/ports/renesas-ra/modmachine.c b/ports/renesas-ra/modmachine.c index b4479e1260..5bfbb919f4 100644 --- a/ports/renesas-ra/modmachine.c +++ b/ports/renesas-ra/modmachine.c @@ -63,13 +63,7 @@ #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \ { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, \ { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_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_reset_cause), MP_ROM_PTR(&machine_reset_cause_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) }, \ \ @@ -183,19 +177,16 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info); // Returns a string of 16 bytes (128 bits), which is the unique ID for the MCU. -STATIC mp_obj_t machine_unique_id(void) { +STATIC mp_obj_t mp_machine_unique_id(void) { uint8_t id[16]; get_unique_id((uint8_t *)&id); return mp_obj_new_bytes(id, 16); } -MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); // Resets the pyboard in a manner similar to pushing the external RESET button. -STATIC mp_obj_t machine_reset(void) { +NORETURN STATIC void mp_machine_reset(void) { powerctrl_mcu_reset(); - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); // Activate the bootloader without BOOT* pins. NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { @@ -218,17 +209,13 @@ NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { } // get or set the MCU frequencies -STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - // get - return mp_obj_new_int(SystemCoreClock); - } else { - // set - mp_raise_NotImplementedError(MP_ERROR_TEXT("machine.freq set not supported yet")); - return mp_const_none; - } +STATIC mp_obj_t mp_machine_get_freq(void) { + return mp_obj_new_int(SystemCoreClock); +} + +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("machine.freq set not supported yet")); } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 4, machine_freq); // idle() // This executies a wfi machine instruction which reduces power consumption @@ -237,27 +224,22 @@ STATIC void mp_machine_idle(void) { __WFI(); } -STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { if (n_args != 0) { mp_obj_t args2[2] = {MP_OBJ_NULL, args[0]}; machine_rtc_wakeup(2, args2); } powerctrl_enter_stop_mode(); - 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) { +NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) { if (n_args != 0) { mp_obj_t args2[2] = {MP_OBJ_NULL, args[0]}; machine_rtc_wakeup(2, args2); } powerctrl_enter_standby_mode(); - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep); -STATIC mp_obj_t machine_reset_cause(void) { - return MP_OBJ_NEW_SMALL_INT(reset_cause); +STATIC mp_int_t mp_machine_reset_cause(void) { + return reset_cause; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); diff --git a/ports/renesas-ra/modmachine.h b/ports/renesas-ra/modmachine.h index d9e6961063..e64b6cc3d7 100644 --- a/ports/renesas-ra/modmachine.h +++ b/ports/renesas-ra/modmachine.h @@ -33,25 +33,13 @@ extern const mp_obj_type_t machine_touchpad_type; extern const mp_obj_type_t machine_dac_type; extern const mp_obj_type_t machine_sdcard_type; - void machine_init(void); void machine_deinit(void); void machine_pin_init(void); void machine_pin_deinit(void); void machine_i2s_init0(void); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj); -MP_DECLARE_CONST_FUN_OBJ_0(machine_unique_id_obj); -MP_DECLARE_CONST_FUN_OBJ_0(machine_reset_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bootloader_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj); - -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj); - MP_DECLARE_CONST_FUN_OBJ_0(machine_disable_irq_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_enable_irq_obj); -MP_DECLARE_CONST_FUN_OBJ_0(pyb_irq_stats_obj); - #endif // MICROPY_INCLUDED_RENESAS_RA_MODMACHINE_H diff --git a/ports/renesas-ra/mpconfigport.h b/ports/renesas-ra/mpconfigport.h index 66655a8976..99286fa530 100644 --- a/ports/renesas-ra/mpconfigport.h +++ b/ports/renesas-ra/mpconfigport.h @@ -131,6 +131,7 @@ #ifndef MICROPY_PY_MACHINE #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/renesas-ra/modmachine.c" +#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1) #define MICROPY_PY_MACHINE_BOOTLOADER (1) #define MICROPY_PY_MACHINE_ADC (1) #define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/renesas-ra/machine_adc.c" diff --git a/ports/renesas-ra/powerctrl.c b/ports/renesas-ra/powerctrl.c index 26dc1cbe3a..548d31679a 100644 --- a/ports/renesas-ra/powerctrl.c +++ b/ports/renesas-ra/powerctrl.c @@ -245,7 +245,7 @@ void powerctrl_enter_stop_mode(void) { enable_irq(irq_state); } -void powerctrl_enter_standby_mode(void) { +NORETURN void powerctrl_enter_standby_mode(void) { rtc_init_finalise(); #if defined(MICROPY_BOARD_ENTER_STANDBY) @@ -301,4 +301,6 @@ void powerctrl_enter_standby_mode(void) { // enter standby mode // we never return; MCU is reset on exit from standby + + powerctrl_mcu_reset(); } diff --git a/ports/renesas-ra/powerctrl.h b/ports/renesas-ra/powerctrl.h index 1743150b32..34c40ea1ad 100644 --- a/ports/renesas-ra/powerctrl.h +++ b/ports/renesas-ra/powerctrl.h @@ -37,6 +37,6 @@ NORETURN void powerctrl_enter_bootloader(uint32_t r0, uint32_t bl_addr); void powerctrl_check_enter_bootloader(void); void powerctrl_enter_stop_mode(void); -void powerctrl_enter_standby_mode(void); +NORETURN void powerctrl_enter_standby_mode(void); #endif // MICROPY_INCLUDED_RA_POWERCTRL_H diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index 8cd9dd462e..2e9d1f20c0 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -48,14 +48,6 @@ #define RP2_RESET_WDT (3) #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \ - { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_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_freq), MP_ROM_PTR(&machine_freq_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) }, \ \ @@ -66,32 +58,28 @@ { MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(RP2_RESET_PWRON) }, \ { MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(RP2_RESET_WDT) }, \ -STATIC mp_obj_t machine_unique_id(void) { +STATIC mp_obj_t mp_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_reset(void) { +NORETURN STATIC void mp_machine_reset(void) { watchdog_reboot(0, SRAM_END, 0); for (;;) { __wfi(); } - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); -STATIC mp_obj_t machine_reset_cause(void) { +STATIC mp_int_t mp_machine_reset_cause(void) { int reset_cause; if (watchdog_caused_reboot()) { reset_cause = RP2_RESET_WDT; } else { reset_cause = RP2_RESET_PWRON; } - return MP_OBJ_NEW_SMALL_INT(reset_cause); + return reset_cause; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { MICROPY_BOARD_ENTER_BOOTLOADER(n_args, args); @@ -101,28 +89,26 @@ NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { } } -STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - return MP_OBJ_NEW_SMALL_INT(mp_hal_get_cpu_freq()); - } else { - mp_int_t freq = mp_obj_get_int(args[0]); - if (!set_sys_clock_khz(freq / 1000, false)) { - mp_raise_ValueError(MP_ERROR_TEXT("cannot change frequency")); - } - #if MICROPY_HW_ENABLE_UART_REPL - setup_default_uart(); - mp_uart_init(); - #endif - return mp_const_none; - } +STATIC mp_obj_t mp_machine_get_freq(void) { + return MP_OBJ_NEW_SMALL_INT(mp_hal_get_cpu_freq()); +} + +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { + mp_int_t freq = mp_obj_get_int(args[0]); + if (!set_sys_clock_khz(freq / 1000, false)) { + mp_raise_ValueError(MP_ERROR_TEXT("cannot change frequency")); + } + #if MICROPY_HW_ENABLE_UART_REPL + setup_default_uart(); + mp_uart_init(); + #endif } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq); STATIC void mp_machine_idle(void) { __wfe(); } -STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { mp_int_t delay_ms = 0; bool use_timer_alarm = false; @@ -131,7 +117,7 @@ STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { if (delay_ms <= 1) { // Sleep is too small, just use standard delay. mp_hal_delay_ms(delay_ms); - return mp_const_none; + return; } use_timer_alarm = delay_ms < (1ULL << 32) / 1000; if (use_timer_alarm) { @@ -148,7 +134,7 @@ STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { #if MICROPY_PY_NETWORK_CYW43 if (cyw43_has_pending && cyw43_poll != NULL) { restore_interrupts(my_interrupts); - return mp_const_none; + return; } #endif // Disable USB and ADC clocks. @@ -204,16 +190,12 @@ STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { // Bring back all clocks. clocks_init(); restore_interrupts(my_interrupts); - - 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(); +NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) { + mp_machine_lightsleep(n_args, args); + mp_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(); diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 8e9d29cf76..ee1f24b87a 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -112,6 +112,7 @@ #define MICROPY_PY_RANDOM_SEED_INIT_FUNC (rosc_random_u32()) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/rp2/modmachine.c" +#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1) #define MICROPY_PY_MACHINE_BOOTLOADER (1) #define MICROPY_PY_MACHINE_ADC (1) #define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/rp2/machine_adc.c" diff --git a/ports/samd/modmachine.c b/ports/samd/modmachine.c index 3271e160e1..9d53048b13 100644 --- a/ports/samd/modmachine.c +++ b/ports/samd/modmachine.c @@ -55,19 +55,12 @@ #endif #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \ - { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, \ - \ { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, \ { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \ MICROPY_PY_MACHINE_RTC_ENTRY \ \ { 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_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_lightsleep_obj) }, \ \ /* Class constants. */ \ /* Use numerical constants instead of the symbolic names, */ \ @@ -81,15 +74,13 @@ extern bool EIC_occured; extern uint32_t _dbl_tap_addr; -STATIC mp_obj_t machine_reset(void) { +NORETURN STATIC void mp_machine_reset(void) { *DBL_TAP_ADDR = DBL_TAP_MAGIC_RESET; #ifdef DBL_TAP_ADDR_ALT *DBL_TAP_ADDR_ALT = DBL_TAP_MAGIC_RESET; #endif NVIC_SystemReset(); - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { *DBL_TAP_ADDR = DBL_TAP_MAGIC_LOADER; @@ -99,25 +90,22 @@ NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { NVIC_SystemReset(); } -STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - return MP_OBJ_NEW_SMALL_INT(get_cpu_freq()); - } else { - uint32_t freq = mp_obj_get_int(args[0]); - if (freq >= 1000000 && freq <= MAX_CPU_FREQ) { - set_cpu_freq(freq); - } - return mp_const_none; +STATIC mp_obj_t mp_machine_get_freq(void) { + return MP_OBJ_NEW_SMALL_INT(get_cpu_freq()); +} + +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { + uint32_t freq = mp_obj_get_int(args[0]); + if (freq >= 1000000 && freq <= MAX_CPU_FREQ) { + set_cpu_freq(freq); } } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq); -STATIC mp_obj_t machine_unique_id(void) { +STATIC mp_obj_t mp_machine_unique_id(void) { samd_unique_id_t id; samd_get_unique_id(&id); return mp_obj_new_bytes((byte *)&id.bytes, sizeof(id.bytes)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); STATIC void mp_machine_idle(void) { MICROPY_EVENT_POLL_HOOK; @@ -136,18 +124,17 @@ STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) { } MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq); -STATIC mp_obj_t machine_reset_cause(void) { +STATIC mp_int_t mp_machine_reset_cause(void) { #if defined(MCU_SAMD21) - return MP_OBJ_NEW_SMALL_INT(PM->RCAUSE.reg); + return PM->RCAUSE.reg; #elif defined(MCU_SAMD51) - return MP_OBJ_NEW_SMALL_INT(RSTC->RCAUSE.reg); + return RSTC->RCAUSE.reg; #else - return MP_OBJ_NEW_SMALL_INT(0); + return 0; #endif } -MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); -STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { int32_t duration = -1; uint32_t freq = get_cpu_freq(); if (n_args > 0) { @@ -197,6 +184,9 @@ STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { #endif // Speed up again set_cpu_freq(freq); - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine_lightsleep); + +NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) { + mp_machine_lightsleep(n_args, args); + mp_machine_reset(); +} diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 90d014e811..1c29b024cf 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -94,6 +94,7 @@ #define MICROPY_PY_TIME_INCLUDEFILE "ports/samd/modtime.c" #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/samd/modmachine.c" +#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1) #define MICROPY_PY_MACHINE_BOOTLOADER (1) #define MICROPY_PY_OS (1) #define MICROPY_PY_OS_INCLUDEFILE "ports/samd/modos.c" diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 781ceea7d3..206d28a89e 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -97,14 +97,8 @@ #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \ { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, \ - { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, \ MICROPY_PY_MACHINE_RNG_ENTRY \ { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_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_reset_cause), MP_ROM_PTR(&machine_reset_cause_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) }, \ @@ -283,18 +277,15 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info); // Returns a string of 12 bytes (96 bits), which is the unique ID for the MCU. -STATIC mp_obj_t machine_unique_id(void) { +STATIC mp_obj_t mp_machine_unique_id(void) { byte *id = (byte *)MP_HAL_UNIQUE_ID_ADDRESS; return mp_obj_new_bytes(id, 12); } -MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id); // Resets the pyboard in a manner similar to pushing the external RESET button. -STATIC mp_obj_t machine_reset(void) { +NORETURN STATIC void mp_machine_reset(void) { powerctrl_mcu_reset(); - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); // Activate the bootloader without BOOT* pins. NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { @@ -323,57 +314,53 @@ NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args) { } // get or set the MCU frequencies -STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - // get - mp_obj_t tuple[] = { - mp_obj_new_int(HAL_RCC_GetSysClockFreq()), - mp_obj_new_int(HAL_RCC_GetHCLKFreq()), - mp_obj_new_int(HAL_RCC_GetPCLK1Freq()), - #if !defined(STM32F0) && !defined(STM32G0) - mp_obj_new_int(HAL_RCC_GetPCLK2Freq()), - #endif - }; - return mp_obj_new_tuple(MP_ARRAY_SIZE(tuple), tuple); - } else { - // set - #if defined(STM32F0) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32G0) - mp_raise_NotImplementedError(MP_ERROR_TEXT("machine.freq set not supported yet")); - #else - mp_int_t sysclk = mp_obj_get_int(args[0]); - mp_int_t ahb = sysclk; - #if defined(STM32H7) - if (ahb > 200000000) { - ahb /= 2; - } +STATIC mp_obj_t mp_machine_get_freq(void) { + mp_obj_t tuple[] = { + mp_obj_new_int(HAL_RCC_GetSysClockFreq()), + mp_obj_new_int(HAL_RCC_GetHCLKFreq()), + mp_obj_new_int(HAL_RCC_GetPCLK1Freq()), + #if !defined(STM32F0) && !defined(STM32G0) + mp_obj_new_int(HAL_RCC_GetPCLK2Freq()), #endif - #if defined(STM32WB) - mp_int_t apb1 = ahb; - mp_int_t apb2 = ahb; - #else - mp_int_t apb1 = ahb / 4; - mp_int_t apb2 = ahb / 2; - #endif - if (n_args > 1) { - ahb = mp_obj_get_int(args[1]); - if (n_args > 2) { - apb1 = mp_obj_get_int(args[2]); - if (n_args > 3) { - apb2 = mp_obj_get_int(args[3]); - } + }; + return mp_obj_new_tuple(MP_ARRAY_SIZE(tuple), tuple); +} + +STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { + #if defined(STM32F0) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32G0) + mp_raise_NotImplementedError(MP_ERROR_TEXT("machine.freq set not supported yet")); + #else + mp_int_t sysclk = mp_obj_get_int(args[0]); + mp_int_t ahb = sysclk; + #if defined(STM32H7) + if (ahb > 200000000) { + ahb /= 2; + } + #endif + #if defined(STM32WB) + mp_int_t apb1 = ahb; + mp_int_t apb2 = ahb; + #else + mp_int_t apb1 = ahb / 4; + mp_int_t apb2 = ahb / 2; + #endif + if (n_args > 1) { + ahb = mp_obj_get_int(args[1]); + if (n_args > 2) { + apb1 = mp_obj_get_int(args[2]); + if (n_args > 3) { + apb2 = mp_obj_get_int(args[3]); } } - int ret = powerctrl_set_sysclk(sysclk, ahb, apb1, apb2); - if (ret == -MP_EINVAL) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid freq")); - } else if (ret < 0) { - MICROPY_BOARD_FATAL_ERROR("can't change freq"); - } - return mp_const_none; - #endif } + int ret = powerctrl_set_sysclk(sysclk, ahb, apb1, apb2); + if (ret == -MP_EINVAL) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid freq")); + } else if (ret < 0) { + MICROPY_BOARD_FATAL_ERROR("can't change freq"); + } + #endif } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 4, machine_freq); // idle() // This executies a wfi machine instruction which reduces power consumption @@ -382,27 +369,22 @@ STATIC void mp_machine_idle(void) { __WFI(); } -STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { +STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { if (n_args != 0) { mp_obj_t args2[2] = {MP_OBJ_NULL, args[0]}; pyb_rtc_wakeup(2, args2); } powerctrl_enter_stop_mode(); - 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) { +STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) { if (n_args != 0) { mp_obj_t args2[2] = {MP_OBJ_NULL, args[0]}; pyb_rtc_wakeup(2, args2); } powerctrl_enter_standby_mode(); - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep); -STATIC mp_obj_t machine_reset_cause(void) { - return MP_OBJ_NEW_SMALL_INT(reset_cause); +STATIC mp_int_t mp_machine_reset_cause(void) { + return reset_cause; } -MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); diff --git a/ports/stm32/modmachine.h b/ports/stm32/modmachine.h index 8c9e1241b1..899a29be8e 100644 --- a/ports/stm32/modmachine.h +++ b/ports/stm32/modmachine.h @@ -33,13 +33,6 @@ void machine_deinit(void); void machine_i2s_init0(); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj); -MP_DECLARE_CONST_FUN_OBJ_0(machine_unique_id_obj); -MP_DECLARE_CONST_FUN_OBJ_0(machine_reset_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj); - -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj); -MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj); -MP_DECLARE_CONST_FUN_OBJ_0(machine_reset_cause_obj); MP_DECLARE_CONST_FUN_OBJ_0(machine_disable_irq_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_enable_irq_obj); diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index db48c8c3b6..e11e721030 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -111,6 +111,7 @@ #ifndef MICROPY_PY_MACHINE #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_INCLUDEFILE "ports/stm32/modmachine.c" +#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1) #define MICROPY_PY_MACHINE_BOOTLOADER (1) #define MICROPY_PY_MACHINE_ADC (1) #define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/stm32/machine_adc.c" diff --git a/ports/stm32/powerctrl.c b/ports/stm32/powerctrl.c index 1bb66c9781..712021edb6 100644 --- a/ports/stm32/powerctrl.c +++ b/ports/stm32/powerctrl.c @@ -993,7 +993,7 @@ void powerctrl_enter_stop_mode(void) { enable_irq(irq_state); } -void powerctrl_enter_standby_mode(void) { +NORETURN void powerctrl_enter_standby_mode(void) { rtc_init_finalise(); #if defined(MICROPY_BOARD_ENTER_STANDBY) @@ -1114,5 +1114,7 @@ void powerctrl_enter_standby_mode(void) { // enter standby mode HAL_PWR_EnterSTANDBYMode(); - // we never return; MCU is reset on exit from standby + + // MCU is reset on exit from standby, but just in case it's not, do an explicit reset. + powerctrl_mcu_reset(); } diff --git a/ports/stm32/powerctrl.h b/ports/stm32/powerctrl.h index e9adc52965..5b92405611 100644 --- a/ports/stm32/powerctrl.h +++ b/ports/stm32/powerctrl.h @@ -47,6 +47,6 @@ void powerctrl_config_systick(void); int powerctrl_rcc_clock_config_pll(RCC_ClkInitTypeDef *rcc_init, uint32_t sysclk_mhz, bool need_pllsai); int powerctrl_set_sysclk(uint32_t sysclk, uint32_t ahb, uint32_t apb1, uint32_t apb2); void powerctrl_enter_stop_mode(void); -void powerctrl_enter_standby_mode(void); +NORETURN void powerctrl_enter_standby_mode(void); #endif // MICROPY_INCLUDED_STM32_POWERCTRL_H diff --git a/ports/zephyr/modmachine.c b/ports/zephyr/modmachine.c index 16109a3af8..7015989784 100644 --- a/ports/zephyr/modmachine.c +++ b/ports/zephyr/modmachine.c @@ -50,13 +50,13 @@ STATIC mp_obj_t machine_reset(void) { // Won't get here, Zephyr has infiniloop on its side return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); +MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); STATIC mp_obj_t machine_reset_cause(void) { printf("Warning: %s is not implemented\n", __func__); return MP_OBJ_NEW_SMALL_INT(42); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); +MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); STATIC void mp_machine_idle(void) { k_yield(); diff --git a/ports/zephyr/modmachine.h b/ports/zephyr/modmachine.h index e3a69d8c1b..19dbe6a16a 100644 --- a/ports/zephyr/modmachine.h +++ b/ports/zephyr/modmachine.h @@ -3,8 +3,6 @@ #include "py/obj.h" -MP_DECLARE_CONST_FUN_OBJ_0(machine_info_obj); - typedef struct _machine_pin_obj_t { mp_obj_base_t base; const struct device *port;