驱动代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//中断
struct device_node *dnode;
unsigned int key_irqno;
unsigned int gpiono;
//字符设备驱动
int major;
//自动创建设备节点
struct class *cls;
struct device *dev;
int number = 0;
unsigned int condition = 0;
wait_queue_head_t wq_head; //等待队列头
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
char kbuf[128] = {0};
sprintf(kbuf, "number = %d", number);
if (file->f_flags & O_NONBLOCK) {
} else {
//wait_event_interruptible(wq_head, condition);
}
int ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy_to_user err \n");
return ret;
}
condition = 0;
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
__poll_t mycdev_poll(struct file *file, struct poll_table_struct *wait) {
__poll_t mask = 0;
//向上提交等待队列头
poll_wait(file, &wq_head, wait);
if (condition) {
mask = POLLIN;
}
return mask;
}
// 定义操作方法结构体遍历并且初始化
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
.poll = mycdev_poll,
};
irqreturn_t key_handler(int irq, void *dev)
{
if (number == 0) {
number = 1;
} else {
number = 0;
}
gpio_set_value(gpiono, number);
condition = 1;
wake_up_interruptible(&wq_head);
return IRQ_HANDLED;
}
static int __init mycdev_init(void)
{
// 初始化等待队列头
init_waitqueue_head(&wq_head);
/**
* 注册设备驱动
*/
major = register_chrdev(0, "mychrdev", &fops);
if (major < 0) {
printk("字符设备驱动注册失败 \n");
return major;
}
printk("字符设备驱动注册成功\n");
/**
* 自动创建设备节点
*/
//向上提交目录信息
cls = class_create(THIS_MODULE, "mycdev");
if (IS_ERR(cls)) {
printk("向上提交目录信息失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录信息成功\n");
//向上提交设备信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev0");
if (IS_ERR(dev)) {
printk("向上提交设备节点失败\n");
return -PTR_ERR(cls);
}
/**
* 中断
*/
dnode = of_find_compatible_node(NULL, NULL, "lxf,homework");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析按键的设备树节点成功\n");
// 获取软中断号
key_irqno = irq_of_parse_and_map(dnode, 0);
if (!key_irqno)
{
printk("获取软中断号失败\n");
return -1;
}
printk("获取软中断号成功%d\n", key_irqno);
// 注册中断
int ret = request_irq(key_irqno, key_handler, IRQF_TRIGGER_FALLING, "key1", NULL);
if (ret < 0)
{
printk("注册中断失败\n");
return -1;
}
printk("注册中断成功\n");
/**
* LED灯
*/
//获取gpio编号
gpiono = of_get_named_gpio(dnode, "led1", 0);
if (gpiono < 0) {
printk("gpio编号解析失败\n");
return -ENXIO;
}
printk("解析gpio编号成功\n");
//申请gpio编号
ret = gpio_request(gpiono, NULL);
if (ret) {
printk("申请gpio编号失败\n");
return -1;
}
printk("申请gpio编号成功\n");
//设置为输出模式
gpio_direction_output(gpiono, 0);
return 0;
}
static void __exit mycdev_exit(void)
{
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, "mychrdev");
free_irq(key_irqno, NULL);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
测试代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char *argv[]) {
int fd, epfd;
struct epoll_event event;
struct epoll_event events[10];
char buf[128] = {0};
epfd = epoll_create(1);
if (epfd < 0) {
printf("epoll_create failed \n");
return -1;
}
fd = open("/dev/mycdev0", O_RDWR);
if (fd < 0) {
printf("打开文件失败\n");
return -1;
}
event.events = EPOLLIN;
event.data.fd = fd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event) < 0) {
printf("epoll_ctl add failed\n");
return -1;
}
int ret, i;
while(1) {
ret = epoll_wait(epfd, events, 10, -1);
if (ret < 0) {
printf("epoll_wait failed\n");
return -1;
}
for (i=0; i