Linux 开发
SDK 概览
LY3568-SOM 提供两套 Linux 构建系统:
| SDK | 特点 | 适用场景 |
|---|---|---|
| Buildroot | 轻量级,全自动构建,编译快 | 嵌入式产品(无桌面环境需求) |
| Yocto | 模块化,层级化,可定制性强 | 复杂系统,需要包管理 |
| Debian | 开箱即用,apt 包管理 | 原型验证,快速开发 |
Buildroot SDK
环境准备
推荐使用 Ubuntu 20.04 / 22.04(64位),安装必要依赖:
sudo apt update
sudo apt install -y build-essential gcc g++ gawk bison flex \
texinfo libncurses5-dev libssl-dev bc git python3 python3-pip \
swig libtool rsync cpio unzip wget file
获取 SDK
# 解压 SDK 包
tar -xvf LY3568-SOM_Buildroot_SDK_v1.0.tar.gz
cd ly3568-buildroot
# 或从 Git 仓库获取(如有)
git clone https://github.com/lyai/ly3568-buildroot.git
cd ly3568-buildroot
git submodule update --init
配置与编译
# 加载默认配置
make ly3568_som_defconfig
# 自定义配置(可选)
make menuconfig
# 编译(首次编译约 30-60 分钟)
make -j$(nproc)
# 输出文件位于 output/images/
# - sdcard.img: SD 卡镜像
# - rootfs.ext2: 根文件系统
# - uImage / Image: 内核镜像
# - rk3568-ly3568-som.dtb: 设备树
自定义 RootFS
# 添加自定义软件包
make menuconfig
# 进入 Target packages → 选择需要的包
# 添加自定义文件
# 创建 overlay 目录
mkdir -p board/lyai/ly3568/overlay/etc/
echo "LY3568-SOM" > board/lyai/ly3568/overlay/etc/hostname
# 重新编译
make -j$(nproc)
Yocto SDK
环境准备
# 安装 Yocto 依赖
sudo apt install -y gawk wget git diffstat unzip texinfo gcc \
build-essential chrpath socat cpio python3 python3-pip \
python3-pexpect xz-utils debianutils iputils-ping \
python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev \
pylint xterm python3-subunit mesa-common-dev zstd liblz4-tool
获取与构建
# 解压或克隆 Yocto 环境
tar -xvf LY3568-SOM_Yocto_SDK_v1.0.tar.gz
cd ly3568-yocto
# 初始化构建环境
source sources/poky/oe-init-build-env build
# 设置目标机
# 在 conf/local.conf 中确认 MACHINE = "ly3568-som"
# 构建最小镜像
bitbake core-image-minimal
# 构建完整镜像(含桌面)
bitbake core-image-sato
交叉编译
工具链说明
交叉编译工具链用于在 x86 主机上编译 ARM64 目标程序。
Buildroot 工具链
# 工具链路径(Buildroot 编译后自动生成)
export PATH=$PATH:$(pwd)/output/host/bin
export CROSS_COMPILE=aarch64-linux-
# 测试工具链
aarch64-linux-gcc --version
示例:Hello World
# hello.c
cat > hello.c << 'EOF'
#include <stdio.h>
int main() {
printf("Hello from LY3568-SOM!\n");
return 0;
}
EOF
# 交叉编译
aarch64-linux-gcc -o hello hello.c
# 传输到开发板
scp hello root@192.168.1.100:/tmp/
# 或通过 U 盘 / SD 卡拷贝
# 开发板上执行
ssh root@192.168.1.100
chmod +x /tmp/hello
/tmp/hello
GPIO 编程
sysfs 方式(简单)
# GPIO 编号 = base + offset(详见面板引脚定义)
# 以 GPIO3_A4 为例: bank=3, pin=4 → GPIO 编号 = 3*32+4+0 = 100
echo 100 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio100/direction
echo 1 > /sys/class/gpio/gpio100/value # 输出高电平
echo 0 > /sys/class/gpio/gpio100/value # 输出低电平
echo 100 > /sys/class/gpio/unexport
libgpiod 方式(推荐)
#include <gpiod.h>
#include <stdio.h>
int main() {
struct gpiod_chip *chip;
struct gpiod_line *line;
chip = gpiod_chip_open("/dev/gpiochip3");
line = gpiod_chip_get_line(chip, 4); // GPIO3_A4
gpiod_line_request_output(line, "example", 0);
gpiod_line_set_value(line, 1); // 高电平
gpiod_line_release(line);
gpiod_chip_close(chip);
return 0;
}
编译:
aarch64-linux-gcc -o gpio_test gpio_test.c -lgpiod
I2C 通信
用户空间工具
# 检测 I2C 总线上的设备
i2cdetect -y 0 # I2C0
i2cdetect -y 1 # I2C1
# 读写 I2C 寄存器
i2cget -y 1 0x50 0x00 # 从地址 0x50 读寄存器 0x00
i2cset -y 1 0x50 0x00 0x55 # 向地址 0x50 写寄存器 0x00 = 0x55
C 语言 I2C 编程示例
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int fd = open("/dev/i2c-1", O_RDWR);
ioctl(fd, I2C_SLAVE, 0x50);
char reg = 0x00;
write(fd, ®, 1);
char data;
read(fd, &data, 1);
printf("Device 0x50 Register 0x00 = 0x%02x\n", data);
close(fd);
return 0;
}
SPI 通信
用户空间工具
# 测试 SPI 收发
spidev_test -D /dev/spidev0.0 -s 10000000
C 语言 SPI 编程示例
#include <linux/spi/spidev.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
int main() {
int fd = open("/dev/spidev0.0", O_RDWR);
uint8_t tx[] = {0xAA, 0xBB, 0xCC};
uint8_t rx[3];
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = 3,
.speed_hz = 10000000,
.bits_per_word = 8,
};
ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
for (int i = 0; i < 3; i++)
printf("RX[%d] = 0x%02x\n", i, rx[i]);
close(fd);
return 0;
}
开发板网络连接
# Ethernet 配置(DHCP)
udhcpc -i eth0
# 静态 IP
ifconfig eth0 192.168.1.100 netmask 255.255.255.0
route add default gw 192.168.1.1
# WiFi 连接
wpa_passphrase "SSID" "password" > /etc/wpa_supplicant.conf
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf
udhcpc -i wlan0
内核模块开发
# 进入内核源码目录
cd ly3568-buildroot/output/build/linux-*/
# 配置内核
make ARCH=arm64 CROSS_COMPILE=aarch64-linux- menuconfig
# 编译内核
make ARCH=arm64 CROSS_COMPILE=aarch64-linux- -j$(nproc)
# 编译模块
make ARCH=arm64 CROSS_COMPILE=aarch64-linux- modules