linux烟雾传感器程序,Linux嵌入式学习-烟雾传感器驱动-字符设备驱动-按键驱动...

#include #include#include#include#include#include#include#include#include#include#include#include

#define GPH2CON 0xE0200c60

#define GPH2DAT 0xE0200c64

static struct class *fog_class; //创建类

static struct class_device *fog_class_devs; //创建类对应的设备

struct work_struct *work1;structtimer_list fogs_timer;

unsignedint *gpio_data;

unsignedint fog_num = 0;

wait_queue_head_t fog_q;void work1_func(struct work_struct *work)

{

mod_timer(&fogs_timer, jiffies + (HZ /10));

}void fogs_timer_function(unsigned longdata)

{

unsignedintfog_val;

fog_val= readw(gpio_data)&0x08;if (fog_val == 0)

{

fog_num= 1;

printk("press down\n");

}

wake_up(&fog_q);

}

irqreturn_t fog_int(int irq, void *dev_id)

{

schedule_work(work1);//return 0;

returnIRQ_HANDLED;

}voidfog_hw_init()

{

unsignedint *gpio_config;

unsignedshortdata;

gpio_request(S5PV210_GPH2(3),"my_fog");

gpio_config= ioremap(GPH2CON,4);

data=readw(gpio_config);

data&= ~(0b1111<<12);

data|= 0b1111<<12;

writew(data,gpio_config);

gpio_data= ioremap(GPH2DAT,1);

}int fog_open(struct inode *node,struct file *filp)

{return 0;

}

ssize_t fog_read(struct file *filp, char __user *buf, size_t size, loff_t *pos)

{

wait_event(fog_q,fog_num);//printk("in kernel :fog num is %d\n",fog_num);

copy_to_user(buf,&fog_num, 4);

fog_num= 0;return 4;

}struct file_operations fog_fops ={

.open=fog_open,

.read=fog_read,

};struct miscdevice fog_miscdev ={

.minor= 200,

.name= "fog",

.fops= &fog_fops,

};intmajor;static intfog_init()

{intret;

major= register_chrdev( 0,"fog_drv", &fog_fops );

fog_class= class_create(THIS_MODULE,"fog_class");

fog_class_devs= device_create(fog_class,NULL,MKDEV(major,0),NULL,"my_fog");if (ret !=0)

printk("register fail!\n");//×¢2á?D????àí3ìDò

request_irq(IRQ_EINT(27),fog_int,IRQF_TRIGGER_FALLING,"fog",0);//°??ü3?ê??ˉ

fog_hw_init();//. ????1€×÷

work1= kmalloc(sizeof(structwork_struct),GFP_KERNEL);

INIT_WORK(work1, work1_func);/*3?ê??ˉ??ê±?÷*/init_timer(&fogs_timer);

fogs_timer.function=fogs_timer_function;/*?ò?úo?×¢2áò?????ê±?÷*/add_timer(&fogs_timer);/*3?ê??ˉμè?y?óáD*/init_waitqueue_head(&fog_q);return 0;

}static voidfog_exit()

{

del_timer(&fogs_timer);

unregister_chrdev( major,"fog_drv");

device_unregister(fog_class_devs);

class_destroy(fog_class);

}

module_init(fog_init);

module_exit(fog_exit);

MODULE_LICENSE("GPL");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值