LVGL on PYNQ-Z2 Episode 2: Compiling and Running an LVGL Application
Hello! Welcome back to Episode 2 of the "LVGL on PYNQ-Z2" series.
In Episode 1, we successfully prepared an SD Card Image with Petalinux and the Framebuffer driver for our SPI LCD (ILI9486). In this episode, we will reach the most fun part, which is bringing the LVGL graphics library to compile and run on our PYNQ-Z2 board to create a beautiful UI on the screen!
We will use the SDK obtained from Petalinux to cross-compile the LVGL library and the example application. Then, we will test it on the prepared PYNQ-Z2 board.
1. Preparing the Environment for Cross-Compile
First, we need to install the tools used to compile programs for the PYNQ-Z2 board on our computer.
1.1 Installing Petalinux SDK
The SDK (Software Development Kit) is a set of tools that includes all the necessary cross-compilers and libraries. It allows us to compile programs on our computer (Host) and get a binary file that can run on the PYNQ-Z2 board (Target), which uses the ARM architecture.
This SDK is generated from the Petalinux project in Episode 1 using the petalinux-build --sdk command. Let's take the resulting sdk.sh file and install it on our Host machine:
hong@ubuntu:~/pynq_z2/images/linux$ ./sdk.sh
During installation, the installer will ask for the target directory for the SDK:
PetaLinux SDK installer version 2019.2
======================================
Enter target directory for SDK (default: /opt/petalinux/2019.2): /opt/petalinux/2019.2
You are about to install the SDK to "/opt/petalinux/2019.2". Proceed[Y/n]? y
[sudo] password for hong:
Extracting SDK................................................done
Setting it up...done
SDK has been successfully set up and is ready to be used.
Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
$ . /opt/petalinux/2019.2/environment-setup-cortexa9t2hf-neon-xilinx-linux-gnueabi
1.2 Setting up the SDK Environment
Every time a new Terminal is opened to cross-compile, we need to run a script to set up various environment variables (such as PATH, CC, SYSROOT) to point to the correct toolchain first.
$ source /opt/petalinux/2019.2/environment-setup-cortexa9t2hf-neon-xilinx-linux-gnueabi
1.3 Testing the Cross-Compiler
To ensure our SDK is working correctly, we will try creating a simple test.c file and compiling it.
// test.c
#include <stdio.h>
int main() {
printf("Hello, ARM world!\n");
return 0;
}
Then use the cross-compiler available in the environment ($CC) to compile it, and use the file command to verify the file type obtained:
hong@ubuntu:~/Desktop$ $CC -O2 -o test_arm test.c
hong@ubuntu:~/Desktop$ file test_arm
test_arm: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, ...
If the result shows it as an ARM file, it means our cross-compiler is ready to use!
2. Compiling LVGL
Now that we have the tools ready, let's move on to compiling LVGL.
2.1 Downloading Source Code
We will use the lv_port_linux project, which is the official port of LVGL specifically for use on Linux. In this article, we will use version release/v8.2.
$ git clone https://github.com/lvgl/lv_port_linux.git -b release/v8.2
$ cd lv_port_linux-release-v8.2
2.2 Porting LVGL for our Framebuffer
The downloaded code is just a starting point. We need to modify some parts to make LVGL work correctly with the Framebuffer Driver (/dev/fb0) that we created in Episode 1.
2.2.1 lv_conf.h (LVGL Configuration File)
Create an lv_conf.h file in the root of the project to set the basic configuration of LVGL to match our hardware.
#ifndef LV_CONF_H
#define LV_CONF_H
#include <stdint.h>
/*====================
COLOR SETTINGS
====================*/
/*Color depth: 1, 8, 16, 24, 32*/
#define LV_COLOR_DEPTH 16
#define LV_COLOR_16_SWAP 0
/*=========================
Memory manager
=========================*/
#define LV_MEM_CUSTOM 0
#if LV_MEM_CUSTOM == 0
# define LV_MEM_SIZE (32U * 1024U)
#endif
/*====================
DISPLAY SETTINGS
====================*/
#define LV_HOR_RES_MAX 320
#define LV_VER_RES_MAX 480
/*=======================
* FEATURE CONFIGURATION
*=======================*/
#define LV_USE_LOG 1
#if LV_USE_LOG
# define LV_LOG_LEVEL LV_LOG_LEVEL_INFO
#endif
#endif
LV_COLOR_DEPTH 16: Set the color depth to 16-bit (RGB565) to match what our driver and screen support.LV_HOR_RES_MAX 320,LV_VER_RES_MAX 480: Set the resolution to match our ILI9486 screen.
2.2.2 lv_drivers/display/fbdev.c (LVGL Framebuffer Driver)
This is the core of porting. We need to modify the fbdev_flush function to work with the driver we wrote.
Modify the lv_drivers/display/fbdev.c file as follows:
/**
* @file fbdev.c
*/
#include "fbdev.h"
#if USE_FBDEV || USE_BSD_FBDEV
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
static struct fb_var_screeninfo vinfo;
static struct fb_fix_screeninfo finfo;
static char *fbp = 0;
static long int screensize = 0;
static int fbfd = 0;
void fbdev_init(void)
{
fbfd = open("/dev/fb0", O_RDWR);
if (fbfd < 0) {
perror("fbdev open failed");
return;
}
ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo);
ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo);
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
fbp = mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if (fbp == MAP_FAILED) {
perror("mmap failed");
fbp = NULL;
close(fbfd);
return;
}
}
void fbdev_exit(void)
{
munmap(fbp, screensize);
close(fbfd);
}
void fbdev_flush(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_p)
{
if (!fbp) {
lv_disp_flush_ready(drv);
return;
}
int32_t x, y;
int32_t width = lv_area_get_width(area);
int32_t height = lv_area_get_height(area);
uint32_t fb_line_bytes = finfo.line_length;
uint32_t src_line_bytes = width * (vinfo.bits_per_pixel / 8);
uint8_t *dst_ptr = (uint8_t *)fbp;
uint8_t *src_ptr = (uint8_t *)color_p;
for (y = 0; y < height; y++) {
// คำนวณตำแหน่ง Destination ใน Framebuffer
uint8_t *dst = dst_ptr + ((area->y1 + y) * fb_line_bytes) + (area->x1 * (vinfo.bits_per_pixel / 8));
// คัดลอกข้อมูล 1 บรรทัด
memcpy(dst, src_ptr, src_line_bytes);
// เลื่อน Source pointer ไปยังบรรทัดถัดไป
src_ptr += src_line_bytes;
}
/*
* เราไม่ต้องทำอะไรเพิ่มเติมตรงนี้!
* Kernel Deferred IO จะตรวจจับการเปลี่ยนแปลงใน memory (dirty page)
* แล้วเรียก ili9486_deferred_io() ที่อยู่ในไดรเวอร์ของเราให้เอง
* เพื่อทำการ flush ข้อมูลไปยังหน้าจอ
*/
lv_disp_flush_ready(drv);
}
#endif
2.2.3 Makefile
Create a Makefile to define the compile process for the entire project, ensuring that the CC and SYSROOT values are correct as obtained from the environment source script.
# Makefile
TARGET = demo_lvgl
# Cross-compiler toolchain settings, will be inherited from the environment
# CC ?= gcc
# SYSROOT ?=
# Default CFLAGS for Zynq-7000 (Cortex-A9)
CFLAGS += -march=armv7-a -mthumb -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 \
--sysroot=$(SYSROOT) \
-O3 -g -Wall -Wextra \
-I. \
-D LV_LVGL_H_INCLUDE_SIMPLE
LDFLAGS += --sysroot=$(SYSROOT) -lpthread -lm
# Collect source files
LVGL_SRC_DIR = ./lvgl
LV_DRIVERS_DIR = ./lv_drivers
# Include all LVGL source files
LVGL_SRCS = $(shell find $(LVGL_SRC_DIR)/src -name "*.c")
# Include framebuffer and input device drivers for linux
DRIVER_SRCS = $(LV_DRIVERS_DIR)/display/fbdev.c \
$(LV_DRIVERS_DIR)/indev/evdev.c
# Main application source
MAIN_SRC = main.c
SRCS = $(LVGL_SRCS) $(DRIVER_SRCS) $(MAIN_SRC)
OBJS = $(SRCS:.c=.o)
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) $(LDFLAGS) $(OBJS) -o $@
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(TARGET) $(OBJS)
2.2.4 main.c (Example Application)
Finally, there's main.c, which is our main program. We will use sample code that creates a full-screen rainbow gradient animation to test that everything works correctly.
// main.c
#include "lvgl/lvgl.h"
#include "lv_drivers/display/fbdev.h"
#include <unistd.h>
#include <stdio.h>
#define SCR_W 320
#define SCR_H 480
static void anim_timer_cb(lv_timer_t *timer)
{
static uint32_t hue = 0;
lv_obj_t *screen_obj = (lv_obj_t *)timer->user_data;
hue = (hue + 2) % 360;
lv_color_t c = lv_color_hsv_to_rgb(hue, 100, 100);
lv_obj_set_style_bg_color(screen_obj, c, LV_PART_MAIN);
}
int main(void)
{
printf("[main] LVGL Full-screen Animation Demo\n");
lv_init();
fbdev_init();
static lv_color_t buf1[SCR_W * 100];
static lv_disp_draw_buf_t draw_buf;
lv_disp_draw_buf_init(&draw_buf, buf1, NULL, SCR_W * 100);
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.draw_buf = &draw_buf;
disp_drv.flush_cb = fbdev_flush;
disp_drv.hor_res = SCR_W;
disp_drv.ver_res = SCR_H;
lv_disp_drv_register(&disp_drv);
lv_obj_t *screen_obj = lv_obj_create(lv_scr_act());
lv_obj_set_size(screen_obj, SCR_W, SCR_H);
lv_obj_center(screen_obj);
lv_obj_clear_flag(screen_obj, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_style_border_width(screen_obj, 0, 0);
lv_timer_create(anim_timer_cb, 20, screen_obj); /* ~50 FPS */
printf("[main] Animation running...\n");
while (1) {
lv_tick_inc(5);
lv_timer_handler();
usleep(5000);
}
return 0;
}
2.3 Starting the Compilation
When all the files are prepared, run the make command in the Terminal where we have sourced the environment script.
$ source /opt/petalinux/2019.2/environment-setup-cortexa9t2hf-neon-xilinx-linux-gnueabi
$ make
If there are no errors, we will get the demo_lvgl file, which is an ARM executable.
$ file demo_lvgl
demo_lvgl: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, ...
3. Testing on the PYNQ-Z2 Board
3.1 Transferring the File to the Board
Transfer the compiled demo_lvgl file to the PYNQ-Z2 board. The easiest way is to use scp over the network (requires knowing the board's IP Address).
$ scp demo_lvgl root@<PYNQ-Z2_IP_ADDRESS>:~/
3.2 Running the Application
Connect to the PYNQ-Z2 board via ssh.
$ ssh root@<PYNQ-Z2_IP_ADDRESS>
Then, follow these steps on the board:
- Load the driver: Load the Kernel Module we created in Episode 1.
root@pynq_z2:~# cd /lib/modules/4.19.0-xilinx/extra/ root@pynq_z2:/lib/modules/4.19.0-xilinx/extra# modprobe fb-ili9486.ko - Grant permissions and run the program:
root@pynq_z2:~# chmod +x demo_lvgl root@pynq_z2:~# ./demo_lvgl [main] LVGL Full-screen Animation Demo [main] Animation running...