Getting Started with RT-Thread

On this page

Getting Started with RT-Thread

Setting Up Development Environment For RT-Thread in a Linux Machine

Background

My project recently got accepted to 2023 IoT contest by RT-Thread and I am so excited to get started. I new beginner to RT-Thread and I am going to document my journey here. I will be using a Linux machine for my development. Our project, Smart Beehive aims to improve way of beekeeping in Kenya by leveraging IoT technologies. We will be using CH32V208W-R0 Development board that will be shipped to us by RT-Thread.

Introduction to RT-Thread

RT-Thread is Free and Open source, Embedded real-time operating RTOS licensed under Apache License 2.0 (From version 3.1.0). It supports plethora of architectures and Microcontrollers. It’s also designed for resource-constrained devices, the minimum kernel requires only 1.2KB of RAM and 3 KB of Flash

The architectures include:

  • ARM Cortex-M0/M0+/M3/M4/M7/M23/M33/R4/A8/A9
  • ARM7/9/11
  • RISC-V

The Microcontrollers it can support can be found here: Supported MCU. It is also Feature rich, More information can be found here

RT-Thread Architecture

RT-Thread is rich in components and it’s architecture is shown below:

RT-Thread Architecture

The Layers includes:

  • Kernel layer - This is the core of RT-Thread, it impliments objects such as multi-threading and its scheduling, semaphore, mailbox, message queue, memory management, timer,etc.
  • Components and Service layer - They are based on upper level of kernel layer, and provide a lot of components and services, such as device virtual file system, device frameworks, Network frameworks, etc.
  • RT-Thread Software package - A general-purpose software component running on the RT-Thread IoT operating system platform for different application areas, consisting of description information, source code or library files.

Prerequisites

Although RT-Thread Studio which is one stop development tool is available, Linux users can’t take advantage of it since it’s only available for Windows. I will be setting up environment such way we can use any other IDE like VS code to write and compile the code in Linux.

Install Toolchain Dependencies

Install the following dependencies in your Debian/Ubuntu machine:

1sudo apt-get install build-essential git  python3 python3-pip scons qemu qemu-system-arm scons libncurses5-dev wget curl

Installing ARM toolchain

I made a bash script to simplify the process of installing ARM and RISC-V toolchain. You can run it by running the following command on your terminal:

1curl  https://raw.githubusercontent.com/k-kipruto/Smart_Beehive/main/tools/gcc-arm.sh | bash

Once ARM toolchain is installed using the script, you can test if the toolchain is working by running the following command:

1arm-none-eabi-gcc --version

If you get any error, open an issue on this repository https://github.com/k-kipruto/Smart_Beehive

Installing RISC-V toolchain

1curl https://raw.githubusercontent.com/k-kipruto/Smart_Beehive/main/tools/gcc-riscv.sh | bash

Once RISC-V toolchain is installed using the script, you can test if the toolchain is working by running the following command:

1riscv-none-embed-gcc --print-multi-lib

Creating New Project in VS Code

To add new project in VS Code, You go to files open folder and then create a new folder for your project

It’s always a good practice to initialize a git repository for your project. You can do that by running the following commands:

1echo "# YOUR-REPOSITORY-NAME" >> README.md
2git init
3git add README.md
4git commit -m "first commit"
5git branch -M main
6git remote add origin git@github.com:YOUR-GITHUB-USERNAME/YOUR-REPOSITORY-NAME.git
7git push -u origin main

Adding RT-Thread to your project

To add RT-Thread to your project, you need to add the RT-Thread repository as git submodule to your project folder. You can do that by running the following command:

1git submodule add -b  master https://github.com/RT-Thread/rt-thread.git

RT-Thread git submodule will look like this:

RT-Thread Submodule

In RT-Thread they have the following folders:

NameDescription
BSPBoard Support Package based on the porting of various development boards
componentsComponents, such as finsh shell, file system, protocol stack etc.
documentationRelated documents, like coding style, doxygen etc.
examplesRelated sample code
includeHead files of RT-Thread kernel
libcpuCPU porting code such as ARM/MIPS/RISC-V etc.
srcThe source files for the RT-Thread kernel.
toolsThe script files for the RT-Thread command build tool.

RT-Thread Folders

Start RT-Thread Qemu Simulation Project

Under BSP folder, there are various boards that are supported by RT-Thread. We will be using qemu-vexpress-a9 for our project. You can find the board under bsp/qemu-vexpress-a9 folder.

To copy to our project, we will use the following command:

1cp -r  rt-thread/bsp/qemu-vexpress-a9/ ./

This will copy the board to our project root directory.

After that we need to install ENV and Configure BSP.

1cd qemu-vexpress-a9
2export RTT_ROOT=../rt-thread
3export RTT_EXEC_PATH=/opt/gcc-arm-none-eabi/bin/
4source ~/.env/env.sh 
5scons --menuconfig
  1. First command will change directory to the board folder
  2. Second command will export RTT_ROOT variable which is the path to RT-Thread root directory
  3. Third command will export RTT_EXEC_PATH variable which is the path to ARM toolchain
  4. Fourth command will source the env.sh file which will set the environment variables so that we can update the packages with pkgs –update command
  5. Fifth command will open the menuconfig where we can configure the BSP

When you run scons –menuconfig command, you will get the following output where you will configure your RT-Thread project. Use arrow keys to navigate the menu. After you are done save your changes and press ESC key twice to exit the menuconfig.

RT-Thread Menuconfig

You can now run the pkgs –update command to update the packages.

1pkgs --update

RT-Thread Packages Update

The setup is now complete and you can now compile the project by running the following command:

1scons

RT-Thread Compilation

This will build the project and generate the elf file which you can run on Qemu.

Running the project on Qemu

To run the project on Qemu, you need to run the following command:

1./qemu.sh

RT-Thread Qemu