QEMU 仿真RISC-V freeRTOS 程序

1. 安裝RISC-V 仿真環境 --QEMU

安裝包下載地址: https://www.qemu.org/

安裝命令及安裝成功效果如下所示,

target-list 設定爲riscv32-softmmu,

bash 复制代码
$ cat ~/project/qemu-8.0.4/install.sh 
sudo apt-get install libglib2.0-dev
sudo apt-get install libpixman-1-dev
./configure --target-list=riscv32-softmmu
make -j24
sudo make install -j24
bash 复制代码
$ qemu-system-riscv32 -h
QEMU emulator version 8.0.4
Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers
usage: qemu-system-riscv32 [options] [disk_image]

2. 安裝RISC-V 編譯器, (過程略)

如下cmd 可檢查是否安裝成功並顯示版本號 12.2.0

bash 复制代码
$ /opt/riscv/bin/riscv64-unknown-elf-gcc --version
riscv64-unknown-elf-gcc (g2ee5e430018) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

3. 準備RISC-V 可執行程序

修改例程編譯器路徑徑, 並且由於編譯器版本不匹配, 需要修更換指令集

bash 复制代码
$ git diff FreeRTOS/Demo/RISC-V-Qemu-virt_GCC/
diff --git a/FreeRTOS/Demo/RISC-V-Qemu-virt_GCC/Makefile b/FreeRTOS/Demo/RISC-V-Qemu-virt_GCC/Makefile
index bd24333a2..736fe8fcb 100644
--- a/FreeRTOS/Demo/RISC-V-Qemu-virt_GCC/Makefile
+++ b/FreeRTOS/Demo/RISC-V-Qemu-virt_GCC/Makefile
@@ -1,6 +1,7 @@
-CROSS   = riscv64-unknown-elf-
+CROSS   = /opt/riscv/bin/riscv64-unknown-elf-
 CC      = $(CROSS)gcc
 OBJCOPY = $(CROSS)objcopy
+OBJDUMP = $(CROSS)objdump
 ARCH    = $(CROSS)ar
 
 BUILD_DIR       = build
@@ -14,7 +15,7 @@ CPPFLAGS = \
        -I $(RTOS_SOURCE_DIR)/include \
        -I $(RTOS_SOURCE_DIR)/portable/GCC/RISC-V \
        -I $(RTOS_SOURCE_DIR)/portable/GCC/RISC-V/chip_specific_extensions/RV32I_CLINT_no_extensions
-CFLAGS  = -march=rv32imac -mabi=ilp32 -mcmodel=medany \
+CFLAGS  = -march=rv32imac_zicsr -mabi=ilp32 -mcmodel=medany \
        -Wall \
        -fmessage-length=0 \
        -ffunction-sections \
@@ -56,6 +57,9 @@ DEPS = $(SRCS:%.c=$(BUILD_DIR)/%.d) $(ASMS:%.S=$(BUILD_DIR)/%.d)
 
 $(BUILD_DIR)/RTOSDemo.axf: $(OBJS) fake_rom.lds Makefile
        $(CC) $(LDFLAGS) $(OBJS) -o $@
+       $(OBJCOPY) -O ihex $(BUILD_DIR)/RTOSDemo.axf $(BUILD_DIR)/RTOSDemo.hex
+       $(OBJCOPY) -O binary $(BUILD_DIR)/RTOSDemo.axf $(BUILD_DIR)/RTOSDemo.bin
+       $(OBJDUMP) -S -d $(BUILD_DIR)/RTOSDemo.axf > $(BUILD_DIR)/RTOSDemo.asm
 
 $(BUILD_DIR)/%.o: %.c Makefile
        @mkdir -p $(@D)
  • 編譯完成後, BUILD_DIR 下生成RTOSDemo.axf 可執行文件

4.運行RISC-V程序

執行如下命令, 運行上一步準備好的RTOSDemo.axf, 觀察到周期性打性LOG,

證明程序有正常運行起來, 檢查與上一步例程功能一致, 完結收工~

bash 复制代码
$ qemu-system-riscv32 -nographic -machine virt -net none -chardev stdio,id=con,mux=on -serial chardev:con -mon chardev=con,mode=readline -bios none -smp 4 -kernel ~/project/freeRTOS/FreeRTOS_main/FreeRTOS/Demo/RISC-V-Qemu-virt_GCC/build/RTOSDemo.axf 
Hello FreeRTOS!
0: Tx: Transfer1
0: Rx: Blink1
0: Tx: Transfer2
0: Rx: Blink2
0: Tx: Transfer1
0: Rx: Blink1
0: Tx: Transfer2

5. 參考資料

QEMU RISC-V 官方文檔: qemu-8.0.4/docs/system/riscv/virt.rst

freeRTOS RISC-V QEMU 官方使用文檔: FreeRTOS/Demo/RISC-V-Qemu-virt_GCC/Readme.md

相关推荐
Zevalin爱灰灰3 天前
FreeRTOS从入门到精通 第七章(FreeRTOS列表和列表项)
stm32·操作系统·freertos
世事如云有卷舒5 天前
RISC-V学习笔记
笔记·学习·risc-v
PegasusYu6 天前
STM32CUBEIDE FreeRTOS操作教程(十二):std dynamic memory 标准动态内存
stm32·freertos·rtos·stm32cubeide·free-rtos·标准动态内存·dynamic memeory
oahrzvq7 天前
【CPU】RISC-V 与 x86 操作数字段的区别
系统架构·risc-v
MounRiver_Studio7 天前
基于VSCode软件框架的RISC-V IDE MRS2正式上线发布
ide·vscode·mcu·risc-v
京雨17 天前
Qemu 加载你指定的 initrd、dtb 到哪里?
qemu·riscv64·fdt·initrd
东辰芯力20 天前
探索未来物联网开发——HiSpark平台与海思IDE安装指南
人工智能·单片机·嵌入式硬件·算法·risc-v
杭州_燕十三24 天前
从零开始学习 sg200x 多核开发之 sophpi 编译生成 fip.bin 流程梳理
risc-v·sg2002
luoganttcc24 天前
优秀的 Verilog/FPGA开源项目介绍(二)-RISC-V
fpga开发·开源·risc-v
Krysdon25 天前
Freertos任务切换
stm32·freertos