[技术讨论] 全志D1s开发板软件入门之Hello World演示

[复制链接]
 楼主| 神棍地海棠 发表于 2024-3-6 11:31 | 显示全部楼层 |阅读模式
本章节将讲解如何使用电脑(上位机)交叉编译一个打印 hello word 的小应用,并将其push到开发板(下位机)上运行起来,打印出 hello word。这是嵌入式应用开发的最基础步骤。在此之前,你需要具备嵌入式编程的基本知识,如下。
前要知识储备
  • 掌握Linux基本命令行指令及工具
  • 如 git make 以及文件操作等
  • 掌握Linux 简单编程
  • 基本的C语言编程
  • 掌握嵌入式基本知识
  • 嵌入式设备组成
  • 嵌入式外设连接方式
    • 如UART USB 网口 等基本接口
  • 了解开发板开发载资源与对应接口分布
编译工具链准备
在Tina SDK中带有完整的编译工具链,在如下路径:
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">tina/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702
如果您只是想先试玩一下,还没下载完整源码,可以单独下载编译工具链:
[color=var(--md-typeset-a-color)]riscv64-glibc-gcc-thead_20200702下载
下载后放到Linux环境中解压:
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">tar -zxvf riscv64-glibc-gcc-thead_20200702.tar.gz
解压后可以看到已经解压出来的交叉编译链的文件夹:
*注意:如果是Windows环境下的Ubuntu虚拟机,不要放在共享文件夹下解压,否则会权限报错,要复制到Ubuntu非共享目录下再解压。
*同时可以到平头哥社区下载最新/各版本C900系列编译工具链:
[color=var(--md-typeset-a-color)]https://occ.t-head.cn/community/download?id=3803060952769826816
代码编写创建文件
如果只是用编译工具链单独尝试编译一个小demo,也可以在Ubuntu任意目录下创建文件,比如就放到编译工具链路径下。
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;"># 创建hello_word.c文件touch hello_word.c编写 Hello Word 代码
编写打印 Hello Word 代码的小demo,在 hello_word.c 中写入:
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">#include <stdio.h>int main(int argc, char const *argv[]){    printf("Hello NeZha\n");    return 0;}交叉编译
交叉编译是指在我们的PC机上编译可以在开发板上运行的可执行程序文件,因为是在上位机上编译,然后在不同体系结构的开发板上跑,所以叫交叉编译。
编译命令:
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702/bin/riscv64-unknown-linux-gnu-gcc -o hello_word hello_word.c
编译完成后会在当前文件夹生成名为hello_word文件,这个就是可以在开发板上运行的可执行文件。
[color=var(--md-typeset-a-color)]
下载 Hello Word 文件
编译完成后需要将编译好的hello_word文件下载到开发板上运行。
传入文件可使用的方法多种多样,仁者见仁智者见智。可用的方法简传单列举:
  • ADB工具
  • nfs挂载文件系统
  • 使用SD卡挂载
在这里推荐使用我们的ADB工具来进行传输,不需要增加多余的连接,仅仅只需要一根USB线即可。
ADB
ADB的使用及介绍链接就贴在这里了:[color=var(--md-typeset-a-color)]ADB使用上手连接
确认设备连接正常后:
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">adb push hello_word ./.
ADB为Windows下工具,所以使用cmd来执行。
确保ADB已经添加进环境变量中
*注意:Windows 下的路径为反斜线 Linux中为斜线
此时在Tina跟文件系统中的/root目录下就有hello_word文件。
赋予它可执行权限
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">chmod +x hello_word./hello_word
执行结果:
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">root@TinaLinux:~# lshello_word  mainroot@TinaLinux:~# chmod +x hello_wordroot@TinaLinux:~# ./hello_wordHello NeZha
如果你看到 "Hello NeZha" 这行打印,那么恭喜你,你已经开发出在哪吒上的第一个应用了!
进阶:使用makefile编写一个工程
根据Tina开发惯例,建议开发者的应用工程放在package下,在package中创建test文件夹,在test文件夹中创建hello_word.c文件。
为了紧密结合嵌入式开发,此处提供使用Makefile 文件来进行编译Hello word 方法:
在源码目录创建Makefile文件:
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">touch Makefile
编写Makefile:
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">#设置编译链路径及工具CTOOL := riscv64-unknown-linux-gnu-CCL := /home/kunyao/workspace/d_tina_d1_open_v1.0/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702CC := ${CCL}/bin/${CTOOL}gcc#设置编译规则hello_word:hello_word.c    ${CC} -o hello_word hello_word.c#清理规则clean:    rm hello_word
保存后在终端make即可生成hello_word文件,用如上ADB方法将其传入开发板即可。
*可选:将交叉编译链设置为当前环境变量
code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">export PATH=**/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_
您需要登录后才可以回帖 登录 | 注册

本版积分规则

284

主题

292

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

284

主题

292

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部