免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2494 | 回复: 7
打印 上一主题 下一主题

IO端口是否要配置 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-04 16:28 |只看该作者 |倒序浏览
在写开发板的按键驱动时,我并没有对按键的端口进行设置,看datasheet上,默认配置是那些端口工作在输入方式,但我使用中断方式的驱动还是可以正常工作,这是怎么回事呢?

论坛徽章:
0
2 [报告]
发表于 2010-08-05 07:17 |只看该作者
哦,我现在也迷惑在按键驱动中,可否看一下你的代码?

论坛徽章:
0
3 [报告]
发表于 2010-08-05 08:39 |只看该作者
本帖最后由 heyangya 于 2010-08-05 14:54 编辑
哦,我现在也迷惑在按键驱动中,可否看一下你的代码?
linuxman-phil 发表于 2010-08-05 07:17

  1. /*
  2. S2、S3、S4、S5 四个按键用到以下四个I/O口:EINT0/GPF0 EINT2/GPF2 EINT3/GPF3 EINT4/GPF4 当键被按下去时,会产生相应的中断


  3. */


  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <linux/fs.h>
  9. #include <linux/kdev_t.h>
  10. #include <linux/cdev.h>
  11. #include <mach/irqs.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/timer.h>
  14. #include <asm/atomic.h>

  15. #define DEV_NAME "my_button_driver"
  16. #define DEV_COUNT 4        //按键数目

  17. unsigned int dev_major=230;

  18. //设备结构体
  19. struct my_dev_struct
  20. {
  21.         unsigned int id;//设备ID
  22.         int irq_num;//中断号
  23.         char name[10];//设备名
  24. };

  25. struct my_dev_struct mydevs[]=
  26. {
  27.         {0,IRQ_EINT0,"KEY_0"},
  28.         {1,IRQ_EINT2,"KEY_1"},
  29.         {2,IRQ_EINT3,"KEY_2"},
  30.         {3,IRQ_EINT4,"KEY_3"},
  31. };

  32. struct timer_list key_timers[DEV_COUNT];//定时器数组
  33. atomic_t a_key_press=ATOMIC_INIT(0);//是否被按下

  34. void timer_handler(unsigned long arg)
  35. {
  36.         struct my_dev_struct * devp=(struct my_dev_struct *)arg;
  37.         printk("%s is pressed!\n",devp->name);
  38.         atomic_dec(&a_key_press);
  39. }
  40. irqreturn_t interrupt_hander(int irq, void *dev_id)
  41. {
  42.         atomic_inc(&a_key_press);
  43.         if(atomic_dec_and_test(&a_key_press))
  44.         {
  45.                 struct my_dev_struct * devp=(struct my_dev_struct *)dev_id;
  46.                 int num=devp->id;
  47.                 atomic_inc(&a_key_press);
  48.                 init_timer(&key_timers[num]);
  49.                 key_timers[num].function=timer_handler;
  50.                 key_timers[num].expires=jiffies+HZ/10;
  51.                 key_timers[num].data=(unsigned long)devp;
  52.                 add_timer(&key_timers[num]);
  53.         }
  54.         return IRQ_RETVAL(IRQ_HANDLED);
  55. }

  56. int dev_open(struct inode *inode,struct file *filp)
  57. {
  58.         int i,retval;
  59.         for(i=0;i<DEV_COUNT;i++)
  60.         {
  61.                 //请求中断
  62.                 retval=request_irq(mydevs[i].irq_num,interrupt_hander,IRQF_TRIGGER_LOW|IRQF_DISABLED,mydevs[i].name,(void*)&mydevs[i]);
  63.                 if(retval)
  64.                         goto error;
  65.         }
  66.         return 0;
  67. error:
  68.         i--;
  69.         while(i>=0)
  70.                 free_irq(mydevs[i].irq_num,(void*)&mydevs[i--]);
  71.         return retval;
  72. }

  73. //设备关闭后,释放中断
  74. int dev_release(struct inode *inode,struct file *filp)
  75. {
  76.         int i;
  77.         for(i=0;i<DEV_COUNT;i++)
  78.         {
  79.                 free_irq(mydevs[i].irq_num,(void*)&mydevs[i]);
  80.         }
  81.         return 0;
  82. }



  83. //文件操作结构体
  84. static struct file_operations fops=
  85. {
  86.         .owner        =        THIS_MODULE,
  87.         .open        =        dev_open,
  88.         .release        =        dev_release,
  89. };

  90. //注册设备
  91. static int __init my_button_init(void)
  92. {
  93.         int err;
  94.         err=register_chrdev(dev_major,DEV_NAME,&fops);
  95.         if(err)
  96.         {
  97.                 printk("register_chrdev error!\n");
  98.                 goto error;
  99.         }
  100.         return 0;
  101. error:
  102.         return -EBUSY;
  103. }

  104. //注销字符设备
  105. static void __exit my_button_exit(void)
  106. {
  107.         unregister_chrdev(dev_major,DEV_NAME);
  108. }

  109. module_init(my_button_init);
  110. module_exit(my_button_exit);

  111. MODULE_LICENSE("GPL");
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-08-05 14:45 |只看该作者
在你的中断申请函数里有配置选项:IRQF_TRIGGER_LOW|IRQF_DISABLED,应该是配置成低电平触发,快速中断模式。

论坛徽章:
0
5 [报告]
发表于 2010-08-05 15:13 |只看该作者
应该是这样,谢谢

论坛徽章:
0
6 [报告]
发表于 2010-08-08 13:50 |只看该作者
kan kan

论坛徽章:
0
7 [报告]
发表于 2010-08-09 16:18 |只看该作者
IO响应中断需要设置成输入方式,正好是你的默认配置

论坛徽章:
0
8 [报告]
发表于 2010-08-10 00:39 |只看该作者
LZ做的嵌入式的把? 这个 和 X86区别还是有点大的. 包括你那个 255个设备号的问题,也许都有关系.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP