Skip to content

Commit 7016512

Browse files
committed
ports/embed: Enable use of POSIX VFS.
In the embedding-full example, that requires disabling mphal-backed stdio again as it conflicts with the implementation brought in by vfs_posix_file.c. Signed-off-by: Christian Walther <cwalther@gmx.ch>
1 parent dfc78be commit 7016512

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

examples/embedding-full/main.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ static const char *example_2 =
2828
"help('modules')\n"
2929
"import sys\n"
3030
"help(sys)\n"
31+
// Skip testing stdin when using the POSIX implementation, i.e. it is
32+
// connected to actual stdin, because that makes the program wait for input,
33+
// which can be inconvenient or confusing (giving the appearance of being
34+
// stuck). There is no harm enabling it if you remember to provide some
35+
// input.
36+
#if !MICROPY_VFS_POSIX
3137
"print('sys.stdin.read(3):', repr(sys.stdin.read(3)))\n"
3238
"print('sys.stdin.buffer.read(3):', repr(sys.stdin.buffer.read(3)))\n"
39+
#endif
3340
"sys.stdout.write('hello stdout text\\n')\n"
3441
"sys.stdout.buffer.write(b'hello stdout binary\\n')\n"
3542
"sys.stdout.write('hello stderr text\\n')\n"
@@ -61,12 +68,23 @@ static const char *example_2 =
6168
"import lfshello\n"
6269
"help(lfshello)\n"
6370
"print('lfshello.hello():', lfshello.hello())\n"
71+
"vfs.mount(vfs.VfsPosix('.'), '/posix')\n"
72+
"print('os.listdir(\\'/posix\\'):', os.listdir('/posix'))\n"
73+
"with open('/posix/posixhello.py', 'wb') as f:\n"
74+
" f.write(b'def hello():\\n return \"Hello from a POSIX file!\"\\n')\n"
75+
"with open('/posix/posixhello.py', 'rb') as f:\n"
76+
" print('posixhello.py:', f.read())\n"
77+
"sys.path.append('/posix')\n"
78+
"import posixhello\n"
79+
"help(posixhello)\n"
80+
"print('posixhello.hello():', posixhello.hello())\n"
81+
"os.unlink('/posix/posixhello.py')"
6482
"\n"
6583
"print('finish')\n"
6684
;
6785

6886
// This array is the MicroPython GC heap.
69-
static char heap[12 * 1024];
87+
static char heap[16 * 1024];
7088

7189
int main() {
7290
#if MICROPY_STACK_CHECK

examples/embedding-full/micropython_embed.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ EMBED_EXTRA += \
1616

1717
# Include source for mphal-backed stdio in the output.
1818
# Disable when using POSIX-backed stdio (MICROPY_VFS_POSIX).
19-
EMBED_EXTRA += \
20-
shared/runtime/sys_stdio_mphal.c
19+
#EMBED_EXTRA += \
20+
# shared/runtime/sys_stdio_mphal.c
2121

2222
# Include library sources for littlefs 2 in the output.
2323
EMBED_EXTRA += littlefs2

examples/embedding-full/mpconfigport.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
#define MICROPY_HW_BOARD_NAME "embedded"
2020
#define MICROPY_HW_MCU_NAME "C"
2121

22-
// Enable the VFS subsystem, littlefs, importing from VFS, and the vfs module.
22+
// Enable the VFS subsystem, littlefs and POSIX VFS, importing from VFS, and
23+
// the vfs module.
2324
#define MICROPY_VFS (1)
2425
#define MICROPY_VFS_LFS2 (1)
26+
#define MICROPY_VFS_POSIX (1)
2527
#define MICROPY_READER_VFS (1)
2628
#define MICROPY_PY_VFS (1)
2729

examples/embedding-full/mphal.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ uint64_t mp_hal_time_ns(void) {
6565
#endif
6666

6767
// Text-mode standard output
68+
// (When MICROPY_PY_SYS_STDFILES && MICROPY_VFS_POSIX, this is only used for
69+
// mp_plat_print (mostly debug output), but not for print() and sys.stdout.)
6870
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
6971
// This is a simplistic implementation for demonstration purposes. A real
7072
// 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) {
7577
start_of_line = (len > 0 && (str[len-1] == '\n' || str[len-1] == '\r'));
7678
}
7779

78-
#if MICROPY_PY_SYS_STDFILES
80+
#if MICROPY_PY_SYS_STDFILES && !MICROPY_VFS_POSIX
7981

8082
// Binary-mode standard input
8183
// Receive single character, blocking until one is available.

ports/embed/port/mphalport.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,23 @@
44
#if MICROPY_KBD_EXCEPTION
55
void mp_hal_set_interrupt_char(int c);
66
#endif
7+
8+
#if MICROPY_VFS_POSIX
9+
// This macro is used to implement PEP 475 to retry specified syscalls on EINTR
10+
#define MP_HAL_RETRY_SYSCALL(ret, syscall, raise) { \
11+
for (;;) { \
12+
MP_THREAD_GIL_EXIT(); \
13+
ret = syscall; \
14+
MP_THREAD_GIL_ENTER(); \
15+
if (ret == -1) { \
16+
int err = errno; \
17+
if (err == EINTR) { \
18+
mp_handle_pending(true); \
19+
continue; \
20+
} \
21+
raise; \
22+
} \
23+
break; \
24+
} \
25+
}
26+
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy