Linux内核中断底半部处理--内核微线程
来源:互联网 发布:java判断链表是否有环 编辑:程序博客网 时间:2024/06/06 02:25
内核微线程使用过程:
内核微线程相关函数介绍:
#include <linux/interrupt.h> //头文件包含
微线程创建:
方法一(采用宏):
DECLARE_TASKLET(name, func, data);
参数:微线程名称、任务处理函数、任务处理函数的参数
/*无需提前定义,直接写上name即可*/
方法二(使用初始化函数):
void tasklet_init(struct tasklet_struct *t,void (*func)(unsigned long), unsigned long data);参数1:用户已定义tasklet_struct 变量的地址参数2:tasklet的任务处理函数参数3:func函数的参数
微线程调度函数:
//允许微线程调度(默认为允许)void tasklet_enable(struct tasklet_struct * );//将tasklet加入微线程列表后,立即执行void tasklet_schedule(struct tasklet_struct * );
微线程禁止:
//禁止微线程调度void tasklet_disable(struct tasklet_struct * );void tasklet_disable_nosync(struct tasklet_struct * );其中tasklet_disable_nosync( )函数与disable_irq_nosync( )类似
微线程销毁函数:
void tasklet_kill(struct tasklet_struct * );
微线程小结:
实例代码:
驱动端:
#include <linux/device.h>#include <linux/interrupt.h>#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 <mach/gpio.h>#include <mach/regs-gpio.h> /*S5PV210_GPH3_BASE*/#define EINT_DEVICE_ID1#define DRIVER_NAME"key1_eint"#define err(msg) printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME)#define __debug(fmt, arg...)printk(KERN_DEBUG fmt, ##arg)#define GPH0_CONREG(S5PV210_GPH0_BASE + 0x00)#define GPH0_DATREG(S5PV210_GPH0_BASE + 0x04)#define GPH0_UPREG(S5PV210_GPH0_BASE + 0x08)#define GPH0_DRVREG(S5PV210_GPH0_BASE + 0x0c)#define GPH3CON(unsigned long)(S5PV210_GPH3_BASE+ 0x00)#define GPH3DAT(unsigned long)(S5PV210_GPH3_BASE + 0x04)#define GPH2UP(unsigned long)(S5PV210_GPH2_BASE + 0x08)static int major = 0;/* Driver Major Number */static int minor = 0;/* Driver Minor Number */struct class *key_class;static struct device *key_device;static unsigned char key;/* tasklet variable * @we should do initialization before using it, set the bottom handler * and pass the argument * * 定义全局变量tasklet,使用前需要初始化微线程, * 指定线程处理函数并传递参数 */static struct tasklet_struct tasklet;/* 微线程处理函数tasklet_handler * 该函数完成点亮/熄灭led灯的功能,同时该函数 * 还可以通过参数arg获取当前的键值 */static void tasklet_handler(unsigned long arg){unsigned char key_num = *((unsigned char *)arg);unsigned long reg_data;__debug("the key number is %d\n", key_num);reg_data = readl(GPH0_DATREG);reg_data ^= 0x01<<3;writel(reg_data, GPH0_DATREG);}irqreturn_t buttons_interrupt(int irq, void *dev_id){key = (unsigned int)dev_id;//__debug("in eint function...\n");/*将tasklet添加到微线程的调度列表中并返回*/tasklet_schedule(&tasklet);return IRQ_HANDLED;}/* the initialization of IO ports * @prepare for key device and led device */static void led_io_port_init(void){unsigned long con_val, up_val;con_val = readl(GPH0_CONREG);con_val &= ~(0x0f<<12);con_val |= (0x01<<12);writel(con_val, GPH0_CONREG);up_val = readl(GPH0_UPREG);up_val &= ~(0X03<<6);up_val |= (0X02<<6);writel(up_val, GPH0_UPREG);}static void key_io_port_init(void){unsigned long reg_val;reg_val = readl(GPH3CON);reg_val &= ~((0x0f<<0) | (0x0f<<4));reg_val |= ((0x01<<0) | (0x01<<4));writel(reg_val, GPH3CON);reg_val = readl(GPH3DAT);reg_val &= ~((0x01<<0) | (0x01<<1));writel(reg_val, GPH3DAT);reg_val = readl(GPH2UP);reg_val &= ~(0x03<<8);reg_val |= 0x02<<8;writel(reg_val, GPH2UP);}static ssize_t key_read(struct file *filp, char *buf, size_t count, loff_t *f_pos){int key_num;int cpy_len;int retval;key_num = key;//读取键值key = 0;//清除键值cpy_len = min(sizeof(key_num), count);retval = copy_to_user(buf, &key_num, cpy_len);return (cpy_len - retval);}/* Driver Operation structure */static struct file_operations key_fops = {.owner = THIS_MODULE,.read = key_read,};static int __init key_eint_init(void){int retval;key_io_port_init();led_io_port_init();/* 初始化微线程,指定线程处理函数tasklet_handler, * 传递参数-- key变量的地址 */tasklet_init(&tasklet, tasklet_handler, (unsigned long)&key);retval = set_irq_type(IRQ_EINT(20),IRQ_TYPE_EDGE_FALLING);if(retval){err("IRQ_EINT20 set irq type failed");goto error;}retval = request_irq(IRQ_EINT(20), buttons_interrupt, IRQF_DISABLED, "KEY1", (void *)EINT_DEVICE_ID);if(retval){err("request eint20 failed");goto error;}/* Driver register */major = register_chrdev(major, DRIVER_NAME, &key_fops);if(major < 0){err("register char device fail");retval = major;goto error_register;}key_class=class_create(THIS_MODULE,DRIVER_NAME);if(IS_ERR(key_class)){err("class create failed!");retval = PTR_ERR(key_class);goto error_class;}key_device=device_create(key_class,NULL, MKDEV(major, minor), NULL,DRIVER_NAME);if(IS_ERR(key_device)){err("device create failed!");retval = PTR_ERR(key_device);goto error_device;}__debug("register myDriver OK! Major = %d\n", major);return 0;error_device:class_destroy(key_class);error_class:unregister_chrdev(major, DRIVER_NAME);error_register:free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);error:return retval;}static void __exit key_eint_exit(void){tasklet_kill(&tasklet);free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);unregister_chrdev(major, DRIVER_NAME);device_destroy(key_class,MKDEV(major, minor));class_destroy(key_class);return;}module_init(key_eint_init);module_exit(key_eint_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Eric");
应用层:
#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>int main(){char *devname = "/dev/key1_eint";int fd;unsigned char key;fd = open(devname, O_RDWR);while(1){read(fd, &key, sizeof(key)); if(key)printf("the key = %d\n",key);usleep(100*1000);}close(fd);}
阅读全文
0 0
- Linux内核中断底半部处理--内核微线程
- Linux内核中断处理
- linux内核中断处理
- Linux内核中断底半部处理--工作队列
- Linux内核中断处理流程
- linux内核-中断处理程序
- linux内核中断底半部
- 13 linux内核里实现中断底半部处理的新方法
- linux 内核中的软中断处理
- linux内核分析--中断处理流程
- Linux内核中断处理体系分析
- linux内核架构-中断处理机制
- Linux内核中断处理的irq_thread机制
- 32 linux内核里的中断处理
- Linux内核中断底半部--概述
- 内核中断处理
- 内核中断处理
- 内核中的中断处理
- centos中使用yum下载 RPM 包及其所有依赖
- 梯度提升树(GBDT)原理小结
- Android Butterknife 8.4.0 使用方法总结
- css3选择器--分类(一)
- 关于java中char,byte,short的移位操作
- Linux内核中断底半部处理--内核微线程
- Apache Hive2.1.1安装部署
- Intellij IDEA 创建Web项目并在Tomcat中部署运行
- Qt实现睡眠的几种方法
- linux下获取磁盘的总空间大小和可用空间大小
- pkg-config的用法
- 使用cocoapods加载框架方法
- uiautomatorviewer Unable to connect to adb.Check if adb is installed correctly
- 如何正确编译运行一个包含CocoPods类库的项目