用户工具

站点工具


fpga:petalinux

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
fpga:petalinux [2023/02/09 10:20] – [9. zynq设备上运行指定驱动的app程序] zhangguofpga:petalinux [2026/10/13 22:51] (当前版本) – 外部编辑 127.0.0.1
行 1: 行 1:
-====== petalinux ======+# petalinux 
 +## - create project
  
- +```bash
-===== - create project ===== +
- +
- +
-<code bash>+
 source /xilinx/settings.sh source /xilinx/settings.sh
  
 petalinux-create -t project --template zynq -n xxx_name petalinux-create -t project --template zynq -n xxx_name
 petalinux-config --get-hw-description . petalinux-config --get-hw-description .
 +```
  
-</code> 
  
  
-===== - create modules =====+## - create modules
 相当于创建linux模块驱动 相当于创建linux模块驱动
  
-<code bash>+ 
 +```bash
 petalinux-create -t modules -n xxx_module --enable petalinux-create -t modules -n xxx_module --enable
-</code>+```
  
  
-===== - create app =====+ 
 +## - create app
 创建一个linux平台下的app可执行程序 创建一个linux平台下的app可执行程序
  
-<code bash>+ 
 +```bash
 petalinux-create -t apps -n xxx_app  --enable petalinux-create -t apps -n xxx_app  --enable
-</code>+``` 
  
 然后进入recipes-apps/xxx_app目录,修改files/xxx_app.c文件,默认是一个hello world!打印程序。 然后进入recipes-apps/xxx_app目录,修改files/xxx_app.c文件,默认是一个hello world!打印程序。
行 33: 行 34:
  
  
-===== - build ===== +## - build 
-<code bash>+ 
 +```bash
 petalinux-build -c rootfs petalinux-build -c rootfs
 petalinux-build -c xxx_module petalinux-build -c xxx_module
行 42: 行 44:
  
 petalinux-build petalinux-build
-</code>+```
  
-===== - app单独编译&调试 ===== 
  
-<code bash>+## - app单独编译&调试 
 + 
 +```bash
 petalinux-build -c xxx_app -x do_compile   # 对指定app进行编译, 编译后生成的app可执行文件在以下位置 petalinux-build -c xxx_app -x do_compile   # 对指定app进行编译, 编译后生成的app可执行文件在以下位置
 ls build/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/myapp3/1.0-r0/myapp3 ls build/tmp/work/cortexa9hf-neon-xilinx-linux-gnueabi/myapp3/1.0-r0/myapp3
行 52: 行 55:
 # 然后可以将此文件通过scp的方式,copy到zynq设备,修改权限chmod 777 ./myapp3, 执行程序./myapp3 # 然后可以将此文件通过scp的方式,copy到zynq设备,修改权限chmod 777 ./myapp3, 执行程序./myapp3
 # 这样的方式不用每次app修改后都烧写到flash, 通过scp的方式加快app开发调试速度。 # 这样的方式不用每次app修改后都烧写到flash, 通过scp的方式加快app开发调试速度。
 +```
  
-</code> +## - package to BOOT.BIN
-===== - package to BOOT.BIN =====+
  
-<code bash>+```bash
 petalinux-package --boot --fsbl zynq_fsbl.elf --u-boot --kernel --fpga system.bit --force petalinux-package --boot --fsbl zynq_fsbl.elf --u-boot --kernel --fpga system.bit --force
 # --boot, 打包成BOOT.BIN文件 # --boot, 打包成BOOT.BIN文件
行 64: 行 67:
 # --fpga, 指定FPGA bits文件。 # --fpga, 指定FPGA bits文件。
 #一般是建议先不用--kernel选项生成BOOT.BIN文件,然后将BOOT.BIN文件和image.ub一起copy到SD卡,从SD卡启动看看程序效果,程序稳定后可以再考虑弄成为flash启动,这样调试速度会快一些。 #一般是建议先不用--kernel选项生成BOOT.BIN文件,然后将BOOT.BIN文件和image.ub一起copy到SD卡,从SD卡启动看看程序效果,程序稳定后可以再考虑弄成为flash启动,这样调试速度会快一些。
-</code>+```
  
-===== - 一个简单的驱动开发例程——GPIO流水灯(Petalinux部分) ===== 
-https://blog.csdn.net/u013029731/article/details/85042431/ 
  
 +## - 一个简单的驱动开发例程——GPIO流水灯(Petalinux部分)
 +https://blog.csdn.net/u013029731/article/details/85042431/
  
  
  
-===== - 实现app开机启动 ===== 
  
 +## - 实现app开机启动
 参考ug1144, Ch.7: Customizing the Rootfs 参考ug1144, Ch.7: Customizing the Rootfs
  
 参考: https://blog.csdn.net/qq_37775990/article/details/126951572 参考: https://blog.csdn.net/qq_37775990/article/details/126951572
  
