内核驱动之内核线程示例

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kthread.h> 
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/err.h> 
#include <asm/io.h> 
#define  IBX_GPIO12_OUT   1 << 12
#define  IBX_GPIO44_OUT   1 << 12
#define IBEX_GPIOBASE  (0x500)
#define GP_LVL   (IBEX_GPIOBASE + 0x0c)
#define GP_LVL2   (IBEX_GPIOBASE+0x38)
#define GPIO_USE_SEL   (IBEX_GPIOBASE + 0x00)
#define GP_IO_SEL   (IBEX_GPIOBASE+0x04)
#define GPIO_USE_SEL2   (IBEX_GPIOBASE + 0x30)
#define GP_IO_SEL2   (IBEX_GPIOBASE+0x34)
#define TRUE 1
#define FALSE 0
typedef unsigned int        uint32;
typedef unsigned char       BOOL; 
static struct task_struct * task_test;
BOOL Enablewatchdog( BOOL b)
{    
    uint32 value;
    if(b){
        value=inl(GP_LVL2)&(~IBX_GPIO44_OUT);
        outl(value,GP_LVL2);
        if(inl(GP_LVL2)&(IBX_GPIO44_OUT))
            return FALSE;
        else 
            return TRUE;
    }else{
        value=inl(GP_LVL2)|(IBX_GPIO44_OUT);
        outl(value,GP_LVL2);
        if(inl(GP_LVL2)&(IBX_GPIO44_OUT))
			return TRUE;
        else 
			return FALSE;
    }
}
void feed_dog(void)
{
    uint32 forword;
    forword=inl(GP_LVL)|IBX_GPIO12_OUT;
    outl(forword,GP_LVL);
    if((forword=inl(GP_LVL))&(IBX_GPIO12_OUT)){
        msleep(8000);
        forword&=(~IBX_GPIO12_OUT);
        outl(forword,GP_LVL);
    }else{
        printk(KERN_ALERT"feed dog fault !\n");
    }
}
int watchdog_thread(void *data){
    if(Enablewatchdog(TRUE)){
        printk(KERN_ALERT"Watchdog is start succeed !\n");
        msleep(2000);
    }else{
        printk(KERN_ALERT"Watchdog is start fault !\n");
        return -1;
    }
    while(!kthread_should_stop()){
        feed_dog();
        msleep(10000);
    }
    return 0;
}
static int __init watchdog_init(void)
{
    uint32 value;
    int err;
    value=inl(GPIO_USE_SEL2)|IBX_GPIO44_OUT;
        outl(value,GPIO_USE_SEL2);
    value=inl(GP_IO_SEL2)&(~IBX_GPIO44_OUT);
        outl(value,GP_IO_SEL2);
    value=inl(GPIO_USE_SEL)|IBX_GPIO12_OUT;
        outl(value,GPIO_USE_SEL);
    value=inl(GP_IO_SEL)&(~IBX_GPIO12_OUT);
        outl(value,GP_IO_SEL);
    task_test = kthread_create(watchdog_thread, NULL, "watchdog"); 
    if(IS_ERR(task_test)){
        printk("Unable to start watchdog thread.\n");
        err = PTR_ERR(task_test);
        task_test = NULL;
        return err;
    }
    wake_up_process(task_test);  
    return 0;
}
static void __exit watchdog_exit(void)
{      
    if(Enablewatchdog(FALSE)){
        printk(KERN_ALERT"Watchdog is stop succeed !\n");
         if (task_test) kthread_stop(task_test);
    }else{
        printk(KERN_ALERT"Watchdog is stop fault !\n");
    }
    return;
}
module_init(watchdog_init);
module_exit(watchdog_exit);
MODULE_LICENSE("GPL");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值