当前位置:首页 > linux教程 > 列表

嵌入式Linux中基于framebuffer设备的jpeg格式在本地LCD屏显示

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-22 10:20:51 浏览: 评论:0 

Framebuffer 是用一个视频输出设备从包含完整的帧数据的一个内存缓冲区中来驱动一个视频显示设备,本文我们来看看嵌入式Linux基于framebuffer的jpeg格式在本地LCD屏显示.

在基于Linux的视频监控采集系统中,摄像头采集到的一帧视频图像数据一般都是经过硬件自动压缩成jpeg格式的,然后再保存到摄像头设备的缓冲区.如果要把采集到的jpeg格式显示在本地LCD屏上,由于我们的Linux系统没有移植任何GUI系统,就要考虑以下方面:

1. 将jpeg格式解压缩为位图格式,也就是jpeg解码.

2. 将解码出来的位图格式输出到本地的LCD屏上. 在Linux系统下是通过写入帧缓冲(framebuffer)来实现的.

3. framebuffer相当于为LCD设备提供一个统一的接口,对framebuffer的操控会反映到LCD显示设备上去. 如果配置Linux内核时没有找到支持本地lcd屏这种型号的驱动,那我们要自己写lcd屏驱动,然后选择静态加入内核或以模块的形式加入内核动态加载. 针对以上三点,我们逐一解决:

1.jpeg解码

先了解一下jpeg标准的编码过程:原始的一帧未压缩过的图像可以看成是RGB(红绿蓝)色彩空间上的一组向量集合,但在RGB空间是不利于数据压缩的,因此为了压缩先要把图像映射到利于压缩的YUV空间上(原因是因为人类的眼睛对于亮度差异的敏感度高于色彩变化,而YUV空间的一个基向量Y就是亮度), 这一步叫色彩空间转换.

下一步可以在YUV空间上减少U(色调)和V(饱和度)的成分,也就是在亮度信息不减少的情况下移除部分色彩信息,谁叫人的眼睛对亮度的敏感优于对色彩的敏感呢.这一步叫缩减取样.下一步是将图像从色彩空间映射到频率空间,可以采用的变换方法有:离散余弦变换,傅氏变换,正弦变换等. 其中应用最广的是离散余弦变换(DCT).这一步是无损的,目的是为了在下一步称之为量化的过程中可以经过四舍五入删除高频量得到压缩后的矩阵.量化之后就是对这个矩阵的编码问题了.针对这个矩阵的分布特点,采用"Z"字形的顺序扫描编排,然后进行RLE行程编码,把大量连续重复的数据压缩.最后再用范式Huffman编码.要了解详细的过程,可以查看JPEG标准. 而解码就是以上编码的逆过程了.除非想要自己实现jpeg的编码和解码函数,我们可以不必细究这些过程,而是直接使用别人已经实现的jpeg编码解码库.在Linux平台下,有libjpeg库,它是完全用C语言编写的, 依照它的许可协议,可自由使用,不是GPL协议,它可以用于商业目的. libjpeg的6b版本有个问题,就是解码接口,它只接受文件源.打开源的函数jpeg_stdio_src(j_decompress_ptr cinfo, FILE *infile)要求解码源infile是文件.而我们希望解码的是直接来自映射内存中的数据.要解码内存流的话就要修改libjpeg的源码了,可以参考这里:http://my.unix-center.net/~Simon_fu/?p=565 目前libjpeg的最新版8c已经解决了这个接口不好的问题了,它增加了对内存流解码的支持.通过调用函数

jpeg_mem_src(&cinfo, fdmem, st.st_size);

就可以将保存在内存的jpeg格式数据作为源输入了.因此我们就用libjpeg 8c这个版本来解码,用到的函数主要有:

初始化jpeg解压对象:

  1. /* init jpeg decompress object error handler */ 
  2. cinfo.err = jpeg_std_error(&jerr); 
  3. jpeg_create_decompress(&cinfo); 

绑定jpeg解压对象到输入流:

  1. /* bind jpeg decompress object to infile */ 
  2. READ_FILE    // 从jpeg文件读入 
  3. jpeg_stdio_src(&cinfo, infile); 
  4. f READ_MEM    // 从内存读入jpeg格式 
  5. jpeg_mem_src(&cinfo, fdmem, st.st_size); 
  6. if 

读取jpeg头部信息:

  1. /* read jpeg header */ 
  2. jpeg_read_header(&cinfo, TRUE); 
 

解压过程:

  1. /* decompress process */ 
  2. jpeg_start_decompress(&cinfo); 

调用这个函数之后,可以得到jpeg图像的下面几个参数:

  1. output_width // 图像的宽度 
  2. output_height // 图像的高度 
  3. output_components // 每个像素占用的字节数 

我们采用每扫描一行像素就输出到屏幕的方法的话,根据以上参数可以确定分配一行信息需要的缓冲区:

  1. buffer = (unsigned char *)malloc(cinfo.output_width * 
  2.         cinfo.output_components); 

总共需要扫描output_height行,读取一行扫描数据并输出到LCD屏幕:

  1. y = 0; 
  2. while (cinfo.output_scanline < cinfo.output_height) { 
  3.     jpeg_read_scanlines(&cinfo, &buffer, 1); 
  4.     if (fb_depth == 16) {    // 如果显示设备色深是16位 
  5.         ... 
  6.     } else if (fb_depth == 24) {    // 如果显示设备色深是24位 
  7.         ... 
  8.     } else if (fb_depth == 32) {    // 如果显示设备色深是32位 
  9.         ... 
  10.     } 
  11.     y++; 
  12. } 

结束jpeg解码:

  1. /* finish decompress, destroy decompress object */ 
  2. jpeg_finish_decompress(&cinfo); 
  3. jpeg_destroy_decompress(&cinfo); 

释放缓冲区:

  1. /* release memory buffer */ 
  2. free(buffer); 

2.输出位图到LCD屏

通过framebuffer直接写屏的主要步骤有:

打开framebuffer设备:

  1. /* open framebuffer device */ 
  2. fbdev = fb_open("/dev/fb0"); 

获取framebuffer设备参数:

  1. /* get status of framebuffer device */ 
  2. fb_stat(fbdev, &fb_width, &fb_height, &fb_depth); 

映射framebuffer设备到共享内存:

  1. screensize = fb_width * fb_height * fb_depth / 8; 
  2. fbmem = fb_mmap(fbdev, screensize); 

直接对映射到那片内存进行写操作,LCD屏刷新刷新时就会反应到屏幕上去了.

  1. y = 0; 
  2.        while (cinfo.output_scanline < cinfo.output_height) { 
  3.            jpeg_read_scanlines(&cinfo, &buffer, 1); 
  4.            if (fb_depth == 16) { 
  5.                unsigned short color; 
  6.  
  7.                for (x = 0; x < cinfo.output_width; x++) { 
  8.                    color = 
  9.                        RGB888toRGB565(buffer[x * 3], 
  10.                                buffer[x * 3 + 1], buffer[x * 3 + 2]); 
  11.                    fb_pixel(fbmem, fb_width, fb_height, x, y, color); 
  12.                } 
  13.            } else if (fb_depth == 24) { 
  14.                memcpy((unsigned char *) fbmem + y * fb_width * 3, 
  15.                        buffer, cinfo.output_width * cinfo.output_components); 
  16.            } else if (fb_depth == 32) { 
  17.                // memcpy((unsigned char *) fbmem + y * fb_width * 4, 
  18.                        // buffer, cinfo.output_width * cinfo.output_components); 
  19.                for (x = 0; x < cinfo.output_width; x++) { 
  20.                    *(fbmem + y * fb_width * 4 + x * 4)     = (unsigned char) buffer[x * 3 + 2]; 
  21.                    *(fbmem + y * fb_width * 4 + x * 4 + 1) = (unsigned char) buffer[x * 3 + 1]; 
  22.                    *(fbmem + y * fb_width * 4 + x * 4 + 2) = (unsigned char) buffer[x * 3 + 0]; 
  23.                    *(fbmem + y * fb_width * 4 + x * 4 + 3) = (unsigned char) 0; 
  24.                }  //phpfensi.com 
  25.            } 
  26.            y++;    // next scanline 
  27.        } 

卸载映射framebuffer的那部分内存:

  1.     /* unmap framebuffer's shared memory */ 
  2.     fb_munmap(fbmem, screensize); 
  3.  
  4. 关闭framebuffer设备: 
  5.  
  6.     close(fbdev); 

根据以上两点,可以写一个测试程序,在不开X-window图形系统的情况下,将本地的jpeg文件直接显示到屏幕上.

  1. #include    <stdio.h> 
  2. #include    <string.h> 
  3. #include    <stdlib.h> 
  4. #include    <unistd.h> 
  5. #include    <sys/ioctl.h> 
  6. #include    <sys/types.h> 
  7. #include    <sys/stat.h> 
  8. #include    <errno.h> 
  9. #include    <fcntl.h> 
  10. #include    <sys/mman.h> 
  11. #include    <linux/fb.h> 
  12. #include    "jpeglib.h" 
  13. #include    "jerror.h" 
  14.  
  15. #define FB_DEV  "/dev/fb0" 
  16. #define __fnc__ __FUNCTION__ 
  17.  
  18. #define debug           0 
  19. #define debug_printf    0 
  20. #define BYREAD          0 
  21. #define BYMEM           1 
  22.  
  23. /* function deciaration */ 
  24.  
  25. void usage(char *msg); 
  26. unsigned short RGB888toRGB565(unsigned char red, 
  27.         unsigned char green, unsigned char blue); 
  28. int fb_open(char *fb_device); 
  29. int fb_close(int fd); 
  30. int fb_stat(int fd, unsigned int *width, unsigned int *height, unsigned int *    depth); 
  31. void *fb_mmap(int fd, unsigned int screensize); 
  32. void *fd_mmap(int fd, unsigned int filesize); 
  33. int fb_munmap(void *start, size_t length); 
  34. int fb_pixel(void *fbmem, int width, int height, 
  35.         int x, int y, unsigned short color); 
  36.  
  37. #if(debug) 
  38. void draw(unsigned char *fbp, 
  39.         struct fb_var_screeninfo vinfo, 
  40.         struct fb_fix_screeninfo finfo); 
  41. #endif 
  42.  
  43. /* function implementation */ 
  44.  
  45. int main(int argc, char **argv) 
  46. { 
  47.     struct jpeg_decompress_struct cinfo; 
  48.     struct jpeg_error_mgr jerr; 
  49. #if(BYREAD) 
  50.     FILE *infile; 
  51. #endif 
  52.     int fd; 
  53.     unsigned char *buffer; 
  54.     struct stat st; 
  55.  
  56.     int fbdev; 
  57.     char *fb_device; 
  58.     unsigned char *fbmem; 
  59.     unsigned char *fdmem; 
  60.     unsigned int screensize; 
  61.     unsigned int fb_width; 
  62.     unsigned int fb_height; 
  63.     unsigned int fb_depth; 
  64.     register unsigned int x; 
  65.     register unsigned int y; 
  66.  
  67.     /* check auguments */ 
  68.     if (argc != 2) { 
  69.         usage("insuffient auguments"); 
  70.         exit(-1); 
  71.     } 
  72.  
  73.     /* open framebuffer device */ 
  74.     if ((fb_device = getenv("FRAMEBUFFER")) == NULL) 
  75.         fb_device = FB_DEV; 
  76.     fbdev = fb_open(fb_device); 
  77.  
  78.     /* get status of framebuffer device */ 
  79.     fb_stat(fbdev, &fb_width, &fb_height, &fb_depth); 
  80.  
  81.     /* map framebuffer device to shared memory */ 
  82.     screensize = fb_width * fb_height * fb_depth / 8; 
  83.     fbmem = fb_mmap(fbdev, screensize); 
  84.  
  85. #if (BYREAD) 
  86.     /* open input jpeg file */ 
  87.     if ((infile = fopen(argv[1], "rb")) == NULL) { 
  88.         fprintf(stderr, "open %s failedn", argv[1]); 
  89.         exit(-1); 
  90.     } 
  91. #endif 
  92.  
  93.     if ((fd = open(argv[1], O_RDONLY)) < 0) { 
  94.         perror("open"); 
  95.         exit(-1); 
  96.     } 
  97.  
  98.     if (fstat(fd, &st) < 0) { 
  99.         perror("fstat"); 
  100.         exit(-1); 
  101.     } 
  102.  
  103.     fdmem = fd_mmap(fd, st.st_size); 
  104.  
  105.     /* init jpeg decompress object error handler */ 
  106.     cinfo.err = jpeg_std_error(&jerr); 
  107.     jpeg_create_decompress(&cinfo); 
  108.  
  109.     /* bind jpeg decompress object to infile */ 
  110. #if (BYREAD) 
  111.     jpeg_stdio_src(&cinfo, infile); 
  112. #endif 
  113.  
  114. #if (BYMEM) 
  115.     jpeg_mem_src(&cinfo, fdmem, st.st_size); 
  116. #endif 
  117.  
  118.     /* read jpeg header */ 
  119.     jpeg_read_header(&cinfo, TRUE); 
  120.  
  121.     /* decompress process */ 
  122.     jpeg_start_decompress(&cinfo); 
  123.     if ((cinfo.output_width > fb_width) || 
  124.             (cinfo.output_height > fb_height)) { 
  125.         printf("too large jpeg file, can't displayn"); 
  126. #if (0) 
  127.         return -1; 
  128. #endif 
  129.     } 
  130.  
  131.     buffer = (unsigned char *) malloc(cinfo.output_width * 
  132.             cinfo.output_components); 
  133.  
  134.     struct fb_fix_screeninfo fb_finfo; 
  135.     struct fb_var_screeninfo fb_vinfo; 
  136.  
  137.     if (ioctl(fbdev, FBIOGET_FSCREENINFO, &fb_finfo)) { 
  138.         perror(__fnc__); 
  139.         return -1; 
  140.     } 
  141.  
  142.     if (ioctl(fbdev, FBIOGET_VSCREENINFO, &fb_vinfo)) { 
  143.         perror(__fnc__); 
  144.         return -1; 
  145.     } 
  146.  
  147. #if(debug) 
  148.     draw(fbmem, fb_vinfo, fb_finfo); 
  149. #endif 
  150.     y = 0; 
  151.     while (cinfo.output_scanline < cinfo.output_height) { 
  152.         jpeg_read_scanlines(&cinfo, &buffer, 1); 
  153.         if (fb_depth == 16) { 
  154.             unsigned short color; 
  155.  
  156.             for (x = 0; x < cinfo.output_width; x++) { 
  157.                 color = 
  158.                     RGB888toRGB565(buffer[x * 3], 
  159.                             buffer[x * 3 + 1], buffer[x * 3 + 2]); 
  160.                 fb_pixel(fbmem, fb_width, fb_height, x, y, color); 
  161.             } 
  162.         } else if (fb_depth == 24) { 
  163.             memcpy((unsigned char *) fbmem + y * fb_width * 3, 
  164.                     buffer, cinfo.output_width * cinfo.output_components); 
  165.         } else if (fb_depth == 32) { 
  166.             // memcpy((unsigned char *) fbmem + y * fb_width * 4, 
  167.                     // buffer, cinfo.output_width * cinfo.output_components); 
  168.             for (x = 0; x < cinfo.output_width; x++) { 
  169.                 * (fbmem + y * fb_width * 4 + x * 4)     = (unsigned char)       buffer[x * 3 + 2]; 
  170.                 * (fbmem + y * fb_width * 4 + x * 4 + 1) = (unsigned char)       buffer[x * 3 + 1]; 
  171.                 * (fbmem + y * fb_width * 4 + x * 4 + 2) = (unsigned char)       buffer[x * 3 + 0]; 
  172.                 * (fbmem + y * fb_width * 4 + x * 4 + 3) = (unsigned char) 0; 
  173.             } 
  174.         } 
  175.         y++;    // next scanline 
  176.     } 
  177.  
  178.     /* finish decompress, destroy decompress object */ 
  179.     jpeg_finish_decompress(&cinfo); 
  180.     jpeg_destroy_decompress(&cinfo); 
  181.  
  182.     /* release memory buffer */ 
  183.     free(buffer); 
  184.  
  185. #if (BYREAD) 
  186.     /* close jpeg inputing file */ 
  187.     fclose(infile); 
  188. #endif 
  189.  
  190.     /* unmap framebuffer's shared memory */ 
  191.     fb_munmap(fbmem, screensize); 
  192.  
  193. #if (BYMEM) 
  194.     munmap(fdmem, (size_t) st.st_size); 
  195.     close(fd); 
  196. #endif 
  197.  
  198.     /* close framebuffer device */ 
  199.     fb_close(fbdev); 
  200.  
  201.     return 0; 
  202. } 
  203.  
  204. void usage(char *msg) 
  205. { 
  206.     fprintf(stderr, "%sn", msg); 
  207.     printf("Usage: fv some-jpeg-file.jpgn"); 
  208. } 
  209.  
  210. /* open framebuffer device. 
  211.  * return positive file descriptor if success, 
  212.  * else return -1 
  213.  */ 
  214. int fb_open(char *fb_device) 
  215. { 
  216.     int fd; 
  217.  
  218.     if ((fd = open(fb_device, O_RDWR)) < 0) { 
  219.         perror(__fnc__); 
  220.         return -1; 
  221.     } 
  222.     return fd; 
  223. } 
  224.  
  225. int fb_close(int fd) 
  226. { 
  227.     return (close(fd)); 
  228. } 
  229.  
  230. /* get framebuffer's width, height, and depth. 
  231.  * return 0 if success, else return -1. 
  232.  */ 
  233. int fb_stat(int fd, unsigned int *width, unsigned int *height, unsigned int *    depth) 
  234. { 
  235.     struct fb_fix_screeninfo fb_finfo; 
  236.     struct fb_var_screeninfo fb_vinfo; 
  237.  
  238.     if (ioctl(fd, FBIOGET_FSCREENINFO, &fb_finfo)) { 
  239.         perror(__fnc__); 
  240.         return -1; 
  241.     } 
  242.  
  243.     if (ioctl(fd, FBIOGET_VSCREENINFO, &fb_vinfo)) { 
  244.         perror(__fnc__); 
  245.         return -1; 
  246.     } 
  247.  
  248.     *width = fb_vinfo.xres; 
  249.     *height = fb_vinfo.yres; 
  250.     *depth = fb_vinfo.bits_per_pixel; 
  251.  
  252.     return 0; 
  253. } 
  254.  
  255. /* map shared memory to framebuffer device. 
  256.  * return maped memory if success 
  257.  * else return -1, as mmap dose 
  258.  */ 
  259. void *fb_mmap(int fd, unsigned int screensize) 
  260. { 
  261.     caddr_t fbmem; 
  262.  
  263.     if ((fbmem = mmap(0, screensize, PROT_READ | PROT_WRITE, 
  264.                     MAP_SHARED, fd, 0)) == MAP_FAILED) { 
  265.         perror(__func__); 
  266.         return (void *) (-1); 
  267.     } 
  268.  
  269.     return fbmem; 
  270. } 
  271.  
  272. /* map shared memmory to a opened file */ 
  273. void *fd_mmap(int fd, unsigned int filesize) 
  274. { 
  275.     caddr_t fdmem; 
  276.  
  277.     if ((fdmem = mmap(0, filesize, PROT_READ, 
  278.                     MAP_SHARED, fd, 0)) == MAP_FAILED) { 
  279.         perror(__func__); 
  280.         return (void *) (-1); 
  281.     } 
  282.  
  283.     return fdmem; 
  284. } 
  285.  
  286. /* unmap map memory for framebuffer device */ 
  287. int fb_munmap(void *start, size_t length) 
  288. { 
  289.     return (munmap(start, length)); 
  290. } 
  291.  
  292. /* convert 24bit RGB888 to 16bit RGB565 color format */ 
  293. unsigned short RGB888toRGB565(unsigned char red, 
  294.         unsigned char green, unsigned char blue) 
  295. { 
  296.     unsigned short B = (blue >> 3) & 0x001F; 
  297.     unsigned short G = ((green >> 2) << 5) & 0x07E0; 
  298.     unsigned short R = ((red >> 3) << 11) & 0xF800; 
  299.  
  300.     return (unsigned short) (R | G | B); 
  301. } 
  302.  
  303. /* display a pixel on the framebuffer device. 
  304.  * fbmem is the starting memory of framebuffer, 
  305.  * width and height are dimension of framebuffer, 
  306.  * width and height are dimension of framebuffer, 
  307.  * x and y are the coordinates to display, 
  308.  * color is the pixel's color value. 
  309.  * return 0 if success, otherwise return -1. 
  310.  */ 
  311. int fb_pixel(void *fbmem, int width, int height, 
  312.         int x, int y, unsigned short color) 
  313. { 
  314.     if ((x > width) || (y > height)) 
  315.         return -1; 
  316.  
  317.     unsigned short *dst = ((unsigned short *) fbmem + y * width + x); 
  318.  
  319.     *dst = color; 
  320.     return 0; 
  321. } 

