1. 安裝RISC-V 仿真環境 --QEMU
安裝包下載地址: https://www.qemu.org/
安裝命令及安裝成功效果如下所示,
target-list 設定爲riscv32-softmmu,
$ 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
$ 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
$ /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 可執行程序
- 測試使用freeRTOS 官網提供例程, https://github.com/FreeRTOS/FreeRTOS/tree/main/FreeRTOS/Demo/RISC-V_RV32_QEMU_VIRT_GCChttps://github.com/FreeRTOS/FreeRTOS/tree/main/FreeRTOS/Demo/RISC-V_RV32_QEMU_VIRT_GCC
修改例程編譯器路徑徑, 並且由於編譯器版本不匹配, 需要修更換指令集
$ 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,
證明程序有正常運行起來, 檢查與上一步例程功能一致, 完結收工~
$ 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