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

全部博文(38)

文章存档

2014年(38)

我的朋友

分类: 嵌入式

2014-05-05 15:28:49

1.led驱动程序led.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 *led_class;
  32. static int major;

  33. volatile unsigned long *gpmcon = NULL;

  34. volatile unsigned long *gpmdat = NULL;

  35. static int led_open(struct inode *inode, struct file *file)
  36. {
  37.     /*把LED的GPIO口设置为输出*/
  38.     /* 配置GPM的0,1,2,3为输出 */
  39.     *gpmcon &= ~((0xE<<(0*4)) | (0xE<<(1*4)) | (0xE<<(2*4)) | (0xE<<(3*4)));
  40.     return 0;
  41. }

  42. static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  43. {
  44.     int val;
  45.     
  46.     if( copy_from_user(&val, buf, count) )//从用户(测试程序中)获得val的值,同时判断copy_from_user函数的返回值,如果不为0,说明出错
  47.     {
  48.         printk("error in function ‘copy_from_user’ !\n");
  49.         return -EFAULT;
  50.     }
  51.     
  52.     if(val == 1)
  53.     {
  54.         // 点灯

  55.         *gpmdat &= ~((1<<0) | (1<<1) | (1<<2) | (1<<3));

  56.         printk("my led open\n");
  57.     }
  58.     else
  59.     {
  60.         // 灭灯

  61.         *gpmdat |= ((1<<0) | (1<<1) | (1<<2) | (1<<3));

  62.         printk("my led close\n");
  63.     }    
  64.     
  65.     return 0;
  66. }

  67. static struct file_operations led_fops = {
  68.     .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
  69.     .open = led_open,
  70.     .write = led_write,
  71. };

  72. static int led_init(void)
  73. {
  74.     major = register_chrdev(0, "led_dev", &led_fops);//注册,告诉内核
  75.     led_class = class_create(THIS_MODULE, "led_cls");
  76.     device_create(led_class, NULL, MKDEV(major, 0), NULL, "led");/* /dev/led */
  77.     
  78.     gpmcon = (volatile unsigned long *)ioremap(0x7F008820, 16);//印射虚拟地址

  79.     gpmdat = gpmcon + 1;
  80.     return 0;
  81. }

  82. static void led_exit(void)
  83. {
  84.     unregister_chrdev(major, "led_cls");
  85.     device_destroy(led_class, MKDEV(major, 0));
  86.     class_destroy(led_class);
  87.     iounmap(gpmcon);
  88. }

  89. module_init(led_init);
  90. module_exit(led_exit);

  91. MODULE_LICENSE("GPL");

2.led测试程序led_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.     
  10.     /*打开/dev/led设备*/
  11.     fd = open("/dev/led", O_RDWR);
  12.     if (fd < 0)
  13.         printf("can't open!\n");
  14.     
  15.     /*如果输入参数不等于两个,打印用法*/    
  16.     if ( argc != 2 )
  17.     {
  18.         printf("Usage :\n");

  19.         printf("%s \n", argv[0]);

  20.         return 0;
  21.     }
  22.     
  23.     /*比较第二个参数,若为on,val的值设为1,否则为0*/
  24.     if (strcmp(argv[1], "on") == 0)

  25.     {

  26.         val = 1;

  27.     }

  28.     else

  29.     {

  30.         val = 0;

  31.     }
  32.     
  33.     write(fd, &val, 4);//写val的数值,然后驱动程序利用copy_from_user函数取得val的值
  34.     return 0;
  35. }


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