framebuffer机制

 

一、帧缓冲机制

在谈到lcd驱动时,先谈谈,帧缓冲机制,为什么呢?因为Linux系统是工作在保护模式下,所以用户态进程无法像DOS那要使用显卡BIOS里提供的中断调用来实现直接写屏蔽,Framebuffer是出现在linux 2.2及以后的一种驱动程序接口,它是一项重要技术,基本上都是用它来实现图形界面的。

下面是LCD驱动程序与应用程序以及帧缓冲机制的关系

应用程序要操作LCD,就操作设备节点(/dev/fb0由帧缓冲创建),fbmem.c是提供应用程序的操作接口,fbmem.c本身并不实现这些功能,这就需要下一层接口实现,就是XXXfb.c要实现的与lcd底层硬件相关的操作接口。具体可以参考s3c2410fb.c。

帧缓冲技术是与LCD驱动混在一起从而形成帧缓冲LCD设备驱动程序,主要是由下面几个重要数据结构起关联。

1、Struct fb_info

Struct fb_info这结构记录了帧缓冲设备的全部信息,包括设备的设置参数、状态、以及对底层硬件操作的函数指针。下面具体分析一下。

  1. struct fb_info {
  2. int node;
  3. int flags;
  4. struct fb_var_screeninfo var; /*LCD可变参数结构体 */
  5. struct fb_fix_screeninfo fix; /* LCD固定参数结构体 */
  6. struct fb_monspecs monspecs; /* LCD显示器标准 */
  7. struct work_struct queue; /* 帧缓冲事件队列 */
  8. struct fb_pixmap pixmap; /*图像硬件 mapper */
  9. struct fb_pixmap sprite; /* 光标硬件mapper */
  10. struct fb_cmap cmap; /*当前颜色表 */
  11. struct list_head modelist; /* mode list */
  12. struct fb_videomode *mode; /* 当前的显示模式 */
  13. #ifdef CONFIG_FB_BACKLIGHT
  14. struct backlight_device *bl_dev;/*对应背光设备*/
  15. /* Backlight level curve */
  16. struct mutex bl_curve_mutex;
  17. u8 bl_curve[FB_BACKLIGHT_LEVELS];/*背光调整*/
  18. #endif
  19. 。。。。。。。。
  20. struct fb_ops *fbops;/*对底层硬件操作的函数指针*/
  21. ……
  22. struct device *dev; /* FB设备 */
  23. ……
  24. #ifdef CONFIG_FB_TILEBLITTING
  25. struct fb_tile_ops *tileops; /*图块 Blitting */
  26. #endif
  27. char __iomem *screen_base; /* 虚拟基地址 */
  28. unsigned long screen_size; /* LCD IO 映射的虚拟内存大小*/
  29. void *pseudo_palette; /*伪16色颜色表 */
  30. #define FBINFO_STATE_RUNNING 0
  31. #define FBINFO_STATE_SUSPENDED 1
  32. u32 state; /* LCD挂起或者恢复的状态*/
  33. void *fbcon_par; /* fbcon use-only private area */
  34. /* From here on everything is device dependent */
  35. void *par;
  36. };

其中比较重要的就是struct fb_var_screeninfo var;struct fb_fix_screeninfo fix;

跟struct fb_ops *fbops;

下面各自分析一下

2、struct fb_var_screeninfo

