本文主要是介绍一个简单的framebuffer的显示使用例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本例子中,显示设备是一个oled的显示屏; 没有过多的关于分辨率,刷新频率的设置; 只是演示一个framebuffer的例子。
一, kernel层的驱动代码如下:
1. 注册,这是一个使用i2c通讯的显示设备,因此注册成一个i2c设备。
定义:
struct ssd1307fb_par {struct i2c_client *client;u32 height;struct fb_info *info;struct ssd1307fb_ops *ops;u32 page_offset;struct pwm_device *pwm;u32 pwm_period;int reset;int bklight;u32 width;
};
static const struct i2c_device_id ssd1307fb_i2c_id[] = {{ "ssd1305fb", 0 },{ "ssd1306fb", 0 },{ "ssd1307fb", 0 },{ }
};
MODULE_DEVICE_TABLE(i2c, ssd1307fb_i2c_id);static struct i2c_driver ssd1307fb_driver = {.probe = ssd1307fb_probe,.remove = ssd1307fb_remove,.suspend = ssd1307fb_suspend,.resume = ssd1307fb_resume,.id_table = ssd1307fb_i2c_id,.driver = {.name = "ssd1307fb",.of_match_table = ssd1307fb_of_match,.owner = THIS_MODULE,},
};module_i2c_driver(ssd1307fb_driver);
2. 正常启动后进入probe() 函数, 进行初始化,包括变量的初始化,显示器件寄存器的初始化,和framebuffer的初始化等; 我们只列举关于framebuffer的初始化。
struct fb_info *info;struct device_node *node = client->dev.of_node;u32 vmem_size;struct ssd1307fb_par *par;u8 *vmem;int ret;
info = framebuffer_alloc(sizeof(struct ssd1307fb_par), &client->dev);if (!info) {dev_err(&client->dev, "Couldn't allocate framebuffer.\n");return -ENOMEM;}
par->ops = (struct ssd1307fb_ops *)of_match_device(ssd1307fb_of_match,&client->dev)->data;
vmem_size = par->width * par->height / 8;vmem = vmalloc(vmem_size);if (!vmem) {dev_err(&client->dev, "Couldn't allocate graphical memory.\n");ret = -ENOMEM;goto fb_alloc_error;}info->fbops = &ssd1307fb_ops;info->fix = ssd1307fb_fix;info->fix.line_length = par->width / 8;info->fbdefio = &ssd1307fb_defio;
info->var = ssd1307fb_var;info->var.xres = par->width;info->var.xres_virtual = par->width;info->var.yres = par->height;info->var.yres_virtual = par->height;info->var.red.length = 1;info->var.red.offset = 0;info->var.green.length = 1;info->var.green.offset = 0;info->var.blue.length = 1;info->var.blue.offset = 0;info->screen_base = (char *)vmem;info->fix.smem_start = (unsigned long)vmem;info->fix.smem_len = vmem_size;fb_deferred_io_init(info);
// 以上是对 info 变量的初始化,在我们的例子里,有一些,未必用到;
memcpy(vmem, picture1, vmem_size);ssd1305fb_update_display(par);dev_info(&client->dev, "%s: have update display\n", __func__);ret = register_framebuffer(info);if (ret) {dev_err(&client->dev, "Couldn't register the framebuffer\n");goto panel_init_error;}
// 上面是对共享内存的初始化和, 进行framebuffer的注册。
// 在上面的注册中, 一些文件系统的定义如下:
static struct fb_ops ssd1307fb_ops = {.owner = THIS_MODULE,.fb_read = fb_sys_read,.fb_write = ssd1307fb_write,.fb_fillrect = ssd1307fb_fillrect,.fb_copyarea = ssd1307fb_copyarea,.fb_imageblit = ssd1307fb_imageblit,
};
// 在上面的文件ops注册中, 虽然定义了 fb_read 和 fb_write 等文件节点,但在我们的例子中,这几个节点都没有用到。我们使用的是下面的 fb_deferred_io 的结构。
// 在下面中, ssd1307fb_ssd1305_init 函数会被自动调用,进行oled显示设备的初始化。
static struct ssd1307fb_ops ssd1307fb_ssd1305_ops = {.init = ssd1307fb_ssd1305_init,
};static struct ssd1307fb_ops ssd1307fb_ssd1306_ops = {.init = ssd1307fb_ssd1306_init,
};static const struct of_device_id ssd1307fb_of_match[] = {{.compatible = "solomon,ssd1305fb-i2c",.data = (void *)&ssd1307fb_ssd1305_ops,},{.compatible = "solomon,ssd1306fb-i2c",.data = (void *)&ssd1307fb_ssd1306_ops,},{.compatible = "solomon,ssd1307fb-i2c",.data = (void *)&ssd1307fb_ssd1307_ops,},{},
};
MODULE_DEVICE_TABLE(of, ssd1307fb_of_match);
// 针对共享内存的buff的初始化,如下:
static void ssd1307fb_deferred_io(struct fb_info *info,struct list_head *pagelist)
{ssd1305fb_update_display(info->par);
}static struct fb_deferred_io ssd1307fb_defio = {.delay = HZ,.deferred_io = ssd1307fb_deferred_io,
};
// 这样,当应用层更改共享内存的数据时,则 ssd1307fb_deferred_io() 函数会自动被调用; 从而调用ssd1305fb_update_display()函数,进行显示的刷新。
二、 应用层的调用例子:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>#define SSD1307FB_SSD1305_MAX_COL 128
#define SSD1307FB_SSD1305_MAX_ROW 64
unsigned char picture5[SSD1307FB_SSD1305_MAX_ROW/8][SSD1307FB_SSD1305_MAX_COL]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
... ...
int main()
{int fbfd = 0;struct fb_var_screeninfo vinfo;struct fb_fix_screeninfo finfo;long int screensize = 0;char *fbp = 0;// Open the file for reading and writingfbfd = open("/dev/graphics/fb3", O_RDWR);if (fbfd < 0) {printf("Error: cannot open framebuffer device.\n");exit(1);}printf("The framebuffer device was opened successfully. fbfd=%x\n", fbfd);screensize = 128 * 8;printf("the screensize is %d\n", screensize );// Map the device to memoryfbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,fbfd, 0);if ((int)fbp == -1) {printf("Error: failed to map framebuffer device to memory.\n");exit(4);}printf("The framebuffer device was mapped to memory successfully.\n");memcpy(fbp, picture5, screensize);munmap(fbp, screensize);close(fbfd);return 0;
}
这篇关于一个简单的framebuffer的显示使用例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!