diff --git a/pycardium/meson.build b/pycardium/meson.build index 895be888ee81bdd886b990d12f7c6d63321a6c2e..2a1f08939c5f69273186d28050f44fe1bbe3b8b1 100644 --- a/pycardium/meson.build +++ b/pycardium/meson.build @@ -71,6 +71,7 @@ upy = static_library( elf = executable( name + '.elf', 'main.c', + 'patch.c', 'mphalport.c', frozen_source, modsrc, diff --git a/pycardium/mphalport.c b/pycardium/mphalport.c index 95d4bffa90089570d4e26ab5f098345b646c8dab..ee9e2f44fb7321c7dd5c3c5e1a5b9522575844e6 100644 --- a/pycardium/mphalport.c +++ b/pycardium/mphalport.c @@ -8,6 +8,7 @@ #include "py/mpstate.h" #include "py/obj.h" #include "py/runtime.h" +#include "py/mpprint.h" #include "mxc_delay.h" #include "max32665.h" @@ -15,6 +16,7 @@ #include "epicardium.h" #include "api/common.h" + /****************************************************************************** * Serial Communication */ @@ -36,30 +38,15 @@ int DEBUG_printf(const char *fmt, ...) { va_list args; va_start(args, fmt); - int ret = vprintf(fmt, args); + int ret = mp_vprintf(MP_PYTHON_PRINTER, fmt, args); va_end(args); return ret; } -/* newlib syscall to allow printf to work */ -long _write(int fd, const char *buf, size_t cnt) +void __attribute__((noreturn)) sbrk_is_not_implemented___see_issue_44(void); +intptr_t _sbrk(int incr) { - /* - * Only print one line at a time. Insert `\r` between lines so - * they are properly displayed on the serial console. - */ - size_t i, last = 0; - for (i = 0; i < cnt; i++) { - if (buf[i] == '\n') { - epic_uart_write_str(&buf[last], i - last); - epic_uart_write_str("\r", 1); - last = i; - } - } - if (last != i) { - epic_uart_write_str(&buf[last], cnt - last); - } - return cnt; + sbrk_is_not_implemented___see_issue_44(); } void epic_isr_ctrl_c(void) diff --git a/pycardium/patch.c b/pycardium/patch.c new file mode 100644 index 0000000000000000000000000000000000000000..902653ec58450d7710ea2dffd5729ee9d22d89d9 --- /dev/null +++ b/pycardium/patch.c @@ -0,0 +1,71 @@ +#include "epicardium.h" + +#include <stdarg.h> +#include <stdio.h> + +#include "py/mpprint.h" +#include "py/mphal.h" + +#include <string.h> + +int printf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + int ret = mp_vprintf(MP_PYTHON_PRINTER, fmt, ap); + va_end(ap); + return ret; +} + +#ifdef putc +#undef putc +#endif /* putc */ +int putc(int c, FILE *f) +{ + char chr = (char)c; + mp_hal_stdout_tx_strn_cooked(&chr, 1); + return c; +} + +#ifdef putchar +#undef putchar +#endif /* putchar */ +int putchar(int c) +{ + char chr = (char)c; + mp_hal_stdout_tx_strn_cooked(&chr, 1); + return c; +} + +int puts(const char *s) +{ + int length = strlen(s); + if (length) { + mp_hal_stdout_tx_strn_cooked(s, length); + } + return length; +} + +/* Used by mp_hal_move_cursor_back() */ +int snprintf(char *str, size_t size, const char *format, ...) +{ + /* TODO: What should we do with this? */ + return -EIO; +} + +/* Use by assert() */ +int fiprintf(FILE *f, const char *fmt, ...) +{ + /* TODO: Maybe printing everything to UART isn't the correct thing? */ + va_list ap; + va_start(ap, fmt); + int ret = mp_vprintf(MP_PYTHON_PRINTER, fmt, ap); + va_end(ap); + return ret; +} + +int raise(int sig) +{ + /* Ignore signals */ + return 0; +}