struct fb_var_screeninfo 主要是记录用户可以修改的控制器可变参数

  1. struct fb_var_screeninfo {
  2. __u32 xres; /* 可见屏幕一行有多少个像素点*/
  3. __u32 yres; /*可见屏幕一列有多少个像素点*/
  4. __u32 xres_virtual; /*虚拟屏幕一列有多少个像素点*/
  5. __u32 yres_virtual; /*虚拟屏幕一列有多少个像素点*/
  6. __u32 xoffset; /* 虚拟到可见屏幕之间的行偏移*/
  7. __u32 yoffset; /*虚拟到可见屏幕之间的列偏移 */
  8. __u32 bits_per_pixel; /* 每个像素由多少位组成即BPP*/
  9. __u32 grayscale; /* != 0 Graylevels instead of colors */
  10. /*fb缓存的RGB位域*/
  11. struct fb_bitfield red; /* bitfield in fb mem if true color, */
  12. struct fb_bitfield green; /* else only length is significant */
  13. struct fb_bitfield blue;
  14. struct fb_bitfield transp; /* transparency */
  15. __u32 nonstd; /* != 0 Non standard pixel format */
  16. __u32 activate; /* see FB_ACTIVATE_* */
  17. __u32 height; /*高度mm*/
  18. __u32 width; /* 宽度 mm*/
  19. __u32 accel_flags; /* (OBSOLETE) see fb_info.flags */
  20. /*时间选择:除了像素时钟外,所有的值都以像素时钟为单位*/
  21. /* Timing: All values in pixclocks(像素时钟), except pixclock (of course) */
  22. __u32 pixclock; /* 像素时钟(pico seconds皮秒) */
  23. __u32 left_margin; /* time from sync to picture行切换,从同步到绘图之间的延迟 */
  24. __u32 right_margin; /* time from picture to sync行切换,从绘图到同步延迟 */
  25. __u32 upper_margin; /* time from sync to picture 帧切换,从同步到绘图之间的延迟*/
  26. __u32 lower_margin; /*帧切换,从绘图到同步之间的延迟*/
  27. __u32 hsync_len; /* length of horizontal sync水平同步的长度*/
  28. __u32 vsync_len; /* length of vertical sync 垂直同步的长度*/
  29. __u32 sync; /* see FB_SYNC_* */
  30. __u32 vmode; /* see FB_VMODE_* */
  31. __u32 rotate; /* angle we rotate counter clockwise */
  32. __u32 reserved[5]; /* Reserved for future compatibility */
  33. };

3、struct fb_fix_screeninfo

而struct fb_fix_screeninfo fix;就是固定的控制器配置,比如屏幕缓冲区的物理地址和长度,定义如下:

  1. struct fb_fix_screeninfo {
  2. char id[16]; /* identification string eg "TT Builtin" */
  3. unsigned long smem_start; /* Start of frame buffer mem帧缓冲缓存的开始地址 */
  4. /* (physical address) 物理地址*/
  5. __u32 smem_len; /* Length of frame buffer mem 缓冲的长度*/
  6. __u32 type; /* see FB_TYPE_* */
  7. __u32 type_aux; /* Interleave for interleaved Planes */
  8. __u32 visual; /* see FB_VISUAL_* */
  9. __u16 xpanstep; /* 没硬件panning就置0zero if no hardware panning */
  10. __u16 ypanstep; /*没硬件panning就置0 zero if no hardware panning */
  11. __u16 ywrapstep; /*没硬件ywrap就置0 zero if no hardware ywrap */
  12. __u32 line_length; /*一行的字节数 length of a line in bytes */
  13. unsigned long mmio_start; /*内存映射的开始地址 Start of Memory Mapped I/O */
  14. /* (physical address) */
  15. __u32 mmio_len; /*内存映射的长度 Length of Memory Mapped I/O */
  16. __u32 accel; /* Indicate to driver which */
  17. /* specific chip/card we have */
  18. __u16 reserved[3]; /* Reserved for future compatibility */
  19. };

4、struct fb_ops

