Linux内核中断底半部处理--内核微线程

内核微线程使用过程:



内核微线程相关函数介绍:

#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_ID			1

#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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eric_Xi_BJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值