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

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

文章分类
文章存档

2020年(1)

2018年(1)

2017年(56)

2016年(72)

2015年(151)

分类: 嵌入式

2015-08-18 11:16:22

目的:
应用程序,查询方式读取按键的值,打印出来。
驱动程序实现按键驱动的注册,对按键当前值的读取等。




1. 看硬件原理图,读芯片手册




可知连接图如下:
按键S2 --> EINT0 (GPF0)
按键S3 --> EINT2 (GPF2)
按键S4 --> EINT0 (GPG3)
按键S5 --> EINT2 (GPG11)

按键没有按下的时候,为高电平,按下为低电平。

2. 驱动程序

击(此处)折叠或打开

  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;   // 按键,返回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.       /* 将内核空间key_vals地址处sizeof(key_vals)个字节传送到应用空间的buf中 */
  42.     copy_to_user(buf, key_vals, sizeof(key_vals));
  43.    
  44.     /* 返回成功读取的字节数 */
  45.     return sizeof(key_vals);
  46. }


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


  52. int major;
  53. static int second_drv_init(void)
  54. {
  55.     // 自动方式,获取主设备号
  56.     major = register_chrdev(0, "second_drv", &sencod_drv_fops);

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

  58.     // 生成设备文件 /dev/buttons
  59.     seconddrv_class_dev = class_device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* 设备文件:/dev/buttons */

  60.       /* 地址的重映射 */
  61.     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  62.     gpfdat = gpfcon + 1;

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

  65.     return 0;
  66. }

  67. static void second_drv_exit(void)
  68. {
  69.     unregister_chrdev(major, "second_drv");
  70.     class_device_unregister(seconddrv_class_dev);
  71.     class_destroy(seconddrv_class);

  72.     iounmap(gpfcon);
  73.     iounmap(gpgcon);
  74.     return 0;
  75. }


  76. module_init(second_drv_init);
  77. module_exit(second_drv_exit);

  78. MODULE_LICENSE("GPL");
3. 测试程序

点击(此处)折叠或打开

  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.         /* 轮询读取按键的值,cpu会被一直占用。 */
  21.         read(fd, key_vals, sizeof(key_vals));

  22.         /* 按下按键,返回0值。检测如果有按键按下,则打印信息。 */
  23.         if (!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3])
  24.         {
  25.             printf("%04d key pressed: %d %d %d %d\n", cnt++, key_vals[0], key_vals[1], key_vals[2], key_vals[3]);
  26.         }
  27.     }
  28.     
  29.     return 0;
  30. }

4. 测试

5. 小结
读取按键的值,通过不断查询的方式。
这种方式需要不断的占用CPU,来查询状态。


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