- 论坛徽章:
- 0
|
小弟编了个按键驱动,是用查询法来读按键有没有按下
[code
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/init.h>
#include <asm/hardware.h>
#include <asm/arch/regs-gpio.h>
#include <linux/fs.h>
#include <asm/segment.h>
static int device_major=0;
#define device_name "shushi_key"
unsigned long key_table[]=
{
S3C2410_GPG0, //按键引脚
S3C2410_GPG3,
S3C2410_GPG5,
S3C2410_GPG6,
S3C2410_GPG7,
S3C2410_GPG11,
};
unsigned long key_table_cfg[]=
{
S3C2410_GPG0_INP,
S3C2410_GPG3_INP, //都配置为输入
S3C2410_GPG5_INP,
S3C2410_GPG6_INP,
S3C2410_GPG7_INP,
S3C2410_GPG11_INP,
};
static int shushi_key_ioctl(struct inode *,struct file *,unsigned int,unsigned long);
static int shushi_key_init(void);
static void shushi_key_exit(void);
struct file_operations f_ops=
{
owner:THIS_MODULE,
ioctl:shushi_key_ioctl,
};
static int shushi_key_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
int temp;//arg来选定来读取哪个按键,cmd没有使用,但是再应用程序中都赋值为1,这样arg不会无效
temp=(~(s3c2410_gpio_getpin(key_table[arg])))&0x1;
return temp;
}
static int shushi_key_init(void)
{
int result,i;
result=register_chrdev(device_major,device_name,&f_ops);
if(result<0)
{
printk("<1>register fail\n");
return -1;
}
device_major=result;
for(i=0;i<6;i++) //设置按键为输入
s3c2410_gpio_cfgpin(key_table,key_table_cfg);
for(i=0;i<6;i++) //按键都带上啦电阻,这里到底是0为带上啦,还是1为带上啦呢?
s3c2410_gpio_pullup(key_table,1);
printk("<1>register success\n");
return 0;
}
static void shushi_key_exit(void)
{
unregister_chrdev(device_major,device_name);
}
module_init(shushi_key_init);
module_exit(shushi_key_exit);
按键按下都是直接接再地上面的,但是只能读出一个键值,其他怎么一开始就自动接地了类? |
|