Skip to content

Commit e08fc29

Browse files
unicornxRbb666
authored andcommitted
bsp: k230: use rttpkgtool to package kernel
The original method of packaging and burning kernel depends on installing the K230 RTOS SDK. It is quite cumbersome. Now the relevant logic is extracted and a small tool rttpkgtool is created to package the rtthread.bin. Considering that rttpkgtool still needs to compile opensbi when packaging k230, which means that a separate cross-toolchain needs to be installed. Therefore, in order not to affect the CI operation, the current packaging logic is not integrated into POST_ACTION in rtconfig.py, but requires kernel developers to run the build script separately for packaging. Signed-off-by: Chen Wang <unicorn_wang@outlook.com>
1 parent 427e177 commit e08fc29

File tree

4 files changed

+115
-130
lines changed

4 files changed

+115
-130
lines changed

bsp/k230/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
build/
33
packages/
44
install/
5+
rttpkgtool/
56

67
rtthread.*
78
link.lds.generated

bsp/k230/README.md

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ CanMV-K230 Board Support Package 使用说明
1212
- [3.2.3. Env](#323-env)
1313
- [3.3. 下载 RT-Thread 并更新依赖的软件包](#33-下载-rt-thread-并更新依赖的软件包)
1414
- [3.4. 构建](#34-构建)
15-
- [4. 烧写](#4-烧写)
16-
- [4.1. 制作启动 SD 卡](#41-制作启动-sd-卡)
17-
- [4.1.1. 编译固件](#411-编译固件)
18-
- [4.1.2. 烧录固件](#412-烧录固件)
19-
- [4.2. 单独更新 RT-Thread 内核](#42-单独更新-rt-thread-内核)
20-
- [5. 上电启动](#5-上电启动)
15+
- [4. 采用 rttpkgtool 对内核打包](#4-采用-rttpkgtool-对内核打包)
16+
- [5. 制作基础 SD 卡](#5-制作基础-sd-卡)
17+
- [6. 单独更新 RT-Thread 内核](#6-单独更新-rt-thread-内核)
18+
- [7. 上电启动](#7-上电启动)
2119

2220
<!-- /TOC -->
2321

@@ -105,19 +103,64 @@ $ scons -j$(nproc)
105103

106104
此时在 `$WS/rt-thread/bsp/k230` 下会看到生成了 `rtthread.bin`, 这个就是我们构建出来的内核二进制文件。
107105

106+
# 4. 采用 rttpkgtool 对内核打包
108107

109-
# 4. 烧写
108+
直接构建生成的 `rtthread.bin` 并不能直接用于启动,我们采用 rttpkgtool 对 `rtthread.bin` 进行二次打包生成符合开发板能够识别的格式。
110109

111-
`rtthread.bin` 并不能直接运行。目前我们基于 Canaan 官方的 RTOS-only SDK 制作 SD 卡,并基于 SD 卡启动内核。
110+
可以从以下网站获取 rttpkgtool(**注意需要切换到 `for-k230` 分支**):
112111

113-
## 4.1. 制作启动 SD 卡
112+
- 主站(github):<https://github.com/plctlab/rttpkgtool>
113+
- 中国区镜像(gitee):<https://gitee.com/unicornx/rttpkgtool/>
114114

115-
### 4.1.1. 编译固件
115+
为方便使用,在本 bsp 下提供了一份封装脚本 `build.sh`,可以直接执行后打包生成最终可以烧录的 `opensbi_rtt_system.bin` 文件。
116116

117-
参考 K230 RTOS Only 用户指南中的 “如何编译固件”:<https://developer.canaan-creative.com/k230_rtos/zh/dev/userguide/how_to_build.html>
117+
但需要注意是,`build.sh` 只负责下载 rttpkgtool 并运行 rttpkgtool,但不会自动安装 rttpkgtool 依赖的工具软件,所以在使用 `build.sh` 前请仔细阅读 rttpkgtool 仓库的 “for-k230” 分支上的 `README.md` 文件。提前安装好一些额外的依赖和用于编译 opensbi 的交叉工具链(**注意这个编译 opensbi 的交叉工具链和编译 RTT 的 工具链是不同的**
118118

119-
假设 sdk 安装的路径在 `$WS/rtos_k230`
120-
注意事项如下:
119+
以上依赖和交叉工具链安装好后,可以执行如下命令进行打包:
120+
121+
```shell
122+
$ cd rt-thread/bsp/k230 # 确保在本 bsp 目录下
123+
$ ./build.sh
124+
BSP_PATH: /home/u/ws/canaan/rt-thread/bsp/k230
125+
rttpkgtool does not exist, clone it from https://gitee.com/unicornx/rttpkgtool.git
126+
Cloning into '/home/u/ws/canaan/rt-thread/bsp/k230/rttpkgtool'...
127+
......
128+
Trying to download the opensbi source code ......
129+
/home/u/ws/canaan/rt-thread/bsp/k230/rttpkgtool/output/src/opensbi does not exist, clone it from https://gitee.com/canmv-k230/opensbi.git
130+
Cloning into '/home/u/ws/canaan/rt-thread/bsp/k230/rttpkgtool/output/src/opensbi'...
131+
......
132+
AS-DEP platform/kendryte/fpgac908/firmware/fw_payload.dep
133+
......
134+
CC lib/sbi/riscv_asm.o
135+
......
136+
OBJCOPY platform/kendryte/fpgac908/firmware/fw_payload.bin
137+
~/ws/canaan/rt-thread/bsp/k230/rttpkgtool
138+
Image Name: rtt
139+
Created: Sun Apr 27 15:26:23 2025
140+
Image Type: RISC-V RISC-V OpenSBI Multi-File Image (gzip compressed)
141+
Data Size: 391973 Bytes = 382.79 KiB = 0.37 MiB
142+
Load Address: 00000000
143+
Entry Point: 00000000
144+
Contents:
145+
Image 0: 391965 Bytes = 382.78 KiB = 0.37 MiB
146+
the magic is: b'K230'
147+
----- NO ENCRYPTION + HASH-256 -----
148+
the encryption type: 0
149+
mesg_hash: b'7c469067ab0e5c3f9701f8dbd67ffe90f0213d14aa3ac262f7d9fdeff0343377'
150+
/home/u/ws/canaan/rt-thread/bsp/k230/rttpkgtool
151+
Generate the image file successfully!
152+
The image file is located at /home/u/ws/canaan/rt-thread/bsp/k230/rttpkgtool/output/k230_rtos_01studio_defconfig/images/opensbi/opensbi_rtt_system.bin
153+
```
154+
155+
第一次执行 `build.sh` 会自动 clone 下载 rttpkgtool 以及 opensbi 源码,并完整编译 opensbi 后再执行打包。以后执行 `build.sh` 只会增量打包。
156+
157+
如果希望重新下载 rttpkgtool 以及 opensbi,可以删除 `rt-thread/bsp/k230` 下的 `rttpkgtool` 目录后重新执行 `build.sh` 脚本即可。
158+
159+
# 5. 制作基础 SD 卡
160+
161+
在单独更新内核镜像之前,我们需要先制作一个基础的 SD 卡。可以参考 K230 RTOS Only SDK 用户指南中的 “如何编译固件”:<https://developer.canaan-creative.com/k230_rtos/zh/dev/userguide/how_to_build.html> 生成一个完整的 image。
162+
163+
假设 sdk 安装的路径在 `$WS/rtos_k230`。注意事项如下:
121164

122165
- 目前用户指南的环境只在 Ubuntu 20.04 LTS (x86_64) 上验证过,*其他 Linux 发行版未经充分测试,可能存在兼容性问题*。为避免和 BSP 构建环境冲突,建议在另外一台机器或者虚拟机中搭建此环境。
123166

@@ -140,56 +183,33 @@ $ scons -j$(nproc)
140183

141184
为简单起见,这里选择 “ k230_rtos_01studio_defconfig” 。
142185

143-
最后在 `$WS/rtos_k230/output/k230_rtos_01studio_defconfig/` 下生成 `RtSmart-K230_01Studio_rtsmart_local_nncase_v2.9.0.img`
144-
145-
### 4.1.2. 烧录固件
186+
构建完成后在 `$WS/rtos_k230/output/k230_rtos_01studio_defconfig/` 下生成 `RtSmart-K230_01Studio_rtsmart_local_nncase_v2.9.0.img`
146187

147-
参考 K230 RTOS Only 用户指南中的 “如何烧录固件”:<https://developer.canaan-creative.com/k230_rtos/zh/dev/userguide/how_to_flash.html>, 通过 SD 卡烧录。熟悉 Windows 平台的可以使用 balenaEtcher。
188+
然后参考 K230 RTOS Only SDK 用户指南中的 “如何烧录固件”:<https://developer.canaan-creative.com/k230_rtos/zh/dev/userguide/how_to_flash.html>, 通过 SD 卡烧录。熟悉 Windows 平台的可以使用 balenaEtcher。烧录后,SD 卡上会自动分区和格式化
148189

149-
## 4.2. 单独更新 RT-Thread 内核
190+
**注意本小节的操作只要做一次**。以后只要单独更新 RT-Thread 内核的镜像即可。
150191

151-
在开发 BSP 过程中每次更新内核都烧写整个 SD 卡是一件非常麻烦的事情。以下操作只更新内核所在分区。
152-
153-
首先前面构建生成的 `rtthread.bin` 并不能直接用,而是需要和 opensbi 的 image 打包后才能被 u-boot 加载。我们这里利用 K230 RTOS Only SDK 来打包。具体步骤如下:
154-
155-
先将前面生成的 `rtthread.bin` 拷贝到 `$WS/rtos_k230/output/k230_rtos_01studio_defconfig/images/rtsmart` 下。
156-
157-
修改 `rtos_k230/Makefile`
158-
159-
```diff
160-
diff --git a/Makefile b/Makefile
161-
index 45a3f0c..39a017b 100644
162-
--- a/Makefile
163-
+++ b/Makefile
164-
@@ -93,13 +93,20 @@ rtsmart-menuconfig:
165-
@$(MAKE) -C $(SDK_RTSMART_SRC_DIR) menuconfig
166-
167-
.PHONY: opensbi opensbi-clean opensbi-distclean
168-
-opensbi: .autoconf rtsmart
169-
+opensbi: .autoconf
170-
@$(MAKE) -C $(SDK_OPENSBI_SRC_DIR) all
171-
opensbi-clean:
172-
@$(MAKE) -C $(SDK_OPENSBI_SRC_DIR) clean
173-
opensbi-distclean:
174-
@$(MAKE) -C $(SDK_OPENSBI_SRC_DIR) distclean
175-
```
176-
然后执行:
177-
178-
```shell
179-
$ make opensbi
180-
```
192+
# 6. 单独更新 RT-Thread 内核
181193

182-
会在 `$WS/rtos_k230/output/k230_rtos_01studio_defconfig/images/opensbi` 下生成一个新的 `opensbi_rtt_system.bin`,这也是我们要烧写的最终文件
194+
我们可以利用 rttpkgtool 提供的脚本 `sdcard.sh` 快速更新打包后生成的内核镜像 `opensbi_rtt_system.bin`
183195

184-
将 SD卡通过 USB 读卡器接入 Ubuntu 机器。假设枚举为 `/dev/sdb`执行如下命令烧写:
196+
先将 SD 卡通过 USB 读卡器接入 Ubuntu 机器。以下假设 USB 读卡器设备枚举为 `/dev/sdb`如果不同请阅读 `sdcard.sh` 脚本代码。
185197

186198
```shell
187-
$ sudo dd if=$WS/rtos_k230/output/k230_rtos_01studio_defconfig/images/opensbi/opensbi_rtt_system.bin of=/dev/sdb seek=20480
199+
$ cd rt-thread/bsp/k230 # 确保在本 bsp 目录下
200+
$ ./rttpkgtool/script/sdcard.sh
201+
SRC: /home/u/ws/canaan/rt-thread/bsp/k230/rttpkgtool/output/k230_rtos_01studio_defconfig/images/opensbi/opensbi_rtt_system.bin
202+
DEST: /dev/sdb
203+
[sudo] password for u:
204+
766+1 records in
205+
766+1 records out
206+
392569 bytes (393 kB, 383 KiB) copied, 0.0886941 s, 4.4 MB/s
207+
Done!
188208
```
189209

190-
# 5. 上电启动
210+
# 7. 上电启动
191211

192-
将 SD 卡插入 01Studo 开发板的 SD 卡槽。
212+
将 SD 卡插入 01Studio 开发板的 SD 卡槽。
193213

194214
连接 USB 转 UART 串口线到 “CPU0 调试口”,具体见下图:
195215

bsp/k230/build.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
function download_rttpkgtool()
4+
{
5+
local project_path=$1
6+
local restult=$(curl -m 10 -s http://www.ip-api.com/json)
7+
local country=$(echo $restult | sed 's/.*"country":"\([^"]*\)".*/\1/')
8+
#echo "Country: $country"
9+
10+
if [ "$country" == "China" ]; then
11+
local url_rttpkgtool="https://gitee.com/unicornx/rttpkgtool.git"
12+
else
13+
local url_rttpkgtool="https://github.com/plctlab/rttpkgtool.git"
14+
fi
15+
#echo "rttpkgtool URL: ${url_rttpkgtool}"
16+
17+
if [ ! -d ${project_path}/rttpkgtool ]; then
18+
echo "rttpkgtool does not exist, clone it from ${url_rttpkgtool}"
19+
git clone -b for-k230 ${url_rttpkgtool} ${project_path}/rttpkgtool
20+
21+
if [ $? -ne 0 ]; then
22+
echo "Failed to clone ${url_rttpkgtool} !"
23+
exit 1
24+
fi
25+
else
26+
echo "rttpkgtool already exists"
27+
pushd ${project_path}/rttpkgtool
28+
git checkout for-k230
29+
git pull
30+
popd
31+
fi
32+
}
33+
34+
BSP_PATH=$(realpath $(dirname $0))
35+
echo "BSP_PATH: $BSP_PATH"
36+
37+
download_rttpkgtool $BSP_PATH
38+
39+
pushd $BSP_PATH/rttpkgtool > /dev/null
40+
DPT_PATH_KERNEL=$BSP_PATH ./script/mkpkg.sh
41+
popd > /dev/null

bsp/k230/mkfm.sh

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)