Skip to content

Developer Guide

Introduction

This guide walks you through the development of your first application for RVX. You will set up a development environment, create a simple Hello World application in C, and build it using the RVX Software Development Kit (SDK).

By the end of this guide, you will understand the basic RVX application development workflow and have a project that you can use as a starting point for your own applications.

Prepare the Development Environment

To build applications for RVX, you need the RISC-V GNU Toolchain, which provides the compiler, linker, and other tools required to generate software for RISC-V systems. You also need CMake to manage the build process.

There are two ways to set up your development environment:

  • Using Docker (recommended): Use the prebuilt RVX Docker image, which includes the toolchain, CMake, and all the required build dependencies.
  • Building from source: Build the toolchain from source and install the required build tools on your machine.

Choose the option that best suits your workflow. Both options require Git on your host machine to clone the RVX SDK.

Use the Docker Image

The RVX project provides an Ubuntu-based Docker image with the development tools already installed and configured. This is the easiest way to get started, as it eliminates the need to build the toolchain from source and provides a consistent development environment across different machines.

Install Docker by following the instructions on the Docker website. Once installed, check that Docker is available by running:

docker --version

You will start the development container in Build the Application, after creating the project.

Build the Toolchain from Source

If you prefer to work directly on your machine, follow the build instructions in the RISC-V GNU Toolchain repository. When configuring the toolchain:

  • Use --with-arch=rv32i and --with-abi=ilp32 to match RVX's 32-bit base integer instruction set and application binary interface (ABI).
  • Build the bare-metal toolchain with the Newlib C library.
  • Add the toolchain's bin directory to your PATH.

To verify the installation, run:

riscv32-unknown-elf-gcc --version

If the installation was successful, this command will display the compiler version.

Make sure you also have CMake 3.28 or later and a build tool such as Make or Ninja installed.

Create the Project

Now that you have prepared the development environment, let's create a simple application that sends a Hello World message through RVX's UART (Universal Asynchronous Receiver/Transmitter) interface.

We will use the RVX SDK, which provides the peripheral drivers, startup code, linker script, and build configuration needed to develop applications for RVX. The SDK takes care of the low-level details so that you can focus on writing your application.

First, open a terminal on your host machine and run the following commands to create a project directory named rvx_hello_world and navigate into it:

mkdir rvx_hello_world
cd rvx_hello_world

Next, clone the RVX SDK into the project directory:

git clone https://github.com/rafaelcalcada/rvx-sdk.git

Create a file named rvx_hello_world.c in the project root directory and add the following code:

rvx_hello_world.c
#include "rvx.h"

// RVX configuration.
const RvxSetup rvx_setup = {
    .clock_frequency = 12000000U,
    .trap_handler = RVX_DEFAULT_TRAP_HANDLER
};

void main(void)
{
    // Initialize RVX.
    rvx_init(&rvx_setup);

    // Configure UART0 to operate at 9600 baud.
    rvx_uart_set_baud_rate(RVX_UART0, 9600U);

    // Send a message through UART0.
    rvx_uart_print(RVX_UART0, "Hello World, RVX!\n");
}

The application works as follows:

  1. rvx.h provides access to the SDK's functions and peripheral definitions.
  2. rvx_setup specifies the RVX clock frequency and the default trap handler, which handles interrupts and exceptions.
  3. rvx_init() applies these settings before the application uses any peripherals.
  4. rvx_uart_set_baud_rate() configures UART0 for serial communication at 9600 baud.
  5. rvx_uart_print() sends the message through UART0.

The example assumes that RVX operates at a clock frequency of 12 MHz. If your RVX implementation uses a different frequency, update clock_frequency accordingly. The SDK uses this value to calculate timing parameters, such as those needed to achieve the desired UART baud rate. Setting this value does not change the hardware clock itself.

Build the Application

RVX uses CMake to manage the application build process. The SDK provides a CMake toolchain file that selects the RISC-V build tools and a CMake package named RVX that supplies the compiler and linker settings, SDK code, and boot image generation function.

Together, the toolchain file and the package allow you to build an application without configuring the compiler and linker manually.

Define the Build Configuration

Create a file named CMakeLists.txt in the project root directory. This file defines how the application is built. Add the following content:

CMakeLists.txt
# Project setup

cmake_minimum_required(VERSION 3.28)
set(APP_NAME "rvx_hello_world")
project(${APP_NAME} LANGUAGES C ASM)

# RVX settings

set(RVX_RAM_SIZE_IN_BYTES 8192)
set(RVX_MIN_FREE_SPACE_IN_BYTES 1024)
set(RVX_ENABLE_ZMMUL_EXTENSION FALSE)
set(RVX_DIR "${CMAKE_CURRENT_LIST_DIR}/rvx-sdk")
find_package(RVX CONFIG REQUIRED)

# Build the executable and generate the boot images

add_executable(${APP_NAME} rvx_hello_world.c)
target_link_libraries(${APP_NAME} PRIVATE rvx_sdk)
rvx_generate_boot_image(${APP_NAME})

The RVX settings specify the target hardware, memory requirements, and SDK location:

Setting Description
RVX_RAM_SIZE_IN_BYTES Available application RAM. This example assumes 8 KiB (8192 bytes).
RVX_MIN_FREE_SPACE_IN_BYTES Minimum amount of RAM that must remain free for runtime use, such as the stack.
RVX_ENABLE_ZMMUL_EXTENSION Enables the use of hardware multiplication instructions. Enable it only if supported by your RVX implementation.
RVX_DIR Location of the RVX SDK's CMake package.

Make sure the RAM size and instruction set settings match your RVX hardware configuration.

The remaining commands create the application executable, link it with the SDK, and generate the boot images.

Your project directory should now look like this:

rvx_hello_world/
├── CMakeLists.txt
├── rvx_hello_world.c
└── rvx-sdk/

Start the Development Container (Docker Only)

If you are using Docker, run the following command from the project root directory to start a development container:

docker run -it --rm -v "$(pwd):/workspace" -w /workspace rafaelcalcada/rvx:latest

Docker starts an interactive development container, downloading the image first if it is not already available locally. Your project directory is mounted at /workspace inside the container, which is also its working directory. Any files created in this directory, including the build outputs, remain on your machine after the container is closed.

Run the remaining commands in this section inside the container if you are using Docker. Otherwise, continue in your local terminal.

Configure the Build

From the project root directory, configure the build by running:

cmake -B build --toolchain rvx-sdk/RVXToolchain.cmake

This tells CMake to use the RVX toolchain file for cross-compilation (building software for RVX rather than for your computer) and to generate the build files in a directory named build.

By default, CMake configures a Release build. To configure a Debug build instead, run:

cmake -B build --toolchain rvx-sdk/RVXToolchain.cmake -DCMAKE_BUILD_TYPE=Debug

Compile the Application

Once the build is configured, compile the application by running:

cmake --build build

If the build completes successfully, the following files will be available in the build directory:

File Description
rvx_hello_world.bin Binary boot image for programming an external SPI flash memory.
rvx_hello_world.mem Verilog memory initialization file for preloading RVX RAM in FPGA implementations or simulations.
rvx_hello_world.elf Executable in ELF format, useful for debugging and analyzing the application.
rvx_hello_world.disasm Human-readable disassembly of the compiled application.

After making changes to your application's source code, simply run cmake --build build again to rebuild it and update the generated files.

Run the Application

You have now built your first RVX application! The two boot images (.bin and .mem) support different ways of loading and running your application.

The .bin file is intended for programming an external SPI flash memory connected to RVX. At boot time, RVX loads the application from the SPI flash into RAM and executes it.

The .mem file can be used in FPGA implementations to preload RVX RAM with the application. Set the BOOT_IMAGE_PATH parameter of RVX top-level module to the path of the .mem image before generating the FPGA bitstream to embed the application in the bitstream. RVX will boot directly from RAM and execute the preloaded application as long as no SPI flash is connected.

A serial terminal connected to RVX UART and configured for 9600 baud should display the following message when RVX is powered on:

Hello World, RVX!

You can use this project as a starting point for your own applications and explore the RVX examples to learn how to work with other peripherals and features.