From e73f8fd5f7f887f1fefd67148198dcf55fdbb73a Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 22 Aug 2023 17:15:46 +0100 Subject: [PATCH] ports/rp2: Additions for rp2 init hooks. Signed-off-by: Christopher Parrott --- ports/rp2/CMakeLists.txt | 2 ++ ports/rp2/main.c | 16 ++++++++++++++++ ports/rp2/mpconfigport.h | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index fcc435b7b9..1eaefcfb9e 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -412,6 +412,7 @@ endif() list(APPEND MICROPY_SOURCE_QSTR ${MICROPY_SOURCE_EXTMOD} ${MICROPY_SOURCE_USERMOD} + ${MICROPY_SOURCE_BOARD} ) # Define mpy-cross flags @@ -430,6 +431,7 @@ target_sources(${MICROPY_TARGET} PRIVATE ${MICROPY_SOURCE_LIB} ${MICROPY_SOURCE_DRIVERS} ${MICROPY_SOURCE_PORT} + ${MICROPY_SOURCE_BOARD} ) target_link_libraries(${MICROPY_TARGET} micropy_lib_mbedtls) diff --git a/ports/rp2/main.c b/ports/rp2/main.c index 40374faff9..ec58b70aef 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -76,6 +76,9 @@ int main(int argc, char **argv) { // This is a tickless port, interrupts should always trigger SEV. SCB->SCR |= SCB_SCR_SEVONPEND_Msk; + // Hook for setting up anything that needs to be super early in the bootup process + MICROPY_BOARD_STARTUP(); + #if MICROPY_HW_ENABLE_UART_REPL bi_decl(bi_program_feature("UART REPL")) setup_default_uart(); @@ -146,6 +149,9 @@ int main(int argc, char **argv) { } #endif + // Hook for setting up anything that can wait until after other hardware features are initialised + MICROPY_BOARD_EARLY_INIT(); + for (;;) { // Initialise MicroPython runtime. @@ -169,6 +175,9 @@ int main(int argc, char **argv) { mod_network_lwip_init(); #endif + // Hook for setting up anything that should follow the MicroPy runtime but be prior to _boot.py + MICROPY_BOARD_LATE_INIT(); + // Execute _boot.py to set up the filesystem. #if MICROPY_VFS_FAT && MICROPY_HW_USB_MSC pyexec_frozen_module("_boot_fat.py", false); @@ -207,6 +216,10 @@ int main(int argc, char **argv) { soft_reset_exit: mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); + + // Hook for resetting anything immediately following a soft reboot command + MICROPY_BOARD_EARLY_RESET(); + #if MICROPY_PY_NETWORK mod_network_deinit(); #endif @@ -227,6 +240,9 @@ int main(int argc, char **argv) { gc_sweep_all(); mp_deinit(); + + // Hook for resetting anything after the MicroPy runtime has been deinitialised + MICROPY_BOARD_LATE_RESET(); } return 0; diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index a29692d0be..160a712707 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -275,3 +275,23 @@ extern void lwip_lock_release(void); #define MICROPY_PY_BLUETOOTH_ENTER uint32_t atomic_state = 0; #define MICROPY_PY_BLUETOOTH_EXIT (void)atomic_state; #endif + +#ifndef MICROPY_BOARD_STARTUP +#define MICROPY_BOARD_STARTUP() +#endif + +#ifndef MICROPY_BOARD_EARLY_INIT +#define MICROPY_BOARD_EARLY_INIT() +#endif + +#ifndef MICROPY_BOARD_LATE_INIT +#define MICROPY_BOARD_LATE_INIT() +#endif + +#ifndef MICROPY_BOARD_EARLY_RESET +#define MICROPY_BOARD_EARLY_RESET() +#endif + +#ifndef MICROPY_BOARD_LATE_RESET +#define MICROPY_BOARD_LATE_RESET() +#endif