Description
I have followed the display porting guide in LVGL Docs. And I have successfully run the keyboard example lv_ex_keyboard_1. However, it is not working correctly. It shows some random color pixels when I press the keys and the cursor also distorted. I’m not sure what I’m doing wrong. Any help would be greatly appreciated.
What MCU/Processor/Board and compiler are you using?
Kendryte K210 kd233 development board
Kendtryte FreeRTOS
Display driver IC ST7789
What LVGL version are you using?
7.1 Dev
What do you want to achieve?
Run without any glitches on the screen.
Code to reproduce/What have I tried so far?
Below are my flush_cb
callback function and driver initializing snippet,
void lcd_draw_picture_by_half(uint16_t x1, uint16_t y1, uint16_t width,
uint16_t height, uint16_t *ptr) {
lcd_set_area(x1, y1, x1 + width - 1, y1 + height - 1);
tft_write_half(ptr, width * height);
}
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area,
lv_color_t *color_p) {
lcd_draw_picture_by_half(area->x1, area->y1, (area->x2 - area->x1 + 1),
(area->y2 - area->y1 + 1), color_p);
lv_disp_flush_ready(disp); /* Indicate you are ready with the flushing*/
}
/*A static or global variable to store the buffers*/
static lv_disp_buf_t disp_buf;
/*Static or global buffer(s). The second buffer is optional*/
static lv_color_t buf_1[LV_HOR_RES_MAX * 10];
static lv_color_t buf_2[LV_HOR_RES_MAX * 10];
/*Initialize `disp_buf` with the buffer(s) */
lv_disp_buf_init(&disp_buf, buf_1, buf_2, LV_HOR_RES_MAX * 10);
lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
disp_drv.flush_cb = my_disp_flush; /*Set your driver function*/
disp_drv.buffer = &disp_buf; /*Assign the buffer to the display*/
lv_disp_drv_register(&disp_drv); /*Finally register the driver*/
Display HAL code .h,
#ifndef _NT35310_H_
#define _NT35310_H_
#include <stdint.h>
/* clang-format off */
#define NO_OPERATION 0x00
#define SOFTWARE_RESET 0x01
#define READ_ID 0x04
#define READ_STATUS 0x09
#define READ_POWER_MODE 0x0A
#define READ_MADCTL 0x0B
#define READ_PIXEL_FORMAT 0x0C
#define READ_IMAGE_FORMAT 0x0D
#define READ_SIGNAL_MODE 0x0E
#define READ_SELT_DIAG_RESULT 0x0F
#define SLEEP_ON 0x10
#define SLEEP_OFF 0x11
#define PARTIAL_DISPALY_ON 0x12
#define NORMAL_DISPALY_ON 0x13
#define INVERSION_DISPALY_OFF 0x20
#define INVERSION_DISPALY_ON 0x21
#define GAMMA_SET 0x26
#define DISPALY_OFF 0x28
#define DISPALY_ON 0x29
#define HORIZONTAL_ADDRESS_SET 0x2A
#define VERTICAL_ADDRESS_SET 0x2B
#define MEMORY_WRITE 0x2C
#define COLOR_SET 0x2D
#define MEMORY_READ 0x2E
#define PARTIAL_AREA 0x30
#define VERTICAL_SCROL_DEFINE 0x33
#define TEAR_EFFECT_LINE_OFF 0x34
#define TEAR_EFFECT_LINE_ON 0x35
#define MEMORY_ACCESS_CTL 0x36
#define VERTICAL_SCROL_S_ADD 0x37
#define IDLE_MODE_OFF 0x38
#define IDLE_MODE_ON 0x39
#define PIXEL_FORMAT_SET 0x3A
#define WRITE_MEMORY_CONTINUE 0x3C
#define READ_MEMORY_CONTINUE 0x3E
#define SET_TEAR_SCANLINE 0x44
#define GET_SCANLINE 0x45
#define WRITE_BRIGHTNESS 0x51
#define READ_BRIGHTNESS 0x52
#define WRITE_CTRL_DISPALY 0x53
#define READ_CTRL_DISPALY 0x54
#define WRITE_BRIGHTNESS_CTL 0x55
#define READ_BRIGHTNESS_CTL 0x56
#define WRITE_MIN_BRIGHTNESS 0x5E
#define READ_MIN_BRIGHTNESS 0x5F
#define READ_ID1 0xDA
#define READ_ID2 0xDB
#define READ_ID3 0xDC
#define RGB_IF_SIGNAL_CTL 0xB0
#define NORMAL_FRAME_CTL 0xB1
#define IDLE_FRAME_CTL 0xB2
#define PARTIAL_FRAME_CTL 0xB3
#define INVERSION_CTL 0xB4
#define BLANK_PORCH_CTL 0xB5
#define DISPALY_FUNCTION_CTL 0xB6
#define ENTRY_MODE_SET 0xB7
#define BACKLIGHT_CTL1 0xB8
#define BACKLIGHT_CTL2 0xB9
#define BACKLIGHT_CTL3 0xBA
#define BACKLIGHT_CTL4 0xBB
#define BACKLIGHT_CTL5 0xBC
#define BACKLIGHT_CTL7 0xBE
#define BACKLIGHT_CTL8 0xBF
#define POWER_CTL1 0xC0
#define POWER_CTL2 0xC1
#define VCOM_CTL1 0xC5
#define VCOM_CTL2 0xC7
#define NV_MEMORY_WRITE 0xD0
#define NV_MEMORY_PROTECT_KEY 0xD1
#define NV_MEMORY_STATUS_READ 0xD2
#define READ_ID4 0xD3
#define POSITIVE_GAMMA_CORRECT 0xE0
#define NEGATIVE_GAMMA_CORRECT 0xE1
#define DIGITAL_GAMMA_CTL1 0xE2
#define DIGITAL_GAMMA_CTL2 0xE3
#define INTERFACE_CTL 0xF6
/* clang-format on */
void tft_hard_init(void);
void tft_write_command(uint8_t cmd);
void tft_write_byte(uint8_t* data_buf, uint32_t length);
void tft_write_half(uint16_t* data_buf, uint32_t length);
void tft_write_word(uint32_t* data_buf, uint32_t length);
void tft_fill_data(uint32_t* data_buf, uint32_t length);
#endif
Display HAL code .c,
#include <devices.h>
#include <stdio.h>
#include "project_cfg.h"
#include "jlt32009a.h"
#define SPI_HIGH_CLOCK_RATE 25000000U
#define SPI_LOW_CLOCK_RATE 3200000U
#define WAIT_CYCLE 0U
enum _instruction_length
{
INSTRUCTION_LEN_0 = 0,
INSTRUCTION_LEN_8 = 8,
INSTRUCTION_LEN_16 = 16,
INSTRUCTION_LEN_32 = 32,
} ;
enum _address_length
{
ADDRESS_LEN_0 = 0,
ADDRESS_LEN_8 = 8,
ADDRESS_LEN_16 = 16,
ADDRESS_LEN_32 = 32,
} ;
enum _frame_length
{
FRAME_LEN_0 = 0,
FRAME_LEN_8 = 8,
FRAME_LEN_16 = 16,
FRAME_LEN_32 = 32,
} ;
handle_t gio;
handle_t spi0;
handle_t spi_dfs8;
handle_t spi_dfs16;
handle_t spi_dfs32;
void init_dcx()
{
gio = io_open("/dev/gpio0");
configASSERT(gio);
gpio_set_drive_mode(gio, DCX_GPIONUM, GPIO_DM_OUTPUT);
gpio_set_pin_value(gio, DCX_GPIONUM, GPIO_PV_HIGH);
}
void set_dcx_control()
{
configASSERT(gio);
gpio_set_pin_value(gio, DCX_GPIONUM, GPIO_PV_LOW);
}
void set_dcx_data()
{
configASSERT(gio);
gpio_set_pin_value(gio, DCX_GPIONUM, GPIO_PV_HIGH);
}
void spi_control_init()
{
spi0 = io_open("/dev/spi0");
configASSERT(spi0);
spi_dfs8 = spi_get_device(spi0, SPI_MODE_0, SPI_FF_OCTAL, 1 << SPI_SLAVE_SELECT, FRAME_LEN_8);
spi_dev_config_non_standard(spi_dfs8, INSTRUCTION_LEN_8, ADDRESS_LEN_0, WAIT_CYCLE, SPI_AITM_AS_FRAME_FORMAT);
spi_dfs16 = spi_get_device(spi0, SPI_MODE_0, SPI_FF_OCTAL, 1 << SPI_SLAVE_SELECT, FRAME_LEN_16);
spi_dev_config_non_standard(spi_dfs16, INSTRUCTION_LEN_16, ADDRESS_LEN_0, WAIT_CYCLE, SPI_AITM_AS_FRAME_FORMAT);
spi_dfs32 = spi_get_device(spi0, SPI_MODE_0, SPI_FF_OCTAL, 1 << SPI_SLAVE_SELECT, FRAME_LEN_32);
spi_dev_config_non_standard(spi_dfs32, INSTRUCTION_LEN_0, ADDRESS_LEN_32, WAIT_CYCLE, SPI_AITM_AS_FRAME_FORMAT);
spi_dev_set_clock_rate(spi_dfs8, SPI_LOW_CLOCK_RATE);
spi_dev_set_clock_rate(spi_dfs16, SPI_HIGH_CLOCK_RATE);
spi_dev_set_clock_rate(spi_dfs32, SPI_HIGH_CLOCK_RATE);
}
void tft_hard_init(void)
{
init_dcx();
spi_control_init();
}
void tft_write_command(uint8_t cmd)
{
set_dcx_control();
io_write(spi_dfs8, (const uint8_t *)(&cmd), 1);
}
void tft_write_byte(uint8_t* data_buf, uint32_t length)
{
set_dcx_data();
io_write(spi_dfs8, (const uint8_t *)(data_buf), length);
}
void tft_write_half(uint16_t* data_buf, uint32_t length)
{
set_dcx_data();
io_write(spi_dfs16, (const uint8_t *)(data_buf), length * 2);
}
void tft_write_word(uint32_t* data_buf, uint32_t length)
{
set_dcx_data();
io_write(spi_dfs32, (const uint8_t *)data_buf, length * 4);
}
void tft_fill_data(uint32_t* data_buf, uint32_t length)
{
set_dcx_data();
spi_dev_fill(spi_dfs32, 0, *data_buf, *data_buf, length - 1);
}
LCD.c
#include "lcd.h"
#include <string.h>
#include <sys/unistd.h>
#include "font.h"
#include "jlt32009a.h"
typedef struct {
uint8_t dir;
uint16_t width;
uint16_t height;
} lcd_ctl_t;
static lcd_ctl_t lcd_ctl;
void lcd_init(void) {
uint8_t data;
tft_hard_init();
// soft reset
tft_write_command(SOFTWARE_RESET);
usleep(100 * 1000);
// exit sleep
tft_write_command(SLEEP_OFF);
usleep(100 * 1000);
// pixel format
tft_write_command(PIXEL_FORMAT_SET);
data = 0x55;
tft_write_byte(&data, 1);
lcd_set_direction(DIR_YX_RLUD);
tft_write_command(DISPALY_ON);
}
void lcd_set_direction(enum lcd_dir_t dir) {
dir |= 0x08;
lcd_ctl.dir = dir;
if (dir & DIR_XY_MASK) {
lcd_ctl.width = LCD_Y_MAX - 1;
lcd_ctl.height = LCD_X_MAX - 1;
} else {
lcd_ctl.width = LCD_X_MAX - 1;
lcd_ctl.height = LCD_Y_MAX - 1;
}
tft_write_command(MEMORY_ACCESS_CTL);
tft_write_byte((uint8_t*)&dir, 1);
}
void lcd_set_area(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
uint8_t data[4];
data[0] = (uint8_t)(x1 >> 8);
data[1] = (uint8_t)(x1);
data[2] = (uint8_t)(x2 >> 8);
data[3] = (uint8_t)(x2);
tft_write_command(HORIZONTAL_ADDRESS_SET);
tft_write_byte(data, 4);
data[0] = (uint8_t)(y1 >> 8);
data[1] = (uint8_t)(y1);
data[2] = (uint8_t)(y2 >> 8);
data[3] = (uint8_t)(y2);
tft_write_command(VERTICAL_ADDRESS_SET);
tft_write_byte(data, 4);
tft_write_command(MEMORY_WRITE);
}
void lcd_draw_point(uint16_t x, uint16_t y, uint16_t color) {
lcd_set_area(x, y, x, y);
tft_write_half(&color, 1);
}
void lcd_draw_char(uint16_t x, uint16_t y, char c, uint16_t color) {
uint8_t i, j, data;
for (i = 0; i < 16; i++) {
data = ascii0816[c * 16 + i];
for (j = 0; j < 8; j++) {
if (data & 0x80) lcd_draw_point(x + j, y, color);
data <<= 1;
}
y++;
}
}
void lcd_draw_string(uint16_t x, uint16_t y, char* str, uint16_t color) {
while (*str) {
lcd_draw_char(x, y, *str, color);
str++;
x += 8;
}
}
void ram_draw_string(char* str, uint32_t* ptr, uint16_t font_color,
uint16_t bg_color) {
uint8_t i, j, data, *pdata;
uint16_t width;
uint32_t* pixel;
width = 4 * strlen(str);
while (*str) {
pdata = (uint8_t*)&ascii0816[(*str) * 16];
for (i = 0; i < 16; i++) {
data = *pdata++;
pixel = ptr + i * width;
for (j = 0; j < 4; j++) {
switch (data >> 6) {
case 0:
*pixel = ((uint32_t)bg_color << 16) | bg_color;
break;
case 1:
*pixel = ((uint32_t)bg_color << 16) | font_color;
break;
case 2:
*pixel = ((uint32_t)font_color << 16) | bg_color;
break;
case 3:
*pixel = ((uint32_t)font_color << 16) | font_color;
break;
default:
*pixel = 0;
break;
}
data <<= 2;
pixel++;
}
}
str++;
ptr += 4;
}
}
void lcd_clear(uint16_t color) {
uint32_t data = ((uint32_t)color << 16) | (uint32_t)color;
lcd_set_area(0, 0, lcd_ctl.width, lcd_ctl.height);
tft_fill_data(&data, LCD_X_MAX * LCD_Y_MAX / 2);
}
void lcd_draw_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,
uint16_t width, uint16_t color) {
uint32_t data_buf[640];
uint32_t* p = data_buf;
uint32_t data = color;
uint32_t index;
data = (data << 16) | data;
for (index = 0; index < 160 * width; index++) *p++ = data;
lcd_set_area(x1, y1, x2, y1 + width - 1);
tft_write_word(data_buf, ((x2 - x1 + 1) * width + 1) / 2);
lcd_set_area(x1, y2 - width + 1, x2, y2);
tft_write_word(data_buf, ((x2 - x1 + 1) * width + 1) / 2);
lcd_set_area(x1, y1, x1 + width - 1, y2);
tft_write_word(data_buf, ((y2 - y1 + 1) * width + 1) / 2);
lcd_set_area(x2 - width + 1, y1, x2, y2);
tft_write_word(data_buf, ((y2 - y1 + 1) * width + 1) / 2);
}
void lcd_draw_picture(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height,
uint32_t* ptr) {
lcd_set_area(x1, y1, x1 + width - 1, y1 + height - 1);
tft_write_word(ptr, width * height / 2);
}
Screenshot and/or video
I have uploaded a video to youtube to show you how the issue looks like,
youtu.be/Fhq-zwrYM3U