|
1 |
| -# rt-smart canaan porting |
| 1 | +CanMV-K230 Board Support Package 使用说明 |
2 | 2 |
|
3 |
| -## 下载依赖的软件包 |
| 3 | +<!-- TOC --> |
4 | 4 |
|
5 |
| -在软件包无需变更的情况下只须执行一次 |
| 5 | +- [1. 参考文档](#1-参考文档) |
| 6 | +- [2. 概述](#2-概述) |
| 7 | +- [3. 构建](#3-构建) |
| 8 | + - [3.1. 安装 GCC 工具链](#31-安装-gcc-工具链) |
| 9 | + - [3.2. 安装依赖](#32-安装依赖) |
| 10 | + - [3.2.1. apt 软件包](#321-apt-软件包) |
| 11 | + - [3.2.2. kconfiglib](#322-kconfiglib) |
| 12 | + - [3.2.3. Env](#323-env) |
| 13 | + - [3.3. 下载 RT-Thread 并更新依赖的软件包](#33-下载-rt-thread-并更新依赖的软件包) |
| 14 | + - [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-上电启动) |
| 21 | + |
| 22 | +<!-- /TOC --> |
| 23 | + |
| 24 | +# 1. 参考文档 |
| 25 | + |
| 26 | +- 【参考 1】Canaan CanMV K230 文档: <https://www.canaan-creative.com/k230_canmv/zh/main/index.html> |
| 27 | +- 【参考 2】K230 RTOS Only SDK 文档: <https://developer.canaan-creative.com/k230_rtos/zh/dev/index.html> |
| 28 | +- 【参考 3】01Studio CanMV-K230 教程: <https://wiki.01studio.cc/docs/canmv_k230/intro/canmv_k230/> |
| 29 | + |
| 30 | +# 2. 概述 |
| 31 | + |
| 32 | +CanMV 是一个由嘉楠科技(Canaan)官方创建和维护的开源项目。该项目将 MicroPython 移植到嘉楠科技边缘计算AI 芯片 K230 上,这就是 CanMV-K230 开发板。该项目旨在创建低成本,可扩展的,使用 Python 驱动的 AI 人工智能模块。 |
| 33 | + |
| 34 | +目前市面上生产的以 CanMV-K230 为原型的开发板有多种型号。本 BSP 支持 01Studio CanMV-K230 AI 开发板(内存 512MB)。 |
| 35 | + |
| 36 | +K230 是一款嘉楠科技(Canaan)出品的 64 位双核带硬件 FPU 和卷积加速器的 RISC-V SoC。 |
| 37 | + |
| 38 | +采用本 BSP 构建生成的 RT-Smart 和官方 SDK (【参考 2】) 配套使用并在大核上运行。 |
| 39 | + |
| 40 | +# 3. 构建 |
| 41 | + |
| 42 | +目前只验证了 Ubuntu/Linux 下构建。 |
| 43 | + |
| 44 | +```shell |
| 45 | +$ lsb_release -a |
| 46 | +No LSB modules are available. |
| 47 | +Distributor ID: Ubuntu |
| 48 | +Description: Ubuntu 22.04.5 LTS |
| 49 | +Release: 22.04 |
| 50 | +Codename: jammy |
6 | 51 | ```
|
7 |
| -source ~/.env/env.sh |
8 |
| -pkgs --update |
| 52 | + |
| 53 | +## 3.1. 安装 GCC 工具链 |
| 54 | + |
| 55 | +下载地址:<https://download.rt-thread.org/rt-smart/riscv64/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu_251248.tar.bz2> |
| 56 | + |
| 57 | +正确解压后(假设下载后解压在 `$USER/toolchain`, 也可以自己设定解压后的目录),导出如下环境变量,建议将这些 export 命令写入 `~/.bashrc`。 |
| 58 | + |
| 59 | +```shell |
| 60 | +export RTT_CC="gcc" |
| 61 | +export RTT_CC_PREFIX="riscv64-unknown-linux-musl-" |
| 62 | +export RTT_EXEC_PATH="$USER/toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin" |
9 | 63 | ```
|
10 | 64 |
|
11 |
| -## 将根文件系统编译进内核 |
| 65 | +## 3.2. 安装依赖 |
12 | 66 |
|
13 |
| -为了方便测试,这里将根文件系统制作成CROMFS格式转换成C代码编译进内核。 |
| 67 | +### 3.2.1. apt 软件包 |
14 | 68 |
|
15 |
| -1. 在 https://github.com/RT-Thread/userapps 页面下载riscv64预编译镜像 |
16 |
| -2. 解压后将其中的ext4.img挂载到一个目录中 |
| 69 | +```shell |
| 70 | +$ sudo apt install -y scons python3-pip |
17 | 71 | ```
|
18 |
| -sudo mount ext4.img dir |
| 72 | + |
| 73 | +### 3.2.2. kconfiglib |
| 74 | + |
| 75 | +因为本 BSP 只在 RT-Thread v5.1.0 以上才支持,所以需要确保本地已经安装过 kconfiglib。 |
| 76 | + |
| 77 | +```shell |
| 78 | +$ pip3 install kconfiglib i https://pypi.tuna.tsinghua.edu.cn/simple |
19 | 79 | ```
|
20 |
| -3. 删除其中一些不必要的文件以减小内核体积 |
| 80 | +采用清华源是为了加快安装速度。 |
| 81 | + |
| 82 | +### 3.2.3. Env |
| 83 | + |
| 84 | +安装 env,具体参考 <https://github.com/RT-Thread/env> |
| 85 | + |
| 86 | +## 3.3. 下载 RT-Thread 并更新依赖的软件包 |
| 87 | + |
| 88 | +假设我们的工作路径是 `$WS` |
| 89 | +```shell |
| 90 | +$ cd $WS |
| 91 | +$ git clone [email protected]:RT-Thread/rt-thread.git |
| 92 | +$ cd rt-thread/bsp/k230 |
| 93 | +$ source ~/.env/env.sh |
| 94 | +$ pkgs --update |
21 | 95 | ```
|
22 |
| -du -ha # 查看文件大小 |
23 |
| -sudo rm -rf dir/www dir/usr/share/fonts dir/tc |
24 | 96 |
|
| 97 | +## 3.4. 构建 |
| 98 | + |
| 99 | +在 `$WS/rt-thread/bsp/k230` 下执行如下命令: |
| 100 | + |
| 101 | +```shell |
| 102 | +$ scons --menuconfig # 默认不需要修改,直接保存即可,保存后可以达到同步最新配置的效果 |
| 103 | +$ scons -j$(nproc) |
25 | 104 | ```
|
26 |
| -4. 生成cromfs文件 |
27 |
| -工具位于 https://github.com/RT-Thread/userapps/tree/main/tools/cromfs |
| 105 | + |
| 106 | +此时在 `$WS/rt-thread/bsp/k230` 下会看到生成了 `rtthread.bin`, 这个就是我们构建出来的内核二进制文件。 |
| 107 | + |
| 108 | + |
| 109 | +# 4. 烧写 |
| 110 | + |
| 111 | +`rtthread.bin` 并不能直接运行。目前我们基于 Canaan 官方的 RTOS-only SDK 制作 SD 卡,并基于 SD 卡启动内核。 |
| 112 | + |
| 113 | +## 4.1. 制作启动 SD 卡 |
| 114 | + |
| 115 | +### 4.1.1. 编译固件 |
| 116 | + |
| 117 | +参考 K230 RTOS Only 用户指南中的 “如何编译固件”:<https://developer.canaan-creative.com/k230_rtos/zh/dev/userguide/how_to_build.html>。 |
| 118 | + |
| 119 | +假设 sdk 安装的路径在 `$WS/rtos_k230` |
| 120 | +注意事项如下: |
| 121 | + |
| 122 | +- 目前用户指南的环境只在 Ubuntu 20.04 LTS (x86_64) 上验证过,*其他 Linux 发行版未经充分测试,可能存在兼容性问题*。为避免和 BSP 构建环境冲突,建议在另外一台机器或者虚拟机中搭建此环境。 |
| 123 | + |
| 124 | +- `rtos_k230` 克隆自 `[email protected]:canmv-k230/manifest.git`(国内用户建议从 gitee 下载,否则速度太慢),本文基于的 commit 是 75662ae。 |
| 125 | + |
| 126 | +- 根据指南步骤操作,在 “4.4 选择目标硬件配置” 中选择 01studio 的 defconfig。 |
| 127 | + |
| 128 | + 和 01studio 相关的配置有两项: |
| 129 | + |
| 130 | + ```shell |
| 131 | + $ make list-def |
| 132 | + 1 [ ] k230_canmv_01studio_defconfig |
| 133 | + ...... |
| 134 | + 10 [*] k230_rtos_01studio_defconfig |
| 135 | + ``` |
| 136 | + |
| 137 | + 其中: |
| 138 | + - 1:01Studio 的带 micropython 的 canMV。 |
| 139 | + - 10: 01Studio 的不带 micropython 的 canMV。 |
| 140 | + |
| 141 | + 为简单起见,这里选择 “ k230_rtos_01studio_defconfig” 。 |
| 142 | + |
| 143 | +最后在 `$WS/rtos_k230/output/k230_rtos_01studio_defconfig/` 下生成 `RtSmart-K230_01Studio_rtsmart_local_nncase_v2.9.0.img` |
| 144 | + |
| 145 | +### 4.1.2. 烧录固件 |
| 146 | + |
| 147 | +参考 K230 RTOS Only 用户指南中的 “如何烧录固件”:<https://developer.canaan-creative.com/k230_rtos/zh/dev/userguide/how_to_flash.html>, 通过 SD 卡烧录。熟悉 Windows 平台的可以使用 balenaEtcher。 |
| 148 | + |
| 149 | +## 4.2. 单独更新 RT-Thread 内核 |
| 150 | + |
| 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 |
28 | 175 | ```
|
29 |
| -sudo ./cromfs-tool-x64 dir crom.img ./ # 将生成的cromfs_data.c放入applications目录 |
| 176 | +然后执行: |
| 177 | + |
| 178 | +```shell |
| 179 | +$ make opensbi |
30 | 180 | ```
|
31 | 181 |
|
32 |
| -## 编译 |
| 182 | +会在 `$WS/rtos_k230/output/k230_rtos_01studio_defconfig/images/opensbi` 下生成一个新的 `opensbi_rtt_system.bin`,这也是我们要烧写的最终文件。 |
33 | 183 |
|
34 |
| -``` |
35 |
| -export RTT_EXEC_PATH=/mnt/e/tools/riscv64gc/bin # 你自己的编译器路径 |
| 184 | +将 SD卡通过 USB 读卡器接入 Ubuntu 机器。假设枚举为 `/dev/sdb`。执行如下命令烧写: |
36 | 185 |
|
37 |
| -scons -j8 |
| 186 | +```shell |
| 187 | +$ sudo dd if=$WS/rtos_k230/output/k230_rtos_01studio_defconfig/images/opensbi/opensbi_rtt_system.bin of=/dev/sdb seek=20480 |
38 | 188 | ```
|
| 189 | + |
| 190 | +# 5. 上电启动 |
| 191 | + |
| 192 | +将 SD 卡插入 01Studo 开发板的 SD 卡槽。 |
| 193 | + |
| 194 | +连接 USB 转 UART 串口线到 “CPU0 调试口”,具体见下图: |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | +上电后串口控制台如下所示: |
| 199 | + |
| 200 | +```shell |
| 201 | +U-Boot SPL 2022.10-00015-gcf105752 (Apr 09 2025 - 22:49:49 +0800) |
| 202 | +lpddr4 2660 |
| 203 | +00000000: PMU Major Msg: End of CA training |
| 204 | +00000000: PMU Major Msg: End of initialization |
| 205 | +00000000: PMU Major Msg: End of read enable training |
| 206 | +00000000: PMU Major Msg: End of fine write leveling |
| 207 | +00000000: PMU Major Msg: End of read dq deskew training |
| 208 | +00000000: PMU Major Msg: End of MPR read delay center optimization |
| 209 | +00000000: PMU Major Msg: End of Write leveling coarse delay |
| 210 | +00000000: PMU Major Msg: End of write delay center optimization |
| 211 | +00000000: PMU Major Msg: End of read delay center optimization |
| 212 | +00000000: PMU Major Msg: End of max read latency training |
| 213 | +00000000: PMU Major Msg: Firmware run has completed |
| 214 | +image: uboot load to 0x1000000, compress=1 src=0x2aaaacff len=0x49e27 |
| 215 | + |
| 216 | + |
| 217 | +U-Boot 2022.10-00015-gcf105752 (Apr 09 2025 - 22:49:49 +0800) |
| 218 | + |
| 219 | +CPU: rv64imafdcvsu |
| 220 | +Model: kendryte k230 canmv 01studio |
| 221 | +DRAM: 1 GiB |
| 222 | +Core: 22 devices, 13 uclasses, devicetree: embed |
| 223 | +MMC: mmc0@91580000: 0 |
| 224 | +Loading Environment from MMC... OK |
| 225 | +In: serial@91400000 |
| 226 | +Out: serial@91400000 |
| 227 | +Err: serial@91400000 |
| 228 | +Net: No ethernet found. |
| 229 | +Hit any key to stop autoboot: 0 |
| 230 | +image: rtt load to 0x0, compress=1 src=0x2aaaad07 len=0x5fb7b |
| 231 | +Jump to big hart |
| 232 | + |
| 233 | +OpenSBI v1.1-2-g1de130d |
| 234 | + |
| 235 | + \ | / |
| 236 | +- RT - Thread Smart Operating System |
| 237 | + / | \ 5.2.1 build Apr 23 2025 16:04:02 |
| 238 | + 2006 - 2024 Copyright by RT-Thread team |
| 239 | +lwIP-2.1.2 initialized! |
| 240 | +[I/sal.skt] Socket Abstraction Layer initialize success. |
| 241 | +[I/drivers.serial] Using /dev/ttyS0 as default console |
| 242 | +CromFS mount failed! |
| 243 | +Press any key to stop init process startup ... 3 |
| 244 | +Press any key to stop init process startup ... 2 |
| 245 | +Press any key to stop init process startup ... 1 |
| 246 | +Starting init ... |
| 247 | +Hello RISC-V |
| 248 | +msh /> |
| 249 | +``` |
0 commit comments