您的位置 首页 电路

经过IO端口读取外部数据,带中止

基于FL2440开发板,内核版本2.6.28主机平台:Ubuntu11.04内核版本2.6.39驱动代码#includelinux/kernel.h>#includelinux/init.h&g

根据FL2440开发板,内核版别2.6.28

主机渠道:Ubuntu 11.04 内核版别2.6.39

驱动代码

  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #includeinterrupt.h>/*设置中止方法*/
  7. #include
  8. #include
  9. #include
  10. #include
  11. #include
  12. //设备名
  13. #defineIO_DEVICE_NAME”my_io”
  14. //主设备号
  15. #defineIO_DEVICE_MAJOR240
  16. //次设备号
  17. #defineIO_DEVICE_SECONDARY32
  18. //回来一个数x的第y位
  19. #defineMYBIT(x,y)((x>>y)%2)
  20. #ifndef_LINUX_IRQRETURN_H
  21. #define_LINUX_IRQRETURN_H
  22. typedefintirqreturn_t;
  23. #defineIRQ_NONE(0)
  24. #defineIRQ_HANDLED(1)
  25. #defineIRQ_RETVAL(x)((x)!=0)
  26. #endif
  27. /*
  28. *S3C2410GPIOedgedetectionforIRQs:
  29. *IRQsaregeneratedonFalling-Edge,Rising-Edge,both,lowlevelorhigglevel.
  30. *Thismustbecalled*before*thecorrespondingIRQisregistered.
  31. */
  32. #defineEXT_LOWLEVEL0
  33. #defineEXT_HIGHLEVEL1
  34. #defineEXT_FALLING_EDGE2
  35. #defineEXT_RISING_EDGE4
  36. #defineEXT_BOTH_EDGES6
  37. staticintflag_0,flag_2;//中止转化标志
  38. staticintcnt;
  39. intdata;
  40. DECLARE_WAIT_QUEUE_HEAD(io_wait);//声明等候行列
  41. voidio_con_set();
  42. staticirqreturn_tio_interrupt_0(intirq,void*dev_id,structpt_regs*regs)
  43. {
  44. if(flag_0==0)
  45. {
  46. printk(“**********theinterrupt0works**********\n”);
  47. cnt=(cnt+1)%2;
  48. flag_0=1;
  49. if(cnt==0)
  50. {
  51. printk(“IN\n”);
  52. data=1;
  53. s3c2410_gpio_setpin(S3C2410_GPB5,0);
  54. s3c2410_gpio_setpin(S3C2410_GPB6,1);
  55. }
  56. wake_up_interruptible(&io_wait);
  57. }
  58. returnIRQ_HANDLED;
  59. }
  60. staticirqreturn_tio_interrupt_2(intirq,void*dev_id,structpt_regs*regs)
  61. {
  62. if(flag_2==0)
  63. {
  64. printk(“**********theinterrupt2works**********\n”);
  65. cnt=(cnt+1)%2;
  66. flag_2=1;
  67. if(cnt==0)
  68. {
  69. printk(“OUT\n”);
  70. data=0;
  71. s3c2410_gpio_setpin(S3C2410_GPB5,1);
  72. s3c2410_gpio_setpin(S3C2410_GPB6,0);
  73. }
  74. wake_up_interruptible(&io_wait);
  75. }
  76. returnIRQ_HANDLED;
  77. }
  78. staticintio_open(structinode*inode,structfile*file)//翻开设备函数
  79. {
  80. intret;
  81. set_irq_type(IRQ_EINT0,EXT_FALLING_EDGE);//设置中止0触发方法
  82. set_irq_type(IRQ_EINT2,EXT_FALLING_EDGE);//设置中止2触发方法
  83. //EXT_LOWLEVEL
  84. //EXT_HIGHLEVEL
  85. //EXT_FALLING_EDGE
  86. //EXT_RISING_EDGE
  87. //EXT_BOTH_EDGES
  88. disable_irq(IRQ_EINT0);
  89. disable_irq(IRQ_EINT2);
  90. enable_irq(IRQ_EINT0);
  91. enable_irq(IRQ_EINT2);
  92. ret=request_irq(IRQ_EINT0,io_interrupt_0,IRQF_SHARED,IO_DEVICE_NAME,1);//注册中止0
  93. if(ret<0)
  94. {
  95. printk(“IRQ%dcannotrequest\n”,IRQ_EINT0);
  96. returnret;
  97. }
  98. ret=request_irq(IRQ_EINT2,io_interrupt_2,IRQF_SHARED,IO_DEVICE_NAME,1);//注册中止2
  99. if(ret<0)
  100. {
  101. printk(“IRQ%dcannotrequest\n”,IRQ_EINT2);
  102. returnret;
  103. }
  104. printk(“thedeviceisopened\n”);
  105. io_con_set();
  106. cnt=0;
  107. return0;
  108. }
  109. voidio_con_set()//IO端口操控寄存器初始化
  110. {
  111. s3c2410_gpio_cfgpin(S3C2410_GPF0,S3C2410_GPF0_EINT0);
  112. s3c2410_gpio_cfgpin(S3C2410_GPF2,S3C2410_GPF2_EINT2);
  113. s3c2410_gpio_cfgpin(S3C2410_GPB5,S3C2410_GPB5_OUTP);
  114. s3c2410_gpio_cfgpin(S3C2410_GPB6,S3C2410_GPB6_OUTP);
  115. }
  116. staticintio_close(structinode*inode,structfile*file)//设备封闭函数
  117. {
  118. free_irq(IRQ_EINT0,1);//开释中止
  119. free_irq(IRQ_EINT2,1);//开释中止
  120. printk(“thedeviceisclosed\n”);
  121. return0;
  122. }
  123. staticssize_tio_read(structfile*filp,char*buff,size_tcount,loff_t*f_ops)//读取IO端口
  124. {
  125. wait_event_interruptible(io_wait,flag_0&flag_2);
  126. flag_0=0;
  127. flag_2=0;
  128. copy_to_user(buff,(char*)&data,sizeof(data));
  129. }
  130. staticstructfile_operationsio_device_fops=
  131. {
  132. .owner=THIS_MODULE,
  133. .read=io_read,
  134. .open=io_open,
  135. .release=io_close,
  136. };
  137. staticint__initio_init(void)//insmod加载驱动时碑文
  138. {
  139. intret;
  140. ret=register_chrdev(IO_DEVICE_MAJOR,IO_DEVICE_NAME,&io_device_fops);
  141. if(ret<0)
  142. {
  143. printk(“Failtoregistthedevice\n”);
  144. returnret;
  145. }
  146. return0;
  147. }
  148. staticint__exitio_exit(void)//rmmod卸载驱动时碑文
  149. {
  150. unregister_chrdev(IO_DEVICE_MAJOR,IO_DEVICE_NAME);
  151. printk(“thedevicehasbeenunregisted\n”);
  152. }
  153. module_init(io_init);
  154. module_exit(io_exit);
  155. MODULE_LICENSE(“GPL”);

Makefile

obj-m := my_io.o

KERNELDIR ?= /arm/linux-2.6.28.7-2440

PWD := $(shell pwd)

default:

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:

rm -f *.o *.ko *.order *.symvers

调用代码:

  1. #include
  2. #include
  3. #include
  4. #defineMY_DEVICE”/dev/my_io”
  5. intmain()
  6. {
  7. intret;
  8. intdata;
  9. inti;
  10. ret=open(MY_DEVICE,0);
  11. printf(“ret=%d\n”,ret);
  12. for(i=0;i<20;i++)
  13. {
  14. read(ret,&data,sizeof(data));
  15. printf(“thedatais%d\n”,data);
  16. }
  17. close(ret);
  18. printf(“thedeviceisclosed\n”);
  19. return0;
  20. }


声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/dianlu/264219.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部