Skip to content

Apache NuttX RTOS for QEMU RISC-V 64-bit Kernel Mode (With LED Driver)

Compare
Choose a tag to compare
@lupyuen lupyuen released this 14 Feb 03:44
  1. Copy nuttx to $HOME/nuttx/

  2. Unzip apps-bin.zip and copy the files inside (not the folder) into $HOME/apps/bin/

    (We should see $HOME/apps/bin/qjs and blink.js)

  3. Then run

    $ cd $HOME/nuttx/
    $ qemu-system-riscv64 \
       -semihosting \
       -M virt,aclint=on \
       -cpu rv64 \
       -smp 8 \
       -bios none \
       -kernel nuttx \
       -nographic

See the NuttX Log

Build Script:

#!/usr/bin/env bash
#  Build NuttX for QEMU RISC-V

## TODO: Set PATH
export PATH="$HOME/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-apple-darwin/bin:$PATH"

set -e  #  Exit when any command fails
set -x  #  Echo commands

## Build QuickJS
pushd ../quickjs-nuttx/nuttx; ./build.sh ; popd ; exit

## Build NuttX
function build_nuttx {

  ## Go to NuttX Folder
  pushd ../nuttx

  ## Build NuttX
  make -j 8

  ## Return to previous folder
  popd
}

## Build Apps Filesystem
function build_apps {

  ## Go to NuttX Folder
  pushd ../nuttx

  ## Build Apps Filesystem
  make -j 8 export
  pushd ../apps
  ./tools/mkimport.sh -z -x ../nuttx/nuttx-export-*.tar.gz
  make -j 8 import
  popd

  ## Return to previous folder
  popd
}

## Download the WIP NuttX Source Code
git clone \
  --branch qemuled \
  https://github.com/lupyuen2/wip-pinephone-nuttx \
  nuttx
git clone \
  --branch qemuled \
  https://github.com/lupyuen2/wip-pinephone-nuttx-apps \
  apps
cd nuttx

## Pull updates
git pull && git status && hash1=`git rev-parse HEAD`
pushd ../apps
git pull && git status && hash2=`git rev-parse HEAD`
popd
echo NuttX Source: https://github.com/apache/nuttx/tree/$hash1 >nuttx.hash
echo NuttX Apps: https://github.com/apache/nuttx-apps/tree/$hash2 >>nuttx.hash

## Show the versions of GCC and Zig
riscv64-unknown-elf-gcc -v

## Configure build
tools/configure.sh rv-virt:knsh64

## Build NuttX
build_nuttx

## Build Apps Filesystem
build_apps

## Generate Initial RAM Disk
genromfs -f initrd -d ../apps/bin -V "NuttXBootVol"

## Show the size
riscv64-unknown-elf-size nuttx

## Export the Binary Image to nuttx.bin
riscv64-unknown-elf-objcopy \
  -O binary \
  nuttx \
  nuttx.bin

## Copy the config
cp .config nuttx.config

## Dump the disassembly to nuttx.S
riscv64-unknown-elf-objdump \
  --syms --source --reloc --demangle --line-numbers --wide \
  --debugging \
  nuttx \
  >nuttx.S \
  2>&1

## Dump the init disassembly to init.S
riscv64-unknown-elf-objdump \
  --syms --source --reloc --demangle --line-numbers --wide \
  --debugging \
  ../apps/bin/init \
  >init.S \
  2>&1

## Dump the hello disassembly to hello.S
riscv64-unknown-elf-objdump \
  --syms --source --reloc --demangle --line-numbers --wide \
  --debugging \
  ../apps/bin/hello \
  >hello.S \
  2>&1

## Start the emulator
  qemu-system-riscv64 \
    -semihosting \
    -M virt,aclint=on \
    -cpu rv64 \
    -smp 8 \
    -bios none \
    -kernel nuttx \
    -nographic