美文网首页
Linux 设备驱动: fbtft

Linux 设备驱动: fbtft

作者: wjundong | 来源:发表于2022-08-27 22:34 被阅读0次

直接使用 kernel 中的 fbtft 模块, 比如屏幕 ic 是 st7789V, 分辨率 240*240

  • make menuconfig 后搜索 ST7789 即可找到.
  • vim drivers/staging/fbtft/fb_st7789v.c 改成 240240 分辨率, 若是 240320 直接不用更改

设备树 overlay

/dts-v1/;
/plugin/;
/ {
    compatible = "xunlong,orangepi-3-lts,allwinner,sun50i-h6";

    fragment@0 {
        target = <&spi1>;
        __overlay__ {
            /* needed to avoid dtc warning */
            #address-cells = <1>;
            #size-cells = <0>;
            status = "okay";
            display@0{
                compatible = "sitronix,st7789v";
                reg = <0>;
                spi-max-frequency = <40000000>;
                reset-gpios = <&pio 3 18 1>;
                dc-gpios = <&pio 3 16 0>;
                led-gpios = <&pio 3 15 0>;
                buswidth = <8>;
                spi-cpol;
                spi-cpha;
            };
        };
    };
};
  • 测试程序
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>

int main(int argc, char const *argv[])
{
    int i, j, fd, var;

    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;

    if (argc < 2)
    {
        fprintf(stderr, "Example: %s /dev/fb0\n", argv[0]);
        return -1;
    }

    fd = open(argv[1], O_RDWR);
    if (fd < 0)
    {
        fprintf(stderr, "Can't open file %s: %s\n", argv[1], strerror(errno));
        return -1;
    }

    /* Get variable screen information */
    if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo))
    {
        perror("Can't get FBIOGET_VSCREENINFO");
        return -1;
    }

    /* Get fixed screen information */
    if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo))
    {
        perror("Can't get FBIOGET_VSCREENINFO");
        return -1;
    }

    /* show these information*/
    printf("vinfo.xres          = %d\n", vinfo.xres);
    printf("vinfo.yres          = %d\n", vinfo.yres);
    printf("vinfo.bits_per_bits = %d\n", vinfo.bits_per_pixel);
    printf("vinfo.xoffset       = %d\n", vinfo.xoffset);
    printf("vinfo.yoffset       = %d\n", vinfo.yoffset);
    printf("finfo.line_length   = %d\n", finfo.line_length);

    /* Figure out the size of the screen in bytes */
    int screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

    /* Map the device to memory */
    char *fbp = mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (fbp == MAP_FAILED)
    {
        perror("mmap error");
        return -1;
    }

    memset(fbp, 0xff, screensize);
    
    /* Where we are going to put the pixel */
    for (int x = 0; x < vinfo.xres; x++)
    {
        for (int y = 0; y < vinfo.yres; y++)
        {
            int location = (x + vinfo.xoffset) * (vinfo.bits_per_pixel / 8) +
                       (y + vinfo.yoffset) * finfo.line_length;

            *(fbp + location) = 0xff; /*  blue */
            *(fbp + location + 1) = 0x00;
        }
    }
    
    /* release the memory */
    munmap(fbp, screensize); 
    close(fd);
    printf("all ok\n");

    return 0;
}

相关文章

网友评论

      本文标题:Linux 设备驱动: fbtft

      本文链接:https://www.haomeiwen.com/subject/yvocnrtx.html