前言
本文介绍milkv-duo加载st7735的lcd屏幕,以及屏幕显示log。
参考文章:
记录为Linux配置spi屏幕(st7735s)
https://community.milkv.io/t/milk-v-duo-spi-st7789/131
一、电路图
1.1 pin设置
打开spi2的引脚
duo-buildroot-sdk\build\boards\cv180x\cv1800b_milkv_duo_sd\u-boot\cvi_board_init.c
pinmux_config(PINMUX_SPI2);
PINMUX_CONFIG(SPINOR_MISO, XGPIOA_23);
PINMUX_CONFIG(SD0_PWR_EN, XGPIOA_14);
PINMUX_CONFIG(SPK_EN, XGPIOA_15);
注意这里的DC,属于gpioA
这里的pinmux_config(PINMUX_SPI2);可以在源码中找到对应的配置
case PINMUX_SPI2:
PINMUX_CONFIG(SD1_CMD, SPI2_SDO);
PINMUX_CONFIG(SD1_CLK, SPI2_SCK);
PINMUX_CONFIG(SD1_D0, SPI2_SDI);
PINMUX_CONFIG(SD1_D3, SPI2_CS_X);
break;
1.2 dtsi配置
build\boards\cv180x\cv1800b_milkv_duo_sd\dts_riscv\cv1800b_milkv_duo_sd.dts
&spi2 {
status = "okay";
/delete-node/ spidev@0;
st7789v: st7789v@0{
compatible = "sitronix,st7789v";
reg = <0>;
status = "okay";
spi-max-frequency = <48000000>;
spi-cpol;
spi-cpha;
rotate = <90>;
fps = <30>;
rgb;
buswidth = <8>;
dc-gpios = <&porta 23 GPIO_ACTIVE_HIGH>;
reset-gpios = <&porta 15 GPIO_ACTIVE_HIGH>;
led-gpios = <&porta 14 GPIO_ACTIVE_HIGH>;
debug = <0x0>;
};
};
这里的gpio要结合你的引脚实现。
二、加载驱动
仿照参考文章,使用st7789v代码改动到st7735上。
记录为Linux配置spi屏幕(st7735s)
编译条件
duo-buildroot-sdk\linux_5.10\drivers\staging\fbtft\Makefile
obj-$(CONFIG_FB_TFT_ST7735R) += fb_st7735r.o
obj-$(CONFIG_FB_TFT_ST7789V) += fb_st7789v.o
借用st7789v的代码
st7735s和st7735r一致
引用:https://www.cnblogs.com/milton/p/15614304.html
ST7735的配置
因为ST7735存在多个型号, 对于ST7735R和ST7735S, 使用默认的初始化方法
duo-buildroot-sdk\build\boards\cv180x\cv1800b_milkv_duo_sd\linux\ cvitek_cv1800b_milkv_duo_sd_defconfig
# spi lcd -- st7735s
CONFIG_SPI=y
CONFIG_SPI_MASTER=y
CONFIG_SPI_DESIGNWARE=y
CONFIG_SPI_DW_MMIO=y
CONFIG_SPI_SPIDEV=y
CONFIG_FB=y
CONFIG_FB_TFT=y
# CONFIG_FB_TFT_ST7735R=y
CONFIG_FB_TFT_ST7789V=y
编译过程提示
CC drivers/staging/fbtft/fb_st7789v.o
三、更改驱动
3.1 st7789v驱动
duo-buildroot-sdk\linux_5.10\drivers\staging\fbtft\fb_st7789v.c
参考来源:https://blog.csdn.net/qq_46604211/article/details/116449891
3.1.2 接口更新
由于驱动是st7789v,我的屏幕是st7735,需要更改一些初始化接口。
注意gamma需要注释掉for循环。
static int init_display(struct fbtft_par *par)
{
par->fbtftops.reset(par);
mdelay(50);
write_reg(par,0x11);
mdelay(100);
write_reg(par,0xB1,0x05,0x3c,0x3c);
write_reg(par,0xB2,0x05,0x3c,0x3c);
write_reg(par,0xB3,0x05,0x3c,0x3c,0x05,0x3c,0x3c);
write_reg(par,0xB4,0x03);
write_reg(par,0xC0,0x28,0x08,0x04);
write_reg(par,0xC1,0xc0);
write_reg(par,0xC2,0x0d,0x00);
write_reg(par,0xC3,0x8d,0x2a);
write_reg(par,0xc4,0x8d,0xee);
write_reg(par,0xc5,0x1a);
write_reg(par,0x36,0xc0);
write_reg(par,0xe0,0x04,0x22,0x07,0x0a,0x2e,0x30,0x25,0x2a,0x28,0x26,0x2e,0x3a,0x00,0x01,0x03,0x13);
write_reg(par,0xe1,0x04,0x16,0x06,0x0d,0x2d,0x26,0x23,0x27,0x27,0x25,0x2d,0x3b,0x00,0x01,0x04,0x13);
write_reg(par,0x3A,0x05);
write_reg(par,0x29);
mdelay(100);
return 0;
}
static int set_gamma(struct fbtft_par *par, u32 *curves)
{
int i;
int j;
int c;
static const u8 gamma_par_mask[] = {
0xFF,
0x3F,
0x3F,
0x1F,
0x1F,
0x3F,
0x7F,
0x77,
0x7F,
0x3F,
0x1F,
0x1F,
0x3F,
0x3F,
};
for (i = 0; i < par->gamma.num_curves; i++) {
c = i * par->gamma.num_values;
write_reg(par, PVGAMCTRL + i,
curves[c + 0], curves[c + 1], curves[c + 2],
curves[c + 3], curves[c + 4], curves[c + 5],
curves[c + 6], curves[c + 7], curves[c + 8],
curves[c + 9], curves[c + 10], curves[c + 11],
curves[c + 12], curves[c + 13]);
}
return 0;
}
3.1.3 更改宽高
st7735的屏幕宽高为128*160。
duo-buildroot-sdk\linux_5.10\drivers\staging\fbtft\fb_st7789v.c
static struct fbtft_display display = {
.regwidth = 8,
.width = 128,
.height = 160,
.gamma_num = 2,
.gamma_len = 14,
.gamma = HSD20_IPS_GAMMA,
.fbtftops = {
.init_display = init_display,
.set_var = set_var,
.set_gamma = set_gamma,
.blank = blank,
},
};
3.2 fbtft-core
duo-buildroot-sdk\linux_5.10\drivers\staging\fbtft\fbtft-core.c
3.2.1增加头文件
#include <linux/gpio.h>
#include <linux/of_gpio.h>
3.2.2 更改接口
这是由于内核版本更新导致的,一些接口可能需要改动。
dtsi中的属性加上-gpio可以自行选择。
reset重新实现。
static int fbtft_request_one_gpio(struct fbtft_par *par,
const char *name, int index,
struct gpio_desc **gpiop)
{
struct device *dev = par->info->device;
struct device_node *node = dev->of_node;
int gpio, flags, ret = 0;
enum of_gpio_flags of_flags;
if (of_find_property(node, name, NULL)) {
gpio = of_get_named_gpio_flags(node, name, index, &of_flags);
if (gpio == -ENOENT)
return 0;
if (gpio == -EPROBE_DEFER)
return gpio;
if (gpio < 0) {
dev_err(dev,
"failed to get '%s' from DT\n", name);
return gpio;
}
flags = (of_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW :
GPIOF_OUT_INIT_HIGH;
ret = devm_gpio_request_one(dev, gpio, flags,
dev->driver->name);
if (ret) {
dev_err(dev,
"gpio_request_one('%s'=%d) failed with %d\n",
name, gpio, ret);
return ret;
}
*gpiop = gpio_to_desc(gpio);
fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' = GPIO%d\n",
__func__, name, gpio);
}
return ret;
}
static int fbtft_request_gpios(struct fbtft_par *par)
{
int i;
int ret;
ret = fbtft_request_one_gpio(par, "reset-gpios", 0, &par->gpio.reset);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "dc-gpios", 0, &par->gpio.dc);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "rd-gpios", 0, &par->gpio.rd);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "wr-gpios", 0, &par->gpio.wr);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "cs-gpios", 0, &par->gpio.cs);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "latch-gpios", 0, &par->gpio.latch);
if (ret)
return ret;
for (i = 0; i < 16; i++) {
ret = fbtft_request_one_gpio(par, "db-gpios", i,
&par->gpio.db[i]);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "led-gpios", i,
&par->gpio.led[i]);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "aux-gpios", i,
&par->gpio.aux[i]);
if (ret)
return ret;
}
return 0;
}
static void fbtft_reset(struct fbtft_par *par)
{
if (!par->gpio.reset)
return;
fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
gpiod_set_value_cansleep(par->gpio.reset, 1);
msleep(10);
gpiod_set_value_cansleep(par->gpio.reset, 0);
msleep(200);
gpiod_set_value_cansleep(par->gpio.reset, 1);
msleep(10);
}
四、屏幕显示结果
驱动正常加载。
通过如下两个命令可以测试屏幕功能。
cat /dev/urandom > /dev/fb0
cat /dev/zero > /dev/fb0
echo xxxx > /dev/fb0
仔细看右上角,有一条颜色不同的线,很短,你可以echo后面跟很长的x,x越多,线越长。
这是手动赋值给像素点的。
五、用屏幕显示log
参考来源:
https://community.milkv.io/t/milk-v-duo-spi-st7789/131
5.1 添加编译模块
duo-buildroot-sdk\build\boards\cv180x\cv1800b_milkv_duo_sd\linux\cvitek_cv1800b_milkv_duo_sd_defconfig
# lcd display kernel log
CONFIG_TTY=y
CONFIG_VT=y
CONFIG_CONSOLE_TRANSLATIONS=y
CONFIG_VT_CONSOLE=y
CONFIG_HW_CONSOLE=y
CONFIG_VT_HW_CONSOLE_BINDING=y
CONFIG_FB=y
CONFIG_FB_CMDLINE=y
CONFIG_FB_NOTIFY=y
CONFIG_FONT_SUPPORT=y
CONFIG_FONTS=y
CONFIG_FONT_8x16=y
CONFIG_VGA_CONSOLE=y
CONFIG_DUMMY_CONSOLE=y
CONFIG_DUMMY_CONSOLE_COLUMNS=80
CONFIG_DUMMY_CONSOLE_ROWS=25
CONFIG_FRAMEBUFFER_CONSOLE=y
CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY=y
5.2 添加tty0用于显示log
duo-buildroot-sdk\u-boot-2021.10\include\configs\cv180x-asic.h
#define SET_BOOTARGS "setenv bootargs ${root} ${mtdparts} " \
"console=tty0 console=$consoledev,$baudrate $othbootargs;"
5.3 显示