前言:
把JZ2440翻了出来,无聊写写笔记。
正文:
内核版本:Linux 2.6
硬件图:button接在GPF0这个引脚上。
驱动程序:
#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>
#define BUTTON_IRQ_DRIVER_NAME "button_irq_driver"
enum{
BUTTON_RELEASE = 0XF0,
BUTTON_PRESSED = 0xF1,
};
static int button_irq_driver_open(struct inode *inode, struct file *file);
ssize_t button_irq_driver_read(struct file *file, char __user *buf, size_t size, loff_t *ppos);
static int button_irq_driver_release( struct inode *inode, struct file *file );
static irqreturn_t button_irq(int irq, void *dev_id);
static unsigned int button_value = BUTTON_RELEASE;
static int button_irq_driver_major = 0;
static struct class *button_class;
static struct class_device *button_dev_class;
static const struct file_operations button_irq_driver_file_ops = {
.owner = THIS_MODULE,
.open = button_irq_driver_open,
.read = button_irq_driver_read,
.release = button_irq_driver_release,
};
/* 休眠队列 */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
static volatile int ev_press = 0;
static int button_irq_driver_open(struct inode *inode, struct file *file)
{
printk("button_irq_driver_open\r\n");
s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPIO_INPUT);
s3c2410_gpio_pullup(S3C2410_GPF0, 0); // 0: pullup off
/* request_irq()函数的dev_id 传参内容 要与 free_irq的dev_id 一致 */
request_irq(IRQ_EINT0, button_irq, IRQT_BOTHEDGE, "Button", S3C2410_GPF0);
return 0;
}
ssize_t button_irq_driver_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
printk("button_irq_driver_read\r\n");
if(size != 4) return -EINVAL;
/* 如果没有按键动作, ev_press = 0即休眠 */
wait_event_interruptible(button_waitq, ev_press);
copy_to_user(buf, &button_value, 4);
ev_press = 0;
return size;
}
static int button_irq_driver_release( struct inode *inode, struct file *file )
{
printk("button_irq_driver_release\r\n");
/* request_irq()函数的dev_id 传参内容 要与 free_irq的dev_id 一致 */
free_irq(IRQ_EINT0, S3C2410_GPF0);
return 0;
}
static irqreturn_t button_irq(int irq, void *dev_id)
{
if(s3c2410_gpio_getpin(dev_id))
button_value = BUTTON_RELEASE;
else button_value = BUTTON_PRESSED;
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
return IRQ_RETVAL(IRQ_HANDLED);
}
static int __init button_irq_driver_init(void)
{
printk("button_drive_init\r\n");
button_irq_driver_major = register_chrdev(0, BUTTON_IRQ_DRIVER_NAME, &button_irq_driver_file_ops);
button_class = class_create(THIS_MODULE, BUTTON_IRQ_DRIVER_NAME);
button_dev_class = class_device_create(button_class, NULL, MKDEV(button_irq_driver_major, 0), NULL, BUTTON_IRQ_DRIVER_NAME);
return 0;
}
static void __exit button_irq_driver_exit(void)
{
printk("button_drive_exit\r\n");
class_device_destroy(button_class, MKDEV(button_irq_driver_major, 0));
class_destroy(button_class);
unregister_chrdev(button_irq_driver_major, BUTTON_IRQ_DRIVER_NAME);
}
module_init(button_irq_driver_init);
module_exit(button_irq_driver_exit);
MODULE_LICENSE("GPL");
MakeFile编写:
KERN_DIR = /home/sudaroot/JZ2440/Kernel/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += button_irq_driver.o
编译驱动程序命令:
#make all
生成button_irq_driver.ko
测试程序:
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
enum{
BUTTON_RELEASE = 0XF0,
BUTTON_PRESSED = 0xF1,
};
int main(int argc, char** argv)
{
int fd = 0;
unsigned int val = 1;
fd = open("/dev/button_irq_driver", O_RDWR);
if(fd < 0)
{
printf("open fail\r\n");
return 1;
}
while(1)
{
read(fd, &val, 4);
if(val == BUTTON_PRESSED)
{
printf("Pressed\r\n");
}
else printf("Release\r\n");
}
return 0;
}
编译测试程序命令:
# arm-linux-gcc button_irqAPP.c -o button_irqAPP
开发板测试:
先把前面编好的驱动模块程序button_irq_driver.ko 和 测试程序button_irqAPP复制到开发板上。
装载驱动模块,运行测试程序。
命令行运行详情,按下按键:
卸载驱动:
全篇完。
本人是一个嵌入式未入门小白,博客仅仅代表我个人主观见解,记录成长笔记。
笔记是以最简单的方式,只展示最核心的原理。
若有与 大神大大 见解有歧义,我绝对坚信 大神大大 见解是对的,我的是错的。
若无积分等无法下载源码,留下邮箱。
感谢~!