PC:Windows 10
虚拟机:ubuntu 16.04
vivado:2017.04
PetaLinux:2017.04
开发板:黑金AX7010
根文件系统:debian8
-------------------------------------------------- --------------------传说中的分割线------------------------- -------------------------------------------------- ------
在 zynq linux驱动之传统开发 基础上,实现用设备树开发驱动:
首先回到的的PetaLinux的工程目录下:
find -name "system-top.dts"
cd ./components/plnx_workspace/device-tree/device-tree-generation/
进入该目录
ls
首先打开系统top.dts看一下
发现包涵了一个系统user.dtsi
回到工程根目录,然后查找一下系统user.dtsi
find -name "system-user.dtsi"
直接打开./project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi编辑
/include/ "system-conf.dtsi" compatible = "simple-bus"; compatible = "hello,leds";
使用的的PetaLinux编译一下工程
petalinux-build
编译完成后回到开发板
挂接NFS文件系统
把生成的system.dts文件拷贝到SD的fat分区里
过程略....
重启开发板
接下来编写驱动:
#include <linux/module.h> #include <linux/platform_device.h> #include <linux/device.h> #include <linux/dma-mapping.h> #include <linux/dma-buf.h> #include <linux/string.h> #include <linux/uaccess.h> #include <linux/dmaengine.h> #include <linux/completion.h> #include <linux/pagemap.h> #include <linux/errno.h> /* error codes */ #include <linux/interrupt.h> #include <linux/vmalloc.h> #include <linux/moduleparam.h> #include <linux/miscdevice.h> #include <linux/ioport.h> #include <linux/notifier.h> #define DEVICE_NAME "leds_drv" static struct class* cls; static void __iomem* base_address; static resource_size_t remap_size; static struct device* dev; volatile unsigned int ODR; static int leds_drv_open(struct inode *Inode, struct file *File) static ssize_t leds_drv_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) static ssize_t leds_drv_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) ret = copy_from_user(&tmp_val, buf, count); leds->ODR = (~tmp_val) & 0xf; static struct file_operations dev_fops = static int leds_drv_probe(struct platform_device *pdev) strcpy(devname, DEVICE_NAME); major = register_chrdev(0, devname, &dev_fops); cls = class_create(THIS_MODULE, devname); tmp_dev = device_create(cls, &pdev->dev, MKDEV(major, mijor), NULL, devname); unregister_chrdev(major, devname); res = platform_get_resource(pdev, IORESOURCE_MEM, 0); printk(KERN_ALERT "leds_drv_probe res error!\n"); base_address = devm_ioremap_resource(&pdev->dev, res); if (IS_ERR(base_address)) return PTR_ERR(base_address); printk("base_address = 0x%08x\n", (unsigned int)base_address); remap_size = resource_size(res); printk("remap_size = 0x%08x\n", remap_size); //platform_set_drvdata(pdev, &xxx); leds = (LEDS_T*)base_address; static int leds_drv_remove(struct platform_device *pdev) device_destroy(cls, MKDEV(major, mijor)); unregister_chrdev(major, devname); devm_iounmap(&pdev->dev, base_address); static int leds_drv_suspend(struct device *dev) static int leds_drv_resume(struct device *dev) static const struct dev_pm_ops leds_drv_pm_ops = { .suspend = leds_drv_suspend, .resume = leds_drv_resume, static const struct of_device_id leds_drv_of_match[] = { {.compatible = "hello,leds" }, MODULE_DEVICE_TABLE(of, leds_drv_of_match); static struct platform_driver leds_drv_driver = { .remove = leds_drv_remove, .of_match_table = leds_drv_of_match, module_platform_driver(leds_drv_driver); MODULE_LICENSE("GPL v2");
Makefile文件:
KERN_DIR = /home/zynq/work/kernel/linux-4.9 make -C $(KERN_DIR) M=`pwd` modules make -C $(KERN_DIR) M=`pwd` modules clean
执行一下
make
换到开发板,加载驱动:
编写测试程序:
int main(int argc, char** argv) fd = open("/dev/leds_drv", O_RDWR); printf("fd = %d open fialed!\n", fd);
编译测试程序:
arm-linux-gnueabihf-gcc -o leds leds.c
最后回到开发板
执行./leds
又一次看到4个led做加法了
上一篇:zynq linux驱动之传统开发
下一篇:zynq linux驱动之PL-PS中断
|