Skip to content

Instantly share code, notes, and snippets.

@lupyuen
Last active April 9, 2025 09:07
Show Gist options
  • Save lupyuen/31db63ccfaf0e07c45ba0059814e0fd7 to your computer and use it in GitHub Desktop.
Save lupyuen/31db63ccfaf0e07c45ba0059814e0fd7 to your computer and use it in GitHub Desktop.
Building and Running Python on NuttX
## Building and Running Python on NuttX:
## Based on https://nuttx.apache.org/docs/latest/applications/interpreters/python/index.html#building-and-running-python-on-nuttx
## Assume we have compiled rv-virt:python in Docker: https://gist.github.com/lupyuen/21167fe02656b89632d3cce54aa23024
## Copy the NuttX Image from Docker into Ubuntu
sudo docker cp c23ff7fbe548:/root/nuttx/nuttx .
riscv-none-elf-size nuttx
## Run NuttX Python with with QEMU
ls -l nuttx
dd if=/dev/zero of=./mydisk-1gb.img bs=1M count=1024
qemu-system-riscv32 -semihosting -M virt,aclint=on -cpu rv32 -smp 8 \
-global virtio-mmio.force-legacy=false \
-device virtio-serial-device,bus=virtio-mmio-bus.0 \
-chardev socket,telnet=on,host=127.0.0.1,port=3450,server=on,wait=off,id=foo \
-device virtconsole,chardev=foo \
-device virtio-rng-device,bus=virtio-mmio-bus.1 \
-netdev user,id=u1,hostfwd=tcp:127.0.0.1:10023-10.0.2.15:23,hostfwd=tcp:127.0.0.1:15001-10.0.2.15:5001 \
-device virtio-net-device,netdev=u1,bus=virtio-mmio-bus.2 \
-drive file=./mydisk-1gb.img,if=none,format=raw,id=hd \
-device virtio-blk-device,bus=virtio-mmio-bus.3,drive=hd \
-bios none -kernel ./nuttx -nographic
luppy@thinkstation:/tmp$ sudo docker cp c23ff7fbe548:/root/nuttx/nuttx .
[sudo] password for luppy:
Successfully copied 37.6MB to /tmp/.
luppy@thinkstation:/tmp$ ls -l nuttx
-rwxr-xr-x 1 root root 37638156 Apr 9 16:01 nuttx
luppy@thinkstation:/tmp$ riscv-none-elf-size nuttx
text data bss dec hex filename
13908642 286639 66512 14261793 d99e21 nuttx
luppy@thinkstation:/tmp$ dd if=/dev/zero of=./mydisk-1gb.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.988712 s, 1.1 GB/s
luppy@thinkstation:/tmp$ qemu-system-riscv32 --version
QEMU emulator version 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.6)
Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers
luppy@thinkstation:/tmp$ qemu-system-riscv32 -semihosting -M virt,aclint=on -cpu rv32 -smp 8 \
-global virtio-mmio.force-legacy=false \
-device virtio-serial-device,bus=virtio-mmio-bus.0 \
-chardev socket,telnet=on,host=127.0.0.1,port=3450,server=on,wait=off,id=foo \
-device virtconsole,chardev=foo \
-device virtio-rng-device,bus=virtio-mmio-bus.1 \
-netdev user,id=u1,hostfwd=tcp:127.0.0.1:10023-10.0.2.15:23,hostfwd=tcp:127.0.0.1:15001-10.0.2.15:5001 \
-device virtio-net-device,netdev=u1,bus=virtio-mmio-bus.2 \
-drive file=./mydisk-1gb.img,if=none,format=raw,id=hd \
-device virtio-blk-device,bus=virtio-mmio-bus.3,drive=hd \
-bios none -kernel ./nuttx -nographic
ABC[ 0.102244] board_userled: LED 1 set to 0
[ 0.105429] board_userled: LED 2 set to 0
[ 0.106431] board_userled: LED 3 set to 0
telnetd [4:100]
NuttShell (NSH) NuttX-12.9.0
nsh> uname -a
NuttX 12.9.0 47cdb6a283 Apr 9 2025 08:01:23 risc-v rv-virt
nsh> free
total used free maxused maxfree nused nfree name
19288448 240456 19047992 243432 19034096 75 6 Umem
nsh> ls -l /dev
/dev:
crw-rw-rw- 0 console
crw-rw-rw- 0 null
cr--r--r-- 0 random
crw-rw-rw- 0 telnet
crw-rw-rw- 0 ttyS0
crw-rw-rw- 0 ttyV0
cr--r--r-- 0 urandom
c-w--w--w- 0 userleds
brw-rw-rw- 1073741824 virtblk0
crw-rw-rw- 0 zero
nsh> ps
PID GROUP PRI POLICY TYPE NPX STATE EVENT SIGMASK STACK USED FILLED COMMAND
0 0 0 FIFO Kthread - Ready 0000000000000000 0004032 0000956 23.7% Idle_Task
1 0 224 RR Kthread - Waiting Semaphore 0000000000000000 0001936 0000524 27.0% hpwork 0x80d43c74 0x80d43ca4
2 0 100 RR Kthread - Waiting Semaphore 0000000000000000 0001928 0000524 27.1% lpwork 0x80d43c28 0x80d43c58
3 3 100 RR Task - Running 0000000000000000 0004000 0001964 49.1% nsh_main
4 4 100 RR Task - Waiting Semaphore 0000000000020000 0001968 0001244 63.2% telnetd
nsh> python
[ 40.171743] check_and_mount_romfs: Mounting ROMFS filesystem at target=/usr/local/lib with source=/dev/ram1
Python 3.13.0 (main, Apr 9 2025, 07:59:42) [GCC 13.2.0] on nuttx
Type "help", "copyright", "credits" or "license" for more information.
>>> print(123)
123
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment