Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: Add defconfig for RISC-V QEMU and Documentation entries for Python on NuttX #15099

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Documentation/applications/interpreters/python/index.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/python/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 <https://github.com/apache/nuttx-apps/issues/2884>`_ in the `nuttx-apps <https://github.com/apache/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 <https://docs.python.org/3/glossary.html#term-bytecode>`_ (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 <https://asciinema.org/>`_ 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
Original file line number Diff line number Diff line change
Expand Up @@ -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 </applications/interpreters/python/index>` page.

nsh64
-----

Expand Down
88 changes: 88 additions & 0 deletions boards/risc-v/qemu-rv/rv-virt/configs/python/defconfig
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tools/ci/testlist/risc-v-06.dat
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions tools/ci/testlist/risc-v-07.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/risc-v/qemu-rv/rv-virt/configs/python
Loading