Getting Started with a New RISC-V MPU Platform: RT-Thread Hands-on Milk-V Duo Guide

RT-Thread IoT OS
9 min readAug 21, 2024

--

I Introduction
II References
III Environment Setup
3.1 Preparing Tools
3.2 Introduction to the Development Process
3.2.1 Build Linux Environment
3.2.2 Download Toolchain
3.2.3 Hard Disc Partitioning Tools
3.2.4 Code Editing Tools
3.2.5 Terminal Tool
IV Project Compilation
4.1 Compiling the Milk-V Duo 256M Core
4.2 Compile Milk-V Duo 256M small Core
V Run Firmware
5.1 Boot the System
5.2 Light Up Onboard LEDs
VI Summary

The Milk-V Duo is an MPU developed by RT-Thread in collaboration with MilkV, based on the CVITEK CV1800B chip, capable of running both Linux and RTOS. It is currently priced at $16 with free shipping. Prices may vary between countries adjusted by the AliExpress platform since RT-Thread joined the Aliexpress Choice Program. Order the board here: https://www.aliexpress.com/item/3256807395490463.html?gatewayAdapt=4itemAdapt

The Milk-V Duo offers a reliable, cost-effective, and powerful platform specifically designed for professionals, industrial OEMs, IoT enthusiasts, tech hobbyists, and DIY makers.

The Milk-V Duo is a highly compact embedded development platform built around the CV1800B chip, capable of running both Linux and RTOS. The Milk-V Duo 256M is an enhanced version, featuring 256MB of RAM, designed for applications that demand more memory. Powered by the SG2002 compute chipset, with 1.0TOPS@INT8 performance, it facilitates smooth transitions between RISC-V and ARM architectures and supports dual-system concurrent operation. Additionally, it boasts a rich array of GPIO interfaces such as SPI, UART, making it ideal for various edge intelligent surveillance hardware developments, including IP cameras, smart cat-eye locks, and visual doorbells.

RT-Thread’s mainline BSP fully supports both the Milk-V Duo and the Milk-V Duo 256M. With RT-Thread as the default operating system and a comprehensive set of peripheral drivers, getting started is significantly simplified, allowing users to develop MPU applications directly. This makes the platform an excellent choice for learning about RISC-V, MPUs, and RTOS.

@flyingcys(https://github.com/flyingcys) gives a detailed introduction of Milk-V Duo in his RISC-V RT-Thread Programming Guide — Getting Started(https://riscv-rtthread-programming-manual.readthedocs.io/zh-cn/latest/zh_CN/6.html#), and some of the content below is quoted from this document.

II References

RT-Thread Milk-V BSP(https://github.com/Yaochenger/rt-thread/tree/master/bsp/cvitek

RISC-V RT-Thread Programming Guide(https://riscv-rtthread-programming-manual.readthedocs.io/zh-cn/latest/index.html

Milk-V Introduction Documents(https://milkv.io/zh/docs/duo/getting-started/duo

Milk-V Duo Github Code Repository(https://github.com/milkv-duo

III Environment Setup

3.1 Preparing Tools

The following tools are required to prepare for development based on RT-Thread on Milk-V Duo or Milk-V Duo 256M, and will be used in the Environment Setup section;

● Virtual machine: VMware Workstation Player Download address(http://%20https//customerconnect.vmware.com/en/downloads/details

● Ubuntu image: Ubuntu 22.04 download address(http://%20https//releases.ubuntu.com/22.04.4/ubuntu-22.04.4-desktop-amd64.iso

● MobaXterm: MobaXterm v23.06 download download address(https://download.mobatek.net/2362023122033030/MobaXterm_Installer_v23.6.zip

● RT-Thread source code: RT-Thread Github download address(https://github.com/RT-Thread/rt-thread

● DiskGenius: DiskGenius hard disc partitioning tool download address(https://www.diskgenius.cn/download.php

3.2 Introduction to the Development Process

The community developer @flyingcys(https://github.com/flyingcys) has given the construction method in his RISC-V RT-Thread Programming Guide — Getting Started(https://riscv-rtthread-programming-manual.readthedocs.io/zh-cn/latest/zh_CN/6.html#). Please refer to the Getting Started section of the document for the specific construction steps, and the following section mainly introduces the development flow of using RT-Thread on Milk-V:

3.2.1 Setting Up a Linux Development Environment

Both the Milk-V Duo and Milk-V Duo 256M are MPUs, and the standard approach for developing on these platforms is to set up a cross-compilation environment under Linux. It’s recommended to use a virtual machine for this, with Ubuntu 20.04 as the preferred Linux distribution. Start by installing a virtual machine on your Windows system and then configure a Linux development environment using an Ubuntu 20.04 image. Once this setup is complete, you’ll have a basic Linux environment ready for development.

In the RISC-V RT-Thread Programming Guide — Quick Start (https://riscv-rtthread-programming-manual.readthedocs.io/zh-cn/latest/zh_CN/6.html#), we introduced several essential tools for development within an Ubuntu environment. These tools will be used frequently throughout the development process. To get started, users need to install these tools on their Ubuntu system using the following commands:

Once these tools are installed it is possible to download the RT-Thread source code to our local area under Ubuntu using the Git tool, download command:

3.2.2 Downloading the Toolchain

After setting up the Linux environment, the next crucial step is to install the toolchain. Before proceeding with the installation, let’s briefly introduce the cores of the Milk-V Duo and Milk-V Duo 256M.

Both the Milk-V Duo and Milk-V Duo 256M feature two XuanTie C906 RISC-V cores from T-Head. One core includes an MMU, while the other is a smaller core without an MMU. These two cores operate in a multi-core heterogeneous (AMP) configuration, allowing users to run different operating systems on each core. The larger core can run systems that support an MMU, such as Linux or RT-Smart, but it can also run the standard version of RT-Thread. The smaller core, lacking MMU support, can only run systems that do not require an MMU. To simplify the initial setup, both cores are configured to run the standard version of RT-Thread by default, though users can reconfigure the larger core to run RT-Smart using the menuconfig tool.

Given this setup, we need to use the GCC toolchain provided by T-Head to compile the BSPs for the Milk-V Duo and Milk-V Duo 256M. Since both cores are running RT-Thread Standard, you only need to install the newlib version of libc’s toolchain for the Ubuntu environment.

3.2.3 Disk Partitioning Tools

After completing the previous steps, the next task is to install the DiskGenius tool in your Windows environment. This is essential because the Milk-V Duo and Milk-V Duo 256M support booting from an SD card (we recommend using an SD card with at least 4GB of storage). Once the code is compiled, you can copy it to the SD card, and the system will boot from there. To prepare the SD card, you’ll need to use a disk partitioning tool to divide the card into two partitions. The first partition will store the compiled firmware, while the second partition is reserved for storing project-related data.

3.2.4 Code Editing Tools

VS Code is a powerful editor that we’ll use to edit project code. By establishing an SSH connection with the virtual machine, you can edit the project code on Ubuntu from your Windows environment using VS Code. Additionally, you’ll need to install Samba on Ubuntu to share files between Ubuntu and Windows. This setup allows you to directly copy the firmware compiled on Ubuntu to your Windows system. The firmware consists of two main files: fip.bin (for the small core) and boot.sd (for the big core). If you're only using the small core, copy fip.bin to the SD card. For dual-core usage, copy both files to the first partition of the SD card.

3.2.5 Terminal Tool

The final tool you need to install is MobaXterm. RT-Thread supports Shell via a serial port, and you can use a USB-to-TTL adapter to connect the Milk-V board to your host computer. With MobaXterm, you can configure the necessary parameters and interact with the Milk-V using command-line instructions.

With these setups, the environment configuration is complete.

IV. Project Compilation

Once you’ve installed the SSH plugin in VS Code, you can connect to Ubuntu via SSH and edit code within the Ubuntu environment from Windows. Community developer @flyingcys provides a detailed guide on project compilation in the RISC-V RT-Thread Programming Guide — Project Compilation (https://riscv-rtthread-programming-manual.readthedocs.io/zh-cn/latest/zh_CN/6.html#id6), so we won’t delve into the specifics here. Instead, we’ll focus on troubleshooting common issues encountered during the compilation process.

Below is a brief overview of the Cvitek BSP file structure:

4.1 Compiling the Milk-V Duo 256M Large Core

Navigate to the bsp/cvitek/cv18xx_risc-v directory, open a terminal, and run the command scons --menuconfig. In the configuration interface, select the board model as milkv-duo256m, save the configuration, and exit the menuconfig interface. Next, within the same directory, execute scons -j4 to compile the Milk-V Duo 256M large-core firmware. Once the compilation is complete, the boot.sd file will be generated in the milkv-duo256m folder located under the output directory.

4.2 Compiling the Milk-V Duo 256M Small Core

Navigate to the bsp/cvitek/c906_little directory, open a terminal, and run scons --menuconfig. Again, select the board model as milkv-duo256m, save the configuration, and exit the menuconfig interface. Then, in the same directory, execute scons -j4 to compile the Milk-V Duo 256M small-core firmware. Upon completion, the fip.bin file will be generated in the milkv-duo256m folder under the output directory.

Final Steps

After completing the above steps, you’ll have the Milk-V Duo 256M firmware files — boot.sd and fip.bin—in the bsp/cvitek/output/milkv-duo256m directory. If you're using the Milk-V Duo instead of the 256M version, simply select milkv-duo in the menuconfig step when choosing the board type; the rest of the process remains the same.

Troubleshooting Command Not Found Issues During Compilation

If you encounter issues where certain commands are not found during compilation, refer to the following troubleshooting steps…

The issue mentioned above occurs because the bootloader required for the project fails to download properly. The BSP’s README file offers a solution for this problem:

If your project compiles correctly after switching boards but fails to package, navigate to the cvi_bootloader directory (which was downloaded automatically) and manually run git pull to update it. Alternatively, you can delete the directory and allow it to be downloaded again automatically.

V. Running the Firmware

5.1 Booting the System

  1. Copy the firmware you compiled in the previous steps to the first partition of the SD card.
  2. Use USB-to-TTL adapters to connect the serial ports of both the large and small cores of the Milk-V Duo 256M to your host computer’s USB ports. Set the baud rate of the serial ports to 115200 and open them using MobaXterm.

3. After completing the steps above, power the board using a USB connection (avoid powering the board directly via USB-to-TTL!). You should then see the Milk-V Duo 256M RT-Thread startup information displayed in the MobaXterm terminal.

5.2 Lighting Up the Onboard LEDs

The Milk-V Duo 256M BSP for RT-Thread includes most of the driver adaptations for the board and supports RT-Thread’s device driver framework. By default, the BSP enables RT-Thread’s PIN device, allowing you to control the PIN device through terminal commands. The onboard LED of the Milk-V Duo 256M is connected to PWR_GPIO[2], which is recognized in RT-Thread as device number E2. You can light up the onboard LED by executing the following command in the terminal.

At this stage, both the large core and small core of the Milk-V Duo 256M have successfully booted and are running RT-Thread. The setup process for the Milk-V Duo is identical to that of the Milk-V Duo 256M; however, it’s important to note that the LED pin configurations differ between the two boards. When experimenting with lighting up the LEDs on the Milk-V Duo, you’ll need to change the pin to C24.

VI. Summary

Both the Milk-V Duo and Milk-V Duo 256M are MPUs. Users familiar with MCUs may initially find it challenging to transition to an MPU. However, with the Milk-V Duo and Milk-V Duo 256 adapted for RT-Thread, users can leverage RT-Thread on MPUs just as they do on MCUs, experiencing the performance of both MPU and RISC-V architectures. Additionally, this setup facilitates the learning of RTOS concepts.

If you’re looking to order the Milk-V Duo 256M, check out RT-Thread Aliexpress Shop here: https://aliexpress.com/item/3256807232852836.html?gatewayAdapt=4itemAdapt

--

--

RT-Thread IoT OS

An Open-Source Community-Powered Real-Time Operating System (RTOS) Project! Let’s develop, DIY, create, share, and explore this new IoT World together!