资源与支持

SiFive 博客

来自 RISC-V 专家的最新洞察与深度技术解析

October 30, 2018

Getting Started with Zephyr RTOS v1.13.0 On RISC-V

Hi everyone! I'm Nathaniel Graff, a software engineer here at SiFive, and I'm excited to tell you about the most recent release of Zephyr RTOS, version 1.13.0! Zephyr RTOS is a real-time operating system hosted by The Linux Foundation, featuring support for a myriad of different platforms, architectures, and targets including SiFive's E-series CoreIP, and the HiFive 1 development board.

I'm particularly excited about this release because it incorporates recent work making the RISC-V port of Zephyr compatible with DeviceTree descriptions of SiFive platforms. DeviceTree is the standard markup language used by the Linux Kernel to describe the hardware configuration of a target device. In Zephyr, we use it to automatically configure the Zephyr kernel and device drivers to build and run on your microcontroller of choice. The end result is that it's never been easier to get a full-featured RTOS up and running on your own custom SiFive core and spend more of your own time building your end-user application!

Why Zephyr RTOS?

Zephyr RTOS has a number of great features that make it a good choice for developing embedded applications on RISC-V. First and foremost, it has a welcoming and active development community coordinated by the Linux Foundation, and enjoys major corporate backing from Intel, NXP, Nordic, Linaro, and more. The operating system itself also has a number of powerful features, including a device model, symmetric multiprocess support, and rich networking and bluetooth stacks. This is all in addition to the standard constructs you expect to see from a Real-Time Operating System like deterministic multithreading and inter-thread communication.

Zephyr supports RV32IMAC cores like the SiFive E31, as well as a number of peripherals built in to the FE310-G000 processor on the SiFive HiFive 1, including the UART, GPIO, and the Platform-Level Interrupt Controller.

Getting Started with Zephyr

Installing the Zephyr SDK

The best way to get started with Zephyr is to use the Zephyr SDK, which packages the toolchains needed for all of the build targets Zephyr supports, including RISC-V boards. For Zephyr v1.13.0, install SDK version 0.9.3, not a newer release like 0.9.4 or 0.9.5, to make sure that the qemu_riscv32 target is compatible with the version of QEMU in the SDK.

To install the Zephyr SDK, follow the instructions in Zephyr's Documentation for your platform of choice (Linux, macOS, Windows)

Getting the Zephyr Sources

Zephyr RTOS is developed on GitHub. You can get the sources easily by cloning the official repo:

  1. $ git clone https://github.com/zephyrproject-rtos/zephyr.git
  2. $ cd zephyr
  3. $ git checkout v1.13.0

Building the Hello World Application

Zephyr comes with a number of sample applications which demonstrate different features of Zephyr. The Hello World application is a good place to start to make sure you can build Zephyr applications and run them in your target environment.

Start in the Zephyr Sources you checked out earlier in this tutorial.

  1. $ source zephyr-env.sh
  2. $ cd samples/hello_world
  3. $ mkdir build && cd build
  4. $ cmake -DBOARD=qemu_riscv32 ..
  5. $ make -j$(nproc)

If your build environment is configured properly, the resulting application should be located in zephyr/zephyr.elf.

Running Zephyr in QEMU

To run the application we just built in the QEMU emulator, all we have to do is call $ make run. The Zephyr SDK comes with QEMU for all of its supported targets.

Running Zephyr on the SiFive HiFive 1

To run the Hello World application on the SiFive HiFive 1, we'll need to rebuild the application to target the HiFive 1 board.

Rebuilding an Application for a New Target

If you built samples/hello_world for qemu_riscv32, we'll need to clean the build directory to make sure that Zephyr is properly configured for a new target. Run the following command to clean your build directory

  • $ make pristine

You can then retarget and build the application for HiFive1:

  1. $ cmake -DBOARD=hifive1 ..
  2. $ make -j$(nproc)

If you skipped building for QEMU

If you didn't build your application for QEMU simulation, we'll still need to create a build directory and source the proper environment variables. You can build for HiFive1 by running these commands:

  1. $ source zephyr-env.sh
  2. $ cd samples/hello_world
  3. $ mkdir build && cd build
  4. $ cmake -DBOARD=hifive1 ..
  5. $ make -j$(nproc)

Programming the HiFive 1 Board