-<code bash>+ 
 +```bash
  
  
行 133: 行 137:
  
 完成后进行petalinux-build,使用新生成的镜像,下次就可以开机自启动了。 完成后进行petalinux-build,使用新生成的镜像,下次就可以开机自启动了。
 +```
  
-</code> 
  
  
 开机启动执行log example: 开机启动执行log example:
  
-<code bash>+ 
 +```bash
 Hello PetaLinux World, startup test.... Hello PetaLinux World, startup test....
 blink: loading out-of-tree module taints kernel. blink: loading out-of-tree module taints kernel.
行 204: 行 209:
 PetaLinux 2018.3 petalinux_boot_from_flash /dev/ttyPS0 PetaLinux 2018.3 petalinux_boot_from_flash /dev/ttyPS0
  
-petalinux_boot_from_flash login: +petalinux_boot_from_flash login: 
 +```
  
-</code> 
  
 从上面的执行log看出来,很明显获取IP地址是在开机启动执行程序完成之后才开始的,所以一定要注意这一点。 从上面的执行log看出来,很明显获取IP地址是在开机启动执行程序完成之后才开始的,所以一定要注意这一点。
行 213: 行 218:
  
  
-===== - zynq设备上运行指定驱动的app程序 ===== +## - zynq设备上运行指定驱动的app程序
 在zynq fpga运行过程。 在zynq fpga运行过程。
  
-<code>+ 
 +```
  
 root@petalinux_boot_from_flash:/lib/modules/4.14.0-xilinx-v2018.3/extra# pwd root@petalinux_boot_from_flash:/lib/modules/4.14.0-xilinx-v2018.3/extra# pwd
行 228: 行 233:
 ls /dev/blink_Dev    #  已经有dev ls /dev/blink_Dev    #  已经有dev
 blinkapp    # 运用app程序 blinkapp    # 运用app程序
 +```
  
-</code> 
  
  
  
-===== - linux 加载module =====+## - linux 加载module
  
-<code> +```bash 
-insmode blink.ko+insmod blink.ko
 # 如果有同名文件module已经加载,可以先lsmode查看 # 如果有同名文件module已经加载,可以先lsmode查看
  
-lsmode+lsmod   # 列出已经加载的module
  
-rmmode blink.ko  # 删除已加载的module, 可以在后面两次加载新的module.+rmmod blink.ko  # 删除已加载的module, 可以在后面两次加载新的module. 
 +```
  
  
