diff --git a/rt-thread-version/rt-thread-smart/_sidebar.md b/rt-thread-version/rt-thread-smart/_sidebar.md index 1cba969b08e26fe2413e9a02e12f8181682b77fd..666b14dd53e47843142bab16b23fcab097713c92 100644 --- a/rt-thread-version/rt-thread-smart/_sidebar.md +++ b/rt-thread-version/rt-thread-smart/_sidebar.md @@ -9,6 +9,7 @@ - 快速上手 - [QEMU快速上手(Win)](/rt-thread-version/rt-thread-smart/quick-start/qemu-win/quickstart.md) - [QEMU快速上手(Linux)](/rt-thread-version/rt-thread-smart/quick-start/qemu-linux/quickstart.md) + - [QEMU快速上手(xuantie)](/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/quickstart.md) - [ART-Pi Smart快速上手](/rt-thread-version/rt-thread-smart/quick-start/artpi-smart/quickstart.md) - 设备和驱动 - [DevFS设备管理](/rt-thread-version/rt-thread-smart/device/DevFS/DevFS.md) @@ -21,6 +22,9 @@ +- 移植说明 + - [riscv64架构移植说明](/rt-thread-version/rt-thread-smart/port/riscv64/riscv64.md) + - 应用开发 - [移植Linux应用到smart](/rt-thread-version/rt-thread-smart/application-note/port-app/port-app.md) - [使用VSCode开发GUI应用](/rt-thread-version/rt-thread-smart/application-note/sdl2/sdl2.md) diff --git a/rt-thread-version/rt-thread-smart/port/riscv64/riscv64.md b/rt-thread-version/rt-thread-smart/port/riscv64/riscv64.md new file mode 100644 index 0000000000000000000000000000000000000000..05294064a1a9cbb1e0133c67b81699b19cd8da1c --- /dev/null +++ b/rt-thread-version/rt-thread-smart/port/riscv64/riscv64.md @@ -0,0 +1,75 @@ +# RISCV64 移植说明 + +对于riscv架构移植,其对应的目录结构可以参考`/rt-thread/bsp/qemu-virt64-riscv` + +>注: 本文档只叙述 RT-Thread Smart移植相关的说明,在SMART运行之前理应有SPL、SBI等阶段,此处不再过多叙述 + +## 一、工具链获取 + +对于RT-Thread Smart来说,需要使用RT-Thread官方提供的工具链来完成 + +可以在[RT-Thread仓库](https://github.com/RT-Thread/rt-thread/releases)拿到 [riscv64gc-linux-musleabi_for_x86_64-pc-linux-gnu_252938-345d8b6e45.tar.bz2](https://github.com/RT-Thread/rt-thread/releases/download/v5.2.0/riscv64gc-linux-musleabi_for_x86_64-pc-linux-gnu_252938-345d8b6e45.tar.bz2?)工具链。 +> 注:此工具链只支持 lp64 ABI + +## 二、调整链接脚本 + +在`/rt-thread/bsp/qemu-virt64-riscv/`中`link_smart.lds`定义了内存布局,需要根据实际情况做出调整,或直接使用当前的设置。 +在链接脚本中添加了以下代码: + +~~~lds + +INCLUDE "link_stacksize.lds" +~~~ + +此处我们需要设置栈大小,具体方式通过 `scons--menuconfig`进行配置,该配置位于链接脚本同级的文件夹下的`Kconfig`中,在`SConstruct`中将设置好的值写入到`link_stacksize.lds`中,这里建议将其值设置为**16384及以上** + +## 三、完善libcpu + +### 文件说明 + +在[QEMU快速上手(xuantie)](/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/quickstart.md)中的分析起始可以得知,`libcpu//risc-v/common64`中已经做好了相关内容该目录提供RT-Thread标准版及SMART版本对rv64体系结构支持,其中包括: + +| 文件名 | 文件内容 | 参考标准 | +| :---------------: | :------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | +| context_gcc.S | 线程上下文切换 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| cpuport_gcc.S | 线程统一入口 | | +| cpuport.c | 线程栈初始化 | | +| cpuport.h | 通用寄存器、浮点、向量寄存器个数定义,内存屏障接口 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| interrupt_gcc.S | 异常/中断处理、全局中断使能/关闭 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| io.h | 以字节、字、双字读、写IO地址接口 | | +| encoding.h | CSR寄存器定义 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| ext_context.h | 浮点/向量上下文保存与恢复 | RISC-V "V" Standard Extension for Vector Operations, Version 1.0 "F" Extension for Single-Precision Floating-Point Version 2.2 | +| mmu.c | rv64 sv39 mmu管理接口 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| mmu.h | rv64 sv39 mmu页表相关定义 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| asid.c | rv64 mmu asid支持 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| riscv_mmu.c | 使能/关闭S态访问用户态页表 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| sbi.c | 通过ecall调用SBI相关信息接口 | RISC-V Supervisor Binary Interface Specification Version 1.0 | +| sbi.h | SBI spec相关接口定义 | RISC-V Supervisor Binary Interface Specification Version 1.0 | +| stack.h | 线程栈数据定义 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| stackframe.h | 线程上下文保存/恢复 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| syscall_c.c | 系统调用处理 | | +| tick.c | S态时钟初始化及中断处理 | | +| tlb.h | tlb刷新/无效接口 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | +| trap.c | 异常/中断处理,包括中断分发及用户态异常处理 | The RISC-V Instruction Set Manual Volume II: privileged supervisor-level ISA version 1.12 | + +#### 移植指南 + +(1)增加新的CPU支持 + +创建`libcpu/risc-v//`新目录,同时在`libcpu/risc-v/SConscript`中增加该CPU。 + +(2)PLIC中断控制器支持 + +`libcpu/risc-v/virt64/plic.[c|h]`提供了符合《RISC-V Platform-Level Interrupt Controller Specification version 1.0.0 》标准的PLIC中断控制器驱动代码,可作为移植参考。 +`libcpu/risc-v/virt64/interrupt.[c|h]`提供了plic的使用,并对接到 rt-thread 系统,可作为参考移植 + +上述功能在`borad.c`下的`rt_hw_board_init`函数中完成初始化 + +(3)cache支持 + +`libcpu/risc-v/virt64/cache.[c|h]`提供了cache相关的功能,可作为参考。因为qemu中无法真实模拟cache行为,所以该文件中并未真正实现cache功能,但是要确保相关接口存在。具体实现硬件cache行为的可参考 +`libcpu/risc-v/t-head/c906/cache.[c|h]` + +(4)外设支持 + +外设至少要实现串口驱动在,可参考`bsp/qemu-virt64-riscv/driver/drv_uart.[c|h]`,该串口主要用来实现控制台输出,在`borad.c`下的`rt_hw_board_init`函数中完成初始化。 diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/boot_sequence_riscv.svg b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/boot_sequence_riscv.svg new file mode 100644 index 0000000000000000000000000000000000000000..5334585bbe9d46ab2838343d42d80a8b5ddfa6d3 --- /dev/null +++ b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/boot_sequence_riscv.svg @@ -0,0 +1 @@ +RISC-V 内核启动时序CPU CoreCPU CoreMMUMMU物理内存物理内存SBI固件SBI固件内核入口内核入口MMU初始化阶段rt_hw_mem_setup_early()返回pv_offset(a0)alt[pv_offset ≠ 0 [需要重定位]]地址重定位"PC重定位_after_pc_relocation - a0""GP寄存器重定位gp = gp - a0""初始化栈指针sp =stack_start+STACKSIZE""设置异常入口csrw stvec, trap_entry"系统初始化sbi_init()调用SBI接口primary_cpu_entry()进入内核主流程关键寄存器状态:• a0 = pv_offset• x1 = 返回地址• sp = 栈顶地址 \ No newline at end of file diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/early_mem_setup_sequence.svg b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/early_mem_setup_sequence.svg new file mode 100644 index 0000000000000000000000000000000000000000..f40db8b02d9ef25d9e7db82233fc9f25a84d618b --- /dev/null +++ b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/early_mem_setup_sequence.svg @@ -0,0 +1 @@ +早期内存初始化关键流程CPUCPUMMUMMU物理内存物理内存PV偏移计算读取_start实际地址读取_start_link_addr链接地址计算pv_off = 实际地址 - 链接地址页表准备对齐__bss_end获取early_pgtbl地址清零页表区域alt[pv_off ≠ 0 [需要重映射]]恒等映射建立1GB大页映射(PC当前运行区域)early_pgtbl[pg_idx] = COMBINEPTE(ps, MMU_MAP_EARLY)重定位映射loop[遍历虚拟地址范围]建立高地址映射(vs_idx -> ps)ps += 1GB激活页表写SATP寄存器(SATP_BASE | early_pgtbl PA)触发sfence.vma刷新TLB \ No newline at end of file diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/ext4.png b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/ext4.png new file mode 100644 index 0000000000000000000000000000000000000000..d3c4f84b453bd1e37d86a240255ea576f76693aa Binary files /dev/null and b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/ext4.png differ diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/rtthread_board_init_compact.svg b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/rtthread_board_init_compact.svg new file mode 100644 index 0000000000000000000000000000000000000000..a89b3c24e424821ebffb09c93e9ce8b561056240 --- /dev/null +++ b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/rtthread_board_init_compact.svg @@ -0,0 +1 @@ +RT-Thread板级初始化流程MMU初始化页分配器映射激活堆管理中断系统串口控制台系统时钟组件初始化rt_hw_mmu_map_init()• 初始化内核地址空间• 设置页表基址rt_page_init()• 划分物理页• 初始化空闲链表rt_system_heap_init()• 管理RT_HW_HEAP_BEGIN到RT_HW_HEAP_END区域1.物理内存2.建立映射3.动态内存4.中断5.外设6.时钟7.组件 \ No newline at end of file diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/rtthread_boot_sequence.svg b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/rtthread_boot_sequence.svg new file mode 100644 index 0000000000000000000000000000000000000000..402d4d70e9114047bf96028ea384bb8cb297d2a1 --- /dev/null +++ b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/rtthread_boot_sequence.svg @@ -0,0 +1 @@ +RT-Thread启动流程(SMP关闭)启动入口执行启动函数1. 板级初始化2. 子系统3. 线程初始化相关4. 调度启动primary_cpu_entry()• 关闭全局中断• 调用entry()rtthread_startup()rt_hw_board_init()• 内存堆• 外设• 控制台• ...rt_system_timer_init()• 定时器初始化rt_system_scheduler_init()• 调度器初始化rt_system_signal_init()• 信号初始化rt_application_init()• 应用初始化,创建main线程rt_system_timer_thread_init()• 定时器线程初始化rt_thread_idle_init()• 空闲线程初始化rt_thread_defunct_init()• 调度器初始化rt_system_scheduler_start() \ No newline at end of file diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/samrt_sept.svg b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/samrt_sept.svg new file mode 100644 index 0000000000000000000000000000000000000000..d2a383728404b338fd7aa187bc998796dc5cb493 --- /dev/null +++ b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/samrt_sept.svg @@ -0,0 +1 @@ +QEMU 启动 RISC-V 加载 RT-Thread Smart 系统流程QEMU 模拟 RISC-V Virt 平台- 配置 CPU、内存、VirtIO 设备等- 模拟流程由QEMU完成,用户只需配置 CPU、内存、VirtIO 等参数即可启动 QEMU 并模拟硬件环境- 如果不设置 -bios 则使用默认的OPENSBI- OpenSBI 运行在 M 模式,提供 SBI 接口- 入口点: 0x80000000- 该流程由QEMU完成,如用自己修改的SBI,需在配置中通过-bios指定加载 OpenSBI 固件到 0x80000000- 通过 -kernel 参数加载 RT-Thread Smart 镜像- 典型入口点: 0x80200000- 包含内核、驱动和用户态支持- 该流程由QEMU完成,如果修改加载地址需要同时需改opensbi与qemu和os,这里不做过多处理加载 RT-Thread Smart 内核到 0x80200000- 设置trap_handler,注册sbi_ecall_exts指定范围ecall的处理函数等等- 此处如果需要添加自己的代码,需要重新编译opensbi并通过-bios指定OpenSBI 初始化- 设置 Hart ID (a0) 和 DTB 地址 (a1)- 切换到 S 模式,进入 RT-Thread Smart 内核入口- 如果要修改跳转地址,需要qemu,opensbi,os三者地址统一,不建议修改OpenSBI 设置上下文并跳转到 0x80200000- 早期mmu初始化- 初始化内存管理、线程调度- 加载设备驱动(基于 VirtIO)- 启动用户态环境等等RT-Thread Smart 内核初始化- RT-Thread Smart 提供实时内核功能- 支持用户态应用和设备管理系统运行 \ No newline at end of file diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/sept1.png b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/sept1.png new file mode 100644 index 0000000000000000000000000000000000000000..fcd5db3f5c6b548534e52f9fd27bc75a6e4495de Binary files /dev/null and b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/sept1.png differ diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/sept2.png b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/sept2.png new file mode 100644 index 0000000000000000000000000000000000000000..ff93d1483b565f5ae61ad42260faec782859148b Binary files /dev/null and b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/sept2.png differ diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/sept3.png b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/sept3.png new file mode 100644 index 0000000000000000000000000000000000000000..233ee3456dda3223f23440f298add2e71a8e4068 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/sept3.png differ diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/userapps.png b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/userapps.png new file mode 100755 index 0000000000000000000000000000000000000000..c303398c831189fcb02d01545bc468a0c21a9e88 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/figures/userapps.png differ diff --git a/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/quickstart.md b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/quickstart.md new file mode 100644 index 0000000000000000000000000000000000000000..d0bcffa958542bc33bbdd4c8e4de0b4c38f761f4 --- /dev/null +++ b/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/quickstart.md @@ -0,0 +1,339 @@ +# QEMU 快速上手 (xuantie) + +本教程在 Ubuntu 平台上使用 QEMU 快速上手 RT-Smart,运行 RT-Smart 用户态应用,内核基于 xuantie/virt64/c906 ,其他内核可以用同样的方式运行。 + +- 模拟器: [Xuantie-qemu-x86_64-Ubuntu-20.04-V5.0.5-B20250108-0335.tar.gz](https://www.xrvm.cn/community/download?id=4397435198627713024) + +注:若上述链接中的模拟器不能使用,可以使用下述仓库编译模拟器 +> [XUANTIE-RV/qemu](https://github.com/XUANTIE-RV/qemu) + +## QEMU (xuantie) 启动流程介绍 + +相较于实际的硬件平台,QEMU 的 Virt 平台是一个高度抽象的虚拟化平台,旨在提供一个通用的、与具体硬件无关的环境。它不像真实的硬件平台(如特定的 SoC)需要通过 SPL 或 U-Boot 的早期阶段来初始化 DDR(内存)或 eMMC(存储)。在 QEMU Virt 中,内存(RAM)和存储设备(如 VirtIO 块设备)由 QEMU 模拟器直接提供,并已经在模拟器启动时完成初始化。 + +![samrt_sept.svg](./figures/samrt_sept.svg) + +### OPENSBI 阶段 + +在 QEMU 环境下,OpenSBI 通常作为 RISC-V 系统引导流程的第一阶段,运行在 M 模式(Machine Mode),负责初始化硬件并为 S 模式(Supervisor Mode,即操作系统或裸机程序运行的模式)提供必要的服务。 + +在 QEMU 中,OpenSBI 是默认的固件,如果不使用 `-bios`进行指定,默认为`opensbi-riscv64-generic-fw_dynamic.bin`,该文件位于qemu源码的`pc-bios`目录下,定义在`qemu/include/hw/riscv/boot.h`文件中,用于 RISC-V 的引导流程。 + +> 更过详细内容可查看此文档,这里不再做过多介绍:[OPENSBI介绍](https://www.cnblogs.com/arnoldlu/p/18170952) + +#### OPENSBI编译 + +对于编译OPENSBI,可以通过克隆XUANTIE/opensbi仓库进行编译 + +~~~shell +git clone git@github.com:XUANTIE-RV/opensbi.git + +make PLATFORM=generic PLATFORM_RISCV_XLEN=64 BUILD_INFO=y DEBUG=1 CROSS_COMPILE=/opt/Xuantie-900-gcc-linux-6.6.0-musl64-x86_64-V3.0.2/bin/riscv64-unknown-linux-musl- O=build_rv64 +~~~ + +在`build_rv64/platform/generic/firmware`路径下便可查看到编译好的固件,使用时需要通过 -bios 指向固件位置 +>注:此处使用XUANTIE官方提供的工具链[Xuantie-900-gcc-linux-6.6.0-musl64-mingw-V3.1.0-20250522.tar.gz](https://www.xrvm.cn/community/download?id=4453617141140230144) + +### SMART阶段 + +SMART默认运行在S模式,起始代码入口位于`rt-thread/libcpu/risc-v/common64/startup_gcc.S`中,SMART就不过介绍,详情可查看[SMART简介](../../introduction/rt-smart-intro/rt-smart-intro.md),这里说一下RT-Thread Smart内核初始化做了哪些内容 + +1. 早期初始化 +对于riscv架构64位的芯片,RT-Thread SMART的入口代码位于`rt-thread/libcpu/risc-v/common64/startup_gcc.S`目录中,在BSP目录下的链接脚本中可以看到内存的映射,SMART采用高位地址映射策略,将DRAM区域配置于0xFFFFFFC000200000起始处: + + ~~~lds + MEMORY + { + DRAM : ORIGIN = 0xFFFFFFC000200000, LENGTH = 0x1000000 - 0x200000 + } + ~~~ + + 但此时QEMU开始运行SMART时`$PC`仍然是低地址。在`rt_hw_mem_setup_early`中开始进行mmu初始化,建立SV39多级页表。具体流程如下: + + ![early_mem_setup](./figures/early_mem_setup_sequence.svg) + + 但此时`$PC`仍然位于低地址,这也是做恒等映射的原因。在页表激活后经过`_after_pc_relocation`将`$PC`指向高地址区域,如下: + + ![boot_sequence_riscv](./figures/boot_sequence_riscv.svg) + +2. 系统内部初始化 + 进入到`primary_cpu_entry`会执行以下流程: + + ![rtthread_boot_sequence](./figures/rtthread_boot_sequence.svg) + + 其他流程与系统相关,更多文档可以查阅rt-thread文档中心或官方论坛,这里不再过多说明,与BSP相关的主要`rt_hw_board_init`板级初始化 + +3. 板级初始化 + + `primary_cpu_entry`与`rt_hw_board_init`主要位于`rt-thread/bsp/xuantie/virt64/c906/board/board.c`文件,主要完成如下内容: + + ![rtthread_board_init_compact](./figures/rtthread_board_init_compact.svg) + +## 构建内核镜像 + +### 下载代码 + +下载 smart 用户态应用代码: + +~~~shell +git clone https://github.com/RT-Thread/rt-thread.git +~~~ + +### 配置工具链 + +工具链可以通过 [Xuantie-900-gcc-linux-6.6.0-musl64-x86_64-V3.0.2-20250410.tar.gz](https://github.com/RT-Thread/toolchains-ci/releases/download/v1.9/Xuantie-900-gcc-linux-6.6.0-musl64-x86_64-V3.0.2.tar.gz)获取 + +> 注:与编译opensbi的工具链不是同一个 + +下载完成之后,解压至/opt,然后打开 `~/.bashrc` 文件,添加环境变量: + +~~~shell +export RTT_CC="gcc" +export RTT_EXEC_PATH="/opt/Xuantie-900-gcc-linux-6.6.0-musl64-x86_64-V3.0.2/bin/" +export RTT_CC_PREFIX="riscv64-unknown-linux-musl-" +export PATH="$RTT_EXEC_PATH:$PATH" +~~~ + +### 配置内核 + +~~~shell +cd ./rt-thread/bsp/xuantie/virt64/c906/ #打开 rt-thread 项目目录中的 bsp/xuantie/virt64/c906/ 目录 +scons --menuconfig +~~~ + +基于 rt-thread 仓库的 bsp/xuantie/virt64/c906/ 构建内核镜像,最终生成 `rtthread.bin/rtthread.elf`,需要注意的是,内核默认支持 fat, 如果要挂载 ext4 的文件系统,则还需要额外安装 lwext4 软件包,即使能 `PKG_USING_LWEXT4` +>具体 menuconfig 路径是 (Top) -> RT-Thread online packages -> system packages -> lwext4: an excellent choice of ext2/3/4 filesystem for microcontrollers + +1. **选择 RT-Thread Kernel 选项** + + ![sept1](figures/sept1.png) + +2. **使能 Smart 内核** + + ![sept2](figures/sept2.png) + +3. **使能 lwext4 软件包(不使用ext4文件系统可不选)** + + ![sept3](figures/ext4.png) + +4. **执行 scons 编译** + + ![sept4](figures/sept3.png) + +## 制作根文件系统 + +1. **拉取 [userapps仓库](https://github.com/RT-Thread/userapps)**: + userapps仓库包含了一些应用程序和配置文件,能够快速生成根文件系统,并为 RT-Thread 操作系统构建所需的应用程序。 + + ~~~shell + git clone https://github.com/RT-Thread/userapps + ~~~ + + 在 `userapps` 目录中,将有如下文件: + + ![userapps.png](figures/userapps.png) + + - **apps**:存放应用程序代码,包管理文件夹及编译产物 + - **env.sh**:用于配置环境变量 + - **repo**:用于管理软件包的文件夹 +2. **配置环境**: + + ~~~shell + source env.sh + ~~~ + +3. **拉取工具链和编译根文件系统**: + 为了编译 xuantie 目标平台上的代码,您需要下载并安装交叉编译工具链: + + ~~~shell + cd apps + xmake f -a xuantie + xmake + xmake smart-rootfs + ~~~ + +4. **生成文件系统镜像**: + 生成的文件系统要与kernel互相匹配,如果在上述配置内核时没有使用ext4的文件系统,那么使用如下命令生成fat格式的文件系统,在build目录中生成 `fat.img`文件。 + + ~~~shell + xmake smart-image -f fat + ~~~ + + 如果kernel使用ext4的文件系统,那么使用如下命令生成ext4格式的文件系统,在build目录中生成 `ext4.img`文件。 + + ~~~shell + xmake smart-image + ~~~ + +## 编写自己的应用程序 + +1. **创建新的目录**: + 进入apps目录 + + ~~~bash + cd userapps/apps + mkdir welcome + ~~~ + +2. **创建 C 文件和 `xmake.lua`文件:** + + - **编写 `main.c`:** + + ~~~bash + vim welcome/main.c + ~~~ + + 内容如下: + + ~~~c + #include + + int main(int argc, char **argv) + { + printf("Welcome to RT-Thread!\n"); + + return 0; + } + ~~~ + + - **编写 `xmake.lua`:** + + 指定项目的构建目标,定义这些目标如何依赖源代码文件、头文件、库等。使xmake 知道如何将源代码文件编译成二进制可执行文件或库文件。 + + ~~~bash + vim welcome/xmake.lua + ~~~ + + 内容如下: + + ~~~lua + add_rules("mode.debug", "mode.release") + + target("welcome") + do + add_files("*.c") + end + target_end() + ~~~ + +3. **将创建的文件夹添加至build目录**:因为有些应用kernel还未支持,如sdl库需要图形显示等等。在`userapps/apps/xmake.lua`中的run_apps中添加文件夹路径 + + ~~~shell + local run_apps = { + "busybox", + "cpp", + "hello", + "shm_ping", + "shm_pong", + "smart-fetch", + "zlib", + "welcome", + } + ~~~ + +4. **重新编译**:因为编写了新的应用程序,所以重新编译uesrapps生成新的文件系统镜像。 + + ~~~bash + xmake + xmake smart-rootfs + xmake smart-image / xmake smart-image -f fat (根据kernel支持的文件格式选择不同的指令) + ~~~ + +### 运行 RT-Thread Smart 版 + +qemu下的smart运行需要三个文件,分别为`rtthread.bin`、`ext4.img/fat.img`、`run.sh`、,即内核文件,用户程序镜像,启动脚本。其中,`run.sh`位于bsp`rt-thread/bsp/xuantie/virt64/c906`目录,`rtthread.bin`编译完内核后与`run.sh`位于同一目录,`ext4.img/fat.img`在生成镜像后位于`userapps/apps/build/`目录。 + +将这三个文件放到同一个文件夹下,即可运行smart。示例如下: + +~~~shell +$ bash run.sh ext4.img + +OpenSBI v1.4 + ____ _____ ____ _____ + / __ \ / ____| _ \_ _| + | | | |_ __ ___ _ __ | (___ | |_) || | + | | | | '_ \ / _ \ '_ \ \___ \| _ < | | + | |__| | |_) | __/ | | |____) | |_) || |_ + \____/| .__/ \___|_| |_|_____/|____/_____| + | | + |_| + +Platform Name : riscv-virtio,qemu +Platform Features : medeleg +Platform HART Count : 1 +Platform IPI Device : aclint-mswi +Platform Timer Device : aclint-mtimer @ 10000000Hz +Platform Console Device : uart8250 +Platform HSM Device : --- +Platform PMU Device : --- +Platform Reboot Device : syscon-reboot +Platform Shutdown Device : syscon-poweroff +Platform Suspend Device : --- +Platform CPPC Device : --- +Firmware Base : 0x80000000 +Firmware Size : 323 KB +Firmware RW Offset : 0x40000 +Firmware RW Size : 67 KB +Firmware Heap Offset : 0x48000 +Firmware Heap Size : 35 KB (total), 2 KB (reserved), 9 KB (used), 23 KB (free) +Firmware Scratch Size : 4096 B (total), 328 B (used), 3768 B (free) +Runtime SBI Version : 2.0 + +Domain0 Name : root +Domain0 Boot HART : 0 +Domain0 HARTs : 0* +Domain0 Region00 : 0x0000000000100000-0x0000000000100fff M: (I,R,W) S/U: (R,W) +Domain0 Region01 : 0x0000000010000000-0x0000000010000fff M: (I,R,W) S/U: (R,W) +Domain0 Region02 : 0x0000000002000000-0x000000000200ffff M: (I,R,W) S/U: () +Domain0 Region03 : 0x0000000080040000-0x000000008005ffff M: (R,W) S/U: () +Domain0 Region04 : 0x0000000080000000-0x000000008003ffff M: (R,X) S/U: () +Domain0 Region05 : 0x000000000c400000-0x000000000c5fffff M: (I,R,W) S/U: (R,W) +Domain0 Region06 : 0x000000000c000000-0x000000000c3fffff M: (I,R,W) S/U: (R,W) +Domain0 Region07 : 0x0000000000000000-0xffffffffffffffff M: () S/U: (R,W,X) +Domain0 Next Address : 0x0000000080200000 +Domain0 Next Arg1 : 0x000000008fe00000 +Domain0 Next Mode : S-mode +Domain0 SysReset : yes +Domain0 SysSuspend : yes + +Boot HART ID : 0 +Boot HART Domain : root +Boot HART Priv Version : v1.10 +Boot HART Base ISA : rv64imafdc +Boot HART ISA Extensions : zicntr,zihpm +Boot HART PMP Count : 16 +Boot HART PMP Granularity : 2 bits +Boot HART PMP Address Bits: 54 +Boot HART MHPM Info : 16 (0x0007fff8) +Boot HART MIDELEG : 0x0000000000000222 +Boot HART MEDELEG : 0x000000000000b109 +heap: [0x003404c0 - 0x043404c0] + + \ | / +- RT - Thread Smart Operating System + / | \ 5.2.1 build Jun 13 2025 09:43:35 + 2006 - 2024 Copyright by RT-Thread team +[I/utest] utest is initialize success. +[I/utest] total utest testcase num: (1) +[I/drivers.serial] Using /dev/ttyS0 as default console +[W/DFS.fs] mount / failed with file system type: elm +file system initialization done! +Hello RISC-V +msh />/ # +/ # +/ # ls +bin lib proc sbin usr +dev lost+found root services var +etc mnt run tmp +/ # +~~~ + +## Features + +- vdso完整支持 +- 更多的qemu外设支持,如:网络等 +- 更多的userapps应用支持 + +### 联系人信息 + +如有意向咨询请联系: