字符设备驱动程序之按键——查询方式

本节我们学习的是采用查询方式编写按键驱动程序。
由原理图我们知道三个按键分别接在GPF0、GPF2、GPG3、GPG11这四个引脚上。我们需要将这四个引脚配置成输入引脚,采用查询方式去获得引脚电平特性。我们的具体方案是:在入口函数中进行地址映射,在open函数中配置引脚为输入引脚,在read函数中返回引脚状态,下面请看程序:
首先我们来看按键驱动程序:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#define DEVICE_NAME "leds"

volatile unsigned long *gpfcon=NULL;
volatile unsigned long *gpfdat=NULL;
volatile unsigned long *gpgcon=NULL;
volatile unsigned long *gpgdat=NULL;

static struct class *keys_class;
static struct class_device *keys_class_devs;

static int s3c24xx_keys_open(struct inode *inode, struct file *file)
{
/*配置引脚为输出引脚*/
 /*set GPF0 GPF2 input*/
 *gpfcon &= ~((0x3<<(0))|(0x3<<(2*2)));
 /*set GPF0 GPF2 input*/
 *gpgcon &= ~((0x3<<(3*2))|(0x3<<(11*2)));
   return 0;
}

static int s3c24xx_keys_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
        /*return foure stats*/
        int stats[4];
        int val;
        val=*gpfdat;
        stats[0]=(val&(1<<0)) ? 1:0;
        stats[1]=(val&(1<<2)) ? 1:0;
        val=*gpgdat;
        stats[3]=(val&(1<<3)) ? 1:0;
        stats[4]=(val&(1<<11)) ? 1:0;
/*buff是指要拷贝到的用户空间
  stats是指要拷贝的数据的首地址
  sizeof(stats)是指要拷贝的数据的大小
*/
       copy_to_user(buff,stats,sizeof(stats)); 
        return ;
}

static ssize_t s3c24xx_keys_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
      printk("write ative\n");
      return 0;
}
static struct file_operations s3c24xx_keys_fops = {
    .owner  =   THIS_MODULE,   
    .open   =   s3c24xx_keys_open,     
    .read = s3c24xx_keys_read,   
    .write = s3c24xx_keys_write,   
};
int major;
static int __init s3c24xx_keys_init(void)
{
    major=register_chrdev(0, DEVICE_NAME, &s3c24xx_keys_fops); //auto distribute major
    keys_class = class_create(THIS_MODULE, "keys_class"); //创建类
    keys_class_devs = class_device_create(keys_class, NULL, MKDEV(major, 0), NULL, "keys"); //类下创建设备
    gpfcon=(volatile unsigned long *)ioremap(0x56000050,16); //映射为虚拟地址
    gpfdat=gpfcon+1;
    gpgcon=(volatile unsigned long *)ioremap(0x56000060,16); //映射为虚拟地址
    gpgdat=gpgcon+1;
     printk(DEVICE_NAME " initialized\n");
     return 0;
}

static void __exit s3c24xx_keys_exit(void)
{
    unregister_chrdev(major, DEVICE_NAME);
    class_device_unregister(keys_class_devs);
    class_destroy(keys_class);
    iounmap(gpfcon); //此处参数为映射好的虚拟地址,不要写成物理地址的说
    iounmap(gpgcon); //此处参数为映射好的虚拟地址,不要写成物理地址的说
}
module_init(s3c24xx_keys_init);
module_exit(s3c24xx_keys_exit);
MODULE_LICENSE("GPL");

应用程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    int val[4];
    int fd;
    int i,ret;
    fd=open("/dev/keys",0);
    if (fd < 0) 
    {        
        printf("Can't open /dev/buttons\n");       
        return -1;    
    }
    while(1)
        {
            ret = read(fd, val, sizeof(val));
            for (i = 0; i < sizeof(val)/sizeof(val[0]); i++)
                {
                    if (!val[i])
                        printf("K%d has been pressed", i+1);
                }
        }
}
以上采用查询方式编写的按键驱动程序,但是真实的按键驱动程序如果这样写就完蛋了,因为这样cpu就只能做查询按键状态这么一件事情,显然这是不合理的,为此接下来几节我们将采用poll机制和中断机制来改进这个程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值