#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");
内核驱动之内核线程示例
最新推荐文章于 2022-02-19 16:11:19 发布