struct fb_ops,帧缓冲操作,关联硬件跟应用程序。

  1. /*
  2. * Frame buffer operations
  3. *
  4. * LOCKING NOTE: those functions must _ALL_ be called with the console
  5. * semaphore held, this is the only suitable locking mechanism we have
  6. * in 2.6. Some may be called at interrupt time at this point though.
  7. */
  8. struct fb_ops {
  9. /* open/release and usage marking */
  10. struct module *owner;
  11. 。。。。。。
  12. /*检查可变参数并进行设置*/
  13. /* checks var and eventually tweaks it to something supported,
  14. * DO NOT MODIFY PAR */
  15. int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);
  16. //根据设置的值进行更新,根据info->var
  17. /* set the video mode according to info->var */
  18. int (*fb_set_par)(struct fb_info *info);
  19. //设置颜色寄存器
  20. /* set color register */
  21. int (*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,
  22. unsigned blue, unsigned transp, struct fb_info *info);
  23. /* set color registers in batch */
  24. int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);
  25. //显示空白
  26. /* blank display */
  27. int (*fb_blank)(int blank, struct fb_info *info);
  28. //矩形填充
  29. /* pan display */
  30. int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);
  31. /* Draws a rectangle */
  32. void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);
  33. /* Copy data from area to another */
  34. void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);
  35. /* Draws a image to the display */
  36. void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);
  37. /* Draws cursor */
  38. int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);
  39. /* Rotates the display */
  40. void (*fb_rotate)(struct fb_info *info, int angle);
  41. /* wait for blit idle, optional */
  42. int (*fb_sync)(struct fb_info *info);
  43. /* perform fb specific ioctl (optional) */
  44. int (*fb_ioctl)(struct fb_info *info, unsigned int cmd,
  45. unsigned long arg);
  46. /* Handle 32bit compat ioctl (optional) */
  47. int (*fb_compat_ioctl)(struct fb_info *info, unsigned cmd,
  48. unsigned long arg);
  49. /* perform fb specific mmap */
  50. int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);
  51. /* save current hardware state */
  52. void (*fb_save_state)(struct fb_info *info);
  53. /* restore saved state */
  54. void (*fb_restore_state)(struct fb_info *info);
  55. /* get capability given var */
  56. void (*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,
  57. struct fb_var_screeninfo *var);
  58. };

5、帧缓冲设备以及设备资源

LCD帧缓冲设备在Linux里是作为一个平台设备,在内核arch/arm/plat-s3c24xx/devs.c中定义LCD相关平台设备如下:

  1. /* LCD Controller */
  2. static struct resource s3c_lcd_resource[] = {
  3. [0] = {
  4. .start = S3C24XX_PA_LCD,//寄存器的开始地址
  5. .end = S3C24XX_PA_LCD + S3C24XX_SZ_LCD - 1,//长度
  6. .flags = IORESOURCE_MEM,
  7. },
  8. [1] = {
  9. .start = IRQ_LCD,
  10. .end = IRQ_LCD,
  11. .flags = IORESOURCE_IRQ,
  12. }
  13. };
  14. static u64 s3c_device_lcd_dmamask = 0xffffffffUL;
  15. struct platform_device s3c_device_lcd = {
  16. .name = "s3c2410-lcd",
  17. .id = -1,
  18. .num_resources = ARRAY_SIZE(s3c_lcd_resource),资源数量
  19. .resource = s3c_lcd_resource,
  20. .dev = {
  21. .dma_mask = &s3c_device_lcd_dmamask,
  22. .coherent_dma_mask = 0xffffffffUL
  23. }
  24. };
  25. EXPORT_SYMBOL(s3c_device_lcd);

这里导出s3c_device_lcd是为了在arch/asm/mach-s3c2410/mach-smdk2410.c里的smdk2410_devices[](或者其它smdk2440_devices[])中添加到平台设备列表中。

下面准备分析具体实例,但分析前还要了解LCD的特性以及读写时序。

实在不如别人做得漂亮,做得详细,大家还是去看原文吧,我这里就不接了。

二、LCD的硬件知识

1. LCD工作的硬件需求:

要使一块 LCD正常的显示文字或图像,不仅需要 LCD驱动器,而且还需要

相应的 LCD控制器。在通常情况下,生产厂商把LCD驱动器会以 COF/COG

的形式与 LCD玻璃基板制作在一起,而 LCD控制器则是由外部的电路来实现,

现在很多的 MCU内部都集成了 LCD控制器, 如 S3C2410/2440 等。 通过LCD

控制器就可以产生LCD驱动器所需要的控制信号来控制STN/TFT 屏了。

2. S3C2440内部 LCD 控制器结构图:

我们根据数据手册来描述一下这个集成在 S3C2440 内部的 LCD控制器:

a:LCD控制器由REGBANK、LCDCDMA、TIMEGEN、VIDPRCS寄存器组

成;

b:REGBANK由 17个可编程的寄存器组和一块 256*16 的调色板内存组成,

它们用来配置 LCD控制器的;

c:LCDCDMA是一个专用的 DMA,它能自动地把在侦内存中的视频数据传送

到 LCD驱动器,通过使用这个 DMA通道,视频数据在不需要 CPU的干预的情

况下显示在 LCD屏上;

d:VIDPRCS接收来自 LCDCDMA的数据,将数据转换为合适的数据格式,比

如说 4/8 位单扫,4 位双扫显示模式,然后通过数据端口 VD[23:0]传送视频数

据到 LCD驱动器;

……

在这里我加上《S3c2410 LCD驱动学习心得》因为这里面分析如何确定驱动里的lcd配置参数写得很明白

S3C2410实验箱上的LCD是一款3.5TFT真彩LCD屏,分辩率为240*320,下图为该屏的时序要求。

1.3
通过对比图1.2和图1.3,我们不难看出:
VSPW+1=2 -> VSPW=1

VBPD+1=2 -> VBPD=1
LINVAL+1=320-> LINVAL=319
VFPD+1=3 -> VFPD=2
HSPW+1=4 -> HSPW=3
HBPD+1=7 -> HBPW=6
HOZVAL+1=240-> HOZVAL=239
HFPD+1=31 -> HFPD=30
以上各参数,除了LINVALHOZVAL直接和屏的分辩率有关,其它的参数在实际操作过程中应以上面的为参考,不应偏差太多。

1.3 LCD
控制器主要寄存器功能详解

1.4
LINECNT
:当前行扫描计数器值,标明当前扫描到了多少行。
CLKVAL
:决定VCLK的分频比。LCD控制器输出的VCLK是直接由系统总线(AHB)的工作频率HCLK直接分频得到的。做为240*320TFT屏,应保证得出的VCLK5~10MHz之间。
MMODE
VM信号的触发模式(仅对STN屏有效,对TFT屏无意义)。
PNRMODE
:选择当前的显示模式,对于TFT屏而言,应选择[11],即TFT LCD panel
BPPMODE
:选择色彩模式,对于真彩显示而言,选择16bpp64K色)即可满足要求。
ENVID
:使能LCD信号输出。

1.5
VBPD
LINEVAL VFPD VSPW 的各项含义已经在前面的时序图中得到体现。

1.6
HBPD
HOZVAL HFPD 的各项含义已经在前面的时序图中得到体现。

1.7
HSPW
的含义已经在前面的时序图中得到体现。
MVAL
只对 STN屏有效,对TFT屏无意义。
HSPW
的含义已经在前面的时序图中得到体现,这里不再赘述。
MVAL
只对 STN屏有效,对TFT屏无意义。




1.8
VSTATUS
:当前VSYNC信号扫描状态,指明当前VSYNC同步信号处于何种扫描阶段。
HSTATUS
:当前HSYNC信号扫描状态,指明当前HSYNC同步信号处于何种扫描阶段。
BPP24BL
:设定24bpp显示模式时,视频资料在显示缓冲区中的排列顺序(即低位有效还是高位有效)。对于16bpp64K色显示模式,该设置位无意义。
FRM565
:对于16bpp显示模式,有2中形式,一种是RGB5:5:5:1,另一种是5:6:5。后一种模式最为常用,它的含义是表示64K种色彩的16bit RGB资料中,红色(R)占了5bit,绿色(G)占了6bit,蓝色(B)占了5bit
INVVCLK
INVLINE INVFRAME INVVD :通过前面的时序图,我们知道,CPULCD控制器输出的时序默认是正脉冲,而LCD需要VSYNCVFRAME)、VLINEHSYNC)均为负脉冲,因此 INVLINE INVFRAME 必须设为“1 ”,即选择反相输出。
INVVDEN
INVPWREN INVLEND 的功能同前面的类似。
PWREN
LCD电源使能控制。在CPU LCD控制器的输出信号中,有一个电源使能管脚LCD_PWREN,用来做为LCD屏电源的开关信号。
ENLEND
对普通的TFT屏无效,可以不考虑。
BSWP
HWSWP 为字节(Byte)或半字(Half-Word)交换使能。由于不同的GUIFrameBuffer(显示缓冲区)的管理不同,必要时需要通过调整 BSWP HWSWP 来适应GUI

分析之后,我们能否把这LCD弄成模块?如果可以,又怎么弄?(这跟平台设备驱动有很大关系)

1、修改
linux-2.6.24/arch/arm/mach-s3c2410/mach-smdk2410.c

先加头文件
#include <asm/arch/fb.h>

再加入如下

  1. static struct s3c2410fb_display qt2410_lcd_cfg[] __initdata = {
  2. {
  3. /* Configuration for 640x480 SHARP LQ080V3DG01 */
  4. .lcdcon5 = S3C2410_LCDCON5_FRM565 |
  5. S3C2410_LCDCON5_INVVLINE |
  6. S3C2410_LCDCON5_INVVFRAME |
  7. S3C2410_LCDCON5_PWREN |
  8. S3C2410_LCDCON5_HWSWP,
  9. .type = S3C2410_LCDCON1_TFT,
  10. .width = 640,
  11. .height = 480,
  12. .pixclock = 40000, /* HCLK/4 */
  13. .xres = 640,
  14. .yres = 480,
  15. .bpp = 16,
  16. .left_margin = 44,
  17. .right_margin = 116,
  18. .hsync_len = 96,
  19. .upper_margin = 19,
  20. .lower_margin = 11,
  21. .vsync_len = 15,
  22. }
  23. };
  24. static struct s3c2410fb_mach_info qt2410_fb_info __initdata = {
  25. .displays = qt2410_lcd_cfg,
  26. .num_displays = 1,
  27. .default_display = 0,
  28. //设置引脚模式
  29. .gpccon= 0xaa8002a8,
  30. .gpccon_mask= 0xffc003fc,
  31. .gpcup= 0xf81e,
  32. .gpcup_mask= 0xf81e,
  33. .gpdcon= 0xaa80aaa0,
  34. .gpdcon_mask= 0xffc0fff0,
  35. .gpdup= 0xf8fc,
  36. .gpdup_mask= 0xf8fc,
  37. //.lpcsel = ((0xCE6) & ~7) | 1<<4,
  38. };

然后找到
static void __init smdk2410_init(void)函数
加入
s3c24xx_fb_set_platdata(&qt2410_fb_info);

OK,
文件修改完毕
在make menuconfig 中
Device Drivers --->
Graphics support --->
Display device support --->
勾上S3C2410 LCD framebuffer support
想看开机的小企鹅就勾上
Bootup logo
想换一个启动图案的话,如下

(1)进入linux的kde图形界面,使用The GIMP 图像编辑器打开你想要的图像文件,依次选择图像->模式->索引颜色,将颜色改为224色;至于图片大小,不要大于你的显示器分辨率就好(我只试过80*80和320*240的大小),最后将文件保存为ppm格式(ASCii码),文件名为:logo_linux_clut224.ppm。
(2)将logo_linux_clut224.ppm拷贝到/drivers/video/logo文件夹下,替换原有的文件(记得备份啊,以防万一)。
(3)重新编译内核,tftp到开发板启动。

直接分析源码

Drivers/video/s3c2410fb.c

前面就是注册LCD平台驱动层,看到结构体static struct platform_driver s3c2410fb_driver,

里面就是这个平台设备的函数指针,通过调用platform_driver_register,如果系统里有LCD设备已经注册了(如果直接移植,就是在mach-smdk2410.c里加的内容注册的),就会调用s3c24xxfb_probe

分析s3c24xxfb_probe(假设是采用移植直接编译在内核里,而不是写成模块)

  1. static int __init s3c24xxfb_probe(struct platform_device *pdev,
  2. enum s3c_drv_type drv_type)
  3. {
  4. ……
  5. // 这语句是为了得到lcd平台设备数据,也就是lcd的配置(在mach-smdk2410.c定义)
  6. mach_info = pdev->dev.platform_data;
  7. ……
  8. //拿出lcd配置数据
  9. display = mach_info->displays + mach_info->default_display;
  10. //获中断
  11. irq = platform_get_irq(pdev, 0);
  12. ……
  13. //新创一个struct fb_info
  14. fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);
  15. if (!fbinfo)
  16. return -ENOMEM;
  17. //放在dev->driver_data里
  18. platform_set_drvdata(pdev, fbinfo);
  19. info = fbinfo->par;
  20. info->dev = &pdev->dev;
  21. info->drv_type = drv_type;
  22. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  23. ……
  24. //映射
  25. size = (res->end - res->start) + 1;
  26. info->mem = request_mem_region(res->start, size, pdev->name);
  27. ……
  28. info->io = ioremap(res->start, size);
  29. ……
  30. info->irq_base = info->io + ((drv_type == DRV_S3C2412) ? S3C2412_LCDINTBASE : S3C2410_LCDINTBASE);
  31. strcpy(fbinfo->fix.id, driver_name);
  32. /* Stop the video */
  33. lcdcon1 = readl(info->io + S3C2410_LCDCON1);
  34. writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, info->io + S3C2410_LCDCON1);
  35. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  36. fbinfo->fix.type_aux = 0;
  37. fbinfo->fix.xpanstep = 0;
  38. fbinfo->fix.ypanstep = 0;
  39. fbinfo->fix.ywrapstep = 0;
  40. fbinfo->fix.accel = FB_ACCEL_NONE;
  41. fbinfo->var.nonstd = 0;
  42. fbinfo->var.activate = FB_ACTIVATE_NOW;
  43. fbinfo->var.accel_flags = 0;
  44. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  45. //主要的接口
  46. fbinfo->fbops = &s3c2410fb_ops;
  47. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  48. fbinfo->pseudo_palette = &info->pseudo_pal;
  49. for (i = 0; i < 256; i++)
  50. info->palette_buffer[i] = PALETTE_BUFF_CLEAR;
  51. ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);
  52. ……
  53. //开时钟
  54. info->clk = clk_get(NULL, "lcd");
  55. ……
  56. clk_enable(info->clk);
  57. ……
  58. //将一开始加入的lcd设备配置的尺寸加入
  59. /* find maximum required memory size for display */
  60. for (i = 0; i < mach_info->num_displays; i++) {
  61. unsigned long smem_len = mach_info->displays[i].xres;
  62. smem_len *= mach_info->displays[i].yres;
  63. smem_len *= mach_info->displays[i].bpp;
  64. smem_len >>= 3;
  65. if (fbinfo->fix.smem_len < smem_len)
  66. fbinfo->fix.smem_len = smem_len;
  67. }
  68. /* Initialize video memory */
  69. //申请DRAM作为缓存,用于DMA操作
  70. ret = s3c2410fb_map_video_memory(fbinfo);
  71. ……
  72. fbinfo->var.xres = display->xres;
  73. fbinfo->var.yres = display->yres;
  74. fbinfo->var.bits_per_pixel = display->bpp;
  75. //下面这函数设置lcd寄存器
  76. s3c2410fb_init_registers(fbinfo);
  77. //这个是将lcd的配置写入,这里主要搞清几个结构体的关系
  78. //特别注意fb_info后面还加了一点空间用来保存info的,而info->dev = &pdev->dev,即可以直接调用到传递进来的lcd配置。
  79. s3c2410fb_check_var(&fbinfo->var, fbinfo);
  80. //注册framebuffer创建设备节点
  81. ret = register_framebuffer(fbinfo);
  82. ……
  83. /* create device files */
  84. ret = device_create_file(&pdev->dev, &dev_attr_debug);
  85. ……
  86. }

在注册framebuffer,即register_framebuffer里注册完后,假如你将这编译成模块,会发现最终程序死在fb_notifier_call_chain一直等待,最后出现Segmentation fault具体做法是将这语句注释掉,但如果你是把这驱动编译进内核,注释掉这步不能显示开机logo,原因在于,模块编译缺少依赖的内核模块,主要为cfbcopyarea.ko cfbfillrect.ko cfbimgblt.ko,这几个主要是给下面s3c2410fb_ops里的后三个,因为要 显示logo,在int fb_show_logo(struct fb_info *info)函数中,需要应用fb_ops结构体中的fb_imageblit域来在屏幕上画一幅图象,且是必须的,所以,fb_ops结构中fb_imageblit域不能为NULL,如果为NULL,那么这个函数就没有办法完成功能,也就是一个无用的函数,Fb_ops里面的内容就是完成应用程序系统调用。那register_framebuffer>>fb_notifier_call_chain》》》blocking_notifier_call_chain里的fb_notifier_list,这个内核链表又是哪里定义的?static BLOCKING_NOTIFIER_HEAD(fb_notifier_list);慢慢看就知道定义成什么样子,那接上面的函数深入,到__blocking_notifier_call_chain》》notifier_call_chain,最终会调用nb->notifier_call(nb, val, v);,那什么时候加入到这内核链表?

加的地方有两个,一个是fbcon.c一个是backlight.c,fbcon.c里的是fb_register_client(&fbcon_event_notifier);,backlight.c里的是info->fb_notif

其中

  1. static struct notifier_block fbcon_event_notifier = {
  2. .notifier_call = fbcon_event_notify,
  3. };

fbcon_event_notify,这函数就根据不同的标志做不同的处理,再跟踪到较里面就发现,原来根据 是不是编译成内核模块来执行不同的东西,如果是编译进内核,就会显示logo了。大概完了。

现在更专业的方法是什么?当然是把那三个内核模块都加载进来

  1. static struct fb_ops s3c2410fb_ops = {
  2. .owner = THIS_MODULE,
  3. .fb_check_var = s3c2410fb_check_var,
  4. .fb_set_par = s3c2410fb_set_par,
  5. .fb_blank = s3c2410fb_blank,
  6. .fb_setcolreg = s3c2410fb_setcolreg,
  7. .fb_fillrect = cfb_fillrect,
  8. .fb_copyarea = cfb_copyarea,
  9. .fb_imageblit = cfb_imageblit,
  10. };

这里有一个关于pixclock像素时钟计算问题,它是怎么计算得来的?

其实它就是VCLK的另一种表述,VCLK是每秒多少像素,耐pixclock是每像素多少微微秒,1Hz = 10^12picoseconds,举个例子,当pixclock = 80000(pic) ,VCLK=1/(80000*10^12)/10^6 = 12.5MHz

由于LCD时钟是使用AHB分频得到的HCLK时钟再分频得来的,pixclock确定之后,怎么确定分频值呢?在函数s3c2410fb_set_par》》》》s3c2410fb_activate_var》》》》》s3c2410fb_calc_pixclk就是计算分频值也就是CLKAL的。还有一个提一下,就是在s3c2410fb_calc_pixclk里有一个unsigned long clk = clk_get_rate(fbi->clk);,其实它是得到HCLK的,具体是在clk_get(NULL,”lcd”)时得到的。

应用层操作lcd

采用fb_open(),fb_mmap(),fb_write()等

以华清的例子为例分析

