Chinaunix首页 | 论坛 | 博客
  • 博客访问: 805548
  • 博文数量: 281
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2770
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 19:45
个人简介

邮箱:zhuimengcanyang@163.com 痴爱嵌入式技术的蜗牛

文章分类
文章存档

2020年(1)

2018年(1)

2017年(56)

2016年(72)

2015年(151)

分类: LINUX

2015-11-25 21:10:02


1. 驱动代码


列代码:(韦东山老师的,比较详细),有时间再总结。

点击(此处)折叠或打开

  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/fb.h>
  9. #include <linux/init.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/wait.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/clk.h>

  16. #include <asm/io.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/div64.h>

  19. #include <asm/mach/map.h>
  20. #include <asm/arch/regs-lcd.h>
  21. #include <asm/arch/regs-gpio.h>
  22. #include <asm/arch/fb.h>

  23. static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
  24.              unsigned int green, unsigned int blue,
  25.              unsigned int transp, struct fb_info *info);


  26. struct lcd_regs {
  27.     unsigned long    lcdcon1;
  28.     unsigned long    lcdcon2;
  29.     unsigned long    lcdcon3;
  30.     unsigned long    lcdcon4;
  31.     unsigned long    lcdcon5;
  32.     unsigned long    lcdsaddr1;
  33.     unsigned long    lcdsaddr2;
  34.     unsigned long    lcdsaddr3;
  35.     unsigned long    redlut;
  36.     unsigned long    greenlut;
  37.     unsigned long    bluelut;
  38.     unsigned long    reserved[9];
  39.     unsigned long    dithmode;
  40.     unsigned long    tpal;
  41.     unsigned long    lcdintpnd;
  42.     unsigned long    lcdsrcpnd;
  43.     unsigned long    lcdintmsk;
  44.     unsigned long    lpcsel;
  45. };

  46. static struct fb_ops s3c_lcdfb_ops = {
  47.     .owner        = THIS_MODULE,
  48.     .fb_setcolreg    = s3c_lcdfb_setcolreg,
  49.     .fb_fillrect    = cfb_fillrect,
  50.     .fb_copyarea    = cfb_copyarea,
  51.     .fb_imageblit    = cfb_imageblit,
  52. };


  53. static struct fb_info *s3c_lcd;
  54. static volatile unsigned long *gpbcon;
  55. static volatile unsigned long *gpbdat;
  56. static volatile unsigned long *gpccon;
  57. static volatile unsigned long *gpdcon;
  58. static volatile unsigned long *gpgcon;
  59. static volatile struct lcd_regs* lcd_regs;
  60. static u32 pseudo_palette[16];


  61. /* from pxafb.c */
  62. static inline unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
  63. {
  64.     chan &= 0xffff;
  65.     chan >>= 16 - bf->length;
  66.     return chan << bf->offset;
  67. }


  68. static int s3c_lcdfb_setcolreg(unsigned int regno, unsigned int red,
  69.              unsigned int green, unsigned int blue,
  70.              unsigned int transp, struct fb_info *info)
  71. {
  72.     unsigned int val;
  73.     
  74.     if (regno > 16)
  75.         return 1;

  76.     /* 用red,green,blue三原色构造出val */
  77.     val = chan_to_field(red,    &info->var.red);
  78.     val |= chan_to_field(green, &info->var.green);
  79.     val |= chan_to_field(blue,    &info->var.blue);
  80.     
  81.     //((u32 *)(info->pseudo_palette))[regno] = val;
  82.     pseudo_palette[regno] = val;
  83.     return 0;
  84. }

  85. static int lcd_init(void)
  86. {
  87.     /* 1. 分配一个fb_info */
  88.     s3c_lcd = framebuffer_alloc(0, NULL);

  89.     /* 2. 设置 */
  90.     /* 2.1 设置固定的参数 */
  91.     strcpy(s3c_lcd->fix.id, "mylcd");
  92.     s3c_lcd->fix.smem_len = 240*320*16/8;
  93.     s3c_lcd->fix.type = FB_TYPE_PACKED_PIXELS;
  94.     s3c_lcd->fix.visual = FB_VISUAL_TRUECOLOR; /* TFT */
  95.     s3c_lcd->fix.line_length = 240*2;
  96.     
  97.     /* 2.2 设置可变的参数 */
  98.     s3c_lcd->var.xres = 240;
  99.     s3c_lcd->var.yres = 320;
  100.     s3c_lcd->var.xres_virtual = 240;
  101.     s3c_lcd->var.yres_virtual = 320;
  102.     s3c_lcd->var.bits_per_pixel = 16;

  103.     /* RGB:565 */
  104.     s3c_lcd->var.red.offset = 11;
  105.     s3c_lcd->var.red.length = 5;
  106.     
  107.     s3c_lcd->var.green.offset = 5;
  108.     s3c_lcd->var.green.length = 6;

  109.     s3c_lcd->var.blue.offset = 0;
  110.     s3c_lcd->var.blue.length = 5;

  111.     s3c_lcd->var.activate = FB_ACTIVATE_NOW;
  112.     
  113.     
  114.     /* 2.3 设置操作函数 */
  115.     s3c_lcd->fbops = &s3c_lcdfb_ops;
  116.     
  117.     /* 2.4 其他的设置 */
  118.     s3c_lcd->pseudo_palette = pseudo_palette;
  119.     //s3c_lcd->screen_base = ; /* 显存的虚拟地址 */
  120.     s3c_lcd->screen_size = 240*324*16/8;

  121.     /* 3. 硬件相关的操作 */
  122.     /* 3.1 配置GPIO用于LCD */
  123.     gpbcon = ioremap(0x56000010, 8);
  124.     gpbdat = gpbcon+1;
  125.     gpccon = ioremap(0x56000020, 4);
  126.     gpdcon = ioremap(0x56000030, 4);
  127.     gpgcon = ioremap(0x56000060, 4);

  128.     *gpccon = 0xaaaaaaaa; /* GPIO管脚用于VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND */
  129.     *gpdcon = 0xaaaaaaaa; /* GPIO管脚用于VD[23:8] */
  130.     
  131.     *gpbcon &= ~(3); /* GPB0设置为输出引脚 */
  132.     *gpbcon |= 1;
  133.     *gpbdat &= ~1; /* 输出低电平 */

  134.     *gpgcon |= (3<<8); /* GPG4用作LCD_PWREN */
  135.     
  136.     /* 3.2 根据LCD手册设置LCD控制器, 比如VCLK的频率等 */
  137.     lcd_regs = ioremap(0x4D000000, sizeof(struct lcd_regs));

  138.     /* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手册P14
  139.      * 10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2]
  140.      * CLKVAL = 4
  141.      * bit[6:5]: 0b11, TFT LCD
  142.      * bit[4:1]: 0b1100, 16 bpp for TFT
  143.      * bit[0] : 0 = Disable the video output and the LCD control signal.
  144.      */
  145.     lcd_regs->lcdcon1 = (4<<8) | (3<<5) | (0x0c<<1);

  146. #if 1
  147.     /* 垂直方向的时间参数
  148.      * bit[31:24]: VBPD, VSYNC之后再过多长时间才能发出第1行数据
  149.      * LCD手册 T0-T2-T1=4
  150.      * VBPD=3
  151.      * bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319
  152.      * bit[13:6] : VFPD, 发出最后一行数据之后,再过多长时间才发出VSYNC
  153.      * LCD手册T2-T5=322-320=2, 所以VFPD=2-1=1
  154.      * bit[5:0] : VSPW, VSYNC信号的脉冲宽度, LCD手册T1=1, 所以VSPW=1-1=0
  155.      */
  156.     lcd_regs->lcdcon2 = (3<<24) | (319<<14) | (1<<6) | (0<<0);


  157.     /* 水平方向的时间参数
  158.      * bit[25:19]: HBPD, VSYNC之后再过多长时间才能发出第1行数据
  159.      * LCD手册 T6-T7-T8=17
  160.      * HBPD=16
  161.      * bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239
  162.      * bit[7:0] : HFPD, 发出最后一行里最后一个象素数据之后,再过多长时间才发出HSYNC
  163.      * LCD手册T8-T11=251-240=11, 所以HFPD=11-1=10
  164.      */
  165.     lcd_regs->lcdcon3 = (16<<19) | (239<<8) | (10<<0);

  166.     /* 水平方向的同步信号
  167.      * bit[7:0]    : HSPW, HSYNC信号的脉冲宽度, LCD手册T7=5, 所以HSPW=5-1=4
  168.      */    
  169.     lcd_regs->lcdcon4 = 4;

  170. #else
  171. lcd_regs->lcdcon2 =    S3C2410_LCDCON2_VBPD(5) | \
  172.         S3C2410_LCDCON2_LINEVAL(319) | \
  173.         S3C2410_LCDCON2_VFPD(3) | \
  174.         S3C2410_LCDCON2_VSPW(1);

  175. lcd_regs->lcdcon3 =    S3C2410_LCDCON3_HBPD(10) | \
  176.         S3C2410_LCDCON3_HOZVAL(239) | \
  177.         S3C2410_LCDCON3_HFPD(1);

  178. lcd_regs->lcdcon4 =    S3C2410_LCDCON4_MVAL(13) | \
  179.         S3C2410_LCDCON4_HSPW(0);

  180. #endif
  181.     /* 信号的极性
  182.      * bit[11]: 1=565 format
  183.      * bit[10]: 0 = The video data is fetched at VCLK falling edge
  184.      * bit[9] : 1 = HSYNC信号要反转,即低电平有效
  185.      * bit[8] : 1 = VSYNC信号要反转,即低电平有效
  186.      * bit[6] : 0 = VDEN不用反转
  187.      * bit[3] : 0 = PWREN输出0
  188.      * bit[1] : 0 = BSWP
  189.      * bit[0] : 1 = HWSWP 2440手册P413
  190.      */
  191.     lcd_regs->lcdcon5 = (1<<11) | (0<<10) | (1<<9) | (1<<8) | (1<<0);
  192.     
  193.     /* 3.3 分配显存(framebuffer), 并把地址告诉LCD控制器 */
  194.     s3c_lcd->screen_base = dma_alloc_writecombine(NULL, s3c_lcd->fix.smem_len, &s3c_lcd->fix.smem_start, GFP_KERNEL);
  195.     
  196.     lcd_regs->lcdsaddr1 = (s3c_lcd->fix.smem_start >> 1) & ~(3<<30);
  197.     lcd_regs->lcdsaddr2 = ((s3c_lcd->fix.smem_start + s3c_lcd->fix.smem_len) >> 1) & 0x1fffff;
  198.     lcd_regs->lcdsaddr3 = (240*16/16); /* 一行的长度(单位: 2字节) */    
  199.     
  200.     //s3c_lcd->fix.smem_start = xxx; /* 显存的物理地址 */
  201.     /* 启动LCD */
  202.     lcd_regs->lcdcon1 |= (1<<0); /* 使能LCD控制器 */
  203.     lcd_regs->lcdcon5 |= (1<<3); /* 使能LCD本身 */
  204.     *gpbdat |= 1; /* 输出高电平, 使能背光 */        

  205.     /* 4. 注册 */
  206.     register_framebuffer(s3c_lcd);
  207.     
  208.     return 0;
  209. }

  210. static void lcd_exit(void)
  211. {
  212.     unregister_framebuffer(s3c_lcd);
  213.     lcd_regs->lcdcon1 &= ~(1<<0); /* 关闭LCD本身 */
  214.     *gpbdat &= ~1; /* 关闭背光 */
  215.     dma_free_writecombine(NULL, s3c_lcd->fix.smem_len, s3c_lcd->screen_base, s3c_lcd->fix.smem_start);
  216.     iounmap(lcd_regs);
  217.     iounmap(gpbcon);
  218.     iounmap(gpccon);
  219.     iounmap(gpdcon);
  220.     iounmap(gpgcon);
  221.     framebuffer_release(s3c_lcd);
  222. }

  223. module_init(lcd_init);
  224. module_exit(lcd_exit);

  225. MODULE_LICENSE("GPL");

2. 测试

利用光盘里的patch文件来得到内核文件uImage。
可能出现的问题:


1. make menuconfig去掉原来的驱动程序
-> Device Drivers
  -> Graphics support
S3C2410 LCD framebuffer support

2. make uImage
   make modules  (编译驱动.ko文件之前,必须先执行这条命令,生成模块文件。

3. 使用新的uImage启动开发板:
tftp 32000000 uImage_nolcd  
       (不能是30000000,不知道为什么?改成30000000,加载驱动会出现如下问题: )
bootm 32000000

挂接: mount -t nfs -o nolock,vers=2 192.168.1.10:/work/nfs_root /mnt

4. 在文件夹 /drivers/video/ 下找到cfbcopyarea.ko, cfbfillrect.ko, cfbimgblt.ko
insmod cfbcopyarea.ko
insmod cfbfillrect.ko
insmod cfbimgblt.ko
insmod lcd.ko

echo hello > /dev/tty1  // 可以在LCD上看见hello
cat lcd.ko > /dev/fb0    // 花屏

5. 修改 /etc/inittab
tty1::askfirst:-/bin/sh
用新内核重启开发板

insmod cfbcopyarea.ko
insmod cfbfillrect.ko
insmod cfbimgblt.ko
insmod lcd.ko
insmod buttons.ko 

现象:按下按键 可以出现ls + enter按键,可以看到ls命令,打印出当前目录下的内容。


(韦老师的)内核的编译:
(1)纯净内核树(用光盘里的)
(2)patch内核 (用光盘里的) patch p1 < ../linux_xxx_patch
(3)配置 cp config_ok .config
(4)make menuconfig
(5)make uImage
(6)make modules
再按照前面的测试步骤,来进行试验。


阅读(798) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~