按键驱动编写步骤:
1.驱动框架
2.硬件资料查询
3.代码:
一:轮询查找
二:中断方式
三:poll机制
四:异步通知
五:同步互斥阻塞
轮询查找方式:
一.驱动框架
#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>
static struct class *seconddrv_buttons_class;
static struct class_device *seconddrv_buttons_class_dev;
static int seconddrv_buttons_open(struct inode *inode, struct file *file)
{
printk("seconddrv_open\n");
return 0;
}
static int seconddrv_buttons_read(struct file *filp, char __user *buff,size_t count, loff_t *offp)
{
printk("seconddrv_read\n");
return 0;
}
static struct file_operations seconddrv_buttons_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = seconddrv_buttons_open,
.read = seconddrv_buttons_read,
};
int major;
int seconddrv_buttons_init(void)
{
major = register_chrdev(0,"seconddrv",&seconddrv_buttons_fops);
seconddrv_buttons_class = class_create(THIS_MODULE,"buttons");
seconddrv_buttons_class_dev = class_device_create(seconddrv_buttons_class,NULL,MKDEV(major,0),NULL,"mybuttons");
return 0;
}
void seconddrv_buttons_exit(void)
{
unregister_chrdev(major,"seconddrv");
class_destroy(seconddrv_buttons_class);
class_device_unregister(seconddrv_buttons_class_dev);
return 0;
}
module_init(seconddrv_buttons_init);
module_exit(seconddrv_buttons_exit);
MODULE_LICENSE("GPL");
二.硬件资料查询
1.原理图查看引脚
2.查询物理地址和寄存器资料
我们先用轮询查询的方式,不用中断方式,所以引脚要设置为输入模式,参考芯片手册
物理地址:
GPFCON寄存器和GPGCON寄存器:
GPFDAT寄存器和GPGDAT寄存器:
三.硬件代码编写
思路:先把控制寄存器设置为输入模式,定义四个按键值,读取四个按键值,返回用户空间,在用户空间循环读取和打印信息
static int seconddrv_buttons_open(struct inode *inode, struct file *file)
{
/*配置GPF0,2为输入引脚*/
*gpfcon &=~((0x03<<(0*2))|(0x03<<(2*2)));
/*配置GPG3,11为输入引脚*/
*gpgcon &=~((0x03<<(2*3))|(0x03<<(2*11)));
//printk("seconddrv_open\n");
return 0;
}
ssize_t seconddrv_buttons_read(struct file *file, char __user *buf,size_t size, loff_t *ppos)
{
/* 返回4个引脚的电平 */
unsigned char key_vals[4];
int regval;
if (size != sizeof(key_vals))
return -EINVAL;
/* 读GPF0,2 */
regval = *gpfdat;
key_vals[0] = (regval & (1<<0)) ? 1 : 0;
key_vals[1] = (regval & (1<<2)) ? 1 : 0;
/* 读GPG3,11 */
regval = *gpgdat;
key_vals[2] = (regval & (1<<3)) ? 1 : 0;
key_vals[3] = (regval & (1<<11)) ? 1 : 0;
copy_to_user(buf, key_vals, sizeof(key_vals));
return sizeof(key_vals);
}
static struct file_operations seconddrv_buttons_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = seconddrv_buttons_open,
.read = seconddrv_buttons_read,
};
int major;
int seconddrv_buttons_init(void)
{
major = register_chrdev(0,"seconddrv",&seconddrv_buttons_fops);
seconddrv_buttons_class = class_create(THIS_MODULE,"buttons");
seconddrv_buttons_class_dev = class_device_create(seconddrv_buttons_class,NULL,MKDEV(major,0),NULL,"mybuttons");
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
gpgdat = gpgcon +1;
return 0;
}
四.测试代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
/*
*mybuttons_test
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_vals[4];
int cnt = 0;
fd = open("/dev/mybuttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
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;
}
结果: