Skip to content

Instantly share code, notes, and snippets.

@projectgus
Created October 26, 2022 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save projectgus/55a5a155c87b8fd424fe580d130d1008 to your computer and use it in GitHub Desktop.
Save projectgus/55a5a155c87b8fd424fe580d130d1008 to your computer and use it in GitHub Desktop.
MicroPython patch to output stdout to semihosting whenever USB-CDC is closed
diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt
index 8c8119e06..98c3de93d 100644
--- a/ports/rp2/CMakeLists.txt
+++ b/ports/rp2/CMakeLists.txt
@@ -92,6 +92,7 @@ set(MICROPY_SOURCE_LIB
${MICROPY_DIR}/shared/runtime/interrupt_char.c
${MICROPY_DIR}/shared/runtime/mpirq.c
${MICROPY_DIR}/shared/runtime/pyexec.c
+ ${MICROPY_DIR}/shared/runtime/semihosting.c
${MICROPY_DIR}/shared/runtime/stdout_helpers.c
${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
${MICROPY_DIR}/shared/timeutils/timeutils.c
diff --git a/ports/rp2/main.c b/ports/rp2/main.c
index 0cf22bfec..28f330167 100644
--- a/ports/rp2/main.c
+++ b/ports/rp2/main.c
@@ -37,6 +37,7 @@
#include "shared/readline/readline.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"
+#include "shared/runtime/semihosting.h"
#include "tusb.h"
#include "uart.h"
#include "modmachine.h"
@@ -85,6 +86,8 @@ int main(int argc, char **argv) {
mp_uart_init();
#endif
+ mp_semihosting_init();
+
#if MICROPY_HW_ENABLE_USBDEV
bi_decl(bi_program_feature("USB REPL"))
tusb_init();
diff --git a/ports/rp2/mphalport.c b/ports/rp2/mphalport.c
index 63ff76f5d..c88baac3f 100644
--- a/ports/rp2/mphalport.c
+++ b/ports/rp2/mphalport.c
@@ -146,6 +146,9 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
tud_cdc_write_flush();
i += n2;
}
+ } else {
+ #include "shared/runtime/semihosting.h"
+ mp_semihosting_tx_strn(str, len);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment