Frame Buffer LCD Application Programming and framebuffer Driver Model in Linux

Source: Internet
Author: User

I,LinuxFrame Buffer Device

Frame Buffer is an interface provided by Linux for display devices. It abstracts the video into a device, it allows upper-layer applications to directly read and write the display buffer in graphic mode. Such operations are abstract and unified. Users do not have to worry about the location of Physical video memory, page feed mechanism, and other details. These are all driven by the framebuffer device. Frame buffering is widely used. In Linux desktop systems, XWindow servers use frame buffering to draw windows. In particular, frame buffering can display Chinese character lattice, making it the only feasible solution for Linux localization.

The device file corresponding to the frame buffer device is/dev/FB *. If the system has multiple display cards, Linux supports multiple frame buffer devices, up to 32, they are/dev/fb0 to/dev/fb31 respectively, while/dev/FB is the current default frame buffer device, usually pointing to/dev/fb0. Of course, it is enough to support a display device in an embedded system. The frame buffer device is a standard character device. The primary device number is 29, and the secondary device number ranges from 0 to 31. Corresponding to/dev/fb0-/dev/fb31 respectively.

Through/dev/FB, application operations mainly include:

1. read/write/dev/FB: equivalent to the read/write screen buffer. For example, you can use the CP/dev/fb0 TMP command to copy the content of the current screen to a file, the command cp tmp>/dev/fb0 directly displays the image file TMP on the screen.

2. Map operation: since Linux is working in protection mode, each application has its own virtual address space, so it cannot directly access the physical buffer address in the application. Therefore, Linux provides the MMAP function in the file_operations structure for file operations, which can map the file content to the user space. For frame buffer devices, you can map the physical address of the screen buffer to a virtual address in the user space, then, the user can access the screen buffer by reading and writing the virtual address, and draw a picture on the screen.

3. I/O Control: For frame buffering devices, IOCTL operations on device files can be performed to read/set display device and screen parameters, such as resolution, number of colors, and screen size. IOCTL operations are performed by the underlying driver.

 

The general steps for operating/dev/FB in an application are as follows:

1. Open the/dev/FB device file.

2. Use IOCTL to obtain the parameters of the current display screen, such as the screen resolution and the number of bits per pixel. The screen buffer size can be calculated based on screen parameters.

3. Map the screen buffer to the user space (MMAP ).

4. After the ing, you can directly read and write the screen buffer for plotting and image display.

The typical program section is as follows:

 

# Include <Linux/FB. h> int main () {int fbfd = 0; struct fb_var_screeninfo vinfo; struct fb_fix_screeninfo finfo; long int screensize = 0; /* Open the device file */fbfd = open ("/dev/fb0", o_rdwr);/* obtain the screen parameters */IOCTL (fbfd, fbioget_fscreeninfo, & finfo ); IOCTL (fbfd, fbioget_vscreeninfo, & vinfo);/* calculate the screen buffer size */screensize = vinfo. xres * vinfo. yres * vinfo. bits_per_pixel/8;/* ing the screen buffer to the user address space */FBP = (char *) MMAP (0, screensi Ze, prot_read | prot_write, map_shared, fbfd, 0);/* the following FBP pointer can be used to read and write the buffer */...... /* Release the buffer and close the device */munmap (FBP, screensize); close (fbfd );}

II,IOCTLOperation

IOCTL (fbfd, fbioget_fscreeninfo, & finfo)

Obtain the fb_var_screeninfo structure, which is defined in Linux/include/Linux/FB. h.

IOCTL (fbfd, fbioget_vscreeninfo, & vinfo)

Obtain information about the fb_fix_screeninfon structure. Defined in Linux/include/Linux/FB. h.

Fbfd indicates the device file number.

III.MMAPFunction

Function Description:

The MMAP function is a system call in Unix/Linux.

MMAP maps a file or other objects to the memory. The file is mapped to multiple pages. If the file size is not the sum of the sizes of all pages, the unused space of the last page will be cleared. Munmap performs the opposite operation to delete the object ing of a specific address area.

File-based ing, the st_atime of the mapped file may be updated at any time during MMAP and munmap execution. If the st_atime field is not updated in the preceding circumstances, the value of this field will be updated when the first page index of the ing area is updated for the first time. The file ing established by using the prot_write and map_shared flag. The st_ctime and st_mtime

After writing to the ing area, it is updated before msync () is called using the ms_sync and ms_async flags.

Usage:

#include <sys/mman.h>void *mmap(void *start, size_t length, int prot, int flags,int fd, off_t offset);int munmap(void *start, size_t length); 

