diff --git a/Documentation/applications/interpreters/python/index.rst b/Documentation/applications/interpreters/python/index.rst new file mode 100644 index 0000000000000..bf20bc80782f3 --- /dev/null +++ b/Documentation/applications/interpreters/python/index.rst @@ -0,0 +1,62 @@ +============================= +``Python`` Python interpreter +============================= + +This guide explains how to run Python on NuttX. + +*Yes, you heard it right*. **Python on NuttX**. This is a port of the `CPython `_ repository for NuttX. +The `CPython` repository is the reference implementation of the Python programming language. +It is written in C and is the most widely used Python interpreter. + +.. warning:: + Python for NuttX is still in the experimental stage (thus, it requires ``CONFIG_EXPERIMENTAL`` to be enabled) + It is not fully functional for all the architectures and configurations. + Please check this `issue `_ in the `nuttx-apps `_ repository to know the current status. + +How Does it Work? +================= + +1. Python for NuttX target initially the ``rv-virt`` (RISC-V QEMU) board. +2. Python modules are stored in `pyc `_ (byte-code format) and are loaded from a ROMFS image at startup. +3. Environment variables like ``PYTHONHOME`` and ``PYTHON_BASIC_REPL`` need to be set accordingly. + +Building Python NuttX +===================== + +Use the ``rv-virt:python`` config to build Python for NuttX. Note that the CMake scripts don't work for this configuration. For now, please use the makefile build instead: + +.. code:: console + + $ cd nuttx + $ make distclean + $ ./tools/configure.sh rv-virt:python + $ make -j$(nproc) + $ ls -l nuttx + +This will generate a ``nutxx`` binary. This file can be run using the RISC-V QEMU. + +Try Python in NSH +================= + +.. code:: console + + $ .qemu-system-riscv32 -semihosting -M virt,aclint=on -cpu rv32 -smp 1 -bios none -kernel nuttx -nographic + + ABC + NuttShell (NSH) NuttX-10.4.0 + nsh> mount_modules + Mounting ROMFS filesystem at target=/usr/local/lib/ with source=/dev/ram1 + nsh> export PYTHONHOME /usr/local + nsh> export PYTHON_BASIC_REPL 1 + nsh> python + Python 3.13.0 (main, Dec 4 2024, 17:00:42) [GCC 13.2.0] on nuttx + Type "help", "copyright", "credits" or "license" for more information. + >>> + +Demo +---- + +Check the following `asciinema `_ demo to see how to run Python on NuttX. You can copy and paste the commands from the demo to try it yourself. + +.. image:: https://asciinema.org/a/orkD8fKuahMEgQfBak9abliE4.svg + :target: https://asciinema.org/a/orkD8fKuahMEgQfBak9abliE4 diff --git a/Documentation/platforms/risc-v/qemu-rv/boards/rv-virt/index.rst b/Documentation/platforms/risc-v/qemu-rv/boards/rv-virt/index.rst index 8749b121eb579..90f178a169f09 100644 --- a/Documentation/platforms/risc-v/qemu-rv/boards/rv-virt/index.rst +++ b/Documentation/platforms/risc-v/qemu-rv/boards/rv-virt/index.rst @@ -358,6 +358,14 @@ Configures the NuttShell (nsh) located at examples/nsh. This NSH configuration is focused on low-level, command-line driver testing. This configuration is used for 32-bit RISC-V +python +------ + +Enables the Python interpreter for NuttX. This configuration is based on `nsh`_. + +For more information on how to build and run Python on NuttX, +please refer to the :doc:`Python Interpreter ` page. + nsh64 ----- diff --git a/boards/risc-v/qemu-rv/rv-virt/configs/python/defconfig b/boards/risc-v/qemu-rv/rv-virt/configs/python/defconfig new file mode 100644 index 0000000000000..be9fb6aae29ff --- /dev/null +++ b/boards/risc-v/qemu-rv/rv-virt/configs/python/defconfig @@ -0,0 +1,88 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_DISABLE_OS_API is not set +# CONFIG_NSH_DISABLE_LOSMART is not set +CONFIG_16550_ADDRWIDTH=0 +CONFIG_16550_UART0=y +CONFIG_16550_UART0_BASE=0x10000000 +CONFIG_16550_UART0_CLOCK=3686400 +CONFIG_16550_UART0_IRQ=37 +CONFIG_16550_UART0_SERIAL_CONSOLE=y +CONFIG_16550_UART=y +CONFIG_ARCH="risc-v" +CONFIG_ARCH_BOARD="rv-virt" +CONFIG_ARCH_BOARD_QEMU_RV_VIRT=y +CONFIG_ARCH_CHIP="qemu-rv" +CONFIG_ARCH_CHIP_QEMU_RV32=y +CONFIG_ARCH_CHIP_QEMU_RV=y +CONFIG_ARCH_CHIP_QEMU_RV_ISA_A=y +CONFIG_ARCH_CHIP_QEMU_RV_ISA_C=y +CONFIG_ARCH_CHIP_QEMU_RV_ISA_M=y +CONFIG_ARCH_FLOAT_H=y +CONFIG_ARCH_INTERRUPTSTACK=2048 +CONFIG_ARCH_RISCV=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_BOARDCTL_POWEROFF=y +CONFIG_BOARDCTL_ROMDISK=y +CONFIG_BOARD_LOOPSPERMSEC=6366 +CONFIG_BUILTIN=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_DEV_URANDOM=y +CONFIG_DEV_ZERO=y +CONFIG_ELF=y +CONFIG_EVENT_FD=y +CONFIG_EXAMPLES_HELLO=y +CONFIG_EXPERIMENTAL=y +CONFIG_FRAME_POINTER=y +CONFIG_FS_HOSTFS=y +CONFIG_FS_PROCFS=y +CONFIG_FS_ROMFS=y +CONFIG_FS_TMPFS=y +CONFIG_IDLETHREAD_STACKSIZE=4096 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INIT_STACKSIZE=4096 +CONFIG_INTERPRETER_CPYTHON=y +CONFIG_LIBC_ENVPATH=y +CONFIG_LIBC_EXECFUNCS=y +CONFIG_LIBC_LOCALE=y +CONFIG_LIBC_LOCALE_GETTEXT=y +CONFIG_LIBC_PERROR_STDOUT=y +CONFIG_LIBC_STRERROR=y +CONFIG_LIBM_NEWLIB=y +CONFIG_LIB_SYSCALL=y +CONFIG_LIB_ZLIB=y +CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=300 +CONFIG_NSH_READLINE=y +CONFIG_NSH_VARS=y +CONFIG_PATH_INITIAL="/system/bin" +CONFIG_PIPES=y +CONFIG_PSEUDOFS_SOFTLINKS=y +CONFIG_RAM_SIZE=33554432 +CONFIG_RAM_START=0x80000000 +CONFIG_READLINE_CMD_HISTORY=y +CONFIG_RISCV_SEMIHOSTING_HOSTFS=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_SERIAL_UART_ARCH_MMIO=y +CONFIG_STACK_COLORATION=y +CONFIG_START_MONTH=12 +CONFIG_START_YEAR=2021 +CONFIG_SYMTAB_ORDEREDBYNAME=y +CONFIG_SYSTEM_NSH=y +CONFIG_TESTING_GETPRIME=y +CONFIG_TESTING_OSTEST=y +CONFIG_TIMER_FD=y +CONFIG_TLS_NELEM=10 +CONFIG_TLS_TASK_NELEM=10 +CONFIG_USEC_PER_TICK=1000 diff --git a/tools/ci/testlist/risc-v-06.dat b/tools/ci/testlist/risc-v-06.dat index 21b3406b5b519..1eb0ffe2c1e56 100644 --- a/tools/ci/testlist/risc-v-06.dat +++ b/tools/ci/testlist/risc-v-06.dat @@ -1,6 +1,7 @@ /risc-v/qemu-rv/rv-virt/configs/[d-z]* /risc-v/q[f-z]* /risc-v/[r-z]* +-rv-virt:python # Boards build by CMake CMake,rv-virt:smp diff --git a/tools/ci/testlist/risc-v-07.dat b/tools/ci/testlist/risc-v-07.dat new file mode 100644 index 0000000000000..eabd6683bcc59 --- /dev/null +++ b/tools/ci/testlist/risc-v-07.dat @@ -0,0 +1 @@ +/risc-v/qemu-rv/rv-virt/configs/python