diff --git a/examples/embedding-full/main.c b/examples/embedding-full/main.c index f8318ee7ed..62b18e8e9f 100644 --- a/examples/embedding-full/main.c +++ b/examples/embedding-full/main.c @@ -28,8 +28,15 @@ static const char *example_2 = "help('modules')\n" "import sys\n" "help(sys)\n" + // Skip testing stdin when using the POSIX implementation, i.e. it is + // connected to actual stdin, because that makes the program wait for input, + // which can be inconvenient or confusing (giving the appearance of being + // stuck). There is no harm enabling it if you remember to provide some + // input. +#if !MICROPY_VFS_POSIX "print('sys.stdin.read(3):', repr(sys.stdin.read(3)))\n" "print('sys.stdin.buffer.read(3):', repr(sys.stdin.buffer.read(3)))\n" +#endif "sys.stdout.write('hello stdout text\\n')\n" "sys.stdout.buffer.write(b'hello stdout binary\\n')\n" "sys.stdout.write('hello stderr text\\n')\n" @@ -61,12 +68,23 @@ static const char *example_2 = "import lfshello\n" "help(lfshello)\n" "print('lfshello.hello():', lfshello.hello())\n" + "vfs.mount(vfs.VfsPosix('.'), '/posix')\n" + "print('os.listdir(\\'/posix\\'):', os.listdir('/posix'))\n" + "with open('/posix/posixhello.py', 'wb') as f:\n" + " f.write(b'def hello():\\n return \"Hello from a POSIX file!\"\\n')\n" + "with open('/posix/posixhello.py', 'rb') as f:\n" + " print('posixhello.py:', f.read())\n" + "sys.path.append('/posix')\n" + "import posixhello\n" + "help(posixhello)\n" + "print('posixhello.hello():', posixhello.hello())\n" + "os.unlink('/posix/posixhello.py')" "\n" "print('finish')\n" ; // This array is the MicroPython GC heap. -static char heap[12 * 1024]; +static char heap[16 * 1024]; int main() { #if MICROPY_STACK_CHECK diff --git a/examples/embedding-full/micropython_embed.mk b/examples/embedding-full/micropython_embed.mk index 881e4b11a4..1d30d2a45a 100644 --- a/examples/embedding-full/micropython_embed.mk +++ b/examples/embedding-full/micropython_embed.mk @@ -16,8 +16,8 @@ EMBED_EXTRA += \ # Include source for mphal-backed stdio in the output. # Disable when using POSIX-backed stdio (MICROPY_VFS_POSIX). -EMBED_EXTRA += \ - shared/runtime/sys_stdio_mphal.c +#EMBED_EXTRA += \ +# shared/runtime/sys_stdio_mphal.c # Include library sources for littlefs 2 in the output. EMBED_EXTRA += littlefs2 diff --git a/examples/embedding-full/mpconfigport.h b/examples/embedding-full/mpconfigport.h index 59bbc2dbfa..2bfb38f7eb 100644 --- a/examples/embedding-full/mpconfigport.h +++ b/examples/embedding-full/mpconfigport.h @@ -19,9 +19,11 @@ #define MICROPY_HW_BOARD_NAME "embedded" #define MICROPY_HW_MCU_NAME "C" -// Enable the VFS subsystem, littlefs, importing from VFS, and the vfs module. +// Enable the VFS subsystem, littlefs and POSIX VFS, importing from VFS, and +// the vfs module. #define MICROPY_VFS (1) #define MICROPY_VFS_LFS2 (1) +#define MICROPY_VFS_POSIX (1) #define MICROPY_READER_VFS (1) #define MICROPY_PY_VFS (1) diff --git a/examples/embedding-full/mphal.c b/examples/embedding-full/mphal.c index 76654c6155..c540179a05 100644 --- a/examples/embedding-full/mphal.c +++ b/examples/embedding-full/mphal.c @@ -65,6 +65,8 @@ uint64_t mp_hal_time_ns(void) { #endif // Text-mode standard output +// (When MICROPY_PY_SYS_STDFILES && MICROPY_VFS_POSIX, this is only used for +// mp_plat_print (mostly debug output), but not for print() and sys.stdout.) void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { // This is a simplistic implementation for demonstration purposes. A real // one would probably want to prefix every line, not just at the start of a @@ -75,7 +77,7 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { start_of_line = (len > 0 && (str[len-1] == '\n' || str[len-1] == '\r')); } -#if MICROPY_PY_SYS_STDFILES +#if MICROPY_PY_SYS_STDFILES && !MICROPY_VFS_POSIX // Binary-mode standard input // Receive single character, blocking until one is available. diff --git a/ports/embed/port/mphalport.h b/ports/embed/port/mphalport.h index 43cf41de12..d0e2e8b01c 100644 --- a/ports/embed/port/mphalport.h +++ b/ports/embed/port/mphalport.h @@ -4,3 +4,23 @@ #if MICROPY_KBD_EXCEPTION void mp_hal_set_interrupt_char(int c); #endif + +#if MICROPY_VFS_POSIX +// This macro is used to implement PEP 475 to retry specified syscalls on EINTR +#define MP_HAL_RETRY_SYSCALL(ret, syscall, raise) { \ + for (;;) { \ + MP_THREAD_GIL_EXIT(); \ + ret = syscall; \ + MP_THREAD_GIL_ENTER(); \ + if (ret == -1) { \ + int err = errno; \ + if (err == EINTR) { \ + mp_handle_pending(true); \ + continue; \ + } \ + raise; \ + } \ + break; \ + } \ +} +#endif