Linux embedded Learning-Smoke sensor driver-character device driver-key driver

Source: Internet
Author: User

The MQ-2 Smoke gas sensor module is driven on the X210v3 Development Board.

It is now necessary to drive a MQ-2 smoke gas sensor module. After detecting smoke exceeding a certain standard, it will return a different level, similar to the key driver.

But when writing the driver, you need to use the Gph2_3 number PIN. However, the Ioremap address is then configured in the kernel, and it is not possible to control the GPIO or enter the interrupt.

It was later discovered that if you need to use a gpio, you need to apply before you can use it.

The specific procedures are as follows:

#include <linux/module.h>#include<linux/init.h>#include<linux/miscdevice.h>#include<linux/interrupt.h>#include<linux/io.h>#include<linux/fs.h>#include<linux/slab.h>#include<asm/irq.h>#include<linux/random.h>#include<linux/uaccess.h>#include<linux/device.h>#include<mach/gpio.h>#defineGph2con 0XE0200C60#defineGph2dat 0XE0200C64Static struct class*fog_class;//Create ClassStatic structClass_device *fog_class_devs;//Create a device for a classstructWork_struct *Work1;structtimer_list fogs_timer;unsignedint*gpio_data;unsignedintFog_num =0; wait_queue_head_t fog_q;voidWork1_func (structWork_struct *Work ) {Mod_timer (&fogs_timer, Jiffies + (HZ/Ten));}voidFogs_timer_function (unsignedLongdata) {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 (intIrqvoid*dev_id)    {schedule_work (WORK1); //return 0;    returnirq_handled;}voidFog_hw_init () {unsignedint*Gpio_config; unsigned Shortdata; Gpio_request (S5PV210_GPH2 (3),"My_fog"); Gpio_config= Ioremap (Gph2con,4); Data=READW (gpio_config); Data&= ~ (0b1111<< A); Data|= 0b1111<< A;    Writew (Data,gpio_config); Gpio_data= Ioremap (Gph2dat,1);}intFog_open (structInode *node,structFile *Filp) {    return 0;} ssize_t Fog_read (structFile *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;}structFile_operations Fog_fops ={. Open=Fog_open,. Read=Fog_read,};structMiscdevice Fog_miscdev ={. Minor= $,. 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"); //X¢2á? D???? Àí3ìdòRequest_irq (Irq_eint ( -), Fog_int,irqf_trigger_falling,"Fog",0); //°?? Ü3?ê?? ˉtFog_hw_init (); //. ???? 1€x÷Work1= Kmalloc (sizeof(structwork_struct), Gfp_kernel);    Init_work (Work1, Work1_func); /*3?ê?? ˉt?? ʱ?÷*/Init_timer (&Fogs_timer); Fogs_timer.function=fogs_timer_function; /*? Ò?úo?x¢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");

Linux embedded Learning-Smoke sensor driver-character device driver-key driver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.