summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2023-11-24 18:25:30 +1100
committerDamien George <damien@micropython.org>2023-11-30 16:11:11 +1100
commite1ec6af654b1c5c4a973b6c6b029ee68bb92eb89 (patch)
treebe9ca3b41d2fa993a7af4c3694ebbb63a05d4713
parent48b5a7b06079f0d0aa4780016f015ff023875680 (diff)
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 <damien@micropython.org>
-rw-r--r--extmod/modmachine.c64
-rw-r--r--extmod/modmachine.h6
-rw-r--r--ports/cc3200/mods/modmachine.c41
-rw-r--r--ports/cc3200/mpconfigport.h1
-rw-r--r--ports/esp32/modmachine.c135
-rw-r--r--ports/esp32/mpconfigport.h1
-rw-r--r--ports/esp8266/modmachine.c54
-rw-r--r--ports/esp8266/mpconfigport.h1
-rw-r--r--ports/mimxrt/modmachine.c39
-rw-r--r--ports/mimxrt/mpconfigport.h1
-rw-r--r--ports/nrf/modules/machine/modmachine.c37
-rw-r--r--ports/nrf/modules/machine/modmachine.h5
-rw-r--r--ports/nrf/mpconfigport.h1
-rw-r--r--ports/renesas-ra/modmachine.c42
-rw-r--r--ports/renesas-ra/modmachine.h12
-rw-r--r--ports/renesas-ra/mpconfigport.h1
-rw-r--r--ports/renesas-ra/powerctrl.c4
-rw-r--r--ports/renesas-ra/powerctrl.h2
-rw-r--r--ports/rp2/modmachine.c62
-rw-r--r--ports/rp2/mpconfigport.h1
-rw-r--r--ports/samd/modmachine.c50
-rw-r--r--ports/samd/mpconfigport.h1
-rw-r--r--ports/stm32/modmachine.c114
-rw-r--r--ports/stm32/modmachine.h7
-rw-r--r--ports/stm32/mpconfigport.h1
-rw-r--r--ports/stm32/powerctrl.c6
-rw-r--r--ports/stm32/powerctrl.h2
-rw-r--r--ports/zephyr/modmachine.c4
-rw-r--r--ports/zephyr/modmachine.h2
29 files changed, 316 insertions, 381 deletions
diff --git a/extmod/modmachine.c b/extmod/modmachine.c
index 789d324d6..d56b938bb 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 b0253bbb8..d186ce661 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 1df11c9d2..5eb7e2ef1 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 f2dff028f..7c5273245 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 18bc0965a..0170655ba 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
+ ) {
+ #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
- 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_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 ef55d9ae3..083f1d11c 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 cce6b01ce..d55a656ce 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);
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq);
-STATIC mp_obj_t machine_reset(void) {
+NORETURN STATIC void mp_machine_reset(void) {
system_restart();
- return mp_const_none;
+
+ // we must not return
+ for (;;) {
+ ets_loop_iter();
+ }
}
-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_int_t mp_machine_reset_cause(void) {
+ return 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 fa9acce6c..1925fc3e7 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 8bcf10122..b299112d9 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 ad35bfe8c..8150e3b1f 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 54e45b4d0..7cdeb78c0 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();
+}
+
+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);
}
-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 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 5aab2a602..4574dc001 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 f3e71f675..264b0dfe8 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 b4479e126..5bfbb919f 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 d9e696106..e64b6cc3d 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 66655a897..99286fa53 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 26dc1cbe3..548d31679 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 1743150b3..34c40ea1a 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 8cd9dd462..2e9d1f20c 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 8e9d29cf7..ee1f24b87 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 3271e160e..9d53048b1 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 90d014e81..1c29b024c 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 781ceea7d..206d28a89 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;
- }
- #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;
+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 (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 8c9e1241b..899a29be8 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 db48c8c3b..e11e72103 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 1bb66c978..712021edb 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 e9adc5296..5b9240561 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 16109a3af..701598978 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 e3a69d8c1..19dbe6a16 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;