First into the Android Drive development character device (five-timer)

Source: Internet
Author: User

This character device series, mainly with the help of more easy-to-use character device instances, to explain some of the Linux mechanisms, as well as the more commonly used in the drive interrupt, timer, signal volume and other knowledge, due to my own limited knowledge, the arm of the architecture system is not very familiar with, here, generally only talk about, how to use, For some of the original rational things will not delve into, the future of the article will slowly deepen.

Think about when we played 51 single-chip computer, then the key to shake-up is a hardware, software needs to deal with the place. Software is generally added time-lapse detection and judgment. Of course, here we can also use the mechanism of the timer, do the key driver, here is mainly the key to the example, but not the key to the button shake.

1. Some concepts of timers:

Beat Rate (Hz): It is defined by static preprocessing, and the hardware is set at the HZ value at system startup. Different architectures and different Hz values.

Jiffies: The global variable jiffies is used to record the total number of beats that have been generated since the system started. At startup, the kernel sets it to 0. System run time in seconds, equal to Jiffies/hz.

Timer: It is the basis for managing the elapsed time of the kernel. The kernel often needs to postpone execution of some code, that is, not at the current time. Timers are simple to use, usually do some initialization work, set a time-out, specify a function to execute after the timeout occurs, and then activate the timer. The specified function will be executed automatically when the timer expires. At the end of execution, the timer revokes itself. Therefore, the timer is constantly created and revoked, and its number of runs is unlimited.

Note: Some neir from "Linux kernel design and implementation"


2. Drive Demo:

#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/ irq.h> #include <linux/input.h> #include <linux/platform_device.h> #include <linux/miscdevice.h > #include <mach/gpio.h> #include <linux/io.h> #include <mach/hardware.h> #include <linux/ delay.h> #include <asm/irq.h> #include <asm/uaccess.h> #include <linux/interrupt.h> #include < linux/gpio.h> #include <linux/wait.h> #include <linux/sched.h> #include <plat/gpio-core.h># Include <plat/gpio-cfg.h> #include <plat/gpio-cfg-helpers.h>static struct class *buttondrv_class;static struct device *buttondrv_class_dev;int major;volatile unsigned long *gpccon;volatile unsigned long *gpcdat;//static Declare_wait_queue_head (BUTTON_WAITQ); static struct timer_list button_timer;static unsigned char key_val;static volatile int ev_press = 0;struct pin_desc{unsigned int pin;unsigned int key_val;}; struct Pin_desc pins_dEsc[2] = {{S5pv210_gph3 (7), 0x01},};struct Pins_desc *irq_temp;static irqreturn_t buttons_irq (int irq, void *dev_id) { PRINTK (">>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>buttons_irq\n "); irq_temp = (struct Pin_desc *) Dev_id;mod_timer ( &button_timer, jiffies+hz/100); return Irq_retval (irq_handled);} static int Button_drv_open (struct inode *inode, struct file *file) {PRINTK (">>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> Button_drv_open\n "); int Ret=-1;s3c_gpio_setpull (S5pv210_gph3 (7), s3c_gpio_pull_none); ret = REQUEST_THREADED_IRQ ( GPIO_TO_IRQ (S5pv210_gph3 (7)), null,buttons_irq,irqf_trigger_rising, "S2", &pins_desc[0]);p rintk ("ret=%d irq=%d >>>>>>>>>>>>>>>>>>>>>>>>>\n ", ret, GPIO_TO_IRQ (S5pv210_gph3 (7))); return 0;}int button_drv_close (struct inode *inode, struct file *file) {FREE_IRQ (GPIO_TO_IRQ (7)), S5pv210_gph3 ]); return 0;} static int button_drv_read (struct file *filp, char __user *buf, size_t count, Lof f_t *OFFP) {PRINTK (">>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>button_drv_read\n "); if (count! = 1) return-einval;//wait_ Event_interruptible (BUTTON_WAITQ, ev_press); Copy_to_user (buf, &key_val, 1); key_val=0;ev_press = 0;return 1;} static void Button_timer_function (unsigned Long data) {PRINTK (">>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>button_timer_ function\n "), struct pin_desc *pindesc = irq_temp;unsigned int pinval;pinval = Gpio_get_value (pindesc->pin);p RINTK (" IRQ >>>>>>>>>>>>>>>>> >>>>>>>>>>>pinval =%d \ n ", pinval); if (pinval) {key_val = 0x80 | Pindesc->key_val;}    Else{key_val = Pindesc->key_val;}                ev_press = 1;  Wake_up_interruptible (&AMP;BUTTON_WAITQ);           }static struct File_operations button_drv_fops = {. Owner = This_module,. Open = Button_drv_open, . read=button_drv_read,. Release = button_drv_close,};unsigned long test=0;static int button_drv_init (void) {PRINTK ("& Gt;>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>button_drv_init\n "); button_timer.function = Button_timer_function;button _timer.data=test;init_timer (&button_timer); Add_timer (&button_timer);  Setup_timer (&button_timer,button_timer_function,test);  Before 4 method instead of the one Gpccon = (volatile unsigned long *) Ioremap (0XE0200C60, 8); gpcdat= Gpccon + 1;if (! Gpccon) {Return-eio;} Major = Register_cHrdev (0, "Button_drv", &button_drv_fops); Buttondrv_class = Class_create (This_module, "buttondrv"); Buttondrv_class_dev = Device_create (Buttondrv_class, NULL, MKDEV (Major, 0), NULL, "button"); return 0;} static void Button_drv_exit (void) {PRINTK (">>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>button_drv_exit\n "); unregister _chrdev (Major, "button_drv"); Device_unregister (Buttondrv_class_dev); Class_destroy (Buttondrv_class); Iounmap (Gpccon);} Module_init (Button_drv_init); Module_exit (Button_drv_exit); Module_license ("GPL");

2.1 Code Simple parsing:

Steps for timer operation:

1. Define a timer_list structure: static struct timer_list button_timer;

2. Function of Timer timeout call: Button_timer.function = button_timer_function;

Parameters to the timer operation function: Button_timer.data=test;

Initializes the timer according to the parameters of the Timer_list: Init_timer (&button_timer);

Activation timer: Add_timer (&button_timer);

Note: Setup_timer (&button_timer,button_timer_function,test); This can replace the 2 steps above.

3. In the interrupt handler function, Mod_timer (&button_timer, jiffies+hz/100); 10ms

Note: The dynamic timer does not need to be released manually, and Mod_timer is automatically released after the timeout is executed.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

First into the Android Drive development character device (five-timer)

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.