Parameters:

Start: Start address of the ing area.

Length: the length of the ing area.

Prot: The expected memory protection flag. It cannot conflict with the file opening mode. Is a value of the following, which can be reasonably combined by the or operation

Prot_exec // page content can be executed

Prot_read // The page content can be read

Prot_write // page can be written

Prot_none // page inaccessible

Flags: specifies the type of the ing object, and whether the ing options and ing pages can be shared. Its value can be a combination of one or more of the following digits.

Map_fixed // use the specified ing start address. If the memory zone specified by the start and Len parameters overlaps the existing ing space, the overlapping part will be discarded. If the specified starting address is unavailable, the Operation will fail. And the start address must be on the boundary of the page.

Map_shared // share the ing space with all other processes mapped to this object. Writing to a shared area is equivalent to outputting data to a file. Until msync () or munmap () is called, the file will not actually be updated.

Map_private // create a private ing for copying when writing. Writing in the memory area does not affect the original file. This flag is mutually exclusive with the above one and can only be used.

Map_denywrite // this flag is ignored.

Map_executable // same as above

Map_noreserve // do not reserve swap space for this ing. When the swap space is retained, modification to the ing area may be guaranteed. When the swap space is not retained and the memory is insufficient, modifying the ing area will cause a segment violation signal.

Map_locked // lock the page in the ing area to prevent the page from being swapped out of memory.

Map_growsdown // used for the stack to tell the kernel VM system that the ing area can be extended down.

Map_anonymous // anonymous ing. The ing area is not associated with any files.

The alias map_anon // map_anonymous, which is no longer used.

Map_file // compatible flag, ignored.

Map_32bit // The ing area is 2 GB lower than the process address space. map_fixed indicates that the ing area is ignored. Currently this flag is supported only on the x86-64 platform.

Map_populate // prepare the page table by pre-reading for file ing. Subsequent access to the ing area will not be blocked by PAGE violations.

Map_nonblock // It is meaningful only when used with map_populate. Do not execute pre-read. only create a page table portal for pages that already exist in the memory.

FD: a valid file description. If map_anonymous is set, the value must be-1 for compatibility issues.

Offset: the starting point of the mapped object content.

 

Return description:

Upon successful execution, MMAP () returns the pointer to the ing area, and munmap () returns 0. When a failure occurs, MMAP () returns map_failed [the value is (void *)-1], and munmap returns-1. Errno is set to a value below

Eacces: Access Error

Eagain: The file is locked or too much memory is locked.

Ebadf: FD is not a valid file description.

Einval: one or more parameters are invalid.

Enfile: the system has reached the limit on opening files.

Enodev: The file system where the specified file is located does not support memory ing.

Enomem: the memory is insufficient, or the process has exceeded the maximum memory ing.

Eperm: insufficient power, operation not allowed

Etxtbsy: used to open a file in the write mode, and specify the map_denywrite flag.

SIGSEGV: Try to write data to the read-only zone

Sigbus: Try to access a memory area that does not belong to the Process

 

Iv. programming example:

Kernel: linux-2.6.29.1

Target Board: friendly arm mini2440

Arm-linux-gcc-4.3.2

Download

======================================

Framebuffer Driver Model

The architecture of the current framebuffer device driver is displayed. The most common driver is non-standard. Obviously, he is at the highest level and programming is the easiest. After understanding this figure, you have easily completed an FB driver, such as writing a driver to the LCD controller of the sa1100, s2410, and s2440 series.

Color Map profiling

In the framebuffer driver design, the East-East cmap is too dizzy. Now I want to give you a naked profiling :)

1. struct fb_cmap

/* Color ing table */struct fb_cmap {_ u32 start;/* First entry */_ u32 Len;/* number of entries */_ 2010* red; /* red */_ 2010* green;/* green */_ 2010* blue;/* Blue */_ 2010* Transp;/* Transparency, null allowed */};

This structure is defined in the FB. h file. In the struct fb_ops structure, there are two member functions related to it:

/* Obtain the color table */INT (* fb_get_cmap) (struct fb_cmap * cmap, int kspc, int con, struct fb_info * info);/* set the color table */INT (* fb_set_cmap) (struct fb_cmap * cmap, int kspc, int con, struct fb_info * info );

There are variables in the struct fb_info structure:

struct fb_cmap cmap; /* Current cmap */

Basic fpgen operations:

extern intfbgen_get_cmap(struct fb_cmap *cmap, int kspc, int con, struct fb_info *info);extern intfbgen_set_cmap(struct fb_cmap *cmap, int kspc, int con, struct fb_info *info);

Provide more cmap applications in the file/* Drivers/Video/fbcmap. C */

 

extern int fb_alloc_cmap(struct fb_cmap *cmap, int len, int transp);extern void fb_copy_cmap(struct fb_cmap *from, struct fb_cmap *to, int fsfromto);extern int fb_get_cmap(struct fb_cmap *cmap, int kspc,int (*getcolreg)(u_int, u_int *, u_int *, u_int *,u_int *, struct fb_info *),struct fb_info *fb_info);extern int fb_set_cmap(struct fb_cmap *cmap, int kspc,int (*setcolreg)(u_int, u_int, u_int, u_int, u_int,struct fb_info *),struct fb_info *fb_info);extern struct fb_cmap *fb_default_cmap(int len);extern void fb_invert_cmaps(void);

2. File Parsing

In the anakinfb. c file, cmap

In stifb. c

The device described in this article is the anakinfb. c driver located in the/video directory. Although I don't know the features of that device, we still know how to compile a frame buffer device driver from the analysis of the program.

This article is a standard FB driver. A total of 221 rows, including the following functions:

Static int random (u_int Regno, u_int * red, u_int * green, u_int * Blue, u_int * transp, struct fb_info * info) 31 rows static int anakinfb_setcolreg (u_int Regno, u_int red, u_int green, u_int blue, u_int transp, struct fb_info * info) 45 rows static int anakinfb_get_fix (struct fb_fix_screeninfo * Fix, int con, struct fb_info * info) 57 rows static int anakinfb_get_var (struct fb_var_screeninfo * var, int con, struct fb_info * info) 75 rows static int struct (struct fb_var_screeninfo * var, int con, struct fb_info * info) 111 rows static int anakinfb_get_cmap (struct fb_cmap * cmap, int kspc, int con, struct fb_info * info) 117 rows static int anakinfb_set_cmap (struct fb_cmap * cmap, int kspc, int con, struct fb_info * info) 130 rows static int anakinfb_switch_con (INT con, struct fb_info * info) 147 rows static int anakinfb_updatevar (INT con, struct fb_info * info) row 155 static void anakinfb_blank (INT blank, struct fb_info * info) Row 161 int _ init anakinfb_init (void) Row 178

Function 1 and 2 are used for register operations. Functions 3, 4, 5, 6, and 7 are fb_ops functions. Function 8 is used to switch the console. Function 9 is used to update variables. Function 10 is used to fl the screen. Function 11 is used to initialize the device.

It's strange that there are no read/write functions for the FB device! It is worth noting that the implementation of open, release, read, write, ioctl, MMAP and other functions is implemented by the fbmem. c file. That is to say, after fb_info is specified for all FB devices, all operations are the same. The functions in fbmem. C can work well on the premise of a clear fb_info. So everyone should feel very easy. As long as the above several device-related functions are completed, the driver of the frame buffer device will be completed :)
System Structure

Stifb Driver Model

Linux/Drivers/Video/stifb. C-generic frame buffer driver for HP * workstations with STI (standard text interface) Video firmware.

This driver is completely different from the previous Anakin device because it is not in a standard format, but based on skeletonfb, which wascreated 28 Dec 1997 by Geert uytterhoeven, that is, skeletonfb. the framework provided by C is complete.

A total of 230 rows, including the following functions:

Static int sti_encode_fix (struct fixed * Fix, const void * par, struct fb_info_gen * info) 60 rows static int sti_decode_var (const struct failed * var, void * par, struct fb_info_gen * info) 71 rows static int sti_encode_var (struct fb_var_screeninfo * var, const void * par, struct fb_info_gen * info) 78 rows static void sti_get_par (void * par, struct fb_info_gen * info) 94 rows static void sti_set_par (const void * par, struct fb_info_gen * info) 99 rows static int sti_getcolreg (unsigned Regno, unsigned * red, unsigned * green, unsigned * blue, unsigned * transp, struct fb_info * info) Row 104 static int sti_setcolreg (unsigned Regno, unsigned red, unsigned green, unsigned blue, unsigned transp, struct fb_info * info) row 3 static void sti_set_disp (const void * par, struct display * disp, struct fb_info_gen * info) Row 3 static void sti_detect (void) Row 3 static int sti_blank (INT blk_mode, const struct fb_info * info) 132 rows int _ init stifb_init (void) 161 rows void stifb_cleanup (struct fb_info * info) 201 rows int _ init stifb_setup (char * options) 208 rows

Original article addressHttp://www.dzkf.cn/html/qianrushixitong/2007/0429/2025_3.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.