-</code>+ 
 +## - module驱动代码实例 
 +### - 一个简单的device module 驱动代码 
 +主要描述用户数据和内核数据之间如何互通,因为不能直接访问,需要通过copy_to_user & copy_from_user函数来完成。 
 + 
 + 
 +```c 
 + 
 + 
 +#include <linux/uaccess.h>  // copy_to_user copy_from_user 
 + 
 +#include <asm/uaccess.h> /* for get_user and put_user */  // raw_copy_to_user, raw_copy_from_user 
 + 
 + 
 + 
 +/* 
 +* This function is called whenever a process which has already opened the 
 +* device file attempts to read from it. 
 +*/ 
 +static ssize_t device_read( struct file *file, /* see include/linux/fs.h */ 
 + char __user * buffer, /* buffer to be filled with data */ 
 + size_t length, /* length of the buffer */ 
 + loff_t * offset) 
 +{ 
 +    int ret; 
 + 
 +    char k_buffer[20] = {0}; // kernel core buffer 
 + 
 +    memcpy(k_buffer, "core_to_user_msg02", length); 
 + 
 +    ret = copy_to_user(buffer, k_buffer, length); 
 + 
 + 
 + return SUCCESS; 
 +} 
 +/* 
 +* This function is called when somebody tries to 
 +* write into our device file. 
 +*/ 
 +static ssize_t device_write(struct file *file, 
 + const char __user * buffer,  
 + size_t length,  
 + loff_t * offset) 
 +{ 
 + 
 +    // user function 
 +    int ret; 
 +    char k_buffer[20] = {0}; // kernel core buffer 
 +    ret = copy_from_user(k_buffer, buffer, length); 
 + 
 +     
 +   printk("kernel:::  blink write: %s\n", k_buffer); 
 +    // end 
 + 
 + return SUCCESS; 
 +} 
 +``` 
 + 
 + 
 + 
 + 
 +### - gpio驱动led举例 
 +驱动部分: 
 + 
 + 
 +```c 
 +#include <linux/init.h>     //定义了module_init 
 +#include <linux/module.h>   //最基本的头文件,其中定义了MODULE_LICENSE这一类宏 
 +#include <linux/fs.h>       //file_operations结构体定义在该头文件中 
 +#include <linux/device.h> //class、device结构体的定义位置 
 +#include <linux/kernel.h> //printk头文件 
 + 
 +#include <linux/uaccess.h> //copy_from_user头文件 
 +#include <asm/io.h> //ioremap头文件 
 + 
 +#include <linux/ioport.h> 
 +#include <linux/of.h> 
 +#include <linux/delay.h> 
 + 
 +//定义主设备号  
 +static int major_num; 
 +//  定义设备文件名 
 +#define DEVICE_NAME "leds" 
 + 
 +//定义class、device结构体 
 +#define CLASS_NAME "mygpio"  
 +static struct class* gpio_class;     
 +static struct device* gpio_device;  
 + 
 +#define LEDS_BASE_ADDR (0x41200000) //GPIO的Base addr,用于映射 
 +  
 +static unsigned *leds; 
 + 
 +static int leds_drv_open(struct inode *Inode, struct file *File) 
 +{ 
 + *leds = 0x0;  
 + return 0; 
 +} 
 +  
 +  
 +static ssize_t leds_drv_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 
 +{ 
 + return 0; 
 +} 
 +  
 +static ssize_t leds_drv_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) 
 +{ 
 + unsigned int ret = 0; 
 + unsigned int tmp_val; 
 +  
 + ret = copy_from_user(&tmp_val, buf, count); 
 +  
 + *leds = tmp_val & 0xf;  
 +  
 + return ret; 
 +} 
 +  
 +//  描述与设备文件触发的事件对应的回调函数指针 
 +static struct file_operations dev_fops = 
 +{  
 + .owner = THIS_MODULE,  
 + .open = leds_drv_open, 
 + .read = leds_drv_read,  
 + .write = leds_drv_write, 
 +}; 
 +  
 +//  初始化Linux驱动 
 +static int __init leds_drv_init(void) 
 +{ 
 + int ret;  
 +  
 + leds = ioremap(LEDS_BASE_ADDR, 0x100); 
 +  
 + //获取主设备号 
 + major_num = register_chrdev(0,DEVICE_NAME, &dev_fops); 
 +  
 + //创建设备类 
 + gpio_class = class_create(THIS_MODULE, CLASS_NAME);     
 + 
 + if(IS_ERR(gpio_class)) 
 + {                 
 +    unregister_chrdev(major_num, DEVICE_NAME); 
 +    printk(KERN_ALERT "Failed to register device class\n"); 
 +    return PTR_ERR(gpio_class);           
 +    } 
 +    //注册设备 
 + gpio_device = device_create(gpio_class, NULL, MKDEV(major_num, 0), NULL, DEVICE_NAME); 
 +  
 + if(IS_ERR(gpio_device)) 
 + {                
 +    class_destroy(gpio_class);            
 +    unregister_chrdev(major_num, DEVICE_NAME); 
 +    printk(KERN_ALERT "Failed to create the device\n"); 
 +    return PTR_ERR(gpio_device); 
 +    } 
 +    printk(KERN_INFO "LED_GPIO_init: device created correctly\n"); 
 +  
 + return 0; 
 +} 
 +  
 +// 卸载Linux驱动 
 +static void __exit leds_drv_exit(void) 
 +{ 
 + iounmap(leds); 
 +  
 + //  删除设备文件,后创建的先卸载   
 + device_destroy(gpio_class, MKDEV(major_num, 0));   
 + class_destroy(gpio_class);  
 + 
 + unregister_chrdev(major_num,DEVICE_NAME); //释放设备号 
 + 
 + //  输出日志信息 
 + printk("LED_GPIO_exit success!\n"); 
 +}  
 +  
 +//  注册初始化Linux驱动的函数 
 +module_init(leds_drv_init); 
 +//  注册卸载Linux驱动的函数 
 +module_exit(leds_drv_exit); 
 + 
 +MODULE_LICENSE("GPL"); 
 +``` 
 + 
 + 
 +应用程序app部分: 
 + 
 + 
 +```c 
 +#include <stdio.h> 
 +#include <sys/ioctl.h> 
 +#include <sys/types.h> 
 +#include <sys/stat.h> 
 +#include <fcntl.h> 
 +  
 +  
 +  
 +int main(int argc, char** argv) 
 +{ 
 +        int fd; 
 +         
 +        fd = open("/dev/leds", O_RDWR); 
 +         
 +        if(fd < 0) 
 +        { 
 +                printf("fd = %d open fialed!\n", fd); 
 +        } 
 + 
 +        unsigned int leds = 0; 
 + 
 +        while(1) 
 +        { 
 +        write(fd, &leds, 4); 
 +         
 +        leds++; 
 +        leds %= 0xf; 
 +                sleep(1); 
 +        } 
 + 
 +        close(fd); 
 + 
 +        return 0; 
 +} 
 +``` 
fpga/petalinux.1675909211.txt.gz · 最后更改: (外部编辑)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki