以前沒(méi)有寫Lcd驅(qū)動(dòng)程序,現(xiàn)在開(kāi)始做項(xiàng)目了,才發(fā)現(xiàn)Lcd驅(qū)動(dòng)程序必須認(rèn)真學(xué)習(xí),我總結(jié)所以驅(qū)動(dòng)中LCD和網(wǎng)卡驅(qū)動(dòng)是最難的,LCD主要是需要配置的寄存器太多了,還是老規(guī)矩先上代碼。
內(nèi)核版本:linux-3.4.2 lcd:4.3
上代碼之前我得講解一些基本的知識(shí)點(diǎn),LCD驅(qū)動(dòng)我們只需要寫硬件這一塊的代碼就可以了,下面有三個(gè)函數(shù)內(nèi)核已經(jīng)幫我們寫好了,我們只需要調(diào)用就可以了,這幾個(gè)函數(shù)實(shí)現(xiàn)了內(nèi)核層和應(yīng)用成數(shù)據(jù)的傳遞,有興趣的朋友去分析一下源碼,我里驅(qū)動(dòng)寫好了我們可以選擇動(dòng)態(tài)加載驅(qū)動(dòng)或者直接靜態(tài)編譯進(jìn)內(nèi)核,這個(gè)的區(qū)別在前文中我講解的非常清楚了,希望朋友一定要去看一下,Linux驅(qū)動(dòng)靜態(tài)編譯和動(dòng)態(tài)編譯方法詳解
這個(gè)代碼相對(duì)有點(diǎn)復(fù)雜,朋友們一定要仔細(xì)分析。驅(qū)動(dòng)程序如下:
lcd.c文件如下:
#include 《linux/module.h》
#include 《linux/kernel.h》
#include 《linux/errno.h》
#include 《linux/string.h》
#include 《linux/mm.h》
#include 《linux/slab.h》
#include 《linux/delay.h》
#include 《linux/fb.h》
#include 《linux/init.h》
#include 《linux/dma-mapping.h》
#include 《linux/interrupt.h》
#include 《linux/workqueue.h》
#include 《linux/wait.h》
#include 《linux/platform_device.h》
#include 《linux/clk.h》
#include 《asm/io.h》
#include 《asm/uaccess.h》
#include 《asm/div64.h》
#include 《asm/mach/map.h》
static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
unsigned int green, unsigned int blue,
unsigned int transp, struct fb_info *info);
struct lcd_regs {
unsigned long lcdcon1;
unsigned long lcdcon2;
unsigned long lcdcon3;
unsigned long lcdcon4;
unsigned long lcdcon5;
unsigned long lcdsaddr1;
unsigned long lcdsaddr2;
unsigned long lcdsaddr3;
unsigned long redlut;
unsigned long greenlut;
unsigned long bluelut;
unsigned long reserved[9];
unsigned long dithmode;
unsigned long tpal;
unsigned long lcdintpnd;
unsigned long lcdsrcpnd;
unsigned long lcdintmsk;
unsigned long lpcsel;
};
static struct fb_ops s3c_lcdfb_ops = {
.owner = THIS_MODULE,
.fb_setcolreg = s3c_lcdfb_setcolreg,
.fb_fillrect = cfb_fillrect, /* 這三個(gè)函數(shù)是在內(nèi)核自帶的,動(dòng)態(tài)加載時(shí)候,需要把這個(gè)三個(gè)編譯成模塊當(dāng)吧驅(qū)動(dòng)程序編譯進(jìn)內(nèi)核時(shí)候,就不用去管著三個(gè)函數(shù) */
val |= chan_to_field(blue,&info-》var.blue);
//((u32 *)(info-》pseudo_palette))[regno] = val;
pseudo_palette[regno] = val;
return 0;
}
static int lcd_init(void)
{
/* 1. 分配一個(gè)fb_info */
s3c_lcd = framebuffer_alloc(0, NULL);
/* 2. 設(shè)置 */
/* 2.1 設(shè)置固定的參數(shù) */
strcpy(s3c_lcd-》fix.id, “mylcd”);
s3c_lcd-》fix.smem_len = 480*272*16/8;
s3c_lcd-》fix.type = FB_TYPE_PACKED_PIXELS;
s3c_lcd-》fix.visual = FB_VISUAL_TRUECOLOR; /* TFT */
s3c_lcd-》fix.line_length = 480*2;
/* 2.2 設(shè)置可變的參數(shù) */
s3c_lcd-》var.xres = 480;
s3c_lcd-》var.yres = 272;
s3c_lcd-》var.xres_virtual = 480;
s3c_lcd-》var.yres_virtual = 272;
s3c_lcd-》var.bits_per_pixel = 16;
/* RGB:565 */
s3c_lcd-》var.red.offset = 11;
s3c_lcd-》var.red.length = 5;
s3c_lcd-》var.green.offset = 5;
s3c_lcd-》var.green.length = 6;
s3c_lcd-》var.blue.offset = 0;
s3c_lcd-》var.blue.length = 5;
s3c_lcd-》var.activate = FB_ACTIVATE_NOW;
/* 2.3 設(shè)置操作函數(shù) */
s3c_lcd-》fbops = &s3c_lcdfb_ops;
/* 2.4 其他的設(shè)置 */
s3c_lcd-》pseudo_palette = pseudo_palette;
//s3c_lcd-》screen_base = ; /* 顯存的虛擬地址 */
s3c_lcd-》screen_size = 480*272*16/8;
/* 3. 硬件相關(guān)的操作 */
/* 3.1 配置GPIO用于LCD */
gpbcon = ioremap(0x56000010, 8);
gpbdat = gpbcon+1;
gpccon = ioremap(0x56000020, 4);
gpdcon = ioremap(0x56000030, 4);
gpgcon = ioremap(0x56000060, 4);
*gpccon = 0xaaaaaaaa; /* GPIO管腳用于VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND */
*gpdcon = 0xaaaaaaaa; /* GPIO管腳用于VD[23:8] */
*gpbcon &= ~(3); /* GPB0設(shè)置為輸出引腳 */
*gpbcon |= 1;
*gpbdat &= ~1; /* 輸出低電平 */
*gpgcon |= (3《《8); /* GPG4用作LCD_PWREN */
/* 3.2 根據(jù)LCD手冊(cè)設(shè)置LCD控制器, 比如VCLK的頻率等 */
lcd_regs = ioremap(0x4D000000, sizeof(struct lcd_regs));
/* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手冊(cè)P14
* 10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2]
* CLKVAL = 4
* bit[6:5]: 0b11, TFT LCD
* bit[4:1]: 0b1100, 16 bpp for TFT
* bit[0] : 0 = Disable the video output and the LCD control signal.
*/
lcd_regs-》lcdcon1 = (4《《8) | (3《《5) | (0x0c《《1);
#if 1
/* 垂直方向的時(shí)間參數(shù)
* bit[31:24]: VBPD, VSYNC之后再過(guò)多長(zhǎng)時(shí)間才能發(fā)出第1行數(shù)據(jù)
* LCD手冊(cè) T0-T2-T1=4
* VBPD=3
* bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319
* bit[13:6] : VFPD, 發(fā)出最后一行數(shù)據(jù)之后,再過(guò)多長(zhǎng)時(shí)間才發(fā)出VSYNC
* LCD手冊(cè)T2-T5=322-320=2, 所以VFPD=2-1=1
* bit[5:0] : VSPW, VSYNC信號(hào)的脈沖寬度, LCD手冊(cè)T1=1, 所以VSPW=1-1=0
*/
lcd_regs-》lcdcon2 = (1《《24) | (271《《14) | (1《《6) | (9);
/* 水平方向的時(shí)間參數(shù)
* bit[25:19]: HBPD, VSYNC之后再過(guò)多長(zhǎng)時(shí)間才能發(fā)出第1行數(shù)據(jù)
* LCD手冊(cè) T6-T7-T8=17
* HBPD=16
* bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239
* bit[7:0] : HFPD, 發(fā)出最后一行里最后一個(gè)象素?cái)?shù)據(jù)之后,再過(guò)多長(zhǎng)時(shí)間才發(fā)出HSYNC
* LCD手冊(cè)T8-T11=251-240=11, 所以HFPD=11-1=10
*/
lcd_regs-》lcdcon3 = (1《《19) | (479《《8) | (1);
/* 水平方向的同步信號(hào)
* bit[7:0] : HSPW, HSYNC信號(hào)的脈沖寬度, LCD手冊(cè)T7=5, 所以HSPW=5-1=4
*/
lcd_regs-》lcdcon4 = 40;
#else
lcd_regs-》lcdcon2 = S3C2410_LCDCON2_VBPD(5) | \
S3C2410_LCDCON2_LINEVAL(319) | \
S3C2410_LCDCON2_VFPD(3) | \
S3C2410_LCDCON2_VSPW(1);
lcd_regs-》lcdcon3 = S3C2410_LCDCON3_HBPD(10) | \
S3C2410_LCDCON3_HOZVAL(239) | \
S3C2410_LCDCON3_HFPD(1);
lcd_regs-》lcdcon4 = S3C2410_LCDCON4_MVAL(13) | \
S3C2410_LCDCON4_HSPW(0);
#endif
/* 信號(hào)的極性
* bit[11]: 1=565 format
* bit[10]: 0 = The video data is fetched at VCLK falling edge
* bit[9] : 1 = HSYNC信號(hào)要反轉(zhuǎn),即低電平有效
* bit[8] : 1 = VSYNC信號(hào)要反轉(zhuǎn),即低電平有效
* bit[6] : 0 = VDEN不用反轉(zhuǎn)
* bit[3] : 0 = PWREN輸出0
* bit[1] : 0 = BSWP
* bit[0] : 1 = HWSWP 2440手冊(cè)P413
*/
lcd_regs-》lcdcon5 = (1《《11) | (0《《10) | (1《《9) | (1《《8) | (1《《0);
/* 3.3 分配顯存(framebuffer), 并把地址告訴LCD控制器 */
s3c_lcd-》screen_base = dma_alloc_writecombine(NULL, s3c_lcd-》fix.smem_len, &s3c_lcd-》fix.smem_start, GFP_KERNEL);
lcd_regs-》lcdsaddr1 = (s3c_lcd-》fix.smem_start 》》 1) & ~(3《《30);
lcd_regs-》lcdsaddr2 = ((s3c_lcd-》fix.smem_start + s3c_lcd-》fix.smem_len) 》》 1) & 0x1fffff;
lcd_regs-》lcdsaddr3 = (480*16/16); /* 一行的長(zhǎng)度(單位: 2字節(jié)) */
//s3c_lcd-》fix.smem_start = xxx; /* 顯存的物理地址 */
/* 啟動(dòng)LCD */
lcd_regs-》lcdcon1 |= (1《《0); /* 使能LCD控制器 */
lcd_regs-》lcdcon5 |= (1《《3); /* 使能LCD本身 */
*gpbdat |= 1; /* 輸出高電平, 使能背光 */
/* 4. 注冊(cè) */
register_framebuffer(s3c_lcd);
return 0;
}
static void lcd_exit(void)
{
unregister_framebuffer(s3c_lcd);
lcd_regs-》lcdcon1 &= ~(1《《0); /* 關(guān)閉LCD本身 */
*gpbdat &= ~1; /* 關(guān)閉背光 */
dma_free_writecombine(NULL, s3c_lcd-》fix.smem_len, s3c_lcd-》screen_base, s3c_lcd-》fix.smem_start);
iounmap(lcd_regs);
iounmap(gpbcon);
iounmap(gpccon);
iounmap(gpdcon);
iounmap(gpgcon);
framebuffer_release(s3c_lcd);
}
module_init(lcd_init);
module_exit(lcd_exit);
MODULE_LICENSE(“GPL”);
測(cè)試程序怎么編寫就看你的應(yīng)用程序了,但是框架是不變的如下:下面是在Lcd上顯示一幅圖片,用到了libjpeg庫(kù),這個(gè)地方可以不用關(guān)注,主要是看看應(yīng)用程序怎么調(diào)用驅(qū)動(dòng)程序的,我總結(jié)有如下當(dāng)函數(shù):
static int FBDeviceInit(void) /* FB初始化函數(shù) */
static int FBShowPixel(int iX, int iY, unsigned int dwColor) /* 填充像素,具體怎么填充看如下代碼中 */
static int FBCleanScreen(unsigned int dwBackColor) /* 清屏函數(shù),把顯示屏初始化為黑色 */
#include 《stdio.h》
#include “jpeglib.h”
#include 《setjmp.h》
#include 《sys/types.h》
#include 《sys/stat.h》
#include 《fcntl.h》
#include 《sys/ioctl.h》
#include 《sys/mman.h》
#include 《linux/fb.h》
#include 《string.h》
#include 《stdlib.h》
#define FB_DEVICE_NAME “/dev/fb0”
#define DBG_PRINTF printf
static int g_fd;
static struct fb_var_screeninfo g_tFBVar;
static struct fb_fix_screeninfo g_tFBFix;
static unsigned char *g_pucFBMem;
static unsigned int g_dwScreenSize;
static unsigned int g_dwLineWidth;
static unsigned int g_dwPixelWidth;
static int FBDeviceInit(void)
{
int ret;
g_fd = open(FB_DEVICE_NAME, O_RDWR); /* 打開(kāi)lcd驅(qū)動(dòng)設(shè)備節(jié)點(diǎn) */
if (0 》 g_fd)
{
DBG_PRINTF(“can‘t open %s\n”, FB_DEVICE_NAME);
}
ret = ioctl(g_fd, FBIOGET_VSCREENINFO, &g_tFBVar); /* 獲取lcd可變參數(shù) */
if (ret 《 0)
{
DBG_PRINTF(“can’t get fb‘s var\n”);
return -1;
}
ret = ioctl(g_fd, FBIOGET_FSCREENINFO, &g_tFBFix); /* 獲取lcd固定參數(shù) */
if (ret 《 0)
{
DBG_PRINTF(“can’t get fb‘s fix\n”);
return -1;
}
g_dwScreenSize = g_tFBVar.xres * g_tFBVar.yres * g_tFBVar.bits_per_pixel / 8; /* 計(jì)算lcd屏幕大小 */
g_pucFBMem = (unsigned char *)mmap(NULL , g_dwScreenSize, PROT_READ | PROT_WRITE, MAP_SHARED, g_fd, 0); /* 把顯存映射成內(nèi)存一樣,方便我們直接操作 ,這個(gè)地方是很有意思的*/
if (0 》 g_pucFBMem)
{
DBG_PRINTF(“can’t mmap\n”);
return -1;
}
g_dwLineWidth = g_tFBVar.xres * g_tFBVar.bits_per_pixel / 8;
g_dwPixelWidth = g_tFBVar.bits_per_pixel / 8;
return 0;
}
static int FBShowPixel(int iX, int iY, unsigned int dwColor) /* 填充像素,dwcolor就是我們要在一個(gè)像素顯示的顏色 */
{
unsigned char *pucFB;
unsigned short *pwFB16bpp;
unsigned int *pdwFB32bpp;
unsigned short wColor16bpp; /* 565 */
int iRed;
int iGreen;
int iBlue;
if ((iX 》= g_tFBVar.xres) || (iY 》= g_tFBVar.yres))
{
DBG_PRINTF(“out of region\n”);
return -1;
}
pucFB = g_pucFBMem + g_dwLineWidth * iY + g_dwPixelWidth * iX;
pwFB16bpp = (unsigned short *)pucFB;
pdwFB32bpp = (unsigned int *)pucFB;
switch (g_tFBVar.bits_per_pixel)
{
case 8:
{
*pucFB = (unsigned char)dwColor;
break;
}
case 16:
{
iRed = (dwColor 》》 (16+3)) & 0x1f;
iGreen = (dwColor 》》 (8+2)) & 0x3f;
iBlue = (dwColor 》》 3) & 0x1f;
wColor16bpp = (iRed 《《 11) | (iGreen 《《 5) | iBlue;
*pwFB16bpp = wColor16bpp;
break;
}
case 32:
{
*pdwFB32bpp = dwColor;
break;
}
default :
{
DBG_PRINTF(“can‘t support %d bpp\n”, g_tFBVar.bits_per_pixel);
return -1;
}
}
return 0;
}
static int FBCleanScreen(unsigned int dwBackColor)
{
unsigned char *pucFB;
unsigned short *pwFB16bpp;
unsigned int *pdwFB32bpp;
unsigned short wColor16bpp; /* 565 */
int iRed;
int iGreen;
int iBlue;
int i = 0;
pucFB = g_pucFBMem;
pwFB16bpp = (unsigned short *)pucFB;
pdwFB32bpp = (unsigned int *)pucFB;
switch (g_tFBVar.bits_per_pixel)
{
case 8:
{
memset(g_pucFBMem, dwBackColor, g_dwScreenSize);
break;
}
case 16:
{
iRed = (dwBackColor 》》 (16+3)) & 0x1f;
iGreen = (dwBackColor 》》 (8+2)) & 0x3f;
iBlue = (dwBackColor 》》 3) & 0x1f;
wColor16bpp = (iRed 《《 11) | (iGreen 《《 5) | iBlue;
while (i 《 g_dwScreenSize)
{
*pwFB16bpp = wColor16bpp;
pwFB16bpp++;
i += 2;
}
break;
}
case 32:
{
while (i 《 g_dwScreenSize)
{
*pdwFB32bpp = dwBackColor;
pdwFB32bpp++;
i += 4;
}
break;
}
default :
{
DBG_PRINTF(“can’t support %d bpp\n”, g_tFBVar.bits_per_pixel);
return -1;
}
}
return 0;
}
static int FBShowLine(int iXStart, int iXEnd, int iY, unsigned char *pucRGBArray)
{
int i = iXStart * 3;
int iX;
unsigned int dwColor;
if (iY 》= g_tFBVar.yres)
return -1;
if (iXStart 》= g_tFBVar.xres)
return -1;
if (iXEnd 》= g_tFBVar.xres)
{
iXEnd = g_tFBVar.xres;
}
for (iX = iXStart; iX 《 iXEnd; iX++)
{
/* 0xRRGGBB */
dwColor = (pucRGBArray[i]《《16) + (pucRGBArray[i+1]《《8) + (pucRGBArray[i+2]《《0);
i += 3;
FBShowPixel(iX, iY, dwColor);
}
return 0;
}
/*
Allocate and initialize a JPEG decompression object // 分配和初始化一個(gè)decompression結(jié)構(gòu)體
Specify the source of the compressed data (eg, a file) // 指定源文件
Call jpeg_read_header() to obtain image info // 用jpeg_read_header獲得jpg信息
Set parameters for decompression // 設(shè)置解壓參數(shù),比如放大、縮小
jpeg_start_decompress(。..); // 啟動(dòng)解壓:jpeg_start_decompress
while (scan lines remain to be read)
jpeg_read_scanlines(。..); // 循環(huán)調(diào)用jpeg_read_scanlines
jpeg_finish_decompress(。..); // jpeg_finish_decompress
Release the JPEG decompression object // 釋放decompression結(jié)構(gòu)體
*/
/* Uage: jpg2rgb 《jpg_file》
*/
int main(int argc, char **argv)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * infile;
int row_stride;
unsigned char *buffer;
if (argc != 2)
{
printf(“Usage: \n”);
printf(“%s 《jpg_file》\n”, argv[0]);
return -1;
}
if (FBDeviceInit())
{
return -1;
}
FBCleanScreen(0);
/* 以下部分是把圖片解壓出來(lái),取出各個(gè)像素值放在一個(gè)緩沖區(qū)中,我們只需要把這些像素填進(jìn)lcd當(dāng)中去就行 */
// 分配和初始化一個(gè)decompression結(jié)構(gòu)體
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
// 指定源文件
if ((infile = fopen(argv[1], “rb”)) == NULL) {
fprintf(stderr, “can‘t open %s\n”, argv[1]);
return -1;
}
jpeg_stdio_src(&cinfo, infile);
// 用jpeg_read_header獲得jpg信息
jpeg_read_header(&cinfo, TRUE);
/* 源信息 */
printf(“image_width = %d\n”, cinfo.image_width);
printf(“image_height = %d\n”, cinfo.image_height);
printf(“num_components = %d\n”, cinfo.num_components);
// 設(shè)置解壓參數(shù),比如放大、縮小
printf(“enter scale M/N:\n”);
scanf(“%d/%d”, &cinfo.scale_num, &cinfo.scale_denom);
printf(“scale to : %d/%d\n”, cinfo.scale_num, cinfo.scale_denom);
// 啟動(dòng)解壓:jpeg_start_decompress
jpeg_start_decompress(&cinfo);
/* 輸出的圖象的信息 */
printf(“output_width = %d\n”, cinfo.output_width);
printf(“output_height = %d\n”, cinfo.output_height);
printf(“output_components = %d\n”, cinfo.output_components);
// 一行的數(shù)據(jù)長(zhǎng)度
row_stride = cinfo.output_width * cinfo.output_components;
buffer = malloc(row_stride);
// 循環(huán)調(diào)用jpeg_read_scanlines來(lái)一行一行地獲得解壓的數(shù)據(jù)
while (cinfo.output_scanline 《 cinfo.output_height)
{
(void) jpeg_read_scanlines(&cinfo, &buffer, 1);
// 寫到LCD去
FBShowLine(0, cinfo.output_width, cinfo.output_scanline, buffer);
}
free(buffer);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return 0;
}
評(píng)論
查看更多