Skip to content

Commit

Permalink
NuttX - RV32 blink_leds (QEMU) example
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Oct 12, 2024
1 parent e393824 commit 5405e76
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 0 deletions.
140 changes: 140 additions & 0 deletions nuttx-riscv-blink/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
cmake_minimum_required(VERSION 3.14...3.30)

project(blink
VERSION 1.0
DESCRIPTION "Blink on NuttX"
LANGUAGES Swift
)

if("${CMAKE_Swift_COMPILER_VERSION}" VERSION_LESS 6.0)
message(FATAL_ERROR "Swift 6.0 or later is required")
endif()

if(POLICY CMP0169)
# allow to call FetchContent_Populate directly
cmake_policy(SET CMP0169 OLD)
endif()

option(LIST_ALL_BOARDS "List all available boards" OFF)
option(ENABLE_NUTTX_TRACE "Enable NuttX trace" OFF)

if(ENABLE_NUTTX_TRACE)
set(TRACEFLAG "--trace")
else()
set(TRACEFLAG "")
endif()

set(FETCHCONTENT_QUIET FALSE)
include(FetchContent)
FetchContent_Declare(
apps
GIT_REPOSITORY https://github.com/apache/nuttx-apps.git
GIT_TAG nuttx-12.7.0-RC0
SOURCE_DIR ${CMAKE_BINARY_DIR}/apps
FIND_PACKAGE_ARGS
)
FetchContent_GetProperties(apps)
if(NOT apps_POPULATED)
FetchContent_Populate(apps)
endif()

FetchContent_Declare(
nuttx
GIT_REPOSITORY https://github.com/apache/nuttx.git
GIT_TAG nuttx-12.7.0-RC0
SOURCE_DIR ${CMAKE_BINARY_DIR}/nuttx
FIND_PACKAGE_ARGS
)
FetchContent_GetProperties(nuttx)
if(NOT nuttx_POPULATED)
FetchContent_Populate(nuttx)
endif()


if(NOT "${nuttx_SOURCE_DIR}" STREQUAL "")
list(APPEND CMAKE_MODULE_PATH ${apps_SOURCE_DIR}/cmake)
list(APPEND CMAKE_MODULE_PATH ${nuttx_SOURCE_DIR}/cmake)
endif()

if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
set(SCRIPT_SUFFIX .bat)
else()
set(SCRIPT_SUFFIX .sh)
endif()

if(LIST_ALL_BOARDS)
execute_process(
COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR}
${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH}
${nuttx_SOURCE_DIR}/tools/configure${SCRIPT_SUFFIX} -L
RESULT_VARIABLE result
)
if(result)
message(FATAL_ERROR "Failed to run tools/configure")
endif()
else()
if(NOT DEFINED BOARD_CONFIG)
message(FATAL_ERROR "Please define configuration with BOARD_CONFIG")
else()
message(STATUS "BOARD_CONFIG: ${BOARD_CONFIG}")
endif()

# Send swift-blinky example to nuttx-apps path
file(COPY ${CMAKE_SOURCE_DIR}/leds_swift DESTINATION ${apps_SOURCE_DIR}/examples)
file(COPY ${CMAKE_SOURCE_DIR}/defconfig DESTINATION ${nuttx_SOURCE_DIR}/boards/risc-v/qemu-rv/rv-virt/configs/leds_swift)

add_custom_target(distclean
COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR}
${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH}
make distclean
COMMENT "Clean NuttX"
)

execute_process(
COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR}
${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH}
${nuttx_SOURCE_DIR}/tools/configure${SCRIPT_SUFFIX} -l ${BOARD_CONFIG}
RESULT_VARIABLE result
)
if(result)
message(FATAL_ERROR "Failed to run tools/configure")
endif()

add_custom_target(copy_swift_example
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/leds_swift ${apps_SOURCE_DIR}/examples/leds_swift
COMMENT "Copying leds_swift example to nuttx-apps"
)

add_custom_target(build_nuttx ALL
COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR}
${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH}
make ${TRACEFLAG} -j ${JOB_POOLS}
DEPENDS copy_swift_example
COMMENT "Building NuttX"
)

add_custom_command(
TARGET build_nuttx
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${nuttx_SOURCE_DIR}/nuttx ${CMAKE_BINARY_DIR}/nuttx.elf
)

add_custom_target(export_nuttx
COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR}
${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH}
make export
COMMENT "Exporting NuttX"
)

