Apache NuttX is a Real-Time Operating System (RTOS) that runs on many kinds of devices, from 8-bit to 64-bit. (Think Linux, but a lot smaller and simpler)
In this tutorial we’ll boot NuttX on a 64-bit RISC-V Single Board Computer (SBC): Pine64 Star64, based on the StarFive JH7110 SoC.
(Works also for StarFive VisionFive 2 SBC)
If we're...
- Seeking a simpler, lighter Operating System for our RISC-V SBC (Alternative to Linux and Unix)
- Or just keen to learn how a RISC-V SBC boots from scratch
Then read on!
Before we begin, let's connect a USB Serial Adapter to Star64. We'll use the Pine64 Woodpecker Serial Adapter. (Any CH340 or similar adapter should work)
According to Star64 Schematic (Page 18), UART0 TX and RX (GPIO 5 and 6) are connected to the GPIO Header (Pins 8 and 10). Thus we connect these pins (pic below)...
- Pin 6 (GND) to USB Serial GND (Brown)
- Pin 8 (TX) to USB Serial RX (Red)
- Pin 10 (RX) to USB Serial TX (Orange)
On our USB Serial Adapter, set the Voltage Jumper to 3V3. (Instead of 5V)
Verify that the DIP Switches for GPIO 0 and 1 are set to Low and Low. (Default setting, pic below)
So Star64 should start OpenSBI and U-Boot Bootloader from Internal Flash Memory.
(DIP Switch Labels are inverted: "ON KE" actually means "Low")
Now we prepare the microSD Card that will boot NuttX on our SBC...
- Download the microSD Image sdcard.img (From StarFive VisionFive2 Software Releases)
- Write the downloaded image to a microSD Card with Balena Etcher or GNOME Disks or
dd
- Download the NuttX Flat Image Tree starfiveu.fit and overwrite the file on the microSD Card (How to build NuttX)
Let's boot NuttX on our SBC...
On our computer, connect to the USB Serial Port at 115.2 kbps...
$ screen /dev/ttyUSB0 115200
Insert the microSD Card into Star64 and power up Star64.
NuttX boots on Star64 and NuttShell (nsh) appears in the Serial Console. (Pic above)
NuttX works like a tiny version of Linux, so the commands will look familiar…
NuttShell (NSH) NuttX-12.2.1-RC0
nsh> uname -a
NuttX 12.2.1-RC0 8605714 Aug 7 2023 16:20:45 risc-v star64
nsh> ls -l
/:
dr--r--r-- 0 dev/
dr--r--r-- 0 proc/
dr--r--r-- 0 system/
To see the available commands...
$ help
Booting NuttX over TFTP is also supported on Star64.
To run the "Hello World" demo in NuttX...
$ hello
Hello, World!!
NuttX Apps look very similar to Linux and Unix Apps (because NuttX supports POSIX)...
#include <nuttx/config.h>
#include <stdio.h>
int main(int argc, FAR char *argv[])
{
printf("Hello, World!!\n");
return 0;
}
NuttX Startup ExplainedStep by step, here's everything that happens when NuttX boots on our SBC...
- OpenSBI (Supervisor Binary Interface) is the first thing that boots on our RISC-V SBC. OpenSBI provides Secure Access to the Low-Level System Functions (controlling CPUs, Timers, Interrupts) for the JH7110 SoC. OpenSBI boots in RISC-V Machine Mode, the most powerful mode in a RISC-V system.
- U-Boot Bootloader starts after OpenSBI, in RISC-V Supervisor Mode. (Which is less powerful than Machine Mode)
- U-Boot Bootloader loads into RAM the NuttX Kernel, Device Tree and Initial RAM Disk. Inside the Initial RAM Disk: NuttX Shell and NuttX Apps.
- NuttX Kernel starts in RISC-V Supervisor Mode and executes the NuttX Boot Code (in RISC-V Assembly).
- NuttX Start Code (in C) runs next. It prepares the RISC-V Memory Management Unit, to protect the Kernel Memory and I/O Memory.
- NuttX Kernel starts the NuttX Drivers and mounts the Initial RAM Disk (containing the NuttX Shell and Apps)
- Followed by the NuttX Shell (NSH), for the Command-Line Interface
Finally we talk about the NuttX Shell and Apps...
- NuttX Shell (NSH) and NuttX Apps (like "hello") will run in RISC-V User Mode. (Which is the least powerful mode)
- They will make System Calls to NuttX Kernel, jumping from User Mode to Supervisor Mode. (And back)
- System Calls will happen when NuttX Shell and Apps do Console Output and Console Input.
- Which will trigger RISC-V Interrupts in the 16550 UART Controller.
We welcome your contribution to Apache NuttX RTOS!
There's plenty to be done for NuttX on Star64 JH7110, check out our NuttX Roadmap here.
To learn more about NuttX on Star64, check out the articles here...
Comments