本文档详细介绍如何将LVGL 8.3版本移植到基于AM335x处理器的Linux系统中,包括LVGL库的配置、tslib触摸驱动的移植以及示例应用的编译运行。整个项目使用CMake作为构建系统。
项目使用arm-arago-linux-gnueabi工具链进行交叉编译。在toolchain.cmake中配置如下:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER /opt/am335xt3-cross-devkit/bin/arm-arago-linux-gnueabi-gcc)
set(CMAKE_CXX_COMPILER /opt/am335xt3-cross-devkit/bin/arm-arago-linux-gnueabi-g++)
set(CMAKE_ASM_COMPILER /opt/am335xt3-cross-devkit/bin/arm-arago-linux-gnueabi-as)
在toolchain.cmake中设置了必要的编译标志:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -g -rdynamic -g0 -Wall -Wshadow -Wundef -Wmissing-prototypes -Wall -Wextra -Wno-unused-function -Wno-error=strict-prototypes -Wpointer-arith -fno-strict-aliasing ...")
配置lv_conf.h文件:
配置显示驱动:
主CMakeLists.txt文件配置示例:
cmake_minimum_required(VERSION 3.16)
project(lvgl_project)
# 设置工具链文件
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/toolchain.cmake)
# 添加LVGL库
add_subdirectory(lvgl)
# 添加驱动库
add_subdirectory(lv_drivers)
git clone https://github.com/libts/tslib.git
cd tslib
./autogen.sh
./configure --host=arm-arago-linux-gnueabi
make
make install
export TSLIB_TSDEVICE=/dev/input/touchscreen0
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_PLUGINDIR=/usr/lib/ts
export POINTERCAL_FILE=/etc/pointercal
export TSLIB_CALIBFILE=/etc/pointercal
export TSLIB_CONSOLEDEVICE=none
export TSLIB_FBDEVICE=/dev/fb0
# 查找 pkg-config 模块
set(ENV{PKG_CONFIG_PATH} ${am335xt3_ROOT_PATH}/tslib/lib/pkgconfig)
# 查找 pkg-config 模块
find_package(PkgConfig REQUIRED)
# 查找一个库,例如 tslib
pkg_check_modules(tslib REQUIRED tslib-0.0)
# 包含 tslib 的头文件路径
target_include_directories(helloworld PRIVATE ${tslib_INCLUDE_DIRS})
# 链接 tslib 库
target_link_libraries(helloworld PRIVATE ${tslib_LDFLAGS})
首先,实现tslib的初始化函数:
static struct tsdev *ts;
int tslib_init(void) {
// 打开触摸屏设备
ts = ts_open("/dev/input/touchscreen0", 0);
if (!ts) {
perror("Failed to open touchscreen device");
return -1;
}
// 配置tslib(加载校准参数等)
if (ts_config(ts) < 0) {
perror("Failed to configure tslib");
ts_close(ts);
return -1;
}
return 0;
}
然后,实现触摸数据读取函数:
static bool tslib_read(lv_indev_drv_t *drv, lv_indev_data_t *data) {
struct ts_sample sample;
if (ts_read(ts, &sample, 1) == 1) {
data->point.x = sample.x;
data->point.y = sample.y;
data->state = (sample.pressure > 0) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
return false; // 没有更多数据需要读取
}
return false;
}
最后,注册触摸输入设备驱动:
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = tslib_read; // 使用tslib_read作为回调函数
lv_indev_drv_register(&indev_drv);
完整的HelloWorld示例源码可以在以下地址获取:
https://gitcode.com/am335xt3/lvgl/tree/main/apps/helloworld
apps/helloworld/
├── CMakeLists.txt
├── src/
│ ├── main.c
│ ├── dump.c
│ └── mouse_cursor_icon.c
apps/helloworld/CMakeLists.txt配置示例:
add_executable(helloworld
src/main.c
src/dump.c
src/mouse_cursor_icon.c
)
target_link_libraries(helloworld
lvgl
lv_drivers
ts
)
mkdir build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake
make
scp apps/helloworld/helloworld root@target:/usr/bin/
./helloworld
显示问题:
触摸问题:
显示优化:
内存优化: