1. 电路图
2. 实现原理
采用原子操作打开驱动设备的接口如下:
常用原子操作函数举例:
atomic_t v = ATOMIC_INIT(0); //定义原子变量v并初始化为0
atomic_read(atomic_t *v); //返回原子变量的值
void atomic_inc(atomic_t *v); //原子变量增加1
void atomic_dec(atomic_t *v); //原子变量减少1
int atomic_dec_and_test(atomic_t *v); //自减操作后测试其是否为0,为0则返回true,否则返回false。
使用信号量也可以实现原子操作。
信号量
信号量(semaphore)是用于保护临界区的一种常用方法,只有得到信号量的进程才能执行临界区代码。
当获取不到信号量时,进程进入休眠等待状态。
定义信号量
struct semaphore sem;
初始化信号量
void sema_init (struct semaphore *sem, int val);
void init_MUTEX(struct semaphore *sem);//初始化为0
获得信号量
void down(struct semaphore * sem);
int down_interruptible(struct semaphore * sem);
int down_trylock(struct semaphore * sem);
释放信号量
void up(struct semaphore * sem);
3. 驱动源代码(six_drv.c)
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
static struct class *six_drv_class;
static struct class_device *six_drv_class_dev;
static struct fasync_struct *buttons_async;
/* 键值:按下时, 0x01, 0x02, 0x03, 0x04
* 键值:松开时, 0x81, 0x82, 0x83, 0x84
*/
typedef struct
{
unsigned int pin;
unsigned int key_val;
}s3c2440_buttons_desc;
s3c2440_buttons_desc pins_desc[]={
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
unsigned int key_val;
/* 等待队列:
* 当没有按键被按下时,如果有进程调用s3c24xx_buttons_read函数,
* 它将休眠
*/
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,s3c24xx_buttons_read将它清0 */
static volatile int ev_press = 0;
static atomic_t canopen = ATOMIC_INIT(1); //定义原子变量v并初始化为1
irqreturn_t buttons_irq(int irq, void *dev_id)
{
unsigned pin_val;
s3c2440_buttons_desc *pindesc = (s3c2440_buttons_desc *)dev_id;
/* 读取pin值 */
pin_val = s3c2410_gpio_getpin(pindesc->pin);
/* 确定按键动作 */
if(pin_val)
key_val = pindesc->key_val | 0x80; /* 松 开 */
else
key_val = pindesc->key_val; /* 按 下 */
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
/* 向应用层发送可读信号 */
kill_fasync(&buttons_async, SIGIO, POLL_IN);
return IRQ_HANDLED;
}
int six_drv_open(struct inode *inode, struct file *file)
{
if(atomic_dec_and_test(&canopen)==0)
{
atomic_inc(&canopen);
return -EBUSY;
}
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
ssize_t six_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if(size!=1)
return -EINVAL;
/* 如 果 没 有 按 键 动 作 休 眠 */
wait_event_interruptible(button_waitq, ev_press); /* 如果ev_press等于0,休眠 */
/* 如 果 有 按 键 动 作,返 回 键 值 */
copy_to_user(buf, &key_val, 1);
/* 执行到这里时,ev_press等于1,将它清0 */
ev_press = 0;
return 0;
}
int six_drv_close(struct inode *inode, struct file *file)
{
atomic_dec(&canopen);
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
return 0;
}
unsigned int six_drv_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask=0;
poll_wait(file, &button_waitq, wait); /* 不会立即休眠 */
if(ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
int six_drv_fsync(int fd, struct file *filp, int on)
{
/* 初始化 buttons_async结构体 */
return fasync_helper(fd, filp, on, &buttons_async);
}
static struct file_operations six_drv_fops = {
.owner = THIS_MODULE,
.open = six_drv_open,
.read = six_drv_read,
.release = six_drv_close,
.poll = six_drv_poll,
.fasync = six_drv_fsync,
};
static int major;
static int __init six_drv_init(void)
{
major = register_chrdev(0, "six_drv", &six_drv_fops);
six_drv_class = class_create(THIS_MODULE, "six_drv");
six_drv_class_dev = class_device_create(six_drv_class, NULL, MKDEV(major, 0), NULL, "buttons_atom");
return 0;
}
static void __exit six_drv_exit(void)
{
unregister_chrdev(major, "second_drv");
class_device_unregister(six_drv_class_dev);
class_destroy(six_drv_class);
return 0;
}
module_init(six_drv_init);
module_exit(six_drv_exit);
MODULE_LICENSE("GPL");
4. 测试程序(TestSixDrv.c)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#define DEV_DEVICE "/dev/buttons_atom"
int fd;
void get_key_val(int sig)
{
unsigned char key_val;
read(fd, &key_val, 1);
printf("key_val=0x%x\n", key_val);
}
int main(int argc, char *argv[])
{
int Oflag;
signal(SIGIO, get_key_val);
fd = open(DEV_DEVICE, O_RDWR);
if(fd<0)
{
printf("can't open %s\n", DEV_DEVICE);
return -1;
}
fcntl(fd, F_SETOWN, getpid()); //告诉驱动应用的pid
Oflag = fcntl(fd, F_GETFL); //获取旧的标志
fcntl(fd, F_SETFL, Oflag | FASYNC); //设置FASYNC标志
while(1);
close(fd);
return 0;
}