国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

嵌入式Linux中基于framebuffer設備的jpeg格式在本地LCD屏顯示

2024-08-27 23:58:48
字體:
來源:轉載
供稿:網友

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++; 

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

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.     chan &= 0xffff; 
  49.     chan >>= 16 - bf->length; 
  50.     return chan << bf->offset; 
  51.  
  52. static int s3cfb_setcolreg(unsigned regno, 
  53.                    unsigned red, unsigned green, unsigned blue, 
  54.                    unsigned transp, struct fb_info *info) 
  55.     unsigned int val; 
  56.  
  57.     /* dprintk("setcol: regno=%d, rgb=%d,%d,%dn", regno, red, green, blue); */ 
  58.  
  59.     /* true-colour, use pseuo-palette */ 
  60.  
  61.     if (regno < 16) { 
  62.         u32 *pal = s3c_fb_info->pseudo_palette; 
  63.  
  64.         val  = chan_to_field(red,   &s3c_fb_info->var.red); 
  65.         val |= chan_to_field(green, &s3c_fb_info->var.green); 
  66.         val |= chan_to_field(blue,  &s3c_fb_info->var.blue); 
  67.  
  68.         pal[regno] = val; 
  69.     } 
  70.  
  71.     return 0; 
  72.  
  73. static struct fb_ops s3cfb_ops = { 
  74.     .owner        = THIS_MODULE, 
  75. //    .fb_check_var    = clps7111fb_check_var, 
  76. //    .fb_set_par    = clps7111fb_set_par, 
  77. //    .fb_setcolreg    = clps7111fb_setcolreg, 
  78. //    .fb_blank    = clps7111fb_blank, 
  79.  
  80.     .fb_setcolreg    = s3cfb_setcolreg, 
  81.     .fb_fillrect    = cfb_fillrect, 
  82.     .fb_copyarea    = cfb_copyarea, 
  83.     .fb_imageblit    = cfb_imageblit, 
  84. }; 
  85.  
  86. struct s3c_lcd_regs *s3c_lcd_regs; 
  87. static volatile unsigned long *gpccon; 
  88. static volatile unsigned long *gpdcon; 
  89. static volatile unsigned long *gpgcon; 
  90.  
  91. int s3c_lcd_init(void) 
  92.     extern int debug_lcd; 
  93.     /* 1. 分配一個fb_info結構體 */ 
  94.     s3c_fb_info = framebuffer_alloc(0, NULL); 
  95.     printk("%s %dn"__FUNCTION____LINE__); 
  96.  
  97.     /* 2. 設置fb_info結構體 */ 
  98.     /* 
  99.        2.1 設置固定的信息 
  100.        2.2 設置可變的信息 
  101.        2.3 設置操作函數 
  102.     */ 
  103.  
  104.     /* 24BPP(bits per pixel), 會用到4字節, 其中浪費1字節 */ 
  105.     strcpy(s3c_fb_info->fix.id, "WXCAT35-TG3"); 
  106.     // s3c_fb_info->fix.smem_start // frame buffer's physical address 
  107.     s3c_fb_info->fix.smem_len    = 320*240*32/8; 
  108.     s3c_fb_info->fix.type        = FB_TYPE_PACKED_PIXELS; 
  109.     s3c_fb_info->fix.visual      = FB_VISUAL_TRUECOLOR; 
  110.     s3c_fb_info->fix.line_length = 320 * 4; 
  111.  
  112.     s3c_fb_info->var.xres             = 320; 
  113.     s3c_fb_info->var.yres             = 240; 
  114.     s3c_fb_info->var.xres_virtual     = 320; 
  115.     s3c_fb_info->var.yres_virtual     = 240; 
  116.     s3c_fb_info->var.bits_per_pixel   = 32; 
  117.  
  118.     s3c_fb_info->var.red.offset       = 16; 
  119.     s3c_fb_info->var.red.length       = 8; 
  120.  
  121.     s3c_fb_info->var.green.offset     = 8; 
  122.     s3c_fb_info->var.green.length     = 8; 
  123.  
  124.     s3c_fb_info->var.blue.offset      = 0; 
  125.     s3c_fb_info->var.blue.length      = 8; 
  126.  
  127.     //s3c_fb_info->var.activate         = FB_ACTIVATE; 
  128.  
  129.     s3c_fb_info->fbops                = &s3cfb_ops; 
  130.     s3c_fb_info->pseudo_palette       = colregs; 
  131.  
  132.     /* 3. 硬件相關的操作 */ 
  133.     /* 配置GPIO */ 
  134.     gpccon     = ioremap(0x56000020, 4); 
  135.     gpdcon     = ioremap(0x56000030, 4); 
  136.     gpgcon     = ioremap(0x56000060, 4); 
  137.     *gpccon = 0xaaaaaaaa; 
  138.     *gpdcon = 0xaaaaaaaa; 
  139.     *gpgcon |= (3<<8);  /* GPG4 use as lcd_pwren */ 
  140.     printk("%s %dn"__FUNCTION____LINE__); 
  141.  
  142.     s3c_lcd_regs = ioremap(0X4D000000, sizeof(struct s3c_lcd_regs)); 
  143.  
  144.     /* 
  145.      * VCLK = HCLK / [(CLKVAL+1)x2] = 100M/[(CLKVAL+1)x2] = 6.4 
  146.      * CLKVAL = 6.8 = 7 
  147.      * TFT LCD panel 
  148.      * 24bpp 
  149.      */ 
  150.     s3c_lcd_regs->lcdcon1 = (7<<8)|(0<<7)|(3<<5)|(0x0d<<1)|(0<<0); 
  151.     printk("%s %dn"__FUNCTION____LINE__); 
  152.  
  153.     /* VBPD: 電子槍收到VSYNC信號后,"多長時間"才能跳回第1行 
  154.      * VBPD=14,      LCD: tvb=15 
  155.      * LINEVAL=239,  LCD: 有240行 
  156.      * VFPD=11,      LCD: tvf=12  // 發出最后一行數據后,再過多長時間才發出VSYNC 
  157.      * VSPW=2,       LCD: tvp=3   // VSYNC的寬度 
  158.      */ 
  159.     s3c_lcd_regs->lcdcon2 = (14<<24)|(239<<14)|(11<<6)|(2<<0); 
  160.  
  161.     /* HBPD: 電子槍收到HSYNC信號后,"多長時間"才能跳回第1列 
  162.      * HBPD=37,      LCD: thb=38 
  163.      * HORVAL=319,   LCD: 有320行 
  164.      * HFPD=19,      LCD: thf=20  // 發出最后一象素數據后,再過多長時間才發出HSYNC 
  165.      * HSPW=29,      LCD: thp=30   // VSYNC的寬度 
  166.      */ 
  167.     s3c_lcd_regs->lcdcon3 = (37<<19)|(319<<8)|(19<<0); 
  168.     s3c_lcd_regs->lcdcon4 = 29; 
  169.  
  170.     /* bit10:  在VCLK上升沿取數據  
  171.      * bit9 :  VSYNC低電平有效 
  172.      * bit8 :  HSYNC低電平有效 
  173.      * bit5 :  PWREN低電平有效 
  174.      */     
  175.     s3c_lcd_regs->lcdcon5 = (1<<10)|(1<<9)|(1<<8)|(1<<5)|(0<<3); 
  176.  
  177.     /* 分配frame buffer */ 
  178.     fb_va = (unsigned long)dma_alloc_writecombine(NULL, s3c_fb_info->fix.smem_len, &s3c_fb_handle, GFP_KERNEL); 
  179.  
  180.     printk("fb_va = 0x%x, pa = 0x%xn", fb_va, s3c_fb_handle); 
  181.     s3c_fb_info->fix.smem_start = s3c_fb_handle; 
  182.     s3c_fb_info->screen_base    = fb_va; 
  183.  
  184.     /* 把framebuffer的地址告訴LCD控制器 */ 
  185.     s3c_lcd_regs->lcdsaddr1 = (s3c_fb_info->fix.smem_start >> 1); 
  186.     s3c_lcd_regs->lcdsaddr2 = ((s3c_fb_info->fix.smem_start+320*240*4) >> 1) & 0x1fffff; 
  187.     s3c_lcd_regs->lcdsaddr3 = 320*4/2; 
  188.  
  189.     /* 使能LCD */ 
  190.     s3c_lcd_regs->lcdcon1 |= (1<<0); 
  191.  
  192.     /* 4. register_framebuffer */ 
  193.     printk("%s %dn"__FUNCTION____LINE__); 
  194.     //debug_lcd = 1; 
  195.     register_framebuffer(s3c_fb_info); 
  196.     printk("%s %dn"__FUNCTION____LINE__); 
  197.  
  198.     return 0;  
  199.  
  200. void s3c_lcd_exit(void) 
  201.     unregister_framebuffer(s3c_fb_info); 
  202.     dma_free_writecombine(NULL, s3c_fb_info->fix.smem_len, fb_va, s3c_fb_handle); 
  203.     iounmap(s3c_lcd_regs); 
  204.     iounmap(gpccon); 
  205.     iounmap(gpdcon); 
  206.     iounmap(gpgcon); 
  207.     framebuffer_release(s3c_fb_info); 
  208.  
  209. module_init(s3c_lcd_init); 
  210. module_exit(s3c_lcd_exit); 
  211.  
  212. MODULE_LICENSE("GPL"); 

然后把它加入到內核,以靜態加載的模式啟動.最后,可以把讀取內存jpeg格式數據輸出到LCD屏的這部分整合到mjpg-stream或servfox去,就實現了采集圖像本地顯示了.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳谷县| 美姑县| 高要市| 长岛县| 上蔡县| 开封市| 崇礼县| 四子王旗| 民乐县| 阿坝县| 普兰县| 宣威市| 汉寿县| 新田县| 瑞安市| 凉山| 从化市| 舒城县| 平南县| 特克斯县| 河北省| 无棣县| 常熟市| 福泉市| 马公市| 吉林市| 铅山县| 荆州市| 南涧| 鄂伦春自治旗| 湖州市| 三门县| 库尔勒市| 同仁县| 呼伦贝尔市| 兰州市| 寻乌县| 康乐县| 新野县| 和田市| 修文县|