新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 通過IO端口讀取外部數(shù)據(jù),帶中斷

通過IO端口讀取外部數(shù)據(jù),帶中斷

作者: 時(shí)間:2016-11-20 來源:網(wǎng)絡(luò) 收藏
基于FL2440開發(fā)板,內(nèi)核版本2.6.28

主機(jī)平臺(tái):Ubuntu 11.04 內(nèi)核版本2.6.39

本文引用地址:http://m.butianyuan.cn/article/201611/318855.htm

驅(qū)動(dòng)代碼


  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #includeinterrupt.h>/*設(shè)置中斷方式*/
  7. #include
  8. #include
  9. #include
  10. #include
  11. #include
  12. //設(shè)備名
  13. #defineIO_DEVICE_NAME"my_io"
  14. //主設(shè)備號(hào)
  15. #defineIO_DEVICE_MAJOR240
  16. //次設(shè)備號(hào)
  17. #defineIO_DEVICE_SECONDARY32
  18. //返回一個(gè)數(shù)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;//中斷轉(zhuǎn)換標(biāo)志
  38. staticintcnt;
  39. intdata;
  40. DECLARE_WAIT_QUEUE_HEAD(io_wait);//聲明等待隊(duì)列
  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("INn");
  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("OUTn");
  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)//打開設(shè)備函數(shù)
  79. {
  80. intret;
  81. set_irq_type(IRQ_EINT0,EXT_FALLING_EDGE);//設(shè)置中斷0觸發(fā)方式
  82. set_irq_type(IRQ_EINT2,EXT_FALLING_EDGE);//設(shè)置中斷2觸發(fā)方式
  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);//注冊(cè)中斷0
  93. if(ret<0)
  94. {
  95. printk("IRQ%dcannotrequestn",IRQ_EINT0);
  96. returnret;
  97. }
  98. ret=request_irq(IRQ_EINT2,io_interrupt_2,IRQF_SHARED,IO_DEVICE_NAME,1);//注冊(cè)中斷2
  99. if(ret<0)
  100. {
  101. printk("IRQ%dcannotrequestn",IRQ_EINT2);
  102. returnret;
  103. }
  104. printk("thedeviceisopenedn");
  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)//設(shè)備關(guān)閉函數(shù)
  117. {
  118. free_irq(IRQ_EINT0,1);//釋放中斷
  119. free_irq(IRQ_EINT2,1);//釋放中斷
  120. printk("thedeviceisclosedn");
  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加載驅(qū)動(dòng)時(shí)執(zhí)行
  138. {
  139. intret;
  140. ret=register_chrdev(IO_DEVICE_MAJOR,IO_DEVICE_NAME,&io_device_fops);
  141. if(ret<0)
  142. {
  143. printk("Failtoregistthedevicen");
  144. returnret;
  145. }
  146. return0;
  147. }
  148. staticint__exitio_exit(void)//rmmod卸載驅(qū)動(dòng)時(shí)執(zhí)行
  149. {
  150. unregister_chrdev(IO_DEVICE_MAJOR,IO_DEVICE_NAME);
  151. printk("thedevicehasbeenunregistedn");
  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

調(diào)用代碼:

  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=%dn",ret);
  12. for(i=0;i<20;i++)
  13. {
  14. read(ret,&data,sizeof(data));
  15. printf("thedatais%dn",data);
  16. }
  17. close(ret);
  18. printf("thedeviceisclosedn");
  19. return0;
  20. }



評(píng)論


技術(shù)專區(qū)

關(guān)閉