[Country EMBED strategy] [122] [Button customizer to shake]

Source: Internet
Author: User

Key jitter

The switch used for the button is a mechanical elastic switch, and when the mechanical contact is disconnected and closed, the switch is not immediately and stably switched on or off due to the elastic action of the mechanical contact point. Thus there is always a series of jitter in the moment of closure and disconnection.

There are two main ways to shake keys, one is hardware circuit to shake, the other is software delay to shake. and delay to shake generally divided into two, one is for the loop wait, and the other is the timer delay. In the operating system, for efficiency reasons, it is generally not allowed to use a for loop to wait, only use timers.

Kernel timers

The Linux kernel uses struct timer_list to describe a timer:

struct timer_list{

struct List_head entry;

unsigned long expires; Timing Time

void (*function) (unsigned long); Processing functions

unsigned long data;

struct Tvec_base *base;

};

Timer usage Flow

1. Defining timers

2. Initializing the timer

2.1.init_timer initialization

2.2. Setting the timeout function

3.add_timer Register Timer

4. Start the timer device

4.1. Set the time-out period

4.2.mod_timer Start Timer

Jiffies represents the current time of the system, in units of ticks, with 1000 ticks in a second. Hz represents one second.

Keydev.c

/********************************************************************* Header File ************************************ *********************************/#include<linux/init.h>#include<linux/module.h>#include<linux/miscdevice.h>#include<linux/interrupt.h>#include<linux/io.h>#include<linux/fs.h>/********************************************************************* Macro Definition ************************************ *********************************/#defineGpgcon 0x56000060//Key Control Register address#defineGpgdat 0x56000064//Key Data Register address/********************************************************************* global variable *********************************** **********************************/unsignedint*keycon;//key Control PointerUnsignedint*keydat;//Key Data PointerstructWork_struct *work;//Key WorkstructTimer_list timer;//Key Timing/********************************************************************* Timing Processing *********************************** **********************************///Device TimingvoidKey_timer (unsignedLongdata) {    //Read key StateUnsigned Shortkeytmp; intLevel ; Keytmp= READW (Keydat);//Get Gpgdat valueLevel = Keytmp &0x0001;//get key level//determine the key state    if(Level = =0) {PRINTK ("Key down!\n"); }}/********************************************************************* Interrupt Handling *********************************** **********************************///device interrupt Lower partvoidKey_work (structWork_struct *Work ) {    //start the timing deviceMod_timer (&timer, jiffies + hz/ -);//Jiffies indicates the current tick of the system, 1HZ = 1s = 1000jiffies}//Device Interrupt UpperIrqreturn_t KEY_IRQ (intIrqvoid*dev_id) {    //Handling hardware-related//Commit Hardware Independentschedule_work (work); return 0;}/********************************************************************* Device Method *********************************** **********************************///Device OpenintKey_open (structInode *node,structFile *Filp) {    return 0;}//Device offintKey_close (structInode *node,structFile *Filp) {    return 0;}//Device MethodstructFile_operations Key_fops ={. Open=Key_open,. Release=Key_close};/********************************************************************* Module Installation *********************************** **********************************///Hybrid EquipmentstructMiscdevice Misdev ={. Minor= $,//Secondary Device number. Name ="Keydev",//Device Name. FoPs = &key_fops//Device Method};//Registering HardwarevoidHandware_init (void){    //initializing key control registersUnsignedintkeytmp; Keycon= Ioremap (Gpgcon,4);//Virtual Address mappingkeytmp= Readl (Keycon);//Get Gpgcon valueKeytmp &= ~ (0x3<<0);//gpg0[1:0]:00Keytmp |= (0x2<<0);//Gpg0[1:0]:eint[8]Writel (keytmp, Keycon);//Setting the Gpgcon value//initializing key State registersKeydat = Ioremap (Gpgdat,2);//Virtual Address mapping}//Mounting ModuleStatic intKey_init (void){    //Registering hybrid DevicesMisc_register (&Misdev); //Registering hardware devicesHandware_init (); //Register Interrupt handlingREQUEST_IRQ (Irq_eint8, KEY_IRQ, irqf_trigger_falling,"KEYIRQ",0);//falling edge trigger, irq_eint8 defined in Irqs.h file//Registering a Work queueWork = Kmalloc (sizeof(structwork_struct), Gfp_kernel);        Init_work (work, key_work); //registering a timing deviceInit_timer (&timer);//Initialize TimerTimer.function = Key_timer;//Add a timer functionAdd_timer (&timer);//Adding a timing device        return 0;}//Uninstalling the moduleStatic voidKey_exit (void){    //unregister a promiscuous deviceMisc_deregister (&Misdev); //Logoff Interrupt processingFREE_IRQ (Irq_eint8,0);}/********************************************************************* Module Declaration *********************************** **********************************/Module_license ("GPL"); Module_author ("D"); Module_description (""); Module_version ("v1.0"); Module_init (Key_init); Module_exit (key_exit) ;

[Country EMBED strategy] [122] [Button customizer to shake]

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.