Chinaunix首页 | 论坛 | 博客
  • 博客访问: 95327
  • 博文数量: 38
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 384
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-06 16:52
文章分类

全部博文(38)

文章存档

2014年(38)

我的朋友

分类: 嵌入式

2014-05-05 14:46:08

1.驱动程序hello.c代码如下

点击(此处)折叠或打开

  1. //moudle.h 包含了大量加载模块需要的函数和符号的定义
  2. #include <linux/module.h>
  3. //kernel.h以便使用printk()等函数
  4. #include <linux/kernel.h>
  5. //fs.h包含常用的数据结构,如struct file等
  6. #include <linux/fs.h>
  7. //uaccess.h 包含copy_to_user(),copy_from_user()等函数
  8. #include <linux/uaccess.h>
  9. //io.h 包含inl(),outl(),readl(),writel()等IO口操作函数
  10. #include <linux/io.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/pci.h>
  13. //init.h来指定你的初始化和清理函数,例如:module_init(init_function)、module_exit(cleanup_function)
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/cdev.h>
  18. #include <linux/gpio.h>
  19. #include <linux/irq.h>
  20. #include <linux/sched.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/poll.h>
  23. //irq.h中断与并发请求事件
  24. #include <asm/irq.h>
  25. //下面这些头文件是IO口在内核的虚拟映射地址,涉及IO口的操作所必须包含
  26. //#include <mach/gpio.h>
  27. #include <mach/regs-gpio.h>
  28. #include <plat/gpio-cfg.h>
  29. #include <mach/hardware.h>
  30. #include <mach/map.h>


  31. static struct class *hello_class;
  32. static int major;

  33. static int hello_open(struct inode *inode, struct file *file)
  34. {
  35.     printk("this is hello_open!\n");
  36.     return 0;
  37. }

  38. static ssize_t hello_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  39. {
  40.     printk("this is hello_write!\n");
  41.     return 0;
  42. }

  43. static struct file_operations hello_fops = {
  44.     .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
  45.     .open = hello_open,
  46.     .write = hello_write,
  47. };

  48. static int hello_init(void)
  49. {
  50.     major = register_chrdev(0, "hello_dev", &hello_fops);//注册,告诉内核
  51.     hello_class = class_create(THIS_MODULE, "hello_cls");
  52.     device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello");/* /dev/hello */
  53.     return 0;
  54. }

  55. static void hello_exit(void)
  56. {
  57.     unregister_chrdev(major, "hello_cls");
  58.     device_destroy(hello_class, MKDEV(major, 0));
  59.     class_destroy(hello_class);
  60. }

  61. module_init(hello_init);
  62. module_exit(hello_exit);

  63. MODULE_LICENSE("GPL");

2.Makefile文件代码如下
   /home/ymseven/work6410/linux/linux-3.8.3是开发板作用内核源代码的目录

点击(此处)折叠或打开

  1. KERN_DIR = /home/ymseven/work6410/linux/linux-3.8.3

  2. all:
  3.     make -C $(KERN_DIR) M=`pwd` modules

  4. clean:
  5.     make -C $(KERN_DIR) M=`pwd` modules clean
  6.     rm -rf modules.order

  7. obj-m    += hello.o

3.编译
    把hello.c和Makefile放到同一文件夹下,执行make,可看到生成了hello.ko文件,把hello.ko文件拷贝到开发板上
    在开发板上执行insmod hello.ko命令,驱动就装载成功了
    在开发板上执行 cat /proc/devices 命令,可看到有hello_dev这个设备
    在开发板上执行 ls -l /dev/he* 命令,可看到/dev/hello 字符设备的详细信息
    在开发板山执行rmmod hello 命令,就可以卸载驱动

4.测试程序hello_test.c如下

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>


  5. int main(int argc, char **argv)
  6. {
  7.     int fd;
  8.     int val = 1;
  9.     fd = open("/dev/hello", O_RDWR);
  10.     if (fd < 0)
  11.         printf("can't open!\n");
  12.     write(fd, &val, 4);
  13.     return 0;
  14. }

5.测试
    在hello_test.c的目录下执行arm-linux-gcc -o hello_test hello_test.c ,编译出hello_test文件,把它拷贝到开发板
    执行./hello_test , 可看到打印出this is hello_open!和this is hello_write!
    测试成功。

    

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