4412驱动-7th_buttons_all 定时器去抖动按键驱动

来源:互联网 发布:万网ftp端口 编辑:程序博客网 时间:2024/05/21 08:03

定时器

poll





//***************************************

#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/platform_device.h>#include <linux/fb.h>#include <linux/backlight.h>#include <linux/err.h>#include <linux/pwm.h>#include <linux/slab.h>#include <linux/miscdevice.h>#include <linux/delay.h>#include <linux/timer.h>  /*timer*/#include <asm/uaccess.h>  /*jiffies*/#include <linux/delay.h>#include <linux/interrupt.h> //request_irq#include <mach/irqs.h> //中断号,已包含plat/irqs.h#include <linux/fs.h>#include <linux/device.h> //class_create device_create#include <mach/regs-gpio.h>#include <linux/io.h> //ioremap ioread32 iowrite32#include <linux/sched.h>#include <linux/of.h>  #include <linux/of_device.h>  #include <linux/poll.h>#include <mach/gpio.h>#include <linux/gpio.h>#include <mach/gpio.h>#include <plat/gpio-cfg.h>static struct class *sixthtimerdrv_class;static struct class_device*sixthtimerdrv_class_dev;struct semaphore button_lock;static struct timer_list buttons_timer;static struct fasync_struct *button_async;static DECLARE_WAIT_QUEUE_HEAD(button_waitq);/* 中断事件标志, 中断服务程序将它置1,sixthtimer_drv_read将它清0 */static volatile int ev_press = 0;struct led_reg {u32 gpm4con;u8 gpm4dat;};static struct led_reg *led_reg;struct key_reg {u32 gpm4con;u8 gpm4dat;};static struct key_reg *key_reg;struct beep_reg {u32 gpm4con;u8 gpm4dat;};static struct key_reg *beep_reg;struct pin_desc{unsigned int pin;unsigned int key_val;};/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 *//* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */static unsigned char key_val;struct pin_desc pins_desc[4] = {{EXYNOS4_GPX3(2), 0x01},{EXYNOS4_GPX3(3), 0x02},{EXYNOS4_GPX3(4), 0x03},{EXYNOS4_GPX3(5), 0x04},};static struct pin_desc *irq_pd;int major;/*  * 确定按键值  */static irqreturn_t buttons_irq(int irq, void *dev_id){printk("buttons_irq\n");/* 10ms后启动定时器 */irq_pd = (struct pin_desc *)dev_id;mod_timer(&buttons_timer, jiffies+HZ/100);return IRQ_RETVAL(IRQ_HANDLED);}static int sixthtimer_drv_open(struct inode *inode, struct file *file){int ret;printk("sixthtimer_drv_open\n");#if 0if (!atomic_dec_and_test(&canopen)){atomic_inc(&canopen);return -EBUSY;}#endifif (file->f_flags & O_NONBLOCK){if (down_trylock(&button_lock))return -EBUSY;}else{/* 获取信号量 */down(&button_lock);}//配置4个按键为输入状态,因为按键是从GPXCON[2]开始的,所以要左移8位到对应的位置,将8位以后的16位清0//这样的话就将按键配置的寄存器设置为输入状态了,因为输入是0x0 key_reg->gpm4con  &= ~((0xf<<(2*4)) | (0xf<<(3*4)) | (0xf<<(4*4)) | (0xf<<(5*4)));//先对LED的端口进行清0操作led_reg->gpm4con  &= ~((0xf<<(3*4)) | (0xf<<(2*4)) | (0xf<<(1*4)) | (0xf<<(0*4)));//将4个IO口16位都设置为Output输出状态led_reg->gpm4con |= ((0x1<<(3*4)) | (0x1<<(2*4)) | (0x1<<(1*4)) | (0x1<<(0*4)));//清寄存器 beep_reg->gpm4con &= ~(0xf);//设置io为输出 beep_reg->gpm4con |= (0x1);  ret = request_irq(IRQ_EINT(26), buttons_irq, IRQF_TRIGGER_FALLING , "k1", &pins_desc[0]);ret =request_irq(IRQ_EINT(27), buttons_irq, IRQF_TRIGGER_FALLING , "k2", &pins_desc[1]);ret =request_irq(IRQ_EINT(28), buttons_irq, IRQF_TRIGGER_FALLING , "k3", &pins_desc[2]);ret =request_irq(IRQ_EINT(29), buttons_irq, IRQF_TRIGGER_FALLING , "k4", &pins_desc[3]);return 0;}ssize_t sixthtimer_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos){printk("sixthtimer_drv_read\n");if (size != 1)return -EINVAL;if (file->f_flags & O_NONBLOCK){if (!ev_press)return -EAGAIN;}else{/* 如果没有按键动作, 休眠 */wait_event_interruptible(button_waitq, ev_press);}/* 如果有按键动作, 返回键值 */copy_to_user(buf, &key_val, 1);ev_press = 0;return 1;}int sixthtimer_drv_close(struct inode *inode, struct file *file){//atomic_inc(&canopen);led_reg->gpm4dat |= ((1<<0) | (1<<1) |(1<<2)| (1<<3)) ; beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响free_irq(IRQ_EINT(26), &pins_desc[0]);free_irq(IRQ_EINT(27), &pins_desc[1]);free_irq(IRQ_EINT(28), &pins_desc[2]);free_irq(IRQ_EINT(29), &pins_desc[3]);up(&button_lock);return 0;}int sixthtimer_drv_write(struct file *filp , const char __user *buf , size_t count , loff_t *f_pos){int val;printk("sixthtimer_drv_write\n");//注意,这里是在内核中进行操作,我们需要使用copy_from_user这个函数将用户态的内容拷贝到内核态copy_from_user(&val, buf, count);        switch(val)        {        case 1:printk(KERN_EMERG"led1_on\n");led_reg->gpm4dat &= ~(1<<0) ;printk(KERN_EMERG"beep_on\n");beep_reg->gpm4dat |= 0x1 ;break ;case 2:printk(KERN_EMERG"led2_on\n");led_reg->gpm4dat &= ~(1<<1) ;printk(KERN_EMERG"beep_off\n");beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响break ;case 3:printk(KERN_EMERG"led3_on\n");led_reg->gpm4dat &= ~(1<<2) ;printk(KERN_EMERG"beep_on\n");beep_reg->gpm4dat |= 0x1 ;break ;case 4:printk(KERN_EMERG"led4_on\n");led_reg->gpm4dat &= ~(1<<3) ;printk(KERN_EMERG"beep_off\n");beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响break ;        }return 0;}static unsigned sixthtimer_drv_poll(struct file *file, poll_table *wait){printk("sixthtimer_drv_poll\n");unsigned int mask = 0;poll_wait(file, &button_waitq, wait); // 不会立即休眠if (ev_press)mask |= POLLIN | POLLRDNORM;return mask;}static int sixthtimer_drv_fasync (int fd, struct file *filp, int on){printk("driver: sixthtimer_drv_fasync\n");return fasync_helper (fd, filp, on, &button_async);}static struct file_operations sencod_drv_fops = {    .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */    .open    =  sixthtimer_drv_open,     .read =sixthtimer_drv_read,   .release =  sixthtimer_drv_close,.poll    =  sixthtimer_drv_poll,.fasync =  sixthtimer_drv_fasync,.write = sixthtimer_drv_write,};static void buttons_timer_function(unsigned long data){printk("buttons_timer_function\n");struct pin_desc * pindesc = irq_pd;unsigned int pinval;if (!pindesc)return;pinval = gpio_get_value(pindesc->pin);if (pinval){/* 松开 */key_val = 0x80 | pindesc->key_val;}else{/* 按下 */key_val = pindesc->key_val;}    ev_press = 1;                  /* 表示中断发生了 */    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */kill_fasync (&button_async, SIGIO, POLL_IN);}static int sixthtimer_drv_init(void){init_timer(&buttons_timer);buttons_timer.function = buttons_timer_function;//buttons_timer.expires  = 0;add_timer(&buttons_timer); major = register_chrdev(0, "sixthtimer_drv", &sencod_drv_fops);sixthtimerdrv_class = class_create(THIS_MODULE, "sixthtimer_drv");/* 为了让mdev根据这些信息来创建设备节点 */sixthtimerdrv_class_dev = device_create(sixthtimerdrv_class, NULL, MKDEV(major, 0), NULL, "chenhaipan"); /* /dev/buttons */led_reg = ioremap(0x110002e0, sizeof(struct led_reg));beep_reg = ioremap(0x114000A0, sizeof(struct beep_reg));key_reg = ioremap(0x11000C60, sizeof(struct key_reg));sema_init(&button_lock, 1);return 0;}static void sixthtimer_drv_exit(void){unregister_chrdev(major, "sixthtimer_drv");device_destroy(sixthtimerdrv_class, MKDEV(major, 0));class_destroy(sixthtimerdrv_class);iounmap(led_reg);iounmap(beep_reg);iounmap(key_reg);}module_init(sixthtimer_drv_init);module_exit(sixthtimer_drv_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("xiangtan da xue chenhaipan");  MODULE_VERSION("2017.5.4"); 


//*********************************************



#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>




/* sixthdrvtest 
  */
int fd;


void my_signal_fun(int signum)
{
unsigned char key_val;
read(fd, &key_val, 1);
printf("key_val: 0x%x\n", key_val);
}


int main(int argc, char **argv)
{
unsigned char key_val;
int ret;
int Oflags;


//signal(SIGIO, my_signal_fun);
struct pollfd fds[1];
fd = open("/dev/chenhaipan", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
return -1;
}
fds[0].fd     = fd;
fds[0].events = POLLIN;

//fcntl(fd, F_SETOWN, getpid());

//Oflags = fcntl(fd, F_GETFL); 

//fcntl(fd, F_SETFL, Oflags | FASYNC);




while (1)
{
ret = poll(fds, 1, 5000);
if (ret == 0)
{
//write(fd, &key_val,1);
printf("time out\n");
}
{

ret = read(fd, &key_val, 1);
//write(fd, &key_val,1);
printf("key_val: 0x%x, ret = %d\n", key_val, ret);
//sleep(5);
}
}

return 0;
}



0 0
原创粉丝点击