定時器
定時器主要作用就是:設置超時時間,執行超時函數。
按鍵按下存在抖動,為了消除抖動可以設置定時器,如上圖所示,按下一次按鍵會產生多次抖動,即會產生多次中斷,在每次中斷產生的時候,設置定時器,定時器時間是當前時間+超時時間,這樣每次中斷產生都會重新設置定時器時間,等到按鍵不抖動穩定的時候,就不會再改變定時器時間,這時候我們再記錄按鍵值,就很穩定了。
內核中使用定時器的主要函數?
timer_setup(老版本setup_timer)
設置定時器,主要是初始化timer_list結構體,設置其中參數和函數
#define timer_setup(timer, callback, flags) \__init_timer((timer), (callback), (flags))
add_timer
向內核添加定時器,timer->expires表示超時時間,時間到了內核會自動調用timer->function。
void add_timer(struct timer_list *timer)
{BUG_ON(timer_pending(timer));mod_timer(timer, timer->expires);
}
mod_timer
修改定時器超時時間
int mod_timer(struct timer_list *timer, unsigned long expires)
{return __mod_timer(timer, expires, 0);
}
del_timer
刪除定時器
int del_timer(struct timer_list *timer)
{struct timer_base *base;unsigned long flags;int ret = 0;debug_assert_init(timer);if (timer_pending(timer)) {base = lock_timer_base(timer, &flags);ret = detach_if_pending(timer, base, true);raw_spin_unlock_irqrestore(&base->lock, flags);}return ret;
}
查看系統定時器時間:進入到內核目錄,vi .config 搜索/CONFIG_HZ
3568系統tick如上圖所示?3.33ms發生一次中斷。每發生一次中斷,全局變量jiffies會加1,所以定時器時間都是基于jiffies的。
修改時間有下面兩種方法
/* add_timer之前 */
timer.expires = jiffies + xxx; /* xxx * 3.33ms */
timer.expires = jiffies + 2*HZ;/* jiffies 再加上 2 秒的時間 *//* add_timer之后 */
mod_timer(&timer , jiffies + xxx);
mod_timer(&timer , jiffies + 2*HZ);
?含定時器的驅動代碼
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/gfp.h>
#include <linux/of_irq.h>
#include <linux/poll.h>
#include <linux/timer.h>#define BUF_LEN 128
#define NEXT_POS(x) ((x + 1) % BUF_LEN)static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);
static struct fasync_struct *button_fasync;static struct class *mybutton_class;
static struct gpio_inf *gpio_if;
static int major;
static int g_key[BUF_LEN];
static int r, w;struct gpio_inf {int gpio;int irq;struct gpio_desc *gpiod;enum of_gpio_flags flag;struct timer_list my_button_timer;
};static int is_key_buf_empty(void) {return (r == w);
}static int is_key_buf_full(void) {return (r == NEXT_POS(w));
}static void put_key(int key) {if (!is_key_buf_full()) {g_key[w] = key;w = NEXT_POS(w);}
}static int get_key(void) {int key = 0;if (!is_key_buf_empty()) {key = g_key[r];r = NEXT_POS(r);}return key;
}static void mybutton_keys_timer(struct timer_list *t)
{int val, key;struct gpio_inf *gf = from_timer(gf, t, my_button_timer);if(!gf)return;val = gpio_get_value(gf->gpio);printk("mybutton_keys_timer key %d value%d\n", gf->gpio, val);key = (gf->gpio << 8) | val;put_key(key);wake_up_interruptible(&gpio_key_wait);kill_fasync(&button_fasync, SIGIO, POLL_IN);return;
}static irqreturn_t my_key_handler(int irq, void *dev_id)
{struct gpio_inf * inf = (struct gpio_inf *)dev_id;printk("my_key_handler %s %s %d key:%d\n", __FILE__, __FUNCTION__, __LINE__ , inf->gpio);mod_timer(&inf->my_button_timer, jiffies + HZ/50);return IRQ_HANDLED;
}static ssize_t gpio_button_read(struct file *file, char __user *buf, size_t size, loff_t *off) {int err, key;//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);if(is_key_buf_empty() && (file->f_flags & O_NONBLOCK))return -EAGAIN;wait_event_interruptible(gpio_key_wait, !is_key_buf_empty());key = get_key();err = copy_to_user(buf, &key, 4);return 4;
}static unsigned int gpio_button_poll(struct file *fp, poll_table *wait) {//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);poll_wait(fp, &gpio_key_wait, wait);return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}static int gpio_button_fasync(int fd, struct file *file, int on)
{//printk("%s %s %d\n" , __FILE__ , __FUNCTION__ , __LINE__);if(fasync_helper(fd, file, on , &button_fasync) >= 0)return 0;elsereturn -EIO;
}static struct file_operations button_opr = {.owner = THIS_MODULE,.read = gpio_button_read,.poll = gpio_button_poll,.fasync = gpio_button_fasync,
};static const struct of_device_id my_key[] = {{ .compatible = "my,mybutton" },{},
};MODULE_DEVICE_TABLE(of, my_key);static int chip_demo_gpio_probe(struct platform_device *pdev) {int count, i, err;struct device_node *node;enum of_gpio_flags flag;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);node = pdev->dev.of_node;count = of_gpio_count(node);if (count <= 0) {dev_err(&pdev->dev, "Invalid GPIO count: %d\n", count);return -EINVAL;}gpio_if = kzalloc(count * sizeof(struct gpio_inf), GFP_KERNEL);if (!gpio_if) {dev_err(&pdev->dev, "Failed to allocate memory\n");return -ENOMEM;}for (i = 0; i < count; i++) {gpio_if[i].gpio = of_get_gpio_flags(node, i, &flag);gpio_if[i].irq = gpio_to_irq(gpio_if[i].gpio);gpio_if[i].gpiod = gpio_to_desc(gpio_if[i].gpio);gpio_if[i].flag = flag & OF_GPIO_ACTIVE_LOW;err = request_irq(gpio_if[i].irq, my_key_handler,IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,"my_key", &gpio_if[i]);if (err) {printk("request_irq %d failed\n", gpio_if[i].irq);}timer_setup(&gpio_if[i].my_button_timer, mybutton_keys_timer , 0);gpio_if[i].my_button_timer.expires = ~0; /* 最大超時時間 */add_timer(&gpio_if[i].my_button_timer);}return 0;
}static int chip_demo_gpio_remove(struct platform_device *pdev) {int count, i;struct device_node *node = pdev->dev.of_node;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);count = of_gpio_count(node);for (i = 0; i < count; i++) {del_timer(&gpio_if[i].my_button_timer);free_irq(gpio_if[i].irq, &gpio_if[i]);}kfree(gpio_if);return 0;
}static struct platform_driver my_key_drv = {.probe = chip_demo_gpio_probe,.remove = chip_demo_gpio_remove,.driver = {.name = "my_key_drv",.of_match_table = my_key,}
};static int __init gpio_key_drv_init(void) {int err;// 注冊字符設備major = register_chrdev(0, "my_button", &button_opr);if (major < 0) {printk("register_chrdev failed: %d\n", major);return major;}mybutton_class = class_create(THIS_MODULE, "mybutton_class");if (IS_ERR(mybutton_class)) {unregister_chrdev(major, "my_button");printk("class_create failed\n");return PTR_ERR(mybutton_class);}device_create(mybutton_class, NULL, MKDEV(major, 0), NULL, "my_button");printk("char device /dev/my_button created, major=%d\n", major);// 注冊 platform 驅動err = platform_driver_register(&my_key_drv);if (err) {device_destroy(mybutton_class, MKDEV(major, 0));class_destroy(mybutton_class);unregister_chrdev(major, "my_button");return err;}return 0;
}static void __exit gpio_key_drv_exit(void) {platform_driver_unregister(&my_key_drv);device_destroy(mybutton_class, MKDEV(major, 0));class_destroy(mybutton_class);unregister_chrdev(major, "my_button");printk("char device /dev/my_button removed\n");
}module_init(gpio_key_drv_init);
module_exit(gpio_key_drv_exit);
MODULE_LICENSE("GPL");
?tasklet
當硬件中斷發生時,系統首先調用對應的硬件中斷處理函數(ISR),該函數完成緊急任務后迅速返回。隨后,系統會處理軟中斷(softirq),內核維護了一個軟中斷處理函數數組 softirq_vec[]
,其中包含了用于執行延后任務的函數。作為軟中斷的一種實現,tasklet被安排在軟中斷中執行;當中斷處理函數通過 tasklet_schedule()
調度tasklet時,該tasklet被加入執行鏈表。軟中斷觸發時,系統調用 tasklet_action()
遍歷tasklet鏈表,依次執行每個tasklet的處理函數,從而完成硬件中斷的后續工作。
根據上述流程,可以得出:
1、為每個按鍵添加tasklet。tasklet_init()
2、寫軟中斷執行函數
2、在request_irq的中斷處理函數中,調度tasklet。tasklet_schedule()。
將tasklet加入軟中斷執行鏈表。
驅動程序代碼
這里我在結構體里面添加了last_val,為了判斷按鍵按下是否發生變化,變化則記錄其值,沒變化就不記錄,這是因為正點原子RK3568中使用GPIO引腳電平來模擬按鍵按下和松開,我的板子在這塊賊不穩定,一會就跳出一大堆信息,如下圖所示:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/gfp.h>
#include <linux/of_irq.h>
#include <linux/poll.h>
#include <linux/timer.h>#define BUF_LEN 128
#define NEXT_POS(x) ((x + 1) % BUF_LEN)static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);
static struct fasync_struct *button_fasync;static struct class *mybutton_class;
static struct gpio_inf *gpio_if;
static int major;
static int g_key[BUF_LEN];
static int r, w;struct gpio_inf {int gpio;int irq;int last_val;struct gpio_desc *gpiod;enum of_gpio_flags flag;struct timer_list my_button_timer;struct tasklet_struct task;
};static int is_key_buf_empty(void) {return (r == w);
}static int is_key_buf_full(void) {return (r == NEXT_POS(w));
}static void put_key(int key) {if (!is_key_buf_full()) {g_key[w] = key;w = NEXT_POS(w);}
}static int get_key(void) {int key = 0;if (!is_key_buf_empty()) {key = g_key[r];r = NEXT_POS(r);}return key;
}static void mybutton_keys_timer(struct timer_list *t)
{int val, key;struct gpio_inf *gf = from_timer(gf, t, my_button_timer);if(!gf)return;val = gpio_get_value(gf->gpio);if (val != gf->last_val) {gf->last_val = val; // 更新記錄值return; // 狀態不穩定,忽略這次}printk("mybutton_keys_timer key %d value%d\n", gf->gpio, val);key = (gf->gpio << 8) | val;put_key(key);wake_up_interruptible(&gpio_key_wait);kill_fasync(&button_fasync, SIGIO, POLL_IN);return;
}static void my_button_tasklet(unsigned long data)
{int val, key;struct gpio_inf *gf = (struct gpio_inf *)data;if(!gf)return;val = gpio_get_value(gf->gpio);if (val != gf->last_val) {gf->last_val = val; return; }printk("my_button_tasklet key %d value%d\n", gf->gpio, val);key = (gf->gpio << 8) | val;put_key(key);wake_up_interruptible(&gpio_key_wait);kill_fasync(&button_fasync, SIGIO, POLL_IN);return;
}static irqreturn_t my_key_handler(int irq, void *dev_id)
{struct gpio_inf * inf = (struct gpio_inf *)dev_id;//printk("my_key_handler %s %s %d key:%d\n", __FILE__, __FUNCTION__, __LINE__ , inf->gpio);tasklet_schedule(&inf->task);mod_timer(&inf->my_button_timer, jiffies + HZ/50);return IRQ_HANDLED;
}static ssize_t gpio_button_read(struct file *file, char __user *buf, size_t size, loff_t *off) {int err, key;//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);if(is_key_buf_empty() && (file->f_flags & O_NONBLOCK))return -EAGAIN;wait_event_interruptible(gpio_key_wait, !is_key_buf_empty());key = get_key();err = copy_to_user(buf, &key, 4);return 4;
}static unsigned int gpio_button_poll(struct file *fp, poll_table *wait) {//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);poll_wait(fp, &gpio_key_wait, wait);return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}static int gpio_button_fasync(int fd, struct file *file, int on)
{//printk("%s %s %d\n" , __FILE__ , __FUNCTION__ , __LINE__);if(fasync_helper(fd, file, on , &button_fasync) >= 0)return 0;elsereturn -EIO;
}static struct file_operations button_opr = {.owner = THIS_MODULE,.read = gpio_button_read,.poll = gpio_button_poll,.fasync = gpio_button_fasync,
};static const struct of_device_id my_key[] = {{ .compatible = "my,mybutton" },{},
};MODULE_DEVICE_TABLE(of, my_key);static int chip_demo_gpio_probe(struct platform_device *pdev) {int count, i, err;struct device_node *node;enum of_gpio_flags flag;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);node = pdev->dev.of_node;count = of_gpio_count(node);if (count <= 0) {dev_err(&pdev->dev, "Invalid GPIO count: %d\n", count);return -EINVAL;}gpio_if = kzalloc(count * sizeof(struct gpio_inf), GFP_KERNEL);if (!gpio_if) {dev_err(&pdev->dev, "Failed to allocate memory\n");return -ENOMEM;}for (i = 0; i < count; i++) {gpio_if[i].gpio = of_get_gpio_flags(node, i, &flag);gpio_if[i].irq = gpio_to_irq(gpio_if[i].gpio);gpio_if[i].gpiod = gpio_to_desc(gpio_if[i].gpio);gpio_if[i].flag = flag & OF_GPIO_ACTIVE_LOW;gpio_if[i].last_val = gpio_get_value(gpio_if[i].gpio);err = request_irq(gpio_if[i].irq, my_key_handler,IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,"my_key", &gpio_if[i]);if (err) {printk("request_irq %d failed\n", gpio_if[i].irq);}timer_setup(&gpio_if[i].my_button_timer, mybutton_keys_timer , 0);gpio_if[i].my_button_timer.expires = ~0;add_timer(&gpio_if[i].my_button_timer);tasklet_init(&gpio_if[i].task, my_button_tasklet, (unsigned long)&gpio_if[i]);}return 0;
}static int chip_demo_gpio_remove(struct platform_device *pdev) {int count, i;struct device_node *node = pdev->dev.of_node;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);count = of_gpio_count(node);for (i = 0; i < count; i++) {del_timer(&gpio_if[i].my_button_timer);free_irq(gpio_if[i].irq, &gpio_if[i]);tasklet_kill(&gpio_if[i].task);}kfree(gpio_if);return 0;
}static struct platform_driver my_key_drv = {.probe = chip_demo_gpio_probe,.remove = chip_demo_gpio_remove,.driver = {.name = "my_key_drv",.of_match_table = my_key,}
};static int __init gpio_key_drv_init(void) {int err;// 注冊字符設備major = register_chrdev(0, "my_button", &button_opr);if (major < 0) {printk("register_chrdev failed: %d\n", major);return major;}mybutton_class = class_create(THIS_MODULE, "mybutton_class");if (IS_ERR(mybutton_class)) {unregister_chrdev(major, "my_button");printk("class_create failed\n");return PTR_ERR(mybutton_class);}device_create(mybutton_class, NULL, MKDEV(major, 0), NULL, "my_button");printk("char device /dev/my_button created, major=%d\n", major);// 注冊 platform 驅動err = platform_driver_register(&my_key_drv);if (err) {device_destroy(mybutton_class, MKDEV(major, 0));class_destroy(mybutton_class);unregister_chrdev(major, "my_button");return err;}return 0;
}static void __exit gpio_key_drv_exit(void) {platform_driver_unregister(&my_key_drv);device_destroy(mybutton_class, MKDEV(major, 0));class_destroy(mybutton_class);unregister_chrdev(major, "my_button");printk("char device /dev/my_button removed\n");
}module_init(gpio_key_drv_init);
module_exit(gpio_key_drv_exit);
MODULE_LICENSE("GPL");
工作隊列
中斷下半部(timer,tasklet)都是在中斷上下文中執行的,無法休眠,如果處理復雜事情的時候,無法休眠,會將CPU資源占滿,無法執行用戶程序,這樣就會使得系統卡頓。為了解決該問題,可以使用線程處理復雜事情。線程可以休眠。(在內核中,使用工作隊列,內核會自動創建內核線程)
缺點:當工作隊列中前一個work比較耗時,這樣就會影響到之后的work工作。
驅動要做的部分:
1、構造work,.func
2、將work放入隊列,wake_up喚醒--->schedule_work();
如果處理的事情非常復雜,就不直接使用系統的內核線程,自己創建一個內核線程單獨處理。
container_of()?可以獲得結構體的地址,主要采用反推。
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/gfp.h>
#include <linux/of_irq.h>
#include <linux/poll.h>
#include <linux/timer.h>
#include <linux/workqueue.h>#define BUF_LEN 128
#define NEXT_POS(x) ((x + 1) % BUF_LEN)static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);
static struct fasync_struct *button_fasync;static struct class *mybutton_class;
static struct gpio_inf *gpio_if;
static int major;
static int g_key[BUF_LEN];
static int r, w;struct gpio_inf {int gpio;int irq;int last_val;struct gpio_desc *gpiod;enum of_gpio_flags flag;struct timer_list my_button_timer;struct tasklet_struct task;struct work_struct work;
};static int is_key_buf_empty(void) {return (r == w);
}static int is_key_buf_full(void) {return (r == NEXT_POS(w));
}static void put_key(int key) {if (!is_key_buf_full()) {g_key[w] = key;w = NEXT_POS(w);}
}static int get_key(void) {int key = 0;if (!is_key_buf_empty()) {key = g_key[r];r = NEXT_POS(r);}return key;
}static void mybutton_keys_timer(struct timer_list *t)
{int val, key;struct gpio_inf *gf = from_timer(gf, t, my_button_timer);if(!gf)return;val = gpio_get_value(gf->gpio);if (val != gf->last_val) {gf->last_val = val; // 更新記錄值return; // 狀態不穩定,忽略這次}printk("mybutton_keys_timer key %d value%d\n", gf->gpio, val);key = (gf->gpio << 8) | val;put_key(key);wake_up_interruptible(&gpio_key_wait);kill_fasync(&button_fasync, SIGIO, POLL_IN);return;
}static void my_button_tasklet(unsigned long data)
{int val, key;struct gpio_inf *gf = (struct gpio_inf *)data;if(!gf)return;val = gpio_get_value(gf->gpio);if (val != gf->last_val) {gf->last_val = val; return; }printk("my_button_tasklet key %d value%d\n", gf->gpio, val);key = (gf->gpio << 8) | val;put_key(key);wake_up_interruptible(&gpio_key_wait);kill_fasync(&button_fasync, SIGIO, POLL_IN);return;
}static void my_button_work_func(struct work_struct *work)
{int val, key;struct gpio_inf *gf = container_of(work, struct gpio_inf, work);if(!gf)return;val = gpio_get_value(gf->gpio);if (val != gf->last_val) {gf->last_val = val; return; }printk("my_button_work_func key %d value%d\n", gf->gpio, val);key = (gf->gpio << 8) | val;put_key(key);return;}static irqreturn_t my_key_handler(int irq, void *dev_id)
{struct gpio_inf * inf = (struct gpio_inf *)dev_id;//printk("my_key_handler %s %s %d key:%d\n", __FILE__, __FUNCTION__, __LINE__ , inf->gpio);tasklet_schedule(&inf->task);mod_timer(&inf->my_button_timer, jiffies + HZ/50);schedule_work(&inf->work);return IRQ_HANDLED;
}static ssize_t gpio_button_read(struct file *file, char __user *buf, size_t size, loff_t *off) {int err, key;//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);if(is_key_buf_empty() && (file->f_flags & O_NONBLOCK))return -EAGAIN;wait_event_interruptible(gpio_key_wait, !is_key_buf_empty());key = get_key();err = copy_to_user(buf, &key, 4);return 4;
}static unsigned int gpio_button_poll(struct file *fp, poll_table *wait) {//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);poll_wait(fp, &gpio_key_wait, wait);return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}static int gpio_button_fasync(int fd, struct file *file, int on)
{//printk("%s %s %d\n" , __FILE__ , __FUNCTION__ , __LINE__);if(fasync_helper(fd, file, on , &button_fasync) >= 0)return 0;elsereturn -EIO;
}static struct file_operations button_opr = {.owner = THIS_MODULE,.read = gpio_button_read,.poll = gpio_button_poll,.fasync = gpio_button_fasync,
};static const struct of_device_id my_key[] = {{ .compatible = "my,mybutton" },{},
};MODULE_DEVICE_TABLE(of, my_key);static int chip_demo_gpio_probe(struct platform_device *pdev) {int count, i, err;struct device_node *node;enum of_gpio_flags flag;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);node = pdev->dev.of_node;count = of_gpio_count(node);if (count <= 0) {dev_err(&pdev->dev, "Invalid GPIO count: %d\n", count);return -EINVAL;}gpio_if = kzalloc(count * sizeof(struct gpio_inf), GFP_KERNEL);if (!gpio_if) {dev_err(&pdev->dev, "Failed to allocate memory\n");return -ENOMEM;}for (i = 0; i < count; i++) {gpio_if[i].gpio = of_get_gpio_flags(node, i, &flag);gpio_if[i].irq = gpio_to_irq(gpio_if[i].gpio);gpio_if[i].gpiod = gpio_to_desc(gpio_if[i].gpio);gpio_if[i].flag = flag & OF_GPIO_ACTIVE_LOW;gpio_if[i].last_val = gpio_get_value(gpio_if[i].gpio);err = request_irq(gpio_if[i].irq, my_key_handler,IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,"my_key", &gpio_if[i]);if (err) {printk("request_irq %d failed\n", gpio_if[i].irq);}timer_setup(&gpio_if[i].my_button_timer, mybutton_keys_timer , 0);gpio_if[i].my_button_timer.expires = ~0;add_timer(&gpio_if[i].my_button_timer);tasklet_init(&gpio_if[i].task, my_button_tasklet, (unsigned long)&gpio_if[i]);INIT_WORK(&gpio_if[i].work, my_button_work_func);}return 0;
}static int chip_demo_gpio_remove(struct platform_device *pdev) {int count, i;struct device_node *node = pdev->dev.of_node;printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);count = of_gpio_count(node);for (i = 0; i < count; i++) {del_timer(&gpio_if[i].my_button_timer);free_irq(gpio_if[i].irq, &gpio_if[i]);tasklet_kill(&gpio_if[i].task);}kfree(gpio_if);return 0;
}static struct platform_driver my_key_drv = {.probe = chip_demo_gpio_probe,.remove = chip_demo_gpio_remove,.driver = {.name = "my_key_drv",.of_match_table = my_key,}
};static int __init gpio_key_drv_init(void) {int err;// 注冊字符設備major = register_chrdev(0, "my_button", &button_opr);if (major < 0) {printk("register_chrdev failed: %d\n", major);return major;}mybutton_class = class_create(THIS_MODULE, "mybutton_class");if (IS_ERR(mybutton_class)) {unregister_chrdev(major, "my_button");printk("class_create failed\n");return PTR_ERR(mybutton_class);}device_create(mybutton_class, NULL, MKDEV(major, 0), NULL, "my_button");printk("char device /dev/my_button created, major=%d\n", major);// 注冊 platform 驅動err = platform_driver_register(&my_key_drv);if (err) {device_destroy(mybutton_class, MKDEV(major, 0));class_destroy(mybutton_class);unregister_chrdev(major, "my_button");return err;}return 0;
}static void __exit gpio_key_drv_exit(void) {platform_driver_unregister(&my_key_drv);device_destroy(mybutton_class, MKDEV(major, 0));class_destroy(mybutton_class);unregister_chrdev(major, "my_button");printk("char device /dev/my_button removed\n");
}module_init(gpio_key_drv_init);
module_exit(gpio_key_drv_exit);
MODULE_LICENSE("GPL");
內核線程
?中斷的線程化處理
主要程序代碼
/* 注冊irq時,使用request_threaded_irq */
err = request_threaded_irq(gpio_if[i].irq , my_key_handler , my_key_threaded_func , IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT , "my_key" , &gpio_if[i]);static irqreturn_t my_key_threaded_func(int irq, void *data)
{int val, key;struct gpio_inf *gf = (struct gpio_inf *)data ;val = gpio_get_value(gf->gpio);printk("my_key_threaded_func key %d value%d\n", gf->gpio, val);printk("my_key_threaded_func: the process is %s pid %d\n" , current->comm , current->pid);key = (gf->gpio << 8) | val;put_key(key);return IRQ_HANDLED;
}
結果如圖所示