当前位置:   article > 正文

zynq linux驱动之PL-PS中断_zynq pl-ps中断驱动

zynq pl-ps中断驱动

PC:Windows 10

虚拟机:ubuntu 16.04

vivado:2017.04

PetaLinux:2017.04

开发板:黑金AX7010

根文件系统:debian8

-------------------------------------------------- --------------------传说中的分割线------------------------- -------------------------------------------------- ----

 将  zynq linux驱动之传统开发  里的vivado工程另存为interrupt7010

接下来配置一下中断

这里会出现中断的接口

接下来添加一个引脚

连起来

重新生成一下顶层文件

打开顶层文件,加一个非门(因为PS这边貌似只支持上升沿中断和高电平中断)

在约束文件里面添加触发IRQ的引脚(这里用的是KEY4,HDMI座子旁边的那个按键)

执行生成位文件

结束之后将原来的SDK目录删掉

重新导入硬件和位文件之后打开SDK

将该文件夹拷贝到Ubuntu的里

使用的PetaLinux编译fsbl,u-boot的,内核,设备树文件:

过程略.....

用的PetaLinux制作BOOT.BIN文件,将BOOT.BIN,image.ub,system.dtb文件拷贝到SD卡的fat分区里(这里没有用的PetaLinux生成的根文件系统,用的是debian8)

接下来打开datasheet

找到中断号是61

然后先到开板上看一下

cat /proc/interrupts

再看看datasheet上

没有问题,中断号都对得上

然后再回到ubuntu里看看设备树

vim components/plnx_workspace/device-tree/device-tree-generation/zynq-7000.dtsi

发现这里的中断号跟datasheet 和 /proc/interrupt 里面的对不上

仔细观察发现设备树里面的中断号是datasheet的中断号减去32得到的值

接下来仿照这个写一个自己的

vim project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi
  1. /include/ "system-conf.dtsi"
  2. / {
  3. amba_pl: amba_pl{
  4. #address-cells = <1>;
  5. #size-cells = <1>;
  6. compatible = "simple-bus";
  7. ranges;
  8. irq: irq@0{
  9. compatible = "hello,irq";
  10. interrupt-parent = <&intc>;
  11. interrupts = <0 29 2>;
  12. };
  13. };
  14. };

执行的petaLinux-build编译

编译完成之后

到开发板上,挂接NFS文件系统,然后把编译好的设备树文件(system.dts)拷贝到开发板SD卡的fat分区里,重启开发板

ls /proc/device-tree/amba_pl/

可以看到设备树节点增加了IRQ @ 0

编写驱动:

  1. #include <linux/module.h>
  2. #include <linux/platform_device.h>
  3. #include <linux/types.h>
  4. #include <linux/err.h>
  5. #include <linux/io.h>
  6. #include <linux/device.h>
  7. #include <linux/cdev.h>
  8. #include <linux/of.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/pm.h>
  12. #include <linux/fs.h>
  13. #include <linux/slab.h>
  14. #include <linux/gfp.h>
  15. #include <linux/mm.h>
  16. #include <linux/dma-buf.h>
  17. #include <linux/string.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/dmaengine.h>
  20. #include <linux/completion.h>
  21. #include <linux/wait.h>
  22. #include <linux/init.h>
  23. #include <linux/sched.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/errno.h> /* error codes */
  26. #include <linux/clk.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/ioport.h>
  32. #include <linux/notifier.h>
  33. #include <linux/init.h>
  34. #include <linux/pci.h>
  35. #include <linux/time.h>
  36. #include <linux/timer.h>
  37. //
  38. static char devname[16];
  39. static int major;
  40. static int mijor;
  41. static struct class* cls;
  42. static void __iomem* base_address;
  43. static resource_size_t remap_size;
  44. static int irq;
  45. static struct device* dev;
  46. //
  47. #define DEVICE_NAME "irq_drv"
  48. static volatile int irq_is_open = 0;
  49. static struct fasync_struct *irq_async;
  50. static int irq_drv_open(struct inode *Inode, struct file *File)
  51. {
  52. irq_is_open = 1;
  53. return 0;
  54. }
  55. int irq_drv_release (struct inode *inode, struct file *file)
  56. {
  57. irq_is_open = 0;
  58. return 0;
  59. }
  60. static ssize_t irq_drv_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  61. {
  62. return 0;
  63. }
  64. static ssize_t irq_drv_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  65. {
  66. return 0;
  67. }
  68. static int irq_drv_fasync (int fd, struct file *filp, int on)
  69. {
  70. return fasync_helper (fd, filp, on, &irq_async);
  71. }
  72. static struct file_operations irq_fops = {
  73. .owner = THIS_MODULE,
  74. .open = irq_drv_open,
  75. .read = irq_drv_read,
  76. .write = irq_drv_write,
  77. .fasync = irq_drv_fasync,
  78. .release = irq_drv_release,
  79. };
  80. static irqreturn_t irq_interrupt(int irq, void *dev_id)
  81. {
  82. printk("irq = %d\n", irq);
  83. if(irq_is_open)
  84. {
  85. kill_fasync (&irq_async, SIGIO, POLL_IN);
  86. }
  87. return IRQ_HANDLED;
  88. }
  89. static int irq_probe(struct platform_device *pdev)
  90. {
  91. int err;
  92. struct device *tmp_dev;
  93. memset(devname,0,16);
  94. strcpy(devname, DEVICE_NAME);
  95. major = register_chrdev(0, devname, &irq_fops);
  96. cls = class_create(THIS_MODULE, devname);
  97. mijor = 1;
  98. tmp_dev = device_create(cls, &pdev->dev, MKDEV(major, mijor), NULL, devname);
  99. if (IS_ERR(tmp_dev)) {
  100. class_destroy(cls);
  101. unregister_chrdev(major, devname);
  102. return 0;
  103. }
  104. irq = platform_get_irq(pdev,0);
  105. if (irq <= 0)
  106. return -ENXIO;
  107. dev = &pdev->dev;
  108. err = request_threaded_irq(irq, NULL,
  109. irq_interrupt,
  110. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  111. devname, NULL);
  112. if (err) {
  113. printk(KERN_ALERT "irq_probe irq error=%d\n", err);
  114. goto fail;
  115. }
  116. else
  117. {
  118. printk("irq = %d\n", irq);
  119. printk("devname = %s\n", devname);
  120. }
  121. //保存dev
  122. //platform_set_drvdata(pdev, &xxx);
  123. return 0;
  124. fail:
  125. free_irq(irq, NULL);
  126. device_destroy(cls, MKDEV(major, mijor));
  127. class_destroy(cls);
  128. unregister_chrdev(major, devname);
  129. return -ENOMEM;
  130. }
  131. static int irq_remove(struct platform_device *pdev)
  132. {
  133. device_destroy(cls, MKDEV(major, mijor));
  134. class_destroy(cls);
  135. unregister_chrdev(major, devname);
  136. free_irq(irq, NULL);
  137. printk("irq = %d\n", irq);
  138. return 0;
  139. }
  140. static int irq_suspend(struct device *dev)
  141. {
  142. return 0;
  143. }
  144. static int irq_resume(struct device *dev)
  145. {
  146. return 0;
  147. }
  148. static const struct dev_pm_ops irq_pm_ops = {
  149. .suspend = irq_suspend,
  150. .resume = irq_resume,
  151. };
  152. //MODULE_DEVICE_TABLE(platform, irq_driver_ids);
  153. static const struct of_device_id irq_of_match[] = {
  154. {.compatible = "hello,irq" },
  155. { }
  156. };
  157. MODULE_DEVICE_TABLE(of, irq_of_match);
  158. static struct platform_driver irq_driver = {
  159. .probe = irq_probe,
  160. .remove = irq_remove,
  161. .driver = {
  162. .owner = THIS_MODULE,
  163. .name = "irq@0",
  164. .pm = &irq_pm_ops,
  165. .of_match_table = irq_of_match,
  166. },
  167. };
  168. module_platform_driver(irq_driver);
  169. MODULE_LICENSE("GPL v2");

Makefile文件:

  1. export ARCH=arm
  2. KERN_DIR = /home/zynq/work/kernel/linux-4.9
  3. all:
  4. make -C $(KERN_DIR) M=`pwd` modules
  5. clean:
  6. make -C $(KERN_DIR) M=`pwd` modules clean
  7. rm -rf modules.order
  8. obj-m += irq_drv.o

编译驱动

开发板挂接NFS文件系统,加载驱动

然后按一下按键,内核就能打印出IRQ

编写测试程序:

  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. int fd;
  11. void my_signal_fun(int signum)
  12. {
  13. printf("irq app printf!\n");
  14. }
  15. int main(int argc, char **argv)
  16. {
  17. unsigned char key_val;
  18. int ret;
  19. int Oflags;
  20. signal(SIGIO, my_signal_fun);
  21. fd = open("/dev/irq_drv", O_RDWR);
  22. if (fd < 0)
  23. {
  24. printf("can't open!\n");
  25. }
  26. fcntl(fd, F_SETOWN, getpid());
  27. Oflags = fcntl(fd, F_GETFL);
  28. fcntl(fd, F_SETFL, Oflags | FASYNC);
  29. while (1)
  30. {
  31. sleep(1000);
  32. }
  33. return 0;
  34. }

编译

arm-linux-gnueabihf-gcc -o irq irq.c

回到开发板运行测试程序

按下按键之后能够看到内核和测试程序的打印信息

 

 

 

上一篇:zynq linux驱动之使用设备树开发

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/892904
推荐阅读
相关标签
  

闽ICP备14008679号