3.LCD驱动

我们用到的是一块东华3.5寸数字屏,型号为WXCAT35-TG3.下面的驱动程序是韦东山老师课堂上现场写的,如下:

  1. #include <linux/module.h> 
  2. #include <linux/kernel.h> 
  3. #include <linux/errno.h> 
  4. #include <linux/string.h> 
  5. #include <linux/mm.h> 
  6. #include <linux/slab.h> 
  7. #include <linux/delay.h> 
  8. #include <linux/interrupt.h> 
  9. #include <linux/fb.h> 
  10. #include <linux/init.h> 
  11. #include <linux/ioport.h> 
  12. #include <linux/dma-mapping.h> 
  13.  
  14. #include <asm/uaccess.h> 
  15. #include <asm/system.h> 
  16. #include <asm/irq.h> 
  17. #include <asm/setup.h> 
  18.  
  19. /* WXCAT35-TG3 */ 
  20. struct s3c_lcd_regs { 
  21.     unsigned long    lcdcon1; 
  22.     unsigned long    lcdcon2; 
  23.     unsigned long    lcdcon3; 
  24.     unsigned long    lcdcon4; 
  25.     unsigned long    lcdcon5; 
  26.     unsigned long    lcdsaddr1; 
  27.     unsigned long    lcdsaddr2; 
  28.     unsigned long    lcdsaddr3; 
  29.     unsigned long    redlut; 
  30.     unsigned long    greenlut; 
  31.     unsigned long    bluelut; 
  32.     unsigned long    reserved[9]; 
  33.     unsigned long    dithmode; 
  34.     unsigned long    tpal; 
  35.     unsigned long    lcdintpnd; 
  36.     unsigned long    lcdsrcpnd; 
  37.     unsigned long    lcdintmsk; 
  38.     unsigned long    lpcsel; 
  39. }; 
  40.  
  41. static u32 colregs[16]; 
  42. static struct fb_info *s3c_fb_info; 
  43. static dma_addr_t s3c_fb_handle; 
  44. static unsigned long fb_va; 
  45.  
  46. /* from pxafb.c */ 
  47. static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf) 
  48. { 
  49.     chan &= 0xffff; 
  50.     chan >>= 16 - bf->length; 
  51.     return chan << bf->offset; 
  52. } 
  53.  
  54. static int s3cfb_setcolreg(unsigned regno, 
  55.                    unsigned red, unsigned green, unsigned blue, 
  56.                    unsigned transp, struct fb_info *info) 
  57. { 
  58.     unsigned int val; 
  59.  
  60.     /* dprintk("setcol: regno=%d, rgb=%d,%d,%dn", regno, red, green, blue); */ 
  61.  
  62.     /* true-colour, use pseuo-palette */ 
  63.  
  64.     if (regno < 16) { 
  65.         u32 *pal = s3c_fb_info->pseudo_palette; 
  66.  
  67.         val  = chan_to_field(red,   &s3c_fb_info->var.red); 
  68.         val |= chan_to_field(green, &s3c_fb_info->var.green); 
  69.         val |= chan_to_field(blue,  &s3c_fb_info->var.blue); 
  70.  
  71.         pal[regno] = val; 
  72.     } 
  73.  
  74.     return 0; 
  75. } 
  76.  
  77. static struct fb_ops s3cfb_ops = { 
  78.     .owner        = THIS_MODULE, 
  79. //    .fb_check_var    = clps7111fb_check_var, 
  80. //    .fb_set_par    = clps7111fb_set_par, 
  81. //    .fb_setcolreg    = clps7111fb_setcolreg, 
  82. //    .fb_blank    = clps7111fb_blank, 
  83.  
  84.     .fb_setcolreg    = s3cfb_setcolreg, 
  85.     .fb_fillrect    = cfb_fillrect, 
  86.     .fb_copyarea    = cfb_copyarea, 
  87.     .fb_imageblit    = cfb_imageblit, 
  88. }; 
  89.  
  90. struct s3c_lcd_regs *s3c_lcd_regs; 
  91. static volatile unsigned long *gpccon; 
  92. static volatile unsigned long *gpdcon; 
  93. static volatile unsigned long *gpgcon; 
  94.  
  95. int s3c_lcd_init(void) 
  96. { 
  97.     extern int debug_lcd; 
  98.     /* 1. 分配一个fb_info结构体 */ 
  99.     s3c_fb_info = framebuffer_alloc(0, NULL); 
  100.     printk("%s %dn", __FUNCTION__, __LINE__); 
  101.  
  102.     /* 2. 设置fb_info结构体 */ 
  103.     /* 
  104.        2.1 设置固定的信息 
  105.        2.2 设置可变的信息 
  106.        2.3 设置操作函数 
  107.     */ 
  108.  
  109.     /* 24BPP(bits per pixel), 会用到4字节, 其中浪费1字节 */ 
  110.     strcpy(s3c_fb_info->fix.id, "WXCAT35-TG3"); 
  111.     // s3c_fb_info->fix.smem_start // frame buffer's physical address 
  112.     s3c_fb_info->fix.smem_len    = 320*240*32/8; 
  113.     s3c_fb_info->fix.type        = FB_TYPE_PACKED_PIXELS; 
  114.     s3c_fb_info->fix.visual      = FB_VISUAL_TRUECOLOR; 
  115.     s3c_fb_info->fix.line_length = 320 * 4; 
  116.  
  117.     s3c_fb_info->var.xres             = 320; 
  118.     s3c_fb_info->var.yres             = 240; 
  119.     s3c_fb_info->var.xres_virtual     = 320; 
  120.     s3c_fb_info->var.yres_virtual     = 240; 
  121.     s3c_fb_info->var.bits_per_pixel   = 32; 
  122.  
  123.     s3c_fb_info->var.red.offset       = 16; 
  124.     s3c_fb_info->var.red.length       = 8; 
  125.  
  126.     s3c_fb_info->var.green.offset     = 8; 
  127.     s3c_fb_info->var.green.length     = 8; 
  128.  
  129.     s3c_fb_info->var.blue.offset      = 0; 
  130.     s3c_fb_info->var.blue.length      = 8; 
  131.  
  132.     //s3c_fb_info->var.activate         = FB_ACTIVATE; 
  133.  
  134.     s3c_fb_info->fbops                = &s3cfb_ops; 
  135.     s3c_fb_info->pseudo_palette       = colregs; 
  136.  
  137.     /* 3. 硬件相关的操作 */ 
  138.     /* 配置GPIO */ 
  139.     gpccon     = ioremap(0x56000020, 4); 
  140.     gpdcon     = ioremap(0x56000030, 4); 
  141.     gpgcon     = ioremap(0x56000060, 4); 
  142.     *gpccon = 0xaaaaaaaa; 
  143.     *gpdcon = 0xaaaaaaaa; 
  144.     *gpgcon |= (3<<8);  /* GPG4 use as lcd_pwren */ 
  145.     printk("%s %dn", __FUNCTION__, __LINE__); 
  146.  
  147.     s3c_lcd_regs = ioremap(0X4D000000, sizeof(struct s3c_lcd_regs)); 
  148.  
  149.     /* 
  150.      * VCLK = HCLK / [(CLKVAL+1)x2] = 100M/[(CLKVAL+1)x2] = 6.4 
  151.      * CLKVAL = 6.8 = 7 
  152.      * TFT LCD panel 
  153.      * 24bpp 
  154.      */ 
  155.     s3c_lcd_regs->lcdcon1 = (7<<8)|(0<<7)|(3<<5)|(0x0d<<1)|(0<<0); 
  156.     printk("%s %dn", __FUNCTION__, __LINE__); 
  157.  
  158.     /* VBPD: 电子枪收到VSYNC信号后,"多长时间"才能跳回第1行 
  159.      * VBPD=14,      LCD: tvb=15 
  160.      * LINEVAL=239,  LCD: 有240行 
  161.      * VFPD=11,      LCD: tvf=12  // 发出最后一行数据后,再过多长时间才发出VSYNC 
  162.      * VSPW=2,       LCD: tvp=3   // VSYNC的宽度 
  163.      */ 
  164.     s3c_lcd_regs->lcdcon2 = (14<<24)|(239<<14)|(11<<6)|(2<<0); 
  165.  
  166.     /* HBPD: 电子枪收到HSYNC信号后,"多长时间"才能跳回第1列 
  167.      * HBPD=37,      LCD: thb=38 
  168.      * HORVAL=319,   LCD: 有320行 
  169.      * HFPD=19,      LCD: thf=20  // 发出最后一象素数据后,再过多长时间才发出HSYNC 
  170.      * HSPW=29,      LCD: thp=30   // VSYNC的宽度 
  171.      */ 
  172.     s3c_lcd_regs->lcdcon3 = (37<<19)|(319<<8)|(19<<0); 
  173.     s3c_lcd_regs->lcdcon4 = 29; 
  174.  
  175.     /* bit10:  在VCLK上升沿取数据  
  176.      * bit9 :  VSYNC低电平有效 
  177.      * bit8 :  HSYNC低电平有效 
  178.      * bit5 :  PWREN低电平有效 
  179.      */     
  180.     s3c_lcd_regs->lcdcon5 = (1<<10)|(1<<9)|(1<<8)|(1<<5)|(0<<3); 
  181.  
  182.     /* 分配frame buffer */ 
  183.     fb_va = (unsigned long)dma_alloc_writecombine(NULL, s3c_fb_info->fix.smem_len, &s3c_fb_handle, GFP_KERNEL); 
  184.  
  185.     printk("fb_va = 0x%x, pa = 0x%xn", fb_va, s3c_fb_handle); 
  186.     s3c_fb_info->fix.smem_start = s3c_fb_handle; 
  187.     s3c_fb_info->screen_base    = fb_va; 
  188.  
  189.     /* 把framebuffer的地址告诉LCD控制器 */ 
  190.     s3c_lcd_regs->lcdsaddr1 = (s3c_fb_info->fix.smem_start >> 1); 
  191.     s3c_lcd_regs->lcdsaddr2 = ((s3c_fb_info->fix.smem_start+320*240*4) >> 1) & 0x1fffff; 
  192.     s3c_lcd_regs->lcdsaddr3 = 320*4/2; 
  193.  
  194.     /* 使能LCD */ 
  195.     s3c_lcd_regs->lcdcon1 |= (1<<0); 
  196.  
  197.     /* 4. register_framebuffer */ 
  198.     printk("%s %dn", __FUNCTION__, __LINE__); 
  199.     //debug_lcd = 1; 
  200.     register_framebuffer(s3c_fb_info); 
  201.     printk("%s %dn", __FUNCTION__, __LINE__); 
  202.  
  203.     return 0;  
  204. } 
  205.  
  206. void s3c_lcd_exit(void) 
  207. { 
  208.     unregister_framebuffer(s3c_fb_info); 
  209.     dma_free_writecombine(NULL, s3c_fb_info->fix.smem_len, fb_va, s3c_fb_handle); 
  210.     iounmap(s3c_lcd_regs); 
  211.     iounmap(gpccon); 
  212.     iounmap(gpdcon); 
  213.     iounmap(gpgcon); 
  214.     framebuffer_release(s3c_fb_info); 
  215. } 
  216.  
  217. module_init(s3c_lcd_init); 
  218. module_exit(s3c_lcd_exit); 
  219.  
  220. MODULE_LICENSE("GPL"); 

然后把它加入到内核,以静态加载的模式启动.最后,可以把读取内存jpeg格式数据输出到LCD屏的这部分整合到mjpg-stream或servfox去,就实现了采集图像本地显示了.

Tags: framebuffer 嵌入式Linux

分享到: