字符设备实验之按键查询

主要用到udev的自动创建字符设备的功能,具体的注释说明请参考second_drv_init()函数


#include <linux/module.h>

#include <linux/kernel.h>


#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/slab.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/gpio_keys.h>
#include <linux/workqueue.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/poll.h>




#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>


#include <mach/map.h>
#include <mach/regs-clock.h>
#include <mach/regs-mem.h>
#include <mach/gpio.h>
#include <mach/gpio-smdkc110.h>
#include <mach/regs-gpio.h>


/********************************************************************************************
ioremap(),iounmap()
class_create(),class_destroy()
class_device_create(),class_device_unregister()
********************************************************************************************/
static dev_t devno;  
static struct cdev *pCdev;
static struct class *seconddrv_class;
static struct device *seconddrv_class_dev;




#define DEVICE_NAME "second_drv"
int major,minor;


volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;


static int second_drv_open(struct inode *inode, struct file *file)
{
//printk("do second_drv_open \n");

/*
* K1,K2,K3,K4对应GPH0,GPH1,GPH2,GPH3
*/


/* 配置GPG0,GPG3,GPG5,GPG6为输入引脚 */
*gpgcon &= ~((0xF<<(0*4)) | (0xF<<(1*4)) | (0xF<<(2*4)) | (0xF<<(3*4)));

return 0;
}


static int second_drv_release(struct inode *inode, struct file *file)
{
//printk("do second_drv_release \n");
return 0;
}


static int second_drv_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
/* 返回4个引脚的电平 */
unsigned char key_vals[4];
int regval;

//printk("do second_drv_read \n");

if (count != sizeof(key_vals))
return -EINVAL;


regval = *gpgdat;
key_vals[0] = (regval & (1<<0)) ? 1 : 0;
key_vals[1] = (regval & (1<<1)) ? 1 : 0;
key_vals[2] = (regval & (1<<2)) ? 1 : 0;
key_vals[3] = (regval & (1<<3)) ? 1 : 0;


copy_to_user(buff, key_vals, sizeof(key_vals));

return sizeof(key_vals);
}


static ssize_t second_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
//printk("do second_drv_write \n");
return 0;
}


static struct file_operations second_drv_fops =
{
.owner   = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module 变量*/
.open    = second_drv_open,
.release = second_drv_release,
.read    = second_drv_read,
.write    = second_drv_write,
//.poll    = second_drv_poll,
};


static int __init second_drv_init(void)
{
//major = register_chrdev(0, DEVICE_NAME, &second_drv_fops);
int ret;
  
//使用udev自动生成设备文件,并分配设备号
pCdev = cdev_alloc();
if (!pCdev) {
printk(KERN_WARNING "cdev_alloc failed\n");
//goto out;
}
    pCdev->owner = THIS_MODULE;  
cdev_init(pCdev, &second_drv_fops);

ret = alloc_chrdev_region(&devno, 5, 1, "buttons");  
    if(ret){  
        printk(KERN_ERR "alloc char device region faild!\n");  
        //return ret;  
    }  

major = MAJOR(devno);
minor = MINOR(devno);

    ret = cdev_add(pCdev, devno, 1);  //cat /proc/devices可以查看到多了一个buttons设备
    if(ret){  
        printk(KERN_ERR "add char device faild!\n");  
        //goto add_error;  
    }

#if 1
seconddrv_class = class_create(THIS_MODULE, "buttonsClass");//ls /sys/class/buttonsClass/可以查看到多了一个buttonsClass类
if(IS_ERR(seconddrv_class)){  
        printk(KERN_ERR "create class error!\n");         
    } 

//ls /sys/class/buttonsClass/buttons5/可以查看到多了一个buttons5设备
seconddrv_class_dev = device_create(seconddrv_class, NULL, devno/*MKDEV(major, 0)*/, NULL, "buttons%d", MINOR(devno)); /* /dev/buttons */
if(IS_ERR(seconddrv_class_dev)){  
        printk(KERN_ERR "create buttons device error!\n");          
    } 
#endif

printk("second_drv_init \n");
printk("major = %d,minor = %d \n", major,minor);


gpgcon = (volatile unsigned long *)ioremap(0xE0200C00, 16);
gpgdat = gpgcon + 1;

return 0;
}


static void __exit second_drv_exit(void)
{
//unregister_chrdev(major, DEVICE_NAME);
#if 1
device_destroy(seconddrv_class,devno);
class_destroy(seconddrv_class);
#endif


cdev_del(pCdev);
unregister_chrdev_region(devno, 1);  


iounmap(gpgcon);

printk("second_drv_exit \n");
}




module_init(second_drv_init);
module_exit(second_drv_exit);




MODULE_LICENSE("GPL");


测试程序为:



#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>


/* firstdrvtest on
  * firstdrvtest off
  */
int main(int argc, char **argv)
{
int fd;
int cnt = 0;
unsigned char key_vals[4];

fd = open("/dev/buttons5", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
return 0;
}

while (1)
{
read(fd, key_vals, sizeof(key_vals));
if (!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3])
{
printf("%04d key pressed: %d %d %d %d\n", cnt++, key_vals[0], key_vals[1], key_vals[2], key_vals[3]);
}
}
return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值