Chinaunix首页 | 论坛 | 博客
  • 博客访问: 808808
  • 博文数量: 281
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2770
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 19:45
个人简介

邮箱:zhuimengcanyang@163.com 痴爱嵌入式技术的蜗牛

文章分类
文章存档

2020年(1)

2018年(1)

2017年(56)

2016年(72)

2015年(151)

分类: 嵌入式

2015-08-23 22:14:21

目的

上一节里,实现同一时刻只能有一个进程使用同一个设备,例如:只能有一个进程,在同一时刻里使用/dev/buttons这个设备。

在写单片机的按键程序时,也必将会涉及一点,就是按键去抖动。
按键去抖动的方法无非有二种,一种是硬件电路去抖动,这种在要求不是特别高的情况下是不会被采用的;
另一种就是延时去抖动了。而延时又一般分为二种,一种是for循环死等待,一种是定时延时。这一节里我们来使用内核的定时器去抖动。


问:linux内核定时器有哪些要素?

答:有两个要素:

一、超时时间

二、处理函数

问:linux定时器结构是怎样的?
答:

点击(此处)折叠或打开

  1. struct timer_list {
  2.     struct list_head entry;
  3.     unsigned long expires;

  4.     void (*function)(unsigned long);
  5.     unsigned long data;

  6.     struct tvec_t_base_s *base;
  7. #ifdef CONFIG_TIMER_STATS
  8.     void *start_site;
  9.     char start_comm[16];
  10.     int start_pid;
  11. #endif
  12. };


问:void (*function)(unsigned long data)里面的参数是谁传给它的?

答:
是timer_list.data传给它的,如果需要向function传递参数时,则应该设置timer_list.data,否则可以不设置。

问:与定时器相关的操作函数有哪些?

答:

一、使用init_timer函数初始化定时器

二、设置timer_list.function,并实现这个函数指针

三、使用add_timer函数向内核注册一个定时器

四、使用mod_timer修改定时器时间,并启动定时器

问:int mod_timer(struct timer_list *timer, unsigned long expires)的第二个参数为超时时间,怎么设置超时时间,如果定时为10ms?

答:一般的形式为:   jiffies + (HZ /100),HZ 表示100个jiffies,jiffies的单位为10ms,即HZ = 100*10ms = 1s

