Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90269
  • 博文数量: 30
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 45
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-12 15:01
个人简介

蒹霞苍苍白露为霜

文章分类
文章存档

2016年(1)

2015年(5)

2014年(24)

我的朋友

分类: 嵌入式

2014-06-27 09:25:16

参考了 《linux驱动开发详解》3.4章节,实现建立自己驱动目录
参考 TQ移植手册 实现 hello world 驱动实现

目标内核:2.6.33

一. 建立自己的驱动目录  

   在 drivers 下,建立自己的驱动目录 ywx-driver


  
1. 在 driver下,建立自己目录 ywx-driver

  1. root@yuweixian:/opt/me_linux/linux-2.6.33/drivers#mkdir ywx-driver

  2.复制 driver/char 目录下 Kconfig Makefile 到 driver/ywx-dirver/

  3.修改 driver/ywx-driver/ Kconfig Makefile


修改 ywx-driver/Kconfig ,我们添加了 hello world 驱动程序
  1. #
  2. # ywx device configuration
  3. #

  4. menu "ywx-driver "
  5. comment "ywx-driver "

  6. config HELLO
  7.     tristate "hello world programming "    
  8.     help
  9.      this is hello test programming

  10. endmenu

修改 ywx-driver/Makefile,添加 helloworld 驱动程序

  1. #
  2. # Makefile for the kernel character device drivers.
  3. #

  4. #
  5. # This file contains the font map for the default (hardware) font
  6. #
  7. FONTMAPFILE = cp437.uni

  8. obj-$(CONFIG_HELLO)        += hello.o

   4. 修改 drivers/Kconfig  Makefile

修改 drivers/Kconfig

  1. source "drivers/char/Kconfig"

  2. source "drivers/ywx-driver/Kconfig"  ###添加 自己的目录

  3. source "drivers/i2c/Kconfig"

修改 drivers/Makefile

  1. obj-y                += char/

  2. obj-y                += ywx-driver/  ###添加自己的目录


5.make menuconfig  显示 如下






二、hello world 驱动程序 实现



    1. 在 drivers/ywx-driver/  建立

  1. gedit drivers/ywx-driver/hello.c

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>

  3. MODULE_LICENSE("GPL");

  4. static int __init hello_init(void)
  5. {

  6.     printk("<1>\n Hello,linux!\n");
  7.     printk(KERN_INFO,"Hello,arm!\n");
  8.     printk("<1>\nThis is first driver program.\n\n");

  9.     return 0;
  10. }

  11. static void __exit hello_exit(void)
  12. {
  13.     printk("<1>\n Exit!\n");
  14.     printk("<1>\nGoodbye Linux!\n\n");
  15. }

  16. module_init(hello_init);
  17. module_exit(hello_exit);

  18. MODULE_LICENSE("GPL");

    2. 在 drivers/ywx-driver/Kconfig  Makefile 添加 helloworld 
           在 实现驱动目录时,我们已经添加了helloworld 支持了

    3. 在 2.6.33 目录下

     
  1. make zImage #产生 内核镜像文件

  2. make SUBDIR=drivers/ywx-driver/ modules  生成 hello.ko 驱动文件
   
    4. 在开发板上进行操作了

  1. #cd /lib/
  2. #mkdir modules
  3. #uname -a
  4. #2.6.33-yuweixian 显示
  5. 所以,建立
  6. #mkdir 2.6.33-yuweixian

 
   5.我们复制 hello.ko 文件到 /lib/modules/2.6.33-yuweixian/


可以参考:http://blog.chinaunix.net/space.php?uid=22666248&do=blog&id=263346


   6.  insmod ./hello.ko

  1. [root@yuweixian 2.6.33-yuweixian]# insmod ./hello.ko

  2.      Hello,

  3. This is first driver program.

  4. [root@yuweixian 2.6.33-yuweixian]#

  7.删除  .ko


  1. [root@yuweixian 2.6.33-yuweixian]# lsmod
  2. hello 645 0 - Live 0xbf000000
  3. [root@yuweixian 2.6.33-yuweixian]# rmmod hello
  4.   Exit!

    Goodbye Linux!
  5. [root@yuweixian 2.6.33-yuweixian]#
阅读(1513) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~