Chinaunix首页 | 论坛 | 博客
  • 博客访问: 808312
  • 博文数量: 489
  • 博客积分: 475
  • 博客等级: 下士
  • 技术积分: 3087
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 16:28
文章分类

全部博文(489)

文章存档

2013年(7)

2012年(301)

2011年(181)

分类:

2012-03-13 12:05:54

通过简单的点灯实验后,依然发现有问题。在编译的时候会报一些错误,因为没有声明LICENSE,毕竟是开源的系统,要用就要守规矩。不爽可以不用
现在来说下第二个驱动程序,先看代码以查询方式来检测按键是否被按下。

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <asm/uaccess.h>
  7. #include <asm/irq.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/regs-gpio.h>
  10. #include <asm/hardware.h>

  11. static struct class *seconddrv_class;   // 添加两个类是为了自动创建设备节点
  12. static struct class_device    *seconddrv_class_dev;

  13. volatile unsigned long *gpfcon;
  14. volatile unsigned long *gpfdat;

  15. volatile unsigned long *gpgcon;
  16. volatile unsigned long *gpgdat;

  17. static int second_drv_open(struct inode *inode, struct file *file)
  18. {
  19.     /* 配置GPF0,2为输入引脚 */
  20.     *gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2)));

  21.     /* 配置GPG3,11为输入引脚 */
  22.     *gpgcon &= ~((0x3<<(3*2)) | (0x3<<(11*2)));

  23.     return 0;
  24. }

  25. ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  26. {
  27.     /* 返回4个引脚的电平 */
  28.     unsigned char key_vals[4];
  29.     int regval;

  30.     if (size != sizeof(key_vals))
  31.         return -EINVAL;

  32.     /* 读GPF0,2 */
  33.     regval = *gpfdat;
  34.     key_vals[0] = (regval & (1<<0)) ? 1 : 0;
  35.     key_vals[1] = (regval & (1<<2)) ? 1 : 0;
  36.     

  37.     /* 读GPG3,11 */
  38.     regval = *gpgdat;
  39.     key_vals[2] = (regval & (1<<3)) ? 1 : 0;
  40.     key_vals[3] = (regval & (1<<11)) ? 1 : 0;

  41.     copy_to_user(buf, key_vals, sizeof(key_vals));
  42.     
  43.     return sizeof(key_vals);
  44. }


  45. static struct file_operations sencod_drv_fops = {
  46.     .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
  47.     .open = second_drv_open,
  48.     .read    =    second_drv_read,    
  49. };


  50. int major;
  51. static int second_drv_init(void)
  52. {
  53.     major = register_chrdev(0, "second_drv", &sencod_drv_fops);  // major=0,是让系统自动分配主设备号。

  54.     seconddrv_class = class_create(THIS_MODULE, "second_drv");

  55.     seconddrv_class_dev = class_device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

  56.     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  57.     gpfdat = gpfcon + 1;

  58.     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  59.     gpgdat = gpgcon + 1;

  60.     return 0;
  61. }

  62. static void second_drv_exit(void)
  63. {
  64.     unregister_chrdev(major, "second_drv");
  65.     class_device_unregister(seconddrv_class_dev);
  66.     class_destroy(seconddrv_class);
  67.     iounmap(gpfcon);
  68.     iounmap(gpgcon);
  69.     return 0;
  70. }


  71. module_init(second_drv_init);

  72. module_exit(second_drv_exit);

  73. MODULE_LICENSE("GPL");
这是驱动程序,整体过程也是根据需求写出框架 1,file_operation结构休、open、write 2,入口函数里注册,出口函数里卸载 3,给sysfs提供更多的信息(即udev机制可自动创建设备节点)
接下来就是硬件上的操作了。根据原理图确定相应的引脚,看芯片数据手册确定相应的寄存器。然后就可以写代码了。入口函数将物理地址映射为虚拟地址,出口函数将映射虚拟地址释放。
再看测试程序:

点击(此处)折叠或打开

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

  5. /* seconddrvtest
  6.   */
  7. int main(int argc, char **argv)
  8. {
  9.     int fd;
  10.     unsigned char key_vals[4];
  11.     int cnt = 0;
  12.     
  13.     fd = open("/dev/buttons", O_RDWR);
  14.     if (fd < 0)
  15.     {
  16.         printf("can't open!\n");
  17.     }

  18.     while (1)
  19.     {
  20.         read(fd, key_vals, sizeof(key_vals));
  21.         if (!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3])
  22.         {
  23.             printf("%04d key pressed: %d %d %d %d\n", cnt++, key_vals[0], key_vals[1], key_vals[2], key_vals[3]);
  24.         }
  25.     }
  26.     
  27.     return 0;
  28. }
其中cnt用来统计按键被按下的次数。
Makefile和上一节的基本一样,稍做修改就可以了。在单板上运行测试程序,然后在命令行输入top会发现CPU资料占用严重,只是一个简单的程序,就占用那么多的资源,也太浪费了。主要是原因是测试程序里面用的是一个死循环,程序一直占着CPU在那里不停地检测是否有按键被按下。显然,这种情况是不合理的。
所以还有另一种方式,中断查询方式。
阅读(378) | 评论(0) | 转发(0) |
0

上一篇:Linux驱动学习笔记(1)

下一篇:裸设备

给主人留下些什么吧!~~