We'll need some more tools to connect to the HiFive 1 board and flash the Hello World application, namely GDB and OpenOCD. You can download those tools from our website for your desired platform.

Once you've installed the GNU embedded toolchain and OpenOCD, grab a copy of the OpenOCD configuration file for the HiFive1.

Plug in the HiFive 1. Then open a terminal and run the following command:

  • $ openocd -f openocd.cfg

In a second terminal, navigate to samples/hello_world/build/zephyr/ Then run this command to load the program onto the HiFive 1:

  • $ riscv64-unknown-elf-gdb zephyr.elf --batch -ex 'set remotetimeout 240' -ex 'target extended-remote localhost:3333' -ex 'monitor reset halt' -ex 'monitor flash protect 0 64 last off' -ex 'load' -ex 'monitor resume' -ex 'monitor shutdown' -ex 'quit'

Using your favorite serial terminal monitor (GNU Screen, PuTTY, etc.) connect to the virtual serial port presented by the HiFive 1 at 115200 baud. You should see a zephyr boot message and the phrase "Hello, world!"

Debugging Zephyr on the HiFive 1 from Freedom Studio

Zephyr also supports debug flows within your favorite Eclipse-based Integrated Development Environments, like SiFive Freedom Studio!

Detailed instructions for setting up your Eclipse environment for Zephyr can be found in Zephyr's Documentation.

Screenshot

Building Your Own Zephyr Application

Now that we've built and run a Zephyr application, it's time to get started building your own! Zephyr has thorough documentation on the process to set up and start developing your own application here.

Porting Zephyr to Your SiFive SoC

As I mentioned before, Zephyr v1.13.0 features DeviceTree support for SiFive's products. For example, using our Core Designer, you can configure your core to have your desired quantity of GPIO channels. Zephyr's SiFive GPIO driver automatically scales to the number of GPIO channels specified by the DeviceTree model of your chip. The result is that porting Zephyr to a new SiFive SoC is as simple as dropping in the DeviceTree for your chip and setting Zephyr's configuration options to your choosing.

To make the process of creating a port for a new SiFive chip even easier, I've created a template project called zephyr-sifive-freedom-template. Using the template, all you have to do is provide your DeviceTree, run the customization script, and answer a few questions.

Read more Insights from the RISC-V Experts

P570 Gen 3:系统视角
最新文章
P570 Gen 3:系统视角
然而,CPU 的需求横跨性能、功耗和成本等多个维度。在某些细分市场中,需要在不同的功耗与成本约束下实现性能提升。基于这类 CPU 的系统需要可信赖的产品路线图,才能切实交付新的系统能力。尽管部分供应商已退出“低端市场”,SiFive 仍坚持在整条性能曲线上持续创新。本次发布的 P570 Gen 3 Performance IP,旨在为中低端、具备 Linux 能力的系统提供显著的性价比与能效比提升。
SiFive Performance™ P570 Gen 3 深度解析:面向下一代消费级与商用应用的高性能能效设计
最新文章
SiFive Performance™ P570 Gen 3 深度解析:面向下一代消费级与商用应用的高性能能效设计
SiFive 的核心是 RISC-V,这是 SiFive 创始人在公司成立 5 年前发明的指令集架构 (ISA)。SiFive 正持续演进基于 RISC-V 的 IP 基础模块,重新定义并推动各类计算平台的普及化发展。在技术领域,演进并非一串随机变化的时间线,而是一系列精心规划、环环相扣的里程碑。每一步演进都会创造一系列新的环境条件,从而推动下一次更复杂的跨越成为必然。要赢得这场竞赛,关键在于具备适应变化的灵活性与持续创新能力,而这两点正是 SiFive 与 RISC-V 的核心价值观所在。
全力投入:开启增长新篇章
最新文章
全力投入:开启增长新篇章
我们自信地宣布公司发展历程中最重要的里程碑之一:完成 4 亿美元 的融资。本轮融资由 Atreides Management 领投,其他顶级投资机构\*包括 Apollo Global Management、NVIDIA(英伟达)、Point72 Turion 和 T. Rowe Price Investment Management, Inc.,以及现有投资者 Prosperity7 Ventures 和 Sutter Hill Ventures 参投。此次融资使公司估值达到 36.5 亿美元,并将加速 SiFive 的 RISC-V CPU 及 AI IP 解决方案推向数据中心和 AI 基础设施市场的核心地带。