具体参考《嵌入式linux_Framebuffer驱动开发》,复制不过来,这里我在网上找了一个例子。

1. 显存大小计算:xres * yres * bits_per_pixel/8 (BYTES)

2.编程流程:

1打开设备 open("/dev/fb0",O_RDWR);调用fb_open()

(2) 获取framebuffer设备信息.ioctl(int fb,FBIOGET_FSCREENINFO,&finfo);调用fb_ioctl

ioctl函数是实现对设备的信息获取和设定,第一个参数为文件描述符,第二个参数为具体设备的参数,对于framebuffer,参数在linux/fb.h中定义的。

#define FBIOGET_VSCREENINFO 0x4600 //获取设备无关的数据信息fb_var_screeninfo
#define FBIOPUT_VSCREENINFO 0x4601 //
设定设备无关的数据信息

#define FBIOGET_FSCREENINFO 0x4602 //
获取设备无关的常值信息fb_fix_screeninfo
#define FBIOGETCMAP 0x4604 //
获取设备无关颜色表信息

#define FBIOPUTCMAP 0x4605 //
设定设备无关颜色表信息
#define FBIOPAN_DISPLAY 0x4606
#define FBIO_CURSOR _IOWR('F', 0x08, struct fb_cursor)

第三个参数是存放信息的结构体或者缓冲区

(3)内存映射 mmap函数。头文件:sys/mman.h .常用用法:mmap(0,screensize,PROT_RD |PROT_WR,MAP_SHARED,int fb,0)返回映射的首地址。

3。实例

程序实现在lcd 上全屏写 blue

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <linux/fb.h>
  5. #include <sys/mman.h>
  6. #include <stdlib.h>
  7. int main()
  8. {
  9. int fbfd = 0;
  10. struct fb_var_screeninfo vinfo;
  11. struct fb_fix_screeninfo finfo;
  12. long int screensize = 0;
  13. char *fbp = 0;
  14. int x = 0, y = 0;
  15. long int location = 0;
  16. int sav=0;
  17. /* open device*/
  18. fbfd = open("/dev/fb0", O_RDWR);
  19. if (!fbfd) {
  20. printf("Error: cannot open framebuffer device./n");
  21. exit(1);
  22. }
  23. printf("The framebuffer device was opened successfully./n");
  24. /* Get fixed screen information */
  25. if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
  26. printf("Error reading fixed information./n");
  27. exit(2);
  28. }
  29. /* Get variable screen information */
  30. if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
  31. printf("Error reading variable information./n");
  32. exit(3);
  33. }
  34. /* show these information*/
  35. printf("vinfo.xres=%d/n",vinfo.xres);
  36. printf("vinfo.yres=%d/n",vinfo.yres);
  37. printf("vinfo.bits_per_bits=%d/n",vinfo.bits_per_pixel);
  38. printf("vinfo.xoffset=%d/n",vinfo.xoffset);
  39. printf("vinfo.yoffset=%d/n",vinfo.yoffset);
  40. printf("finfo.line_length=%d/n",finfo.line_length);
  41. /* Figure out the size of the screen in bytes */
  42. screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  43. 总共有多少像素,一像素多少位,转化为字节
  44. /* Map the device to memory */
  45. fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
  46. fbfd, 0);
  47. if ((int)fbp == -1) { printf("Error: failed to map framebuffer device to memory./n"); exit(4);
  48. }
  49. printf("The framebuffer device was mapped to memory successfully./n");
  50. memset(fbp,0,screensize);
  51. /* Where we are going to put the pixel */
  52. for(x=0;x<vinfo.xres;x )
  53. for(y=0;y<vinfo.yres;y )
  54. {
  55. location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8)
  56. (y+vinfo.yoffset) * finfo.line_length;
  57. *(fbp location) = 0xff; /* blue */
  58. *(fbp location 1) = 0x00;
  59. }
  60. munmap(fbp, screensize); /* release the memory */
  61. close(fbfd);
  62. return 0;
  63. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值