add_custom_target(extract_nuttx_export
COMMAND ${CMAKE_COMMAND} -E tar xzf ${nuttx_SOURCE_DIR}/nuttx-export-12.7.0.tar.gz
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E remove ${nuttx_SOURCE_DIR}/nuttx-export-12.7.0.tar.gz
DEPENDS export_nuttx
COMMENT "Extracting NuttX export"
)

add_custom_target(nuttx-libs
DEPENDS build_nuttx export_nuttx extract_nuttx_export
)
endif()
71 changes: 71 additions & 0 deletions nuttx-riscv-blink/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Swift 6 on NuttX RTOS using CMake

## Description

Run blink rv32-blink_leds (QEMU) example on NuttX RTOS.

> [!NOTE]
> CMake is adapted to build NuttX and NuttX-apps (Makefiles) with Swift 6.
## Requirements

- [NuttX](https://github.com/apache/nuttx) & [NuttX-apps](https://github.com/apache/nuttx-apps)
- [kconfig-frontends](https://bitbucket.org/nuttx/tools)
- [CMake](https://cmake.org/download/)
- [QEMU](https://www.qemu.org/)
- [Swift 6](https://swift.org/download/)
- [RISC-V GNU Toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain/releases)

## How to build

```bash
# list all supported boards
cmake -B build -DLIST_ALL_BOARDS=ON | less
# build configuration
cmake -B build -GNinja -DBOARD_CONFIG=rv-virt:leds_swift -DENABLE_NUTTX_TRACE=[ON|OFF]
# build
cmake --build build
# clean
cmake --build build -t distclean
# export NuttX as library
cmake --build build -t nuttx-libs
```

- **Output**
```bash
qemu-system-riscv32 \
-semihosting \
-M virt,aclint=on \
-cpu rv32 -smp 8 \
-bios none \
-kernel build/nuttx.elf -nographic
NuttShell (NSH) NuttX-12.7.0
nsh> leds_swift
leds_main: led_daemon started

led_daemon (pid# 4): Running
led_daemon: Opening /dev/userleds
led_daemon: Supported LEDs 0x7
led_daemon: LED set 0x1
board_userled: LED 1 set to 1
board_userled: LED 2 set to 0
board_userled: LED 3 set to 0
nsh> led_daemon: LED set 0x0
board_userled: LED 1 set to 0
board_userled: LED 2 set to 0
board_userled: LED 3 set to 0
led_daemon: LED set 0x1
board_userled: LED 1 set to 1
board_userled: LED 2 set to 0
board_userled: LED 3 set to 0
led_daemon: LED set 0x0
# [...] see output in QEMU
```

Quit from QEMU: `Ctrl-a x`

## References

- [Nuttx - Compiling with CMake](https://nuttx.apache.org/docs/latest/quickstart/compiling_cmake.html)
- [NuttX - C++ Example using CMake](https://nuttx.apache.org/docs/latest/guides/cpp_cmake.html)
- [NuttX - leds_rust](https://lupyuen.github.io/articles/rust6)
76 changes: 76 additions & 0 deletions nuttx-riscv-blink/defconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# 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_RV64=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_INTERRUPTSTACK=2048
CONFIG_ARCH_RISCV=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BCH=y
CONFIG_BOARDCTL_POWEROFF=y
CONFIG_BOARD_LATE_INITIALIZE=y
CONFIG_BOARD_LOOPSPERMSEC=6366
CONFIG_BUILTIN=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_FULLOPT=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_DEVICE_TREE=y
CONFIG_DEV_ZERO=y
CONFIG_ELF=y
# CONFIG_EXAMPLES_HELLO=y
CONFIG_EXAMPLES_LEDS=y
CONFIG_EXAMPLES_LEDS_SWIFT=y
CONFIG_FS_HOSTFS=y
CONFIG_FS_PROCFS=y
CONFIG_IDLETHREAD_STACKSIZE=2048
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INIT_STACKSIZE=3072
CONFIG_LIBC_ENVPATH=y
CONFIG_LIBC_EXECFUNCS=y
CONFIG_LIBC_PERROR_STDOUT=y
CONFIG_LIBC_STRERROR=y
CONFIG_LIBM=y
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_READLINE=y
CONFIG_PATH_INITIAL="/system/bin"
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_SYSTEM_NSH_STACKSIZE=3072
CONFIG_TESTING_GETPRIME=y
CONFIG_TESTING_OSTEST=y
CONFIG_USEC_PER_TICK=1000
CONFIG_USERLED=y
CONFIG_USERLED_LOWER=y
Loading

0 comments on commit 5405e76

Please sign in to comment.