前言
开发板体验第一步少不了HelloWorld一下。这里先进行C开发环境搭建,然后编写一个简单的HelloWorld,LED点灯,按键输入程序测试。这里推荐使用WIN11+WSL2+UBUNTU的方式搭建开发环境,而不是采用基于VMWARE等虚拟机的方式。下载虚拟机,安装系统等太复杂,如果仅仅是Linux C开发的话基于WSL,安装编译器即可,更便捷。
过程
WIN11下安装WSL2,并安装UBUNTU系统,该过程网上有很多资料就不再赘述。我们基于该环境搭建Linux C开发环境。
准备
接上J3的TTL,调试串口
串口终端登录,这里使用crt
115200-8-N-1
用户名为root 无需密码
可以通过rz和sz指令进行文件的导入导出
sz key导出到PC
导出文件的路径位于
安装编译器
打开WSL的ubuntu终端
sudo apt-get install gcc-aarch64-linux-gnu
aarch64-linux-gnu-gcc -v查看版本
lhj@lhj:~$ aarch64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/aarch64-linux-gnu/9/lto-wrapper
Target: aarch64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.1' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --without-target-system-zlib --enable-libpth-m2 --enable-multiarch --enable-fix-cortex-a53-843419 --disable-werror --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=aarch64-linux-gnu --program-prefix=aarch64-linux-gnu- --includedir=/usr/aarch64-linux-gnu/include
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
lhj@lhj:~$
HelloWorld
vi helloworld.c 编辑源文件
输入i进入插入模式
输入以下内容
#include <stdio.h>
int main(void)
{
printf("Hello World\r\n");
}
按ESC
按:进入命令模式
输入:wq保存
编译aarch64-linux-gnu-gcc helloworld.c -o helloworld生成可执行文件helloworld
将源文件导出到windows下 cp helloworld /mnt/d
然后windows下通过串口或者网口ssh的rz指令导入文件helloworld
添加可执行权限chmod +x helloworld
运行 ./helloworld
LED
从原理图可以看到,提供了一个用户LED
Led设备位于/sys/class/leds/ 下,有一个user1用户LED,蓝色的,默认闪烁。
root@myd-jx8mma7:~# ls /sys/class/leds/
cpu input3::capslock input3::numlock input3::scrolllock mmc0:: mmc1:: mmc2:: pwr_pg pwr_stby user1
root@myd-jx8mma7:~#
用户要使用可先取消自动闪烁
echo none > /sys/class/leds/user1/trigger
查看LED状态
cat /sys/class/leds/user1/brightness
用户控制
echo 1 > /sys/class/leds/user1/brightness
echo 0 > /sys/class/leds/user1/brightness
测试代码
新建led.c文件代码如下
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
printf("Hello World\r\n");
while(1)
{
system("echo 1 > /sys/class/leds/user1/brightness");
sleep(1);
system("echo 0 > /sys/class/leds/user1/brightness");
sleep(1);
}
}
编译aarch64-linux-gnu-gcc ledd.c -o led生成可执行文件led
将源文件导出到windows下 cp led /mnt/d
然后windows下通过串口或者网口ssh的rz指令导入文件led
添加可执行权限chmod +x led
运行 ./led
可以看到LED1秒间隔闪烁
按键
原理图可以看到,有两个用户按键OnOFF和User
其中OnFF是长按开关机按键
所以只有User可以使用
设备位于/dev/input/
root@myd-jx8mma7:~# ls /dev/input/
by-id by-path event0 event1 event2 event3 event4 event5 event6
root@myd-jx8mma7:~#
测试
输入evtest 可以看到1为gpio-keys
2为bd718xx-pwrkey
再选择1
再按User按键,可以看到打印信息
root@myd-jx8mma7:~# evtest
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0: 30370000.snvs:snvs-powerkey
/dev/input/event1: gpio-keys
/dev/input/event2: bd718xx-pwrkey
/dev/input/event3: Gtech Lenovo Eos wireless KM combo
/dev/input/event4: Gtech Lenovo Eos wireless KM combo Mouse
/dev/input/event5: Gtech Lenovo Eos wireless KM combo Consumer Control
/dev/input/event6: Gtech Lenovo Eos wireless KM combo
Select the device event number [0-6]: 1
Input driver version is 1.0.1
Input device ID: bus 0x19 vendor 0x1 product 0x1 version 0x100
Input device name: "gpio-keys"
Supported events:
Event type 0 (EV_SYN)
Event type 1 (EV_KEY)
Event code 2 (KEY_1)
Properties:
Testing ... (interrupt to exit)
[ 35.806595] WLAN_EN: disabling
Event: time 3029530572.3029530572, type 1 (EV_KEY), code 2 (KEY_1), value 1
Event: time 3029530572.3029530572, -------------- SYN_REPORT ------------
Event: time 3029530573.3029530573, type 1 (EV_KEY), code 2 (KEY_1), value 0
Event: time 3029530573.3029530573, -------------- SYN_REPORT ------------
新建key.c文件代码如下
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int s_keys_fd = -1;
uint32_t s_keys_state = 0;
void* key_poll(void* arg)
{
char ret[2];
struct input_event t;
s_keys_fd = open((char*)arg, O_RDONLY);
if(s_keys_fd <= 0)
{
printf("open %s device error!\n",(char*)arg);
return 0;
}
while(1)
{
if(read(s_keys_fd, &t, sizeof(t)) == sizeof(t))
{
if(t.type==EV_KEY)
{
if(t.value==0 || t.value==1)
{
printf("key %d %s\n", t.code, (t.value) ? "Pressed" : "Released");
//if(t.code == KEY_ESC)
// break;
}
}
else
{
///printf("type %d code %d value %d\n", t.type, t.code, t.value);
}
}
}
return 0;
}
void key_init(void* arg)
{
pthread_t id;
/* 创建函数线程,并且指定函数线程要执行的函数 */
int res = pthread_create(&id,NULL,key_poll,arg);
assert(res == 0);
while(1)
{
}
exit(0);
}
int key_getstate(int key)
{
}
int main(int argc, char* argv[])
{
key_init(argv[1]);
while(1);
}
编译aarch64-linux-gnu-gcc key.c -o key -lpthread生成可执行文件key
将源文件导出到windows下 cp key /mnt/d
然后windows下通过串口或者网口ssh的rz指令导入文件key
添加可执行权限chmod +x key
运行 ./key /dev/input/event1
按User按键打印如下
总结
以上基于WIN11+WSL2+UBUNTU快速搭建了C开发环境,以一个Helloworld和LED测试,按键测试的例子进行演示。相对于使用虚拟机安装UBUNTU的方式搭建开发环境,更推荐使用该方式,更快捷简单。