linux下按鍵驅動程式

2022-04-10 22:32:19 字數 3525 閱讀 6251

說明:由於除錯的時候minicom出了問題,傳送大一點的檔案就會失敗,所以下面的程式可能會有點問題,請注意

1.button.c

#include

#include

#include

#include

#include

#include

#include

#include

//#include

#include

//#include

#include

#include

#include

#include

#include

#include

#include

#define dev_name "linux_buttons"

#define led_on_1 ~(1<<5)

#define led_on_2 ~(1<<6)

#define led_on_3 ~(1<<7)

#define led_on_4 ~(1<<8)

static int buttons_major, buttons_minor = 0;

static struct cdev buttons_cdev;

static struct class *buttons_class;

static dev_t dev;

struct button_irq_desc

;static struct button_irq_desc button_irqs[4]=,,

,};static volatile int key_values = ;

static declare_wait_queue_head(button_waitq);

static volatile int ev_press = 0;

static irqreturn_t buttons_interrupt_1(int irq, void *dev_id);

static irqreturn_t buttons_interrupt_4(int irq, void *dev_id);

static irqreturn_t buttons_interrupt_2(int irq, void *dev_id);

static irqreturn_t buttons_interrupt_0(int irq, void *dev_id);

void *sequence_int=;

static irqreturn_t buttons_interrupt_1(int irq, void *dev_id)

}static irqreturn_t buttons_interrupt_4(int irq, void *dev_id)

}static irqreturn_t buttons_interrupt_2(int irq, void *dev_id)

}static irqreturn_t buttons_interrupt_0(int irq, void *dev_id)

}static int buttons_open(struct inode *inode, struct file *filp)

if (err)

return -ebusy;

}return 0;

}static int buttons_close(struct inode *inode, struct file* file)

return 0;

}static int buttons_read(struct file* filp, char __user *buff, size_t count,loff_t *offp)

else

}ev_press = 0;

err = copy_to_user(buff, key_values, count);

memset(key_values, 0, sizeof(key_values));

return err? -efault : 4;

}static unsigned int buttons_poll(struct file *filp, poll_table * wait)

static struct file_operations buttons_fops =

;static int __init buttons_init(void)

else //動態分配裝置號

if (result < 0)

else

cdev_init(&buttons_cdev, &buttons_fops);//初始化cdev結構

buttons_cdev.owner = this_module; //設定cdev.owner成員為hhis_module

//buttons_cdev.ops = &buttons_fops; //設定cdev.ops成員為buttons_fops

result = cdev_add(&buttons_cdev, dev, 1);//設定cdev.dev以及.count成員

if (result < 0)

else

buttons_class = class_create(this_module, dev_name);//建立class結構

if (is_err(buttons_class))

else

/*建立相應的裝置節點*/

device_create(buttons_class, null, mkdev(buttons_major, 0), null, dev_name);

printk(kern_warning"success in buttons_class!\n");

return 0;

}static void __exit buttons_exit(void)

module_init(buttons_init);

module_exit(buttons_exit);

module_license("dual bsd/gpl");

module_author("xiaoheng");

2.buttontest.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

int main(void)

for (;;)

}close(buttons_fd);

return 0;

}3.makefile

obj-m:=button.o

kdir:=/home/xiaoheng/desktop/2.6.30.4/opt/embedsky/linux-2.6.30.4

all:

make -c $(kdir) m=$(shell pwd) modules

clean:

make -c $(kdir) m=$(shell pwd) clean

linux按鍵驅動程式心得

linux按鍵驅動程式心得 在前一章所講的按鍵驅動的時候,用的是迴圈掃瞄的方式,在執行的時候通過ps命令檢視當前的程序,可以看到這個時候按鍵驅動程式的cpu占有率是最高的,這是因為,在寫應用程式的時候,用的是乙個死迴圈,不管有沒有按鍵按下,都會一直的讀取驅動程式傳過來的資料,這個死迴圈是占用cpu最...

中斷 按鍵中斷驅動程式

中斷處理 1 外設的處理速度一般慢於 cpu。2 cpu不能一直等待外部事件,所以裝置必須有一種方法來通知 cpu它的工作進度,這個方法就是中斷,外設與 cpu資訊互動的機制,提高 cpu利用率。處理之外還有查詢,但是查詢會一直占有 cpu資源,導致 cpu低利用率,好處是實現簡單。linux系統中...

按鍵驅動程式(非同步通知)

此驅動程式之前的按鍵驅動程式 中斷方式 上加以優化。用到非同步通知。對於核心來講,既然使用者想得到的是按鍵後的狀態,那麼自然不必時時都要 狀態。當它檢測到中斷發生變主動通知使用者,使用者再來讀。這樣,使用者空間 核心就可以著手幹點其它的事情,而不必忙等按鍵按下或釋放。那麼就先從應用程式上面看。怎麼設...