“tasklet”機制:
閱讀Linux 系統(tǒng)中異常與中斷可知,Linux 系統(tǒng)對中斷處理的演進過程中,實現(xiàn)了中斷的擴展:硬件中斷、軟件中斷
硬件中斷有:GPIO,網(wǎng)絡中斷(net),系統(tǒng)滴答中斷(tick)等
軟件中斷有:定時器,tasklet等
內(nèi)核中的軟中斷:
該數(shù)組里面有個action成員,該成員是個函數(shù),函數(shù)會調(diào)用鏈表里面每個tasklet結(jié)構(gòu)體的軟件中斷處理函數(shù),即下面鏈表的每個成員都是tasklet結(jié)構(gòu)體,有對應的處理函數(shù)和flag標志位
如何觸發(fā)軟中斷?——由軟件決定,對于 X 號軟件中斷,只需要把它的 flag 設置為 1 就表示發(fā)生了該中斷
內(nèi)核函數(shù)
內(nèi)核源碼位置:include\linux\interrupt.h
定義 tasklet
使用結(jié)構(gòu)體tasklet_struct 來表示一個tasklet
struct tasklet_struct
{
struct tasklet_struct *next;
unsigned long state;
atomic_t count;
void (*func)(unsigned long);
unsigned long data;
};
成員解釋:
-
state 有 2 位:
- bit0 表示 TASKLET_STATE_SCHED
- 等于 1 時表示已經(jīng)執(zhí)行了 tasklet_schedule 把該 tasklet 放入隊列了;tasklet_schedule 會判斷該位,如果已經(jīng)等于 1 那么它就不會再次把tasklet 放入隊列。
- bit1 表示 TASKLET_STATE_RUN
- 等于 1 時,表示正在運行 tasklet 中的 func 函數(shù);函數(shù)執(zhí)行完后內(nèi)核會把該位清 0。
- bit0 表示 TASKLET_STATE_SCHED
-
count
- 表示該 tasklet 是否使能:等于 0 表示使能了,非 0 表示被禁止了。對于 count 非 0 的 tasklet,里面的 func 函數(shù)不會被執(zhí)行。
可以用這 2 個宏來定義結(jié)構(gòu)體:
#define DECLARE_TASKLET(name, func, data) \
struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(0), func, data }
#define DECLARE_TASKLET_DISABLED(name, func, data) \
struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
注意:使用 DECLARE_TASKLET_DISABLED 定義的 tasklet 結(jié)構(gòu)體,它是禁止的;使用之前要先調(diào)用 tasklet_enable 使能它。
也可以使用函數(shù)來初始化 tasklet 結(jié)構(gòu)體:
extern void tasklet_init(struct tasklet_struct *t,void (*func)(unsigned long), unsigned long data);
使能/ 禁止 tasklet
static inline void tasklet_enable(struct tasklet_struct *t);
static inline void tasklet_disable(struct tasklet_struct *t);
- tasklet_enable 把 count 增加 1;
- tasklet_disable 把 count 減 1。
調(diào)度 tasklet
static inline void tasklet_schedule(struct tasklet_struct *t);
把 tasklet 放入鏈表,并且設置它的 TASKLET_STATE_SCHED 狀態(tài)為 1。
刪除 tasklet
extern void tasklet_kill(struct tasklet_struct *t);
- 如果一個 tasklet 未被調(diào)度,tasklet_kill 會把它的TASKLET_STATE_SCHED 狀態(tài)清 0;
- 如果一個 tasklet 已被調(diào)度,tasklet_kill 會等待它執(zhí)行完華,再把它TASKLET_STATE_SCHED 狀態(tài)清 0。
通常在卸載驅(qū)動程序時調(diào)用 tasklet_kill
tasklet軟中斷方式的按鍵驅(qū)動程序(stm32mp157)
tasklet使用方法:
- 先定義 tasklet,需要使用時(在硬件中斷處理函數(shù)中)調(diào)用 tasklet_schedule,驅(qū)動卸載前調(diào)用tasklet_kill。tasklet_schedule 只是把 tasklet 放入內(nèi)核隊列,它的 func 函數(shù)會在由內(nèi)核處理軟件中斷的執(zhí)行過程中被調(diào)用
button_test.c
實現(xiàn)功能:首先以非阻塞的方式讀取環(huán)形緩沖區(qū)十次,然后以阻塞的方式讀取按鍵的值
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <signal.h>
static int fd;
/*
* ./button_test /dev/my_gpio_key
*
*/
int main(int argc, char **argv)
{
int val;
struct pollfd fds[1];
int timeout_ms = 5000;
int ret;
int flags;
int i;
/* 1. 判斷參數(shù) */
if (argc != 2)
{
printf("Usage: %s <dev>\n", argv[0]);
return -1;
}
/* 2. 打開文件 */
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if (fd == -1)
{
printf("can not open file %s\n", argv[1]);
return -1;
}
//非阻塞的方式讀取十次
for (i = 0; i < 10; i++)
{
if (read(fd, &val, 4) == 4)
printf("get button: 0x%x\n", val);
else
printf("get button: -1\n");
}
//修改為阻塞的方式,是休眠喚醒機制,沒有數(shù)據(jù)則休眠
flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
while (1)
{
if (read(fd, &val, 4) == 4)
printf("get button: 0x%x\n", val);
else
printf("while get button: -1\n");
}
close(fd);
return 0;
}
gpio_key_drv.c
實現(xiàn)功能,每個按鍵都能打印tasklet里面的軟中斷函數(shù)
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>
struct gpio_key{
int gpio;
struct gpio_desc *gpiod;
int flag;
int irq;
struct timer_list key_timer;
struct tasklet_struct tasklet;//每個按鍵都有軟中斷函數(shù)
} ;
static struct gpio_key *gpio_keys_first;
/* 主設備號 */
static int major = 0;
static struct class *gpio_key_class;
/* 環(huán)形緩沖區(qū) */
#define BUF_LEN 128
static int g_keys[BUF_LEN];
static int r, w;
struct fasync_struct *button_fasync;
#define NEXT_POS(x) ((x+1) % BUF_LEN)
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_keys[w] = key;
w = NEXT_POS(w);
}
}
static int get_key(void)
{
int key = 0;
if (!is_key_buf_empty())
{
key = g_keys[r];
r = NEXT_POS(r);
}
return key;
}
static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);
static void key_timer_expire(struct timer_list *t)
{
struct gpio_key *gpio_key = from_timer(gpio_key, t, key_timer);
int val;
int key;
val = gpiod_get_value(gpio_key->gpiod);
printk("key_timer_expire key %d %d\n", gpio_key->gpio, val);
key = (gpio_key->gpio << 8) | val;
put_key(key);
wake_up_interruptible(&gpio_key_wait);
kill_fasync(&button_fasync, SIGIO, POLL_IN);
}
static void key_tasklet_func(unsigned long data)
{
/* data ==> gpio */
struct gpio_key *gpio_key = data;
int val;
int key;
val = gpiod_get_value(gpio_key->gpiod);
printk("key_tasklet_func key %d %d\n", gpio_key->gpio, val);
}
/* 實現(xiàn)對應的open/read/write等函數(shù),填入file_operations結(jié)構(gòu)體 */
static ssize_t gpio_key_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
//printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
int err;
int key;
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_key_drv_poll(struct file *fp, poll_table * wait)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
poll_wait(fp, &gpio_key_wait, wait);
return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}
static int gpio_key_drv_fasync(int fd, struct file *file, int on)
{
if (fasync_helper(fd, file, on, &button_fasync) >= 0)
return 0;
else
return -EIO;
}
/* 定義自己的file_operations結(jié)構(gòu)體 */
static struct file_operations gpio_key_drv = {
.owner = THIS_MODULE,
.read = gpio_key_drv_read,
.poll = gpio_key_drv_poll,
.fasync = gpio_key_drv_fasync,
};
static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
struct gpio_key *gpio_key = dev_id;
//printk("gpio_key_isr key %d irq happened\n", gpio_key->gpio);
//在硬件中斷函數(shù)里面調(diào)用軟
tasklet_schedule(&gpio_key->tasklet);
mod_timer(&gpio_key->key_timer, jiffies + HZ/50);
return IRQ_HANDLED;
}
/* 1. 從platform_device獲得GPIO
* 2. gpio=>irq
* 3. request_irq
*/
static int gpio_key_probe(struct platform_device *pdev)
{
int err;
struct device_node *node = pdev->dev.of_node;
int count;
int i;
enum of_gpio_flags flag;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
count = of_gpio_count(node);
if (!count)
{
printk("%s %s line %d, there isn't any gpio available\n", __FILE__, __FUNCTION__, __LINE__);
return -1;
}
gpio_keys_first= kzalloc(sizeof(struct gpio_key) * count, GFP_KERNEL);
for (i = 0; i < count; i++)
{
gpio_keys_first[i].gpio = of_get_gpio_flags(node, i, &flag);
if (gpio_keys_first[i].gpio < 0)
{
printk("%s %s line %d, of_get_gpio_flags fail\n", __FILE__, __FUNCTION__, __LINE__);
return -1;
}
gpio_keys_first[i].gpiod = gpio_to_desc(gpio_keys_first[i].gpio);
gpio_keys_first[i].flag = flag & OF_GPIO_ACTIVE_LOW;
gpio_keys_first[i].irq = gpio_to_irq(gpio_keys_first[i].gpio);
//setup_timer(&gpio_keys_first[i].key_timer, key_timer_expire, &gpio_keys_first[i]);
timer_setup(&gpio_keys_first[i].key_timer, key_timer_expire, 0);
gpio_keys_first[i].key_timer.expires = ~0;
add_timer(&gpio_keys_first[i].key_timer);
tasklet_init(&gpio_keys_first[i].tasklet, key_tasklet_func, &gpio_keys_first[i]);//為每個按鍵都注冊軟中斷處理函數(shù),傳入的參數(shù)是按下的按鍵
}
for (i = 0; i < count; i++)
{
err = request_irq(gpio_keys_first[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "my_gpio_key", &gpio_keys_first[i]);
}
/* 注冊file_operations */
major = register_chrdev(0, "my_gpio_key", &gpio_key_drv); /* /dev/gpio_key */
gpio_key_class = class_create(THIS_MODULE, "my_gpio_key_class");
if (IS_ERR(gpio_key_class)) {
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
unregister_chrdev(major, "my_gpio_key");
return PTR_ERR(gpio_key_class);
}
device_create(gpio_key_class, NULL, MKDEV(major, 0), NULL, "my_gpio_key"); /* /dev/my_gpio_key */
return 0;
}
static int gpio_key_remove(struct platform_device *pdev)
{
//int err;
struct device_node *node = pdev->dev.of_node;
int count;
int i;
device_destroy(gpio_key_class, MKDEV(major, 0));
class_destroy(gpio_key_class);
unregister_chrdev(major, "my_gpio_key");
count = of_gpio_count(node);
for (i = 0; i < count; i++)
{
free_irq(gpio_keys_first[i].irq, &gpio_keys_first[i]);
del_timer(&gpio_keys_first[i].key_timer);
tasklet_kill(&gpio_keys_first[i].tasklet);//通常在卸載驅(qū)動程序時調(diào)用 tasklet_kill
}
kfree(gpio_keys_first);
return 0;
}
static const struct of_device_id my_keys[] = {
{ .compatible = "first_key,gpio_key" },
{ },
};
/* 1. 定義platform_driver */
static struct platform_driver gpio_keys_driver = {
.probe = gpio_key_probe,
.remove = gpio_key_remove,
.driver = {
.name = "my_gpio_key",
.of_match_table = my_keys,
},
};
/* 2. 在入口函數(shù)注冊platform_driver */
static int __init gpio_key_init(void)
{
int err;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = platform_driver_register(&gpio_keys_driver);
return err;
}
/* 3. 有入口函數(shù)就應該有出口函數(shù):卸載驅(qū)動程序時,就會去調(diào)用這個出口函數(shù)
* 卸載platform_driver
*/
static void __exit gpio_key_exit(void)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
platform_driver_unregister(&gpio_keys_driver);
}
/* 7. 其他完善:提供設備信息,自動創(chuàng)建設備節(jié)點 */
module_init(gpio_key_init);
module_exit(gpio_key_exit);
MODULE_LICENSE("GPL");
Makefile
# 1. 使用不同的開發(fā)板內(nèi)核時, 一定要修改KERN_DIR
# 2. KERN_DIR中的內(nèi)核要事先配置、編譯, 為了能編譯內(nèi)核, 要先設置下列環(huán)境變量:
# 2.1 ARCH, 比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
# 注意: 不同的開發(fā)板不同的編譯器上述3個環(huán)境變量不一定相同,
# 請參考各開發(fā)板的高級用戶使用手冊
KERN_DIR = /home/book/100ask_stm32mp157_pro-sdk/Linux-5.4
all:
make -C $(KERN_DIR) M=`pwd` modules
$(CROSS_COMPILE)gcc -o button_test button_test.c
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order button_test
# 參考內(nèi)核源碼drivers/char/ipmi/Makefile
# 要想把a.c, b.c編譯成ab.ko, 可以這樣指定:
# ab-y := a.o b.o
# obj-m += ab.o
obj-m += gpio_key_drv.o
修改設備樹文件
對于一個引腳要用作中斷時,
- a) 要通過 PinCtrl 把它設置為 GPIO 功能;【ST 公司對于 STM32MP157 系列芯片,GPIO 為默認模式 不需要再進行配置Pinctrl 信息】
- b) 表明自身:是哪一個 GPIO 模塊里的哪一個引腳【修改設備樹】
打開內(nèi)核的設備樹文件:arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dts
gpio_keys_first {
compatible = "first_key,gpio_key";
gpios = <&gpiog 3 GPIO_ACTIVE_LOW
&gpiog 2 GPIO_ACTIVE_LOW>;
};
與此同時,需要把用到引腳的節(jié)點禁用
注意,如果其他設備樹文件也用到該節(jié)點,需要設置屬性為disabled狀態(tài),在arch/arm/boot/dts目錄下執(zhí)行如下指令查找哪些設備樹用到該節(jié)點
grep "&gpiog" * -nr
如果用到該節(jié)點,需要添加屬性去屏蔽:
status = "disabled";
編譯測試
首先要設置 ARCH、CROSS_COMPILE、PATH 這三個環(huán)境變量后,進入 ubuntu 上板子內(nèi)核源碼的目錄,在Linux內(nèi)核源碼根目錄下,執(zhí)行如下命令即可編譯 dtb 文件:
make dtbs V=1
編譯好的文件在路徑由DTC指定,移植設備樹到開發(fā)板的共享文件夾中,先保存源文件,然后覆蓋源文件,重啟后會掛載新的設備樹,進入該目錄查看是否有新添加的設備節(jié)點
cd /sys/firmware/devicetree/base
編譯驅(qū)動程序,在Makefile文件目錄下執(zhí)行make指令,此時,目錄下有編譯好的內(nèi)核模塊gpio_key_drv.ko和可執(zhí)行文件button_test文件移植到開發(fā)板上
確定一下燒錄系統(tǒng):cat /proc/mounts
,查看boot分區(qū)掛載的位置,將其重新掛載在boot分區(qū):mount /dev/mmcblk2p2 /boot
,然后將共享文件夾里面的設備樹文件拷貝到boot目錄下,這樣的話設備樹文件就在boot目錄下
cp /mnt/stm32mp157c-100ask-512d-lcd-v1.dtb /boot
重啟后掛載,運行文章來源:http://www.zghlxwxcb.cn/news/detail-628976.html
insmod -f gpio_key_drv.ko // 強制安裝驅(qū)動程序
ls /dev/my_gpio_key
./button_test /dev/my_gpio_key & //后臺運行,此時prink函數(shù)打印的內(nèi)容看不到
然后按下按鍵文章來源地址http://www.zghlxwxcb.cn/news/detail-628976.html
到了這里,關于STM32MP157驅(qū)動開發(fā)——按鍵驅(qū)動(tasklet)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!