1. 驱动程序

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <linux/irq.h>
  7. #include <asm/uaccess.h>
  8. #include <asm/irq.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/regs-gpio.h>
  11. #include <asm/hardware.h>
  12. #include <linux/poll.h>


  13. static struct class *sixthdrv_class;
  14. static struct class_device    *sixthdrv_class_dev;

  15. volatile unsigned long *gpfcon;
  16. volatile unsigned long *gpfdat;

  17. volatile unsigned long *gpgcon;
  18. volatile unsigned long *gpgdat;

  19. static struct timer_list buttons_timer;


  20. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

  21. /* 中断事件标志, 中断服务程序将它置1,sixth_drv_read将它清0 */
  22. static volatile int ev_press = 0;

  23. static struct fasync_struct *button_async;


  24. struct pin_desc{
  25.     unsigned int pin;
  26.     unsigned int key_val;
  27. };


  28. /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
  29. /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
  30. static unsigned char key_val;

  31. struct pin_desc pins_desc[4] = {
  32.     {S3C2410_GPF0, 0x01},
  33.     {S3C2410_GPF2, 0x02},
  34.     {S3C2410_GPG3, 0x03},
  35.     {S3C2410_GPG11, 0x04},
  36. };

  37. static struct pin_desc *irq_pd;

  38. //static atomic_t canopen = ATOMIC_INIT(1); //定义原子变量并初始化为1

  39. static DECLARE_MUTEX(button_lock); //定义互斥锁


  40. /*
  41.   * 确定按键值
  42.   */
  43. static irqreturn_t buttons_irq(int irq, void *dev_id)
  44. {
  45.     /* 10ms后启动定时器 */
  46.     irq_pd = (struct pin_desc *)dev_id;
  47.     mod_timer(&buttons_timer, jiffies + HZ/100);  // 修改定时器中断时间。
  48.     return IRQ_RETVAL(IRQ_HANDLED);
  49. }


  50. static int sixth_drv_open(struct inode *inode, struct file *file)
  51. {
  52. #if 0    
  53.     if (!atomic_dec_and_test(&canopen))
  54.     {
  55.         atomic_inc(&canopen);
  56.         return -EBUSY;
  57.     }
  58. #endif        

  59.     if (file->f_flags & O_NONBLOCK)
  60.     {
  61.         if (down_trylock(&button_lock))
  62.             return -EBUSY;
  63.     }
  64.     else
  65.     {
  66.         /* 获取信号量 */
  67.         down(&button_lock);
  68.     }

  69.     /* 配置GPF0,2为输入引脚 */
  70.     /* 配置GPG3,11为输入引脚 */
  71.     request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
  72.     request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
  73.     request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
  74.     request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);    

  75.     return 0;
  76. }

  77. ssize_t sixth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  78. {
  79.     if (size != 1)
  80.         return -EINVAL;

  81.     if (file->f_flags & O_NONBLOCK)
  82.     {
  83.         if (!ev_press)
  84.             return -EAGAIN;
  85.     }
  86.     else
  87.     {
  88.         /* 如果没有按键动作, 休眠 */
  89.         wait_event_interruptible(button_waitq, ev_press);
  90.     }

  91.     /* 如果有按键动作, 返回键值 */
  92.     copy_to_user(buf, &key_val, 1);
  93.     ev_press = 0;
  94.     
  95.     return 1;
  96. }


  97. int sixth_drv_close(struct inode *inode, struct file *file)
  98. {
  99.     //atomic_inc(&canopen);
  100.     free_irq(IRQ_EINT0, &pins_desc[0]);
  101.     free_irq(IRQ_EINT2, &pins_desc[1]);
  102.     free_irq(IRQ_EINT11, &pins_desc[2]);
  103.     free_irq(IRQ_EINT19, &pins_desc[3]);
  104.     
  105.     up(&button_lock);
  106.     return 0;
  107. }

  108. static unsigned sixth_drv_poll(struct file *file, poll_table *wait)
  109. {
  110.     unsigned int mask = 0;
  111.     poll_wait(file, &button_waitq, wait); // 不会立即休眠

  112.     if (ev_press)
  113.         mask |= POLLIN | POLLRDNORM;

  114.     return mask;
  115. }

  116. static int sixth_drv_fasync (int fd, struct file *filp, int on)
  117. {
  118.     printk("driver: sixth_drv_fasync\n");
  119.     return fasync_helper (fd, filp, on, &button_async);
  120. }


  121. static struct file_operations sencod_drv_fops = {
  122.     .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
  123.     .open = sixth_drv_open,
  124.     .read     =    sixth_drv_read,    
  125.     .release = sixth_drv_close,
  126.     .poll = sixth_drv_poll,
  127.     .fasync     = sixth_drv_fasync,
  128. };


  129. int major;

  130. static void buttons_timer_function(unsigned long data)
  131. {
  132.     struct pin_desc * pindesc = irq_pd;
  133.     unsigned int pinval;

  134.     if (!pindesc)
  135.         return;
  136.     
  137.     pinval = s3c2410_gpio_getpin(pindesc->pin);

  138.     if (pinval)
  139.     {
  140.         /* 松开 */
  141.         key_val = 0x80 | pindesc->key_val;
  142.     }
  143.     else
  144.     {
  145.         /* 按下 */
  146.         key_val = pindesc->key_val;
  147.     }

  148.     ev_press = 1; /* 表示中断发生了 */
  149.     wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
  150.     
  151.     kill_fasync (&button_async, SIGIO, POLL_IN);
  152. }


  153. static int sixth_drv_init(void)
  154. {
  155.     // 初始化timer定时器
  156.     init_timer(&buttons_timer);                       //1. 初始化定时器
  157.     buttons_timer.function = buttons_timer_function;  //2. 定时器中断处理函数
  158.     //buttons_timer.expires = 0;                      //3. 超时时间
  159.     add_timer(&buttons_timer);                        //4. 添加定时器

  160.     major = register_chrdev(0, "sixth_drv", &sencod_drv_fops);
  161.     sixthdrv_class = class_create(THIS_MODULE, "sixth_drv");
  162.     sixthdrv_class_dev = class_device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

  163.     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  164.     gpfdat = gpfcon + 1;
  165.     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  166.     gpgdat = gpgcon + 1;

  167.     return 0;
  168. }

  169. static void sixth_drv_exit(void)
  170. {
  171.     unregister_chrdev(major, "sixth_drv");
  172.     class_device_unregister(sixthdrv_class_dev);
  173.     class_destroy(sixthdrv_class);
  174.     iounmap(gpfcon);
  175.     iounmap(gpgcon);
  176.     return 0;
  177. }


  178. module_init(sixth_drv_init);
  179. module_exit(sixth_drv_exit);
  180. MODULE_LICENSE("GPL");

2. 测试程序

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <poll.h>
  6. #include <signal.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>


  10. /* sixthdrvtest
  11.   */
  12. int fd;

  13. void my_signal_fun(int signum)
  14. {
  15.     unsigned char key_val;
  16.     read(fd, &key_val, 1);
  17.     printf("key_val: 0x%x\n", key_val);
  18. }

  19. int main(int argc, char **argv)
  20. {
  21.     unsigned char key_val;
  22.     int ret;
  23.     int Oflags;

  24.     //signal(SIGIO, my_signal_fun);
  25.     
  26.     fd = open("/dev/buttons", O_RDWR);
  27.     if (fd < 0)
  28.     {
  29.         printf("can't open!\n");
  30.         return -1;
  31.     }

  32.     //fcntl(fd, F_SETOWN, getpid());
  33.     //Oflags = fcntl(fd, F_GETFL)
  34.     //fcntl(fd, F_SETFL, Oflags | FASYNC);


  35.     while (1)
  36.     {
  37.         ret = read(fd, &key_val, 1);
  38.         printf("key_val: 0x%x, ret = %d\n", key_val, ret);
  39.         //sleep(5);
  40.     }
  41.     
  42.     return 0;
  43. }

3. 测试


4. 小结

定时器如何使用:【linux定时器使用

一、使用init_timer函数初始化定时器

二、设置timer_list.function,并实现这个函数指针

三、使用add_timer函数向内核注册一个定时器

四、使用mod_timer修改定时器时间,并启动定时器


阅读(628) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~