嘉楠堪智 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

相关推荐
uppp»17 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
楼台的春风3 小时前
【GPIO详解及实践示例】
c语言·stm32·单片机·嵌入式硬件·mcu·物联网·嵌入式
ll7788115 小时前
LeetCode每日精进:20.有效的括号
c语言·开发语言·算法·leetcode·职场和发展
Jackson@ML7 小时前
Python数据可视化简介
开发语言·python·数据可视化
赵琳琅7 小时前
Java语言的云计算
开发语言·后端·golang
lly2024067 小时前
jQuery 杂项方法
开发语言
赵琳琅7 小时前
MDX语言的安全开发
开发语言·后端·golang
coding_rui7 小时前
链表(C语言版)
c语言·数据结构·链表
开开又心心的学嵌入式7 小时前
C语言——指针进阶应用
c语言·开发语言