嘉楠堪智 CanMV K230 进行 C 语言程序开发

本文记录学习、使用 K230 SDK 进行 C 语言程序开发的一些关键步骤,编写程序源代码,如何编译运行在大核和小核的程序,如何使用 SCons 进行编译。

一、编写代码

在 ubuntu 上创建一个 C 文件 hello.c 并加入如下代码:

cpp 复制代码
#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.c
k230_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
python 复制代码
# RT-Thread building script for component

from 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
python 复制代码
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

相关推荐
@小码农11 小时前
202512 电子学会 Scratch图形化编程等级考试三级真题(附答案)
服务器·开发语言·数据结构·数据库·算法
Cosmoshhhyyy11 小时前
《Effective Java》解读第29条:优先考虑泛型
java·开发语言
一路往蓝-Anbo11 小时前
C语言从句柄到对象 (六) —— 继承与 HAL:父类指针访问子类数据
c语言·开发语言·stm32·嵌入式硬件·物联网
北冥有一鲲11 小时前
A2A协议与LangChain.js实战:构建微型软件工厂
开发语言·javascript·langchain
Chen不旧11 小时前
java基于reentrantlock/condition/queue实现阻塞队列
java·开发语言·signal·reentrantlock·await·condition
nuo53420211 小时前
Nuo-Math-Compiler
c语言·编辑器
laplace012311 小时前
Part 3:模型调用、记忆管理与工具调用流程(LangChain 1.0)笔记(Markdown)
开发语言·人工智能·笔记·python·langchain·prompt
风送雨12 小时前
八周Python强化计划(七)
开发语言·python
ππ很开心66612 小时前
DAY 32 函数专题2:装饰器
开发语言·python
Knight_AL12 小时前
阿里《Java 开发手册》下的对象构建与赋值规范实践
java·开发语言