本文记录学习、使用 K230 SDK 进行 C 语言程序开发的一些关键步骤,编写程序源代码,如何编译运行在大核和小核的程序,如何使用 SCons 进行编译。
一、编写代码
在 ubuntu 上创建一个 C 文件 hello.c 并加入如下代码:
#include <stdio.h>
int main (void)
{printf("hello world\n");return 0;
}
将 hello.c 放到与 k230_sdk 同一级目录下:
canaan@develop:~/work$ ls hello.c k230_sdk
二、编译:
编译小核程序
编译适用于小核 linux 的可执行程序:
k230_sdk/toolchain/Xuantie-900-gcc-linux-5.10.4-glibc-x86_64-V2.6.0/bin/riscv64-unknown-linux-gnu-gcc hello.c -o hello
编译大核程序
编译适用于大核 rt-smart 的可执行程序:
k230_sdk/toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/riscv64-unknown-linux-musl-gcc -o hello.o -c -mcmodel=medany -march=rv64imafdcv -mabi=lp64d hello.ck230_sdk/toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/riscv64-unknown-linux-musl-gcc -o hello.elf -mcmodel=medany -march=rv64imafdcv -mabi=lp64d -T k230_sdk/src/big/mpp/userapps/sample/linker_scripts/riscv64/link.lds -Lk230_sdk/src/big/rt-smart/userapps/sdk/rt-thread/lib -Wl,--whole-archive -lrtthread -Wl,--no-whole-archive -n --static hello.o -Lk230_sdk/src/big/rt-smart/userapps/sdk/lib/risc-v/rv64 -Lk230_sdk/src/big/rt-smart/userapps/sdk/rt-thread/lib/risc-v/rv64 -Wl,--start-group -lrtthread -Wl,--end-group
三、运行程序
将编译好的 hello 以及 hello.elf 拷贝到 sd 卡的 vfat 分区内( sd 卡烧写完镜像后可以在 pc 端看到一个可用的盘符),或通过其他方式(参考 sdk 使用说明文档)将可执行程序拷贝到小核的 /sharefs 目录下。
-
开发板启动后,在小核端运行测试程序,小核启动后输入
root
进入控制台
Welcome to Buildroot canaan login: root [root@canaan ~ ]#cd /sharefs [root@canaan /sharefs ]#./hello hello world
-
在大核端运行测试程序
msh /sharefs>hello.elf hello world
四、SCons 编译大核程序
上面例子可以看到,用 musl-gcc 直接编译大核程序的话,编译参数是比较多的,对于初学者来说很不方便,也不太好理解,官方推荐使用 SCons 来编译。
到 k230_sdk/src/big/rt-smart/userapps
目录下创建一个文件夹,命名为 hello
cd k230_sdk/src/big/rt-smart/userapps mkdir hello cd hello
创建以下三个文件:
- hello.c (同上,略)
- SConscript
# RT-Thread building script for componentfrom building import *cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]CPPDEFINES = ['HAVE_CCONFIG_H',
]
group = DefineGroup('hello', src, depend=[''], CPPPATH=CPPPATH, CPPDEFINES=CPPDEFINES)Return('group')
- SConstruct
import os
import sys# add building.py path
sys.path = sys.path + [os.path.join('..','..','tools')]
from building import *BuildApplication('hello', 'SConscript', usr_root = '../')
回到 k230_sdk/src/big/rt-smart/
目录,配置环境变量:
canaan@develop:~/k230_sdk/src/big/rt-smart$ source smart-env.sh riscv64 Arch => riscv64 CC => gcc PREFIX => riscv64-unknown-linux-musl- EXEC_PATH => /home/canaan/k230_sdk/src/big/rt-smart/../../../toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin
进入 k230_sdk/src/big/rt-smart/userapps
目录,编译程序:
canaan@develop:~/k230_sdk/src/big/rt-smart/userapps$ scons --directory=hello scons: Entering directory `/home/canaan/k230_sdk/src/big/rt-smart/userapps/hello' scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... scons: building associated VariantDir targets: build/hello CC build/hello/hello.o LINK hello.elf /home/canaan/k230_sdk/toolchain/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/../lib/gcc/riscv64-unknown-linux-musl/12.0.1/../../../../riscv64-unknown-linux-musl/bin/ld: warning: hello.elf has a LOAD segment with RWX permissions scons: done building targets.
编译好的程序在 hello 文件夹下:
canaan@develop:~/k230_sdk/src/big/rt-smart/userapps$ ls hello/ build cconfig.h hello.c hello.elf SConscript SConstruct
之后即可将 hello.elf 拷贝到小核 linux 上,然后大核 rt-smart 通过 /sharefs 即可运行该程序。
老徐,2024/5/4