引言
ZYNQ7020有两个CPU核心,这两个核心可以采用SMP或AMP方式进行调度,当采用AMP方式进行调度时核0和核1可以运行不同的操作系统,如核0运行Linux系统,提供有些复杂的用户交互工作,核1运行实时操作系统,对设备进行精准控制,由此便引出了两个核心(或者说两个操作系统)之间的数据交互问题,而OpenAMP便是解决此问题的一个良好方案。
提示:
采用SMP调度时核0和核1由同一个操作系统进行管理,两个核心之间的数据交互由操作系统内部实现。
OpenAMP架构
在Linux内核中实现了三个重要的组件,分别是virtIO、RPMsg、Remoteproc。
- virtIO:是一个设备虚拟框架,这里用于管理共享内存,在OpenAMP库实现了用于共享内存管理的virtIO标准
- RPMsg:一种消息总线,用于实现消息传递,在OpenAMP库也实现了相应的标准
- remoteproc:从远端固件的elf文件中解析远端固件资源表,并提供远端固件生命周期管理(即启动和停止核1)和IPI中断管理,
在FreeRTOS端组要包括两个组件,分别是Libmetal、OpenAMP。 - Libmetal:实现一种类似于Linux中总线设备驱动框架的功能。
- OpenAMP:实现用于共享内存管理的virtIO标准和RPMsg标准,与Linux端的virtIO框架和RPMsg框架对应。
在Vitis中编译核1的回环测试代码
-
打开vitis,依次点击File > New > Application Projects打开创建APP的向导
-
在欢迎界面点击next,进入platfrom创建界面(可以顺便吧Skip welcome page next time. (Can be reached with Back button)选上,这样下次就自动略过欢迎界面)
-
platfrom创建界面选择Create a new platform from hardware (XSA)页面,然后点击Browse…按钮,打开xsa文件选择界面,在选择界面中选择从vavido中导出的xsa文件(选择完成后默认使用刚刚选择的xsa文件创建platfrom,并会把Generate boot components勾上),接下来点击next,进入app工程创建界面
-
在APP创建界面设置APP名称和系统名称,然后选择核1创建APP,完成后点击next,进入Domain界面
-
Domain界面选择系统类型为freertos10 xilinx,然后点击next,进入APP模板选择界面
-
在APP模板选择界面选择OpenAMP echo-test,然后点击Finish,至此便完成了OpenAMP回环例程的创建
-
配置platfrom,增加-DUSE_AMP=1编译选项,否则核1启动时会再次初始化中断控制器等公共资源,导致程序崩溃
在petalinux中构建支持OpenAMP的内核
- 创建petalinux工程
#加载petalinux工作环境
source /opt/pkg/petalinux2020.2/settings.sh
#创建空的petalinux工程
petalinux-create -t project --template zynq -n sdrpi-pelainux
- 对petalinux进行配置
#进入工程目录
cd sdrpi-pelainux/
#导入xsa文件,并打开配置菜单
petalinux-config --get-hw-description ../xsa-sdrpi/
进行如下配置:
-*- Subsystem AUTO Hardware Settings ---> Serial Settings ---> FSBL Serial stdin/stdout (ps7_uart_1) ---> #根据硬件选择正确的串口号,我这里选择ps7_uart_1DTG Serial stdin/stdout (ps7_uart_1) ---> #根据硬件选择正确的串口号,我这里选择ps7_uart_1[*] Advanced bootable images storage Settings ---> #为了方便调试,全部选择primary sd,调试完成后根据需求重新进行配置boot image settings --->image storage media (primary sd) --->u-boot env partition settings --->image storage media (primary sd) ---> #还需要在uboot进行相应配置才能将环境变量存储到SDkernel image settings --->image storage media (primary sd) ---> dtb image settings --->image storage media (primary sd) --->
Yocto Settings --->Add pre-mirror url ---> pre-mirror url path #将pre-mirror url path设置为file://downloads_2020.2.tar.gz解压后的目录,我这里是 file:///opt/pkg/petalinux2020.2/downloadsLocal sstate feeds settings--->()local sstate feeds url #将local sstate feeds url设置为sstate_arm_2020.2.tar.gz解压后得到的arm路径下,我这里是/opt/pkg/petalinux2020.2/sstate_arm_2020.2/arm[ ] Enable Network sstate feeds #取消此项选择[*] Enable BB NO NETWORK #选中此选项
- 配置uboot,使能FAT环境变量存储功能,并关闭SPI Falsh环境变量存储功能
#打开uboot配置菜单
petalinux-config -c u-boot
进行如下配置:
Environment --->[*] Environment is in a FAT filesystem #将环境变量存储到FAT文件系统[ ] Environment is in SPI flash #取消flash存储环境变量的选项(mmc) Name of the block device for the environment #块设备名称(0:1) Device and partition for where to store the environemt in FAT #块设备扇区
- 配置Linux内核,使能模块加载支持、remoteproc驱动
#打开内核配置界面
petalinux-config -c kernel
进行如下配置
[*] Enable loadable module support --->
Device Drivers --->Remoteproc drivers ---><*> Support ZYNQ remoteproc
- 配置根文件系统,使能OpenAMP测试应用程序
#打开根文件系统配置界面
petalinux-config -c rootfs
进行如下配置:
Petalinux Package Groups --->packagegroup-petalinux-openamp --->[*] packagegroup-petalinux-openamp
- 修改设备树文件
#打开设备树文件
gedit project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi
修改后的内容如下所示:
/include/ "system-conf.dtsi"
/ {reserved-memory {#address-cells = <1>;#size-cells = <1>;ranges;vdev0vring0: vdev0vring0@3e800000 {no-map;compatible = "shared-dma-pool";reg = <0x3e800000 0x4000>;};vdev0vring1: vdev0vring1@3e804000 {no-map;compatible = "shared-dma-pool";reg = <0x3e804000 0x4000>;};vdev0buffer: vdev0buffer@3e808000 {no-map;compatible = "shared-dma-pool";reg = <0x3e808000 0x100000>;};rproc_0_reserved: rproc@3e000000 {no-map;compatible = "shared-dma-pool";reg = <0x3e000000 0x800000>;};};remoteproc0: remoteproc@0 {compatible = "xlnx,zynq_remoteproc";firmware = "firmware";vring0 = <15>;vring1 = <14>;memory-region = <&rproc_0_reserved>, <&vdev0buffer>, <&vdev0vring0>, <&vdev0vring1>;};
};
- 编译petalinux工程
#进行一次全编译
petalinux-build
- 打包 BOO.BIN
petalinux-package --boot --fsbl --fpga --u-boot --force
运行测试例程
- 找一张SD卡,格式化为FAT32格式,将petalinux编译生成的boot.scr、BOOT.BIN、image.ub和Vitis编译生成的openamp_echo.elf拷贝到SD卡中(petalinux编译生成文件位于images/linux目录),然后将SD查到开发板的SD0接口,再将开发板设置为SD启动即可,若串口配置正确此时便可通过串口看到启动信息。
- 在Linux根文件系统中创建/lib/firmware/目录,然后将vitis生成的固件拷贝到/lib/firmware目录中,然后启动核1的固件
#创建/lib/firmware/目录
mkdir -p /lib/firmware
#将vitis生成的固件拷贝到/lib/firmware目录中
cp /mnt/sd-mmcblk0p1/openamp_echo.elf /lib/firmware
#启动核1的固件
echo openamp_echo.elf > /sys/class/remoteproc/remoteproc0/firmware
echo start > /sys/class/remoteproc/remoteproc0/state
- 运行回环测试程序
#测试程序在跟文件系统的/usr/bin/目录中,所以可直接在命令行执行
echo_test
使用自定义RPMsg驱动进行echo测试
赛灵思官方的测试程序使用的是通用RPMsg驱动进行ceho测试的,此方案应用层代码比较繁琐(需要先利用通用驱动创建一个设备,然后在对设备进行读写操作),而且无法在内核层与核1进行交互,因此便编写了echo测试的Linux驱动,使用此驱动结合应用层的设备读写程序进行ceho测试。
把RPMsg驱动代码添加到petalinux工程中
- 在petalinux中创建内核模块
petalinux-create -t modules --name rpmsg-packet-driver --enable
- 编辑project-spec/meta-user/recipes-modules/rpmsg-packet-driver/files/rpmsg-packet-driver.c文件
gedit project-spec/meta-user/recipes-modules/rpmsg-packet-driver/files/rpmsg-packet-driver.c
文件内容如下:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/rpmsg.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/kthread.h>
#include <linux/ioctl.h>
#include <linux/poll.h>
#include <linux/errno.h>
#include <linux/atomic.h>
#include <linux/skbuff.h>
#include <linux/idr.h>#define RPMSG_BUFFER_SIZE 496
#define RPMSG_DEV_MAX_MINORS 5#define cdev_to_eptdev(i_cdev) container_of(i_cdev, struct _rpmsg_eptdev, cdev)struct _rpmsg_eptdev {struct cdev cdev;struct rpmsg_device *rpdev;struct rpmsg_endpoint *ept;struct mutex mutex_lock;bool open_flag;struct sk_buff_head read_skb_queue;wait_queue_head_t read_wait_queue;uint8_t write_buffer[RPMSG_BUFFER_SIZE];
};static struct class *rpmsg_class;
static dev_t rpmsg_dev_major;
static DEFINE_IDA(rpmsg_minor_ida);static int rpmsg_dev_open(struct inode *inode, struct file *filp)
{struct sk_buff *skb;struct _rpmsg_eptdev *rpmsg_eptdev = cdev_to_eptdev(inode->i_cdev);//设置文件描述符私有数据filp->private_data = rpmsg_eptdev;//获取互斥锁mutex_lock(&rpmsg_eptdev->mutex_lock);//检查打开状态,若已经打开则退出并返回EBUSYif(rpmsg_eptdev->open_flag){mutex_unlock(&rpmsg_eptdev->mutex_lock);printk("device busy\r\n");return -EBUSY;}//复位队列while(!skb_queue_empty(&rpmsg_eptdev->read_skb_queue)){skb = skb_dequeue(&rpmsg_eptdev->read_skb_queue);if(!skb)break;kfree_skb(skb);}//设置为打开状态rpmsg_eptdev->open_flag = true;//释放互斥锁mutex_unlock(&rpmsg_eptdev->mutex_lock);return 0;
}static int rpmsg_dev_release(struct inode *inode, struct file *filp)
{struct sk_buff *skb;struct _rpmsg_eptdev *rpmsg_eptdev = filp->private_data;//获取互斥锁mutex_lock(&rpmsg_eptdev->mutex_lock);//设置为关闭状态rpmsg_eptdev->open_flag = false;//复位队列while(!skb_queue_empty(&rpmsg_eptdev->read_skb_queue)){skb = skb_dequeue(&rpmsg_eptdev->read_skb_queue);if(!skb)break;kfree_skb(skb);}//释放互斥锁mutex_unlock(&rpmsg_eptdev->mutex_lock);return 0;
}static ssize_t rpmsg_driver_write(struct file *filp, const char __user *ubuff, size_t len, loff_t *p_off)
{int result;unsigned long copy_len;struct _rpmsg_eptdev *rpmsg_eptdev = filp->private_data;//暂时将数据拷贝到内核空间copy_len = (len < RPMSG_BUFFER_SIZE) ? len : RPMSG_BUFFER_SIZE;if(copy_from_user(rpmsg_eptdev->write_buffer, ubuff, copy_len)){printk("copy from user failed\r\n");return -EFAULT;}//进行发送if (filp->f_flags & O_NONBLOCK)result = rpmsg_trysend(rpmsg_eptdev->ept, rpmsg_eptdev->write_buffer, copy_len);elseresult = rpmsg_send(rpmsg_eptdev->ept, rpmsg_eptdev->write_buffer, copy_len);return (result < 0) ? result : copy_len;
}static ssize_t rpmsg_driver_read(struct file *filp, char __user *ubuff, size_t len, loff_t *pos)
{int copy_len;struct sk_buff *skb;struct _rpmsg_eptdev *rpmsg_eptdev = filp->private_data;//检查队里是否有数据if(skb_queue_empty(&rpmsg_eptdev->read_skb_queue)){//以非阻塞式打开if(filp->f_flags & O_NONBLOCK)return -EAGAIN;//等待有数据可读if(wait_event_interruptible(rpmsg_eptdev->read_wait_queue, !skb_queue_empty(&rpmsg_eptdev->read_skb_queue)))return -ERESTARTSYS;}//从队列中取出一个sk_buffskb = skb_dequeue(&rpmsg_eptdev->read_skb_queue);//检查是否成功取到sk_buffif(!skb) {printk("Read failed, RPMsg queue is empty.\n");return -EAGAIN;}//将数据拷贝到应用层copy_len = min_t(size_t, len, skb->len);if(copy_to_user(ubuff, skb->data, copy_len)){printk("Failed to copy data to user.\n");kfree_skb(skb);return -EFAULT;}//释放sk_buffkfree_skb(skb);return copy_len;
}static long rpmsg_driver_ioctl(struct file *p_file, unsigned int cmd, unsigned long arg)
{/* No ioctl supported a the moment */return -EINVAL;
}static unsigned int rpmsg_driver_poll(struct file *filp, poll_table *wait)
{unsigned int mask = 0;struct _rpmsg_eptdev *rpmsg_eptdev = filp->private_data;poll_wait(filp, &rpmsg_eptdev->read_wait_queue, wait);//获取队列状态if (!skb_queue_empty(&rpmsg_eptdev->read_skb_queue))mask |= POLLIN | POLLRDNORM;return mask;
}//操作函数
static struct file_operations rpmsg_driver_fops = {.owner = THIS_MODULE,.open = rpmsg_dev_open,.release = rpmsg_dev_release,.write = rpmsg_driver_write,.read = rpmsg_driver_read,.unlocked_ioctl = rpmsg_driver_ioctl,.poll = rpmsg_driver_poll,
};static int _rpmsg_drv_cb(struct rpmsg_device *rpdev, void *data, int len, void *priv, u32 src)
{struct sk_buff *skb;struct _rpmsg_eptdev *rpmsg_eptdev = dev_get_drvdata(&rpdev->dev);;if(rpmsg_eptdev->open_flag && (skb_queue_len(&rpmsg_eptdev->read_skb_queue) < 8)){//分配sk_buffskb = alloc_skb(len, GFP_ATOMIC);if (!skb)return -ENOMEM;//将数据拷贝到sk_buffmemcpy(skb_put(skb, len), data, len);//将sk_buff放入队列skb_queue_tail(&rpmsg_eptdev->read_skb_queue, skb);//唤醒读线程wake_up_interruptible(&rpmsg_eptdev->read_wait_queue);}return 0;
}static int _rpmsg_drv_probe(struct rpmsg_device *rpdev)
{int result;int device_id;dev_t devt;struct device *device;struct _rpmsg_eptdev *rpmsg_eptdev;char device_name[128];printk("rpmsg_packet_probe\r\n");//分配设备句柄rpmsg_eptdev = devm_kzalloc(&rpdev->dev, sizeof(struct _rpmsg_eptdev), GFP_KERNEL);if(!rpmsg_eptdev){printk("alloc _rpmsg_eptdev failed\r\n");result = -ENOMEM;goto error0;}//初始化互斥锁,用于保护打开核关闭过程mutex_init(&rpmsg_eptdev->mutex_lock);//默认为关闭状态rpmsg_eptdev->open_flag = false;//初始化sk_buff_headskb_queue_head_init(&rpmsg_eptdev->read_skb_queue);//初始化read_wait_queueinit_waitqueue_head(&rpmsg_eptdev->read_wait_queue);//绑定rpmsg驱动和rpmsg端点rpmsg_eptdev->rpdev = rpdev;rpmsg_eptdev->ept = rpdev->ept;//分配一个IDdevice_id = ida_simple_get(&rpmsg_minor_ida, 0, RPMSG_DEV_MAX_MINORS, GFP_KERNEL);if(device_id < 0){printk("Not able to get minor id for rpmsg device.\n");goto error0;}//合成设备号devt = MKDEV(MAJOR(rpmsg_dev_major), device_id);//初始化CDEV对象cdev_init(&rpmsg_eptdev->cdev, &rpmsg_driver_fops);rpmsg_eptdev->cdev.owner = THIS_MODULE;//向系统添加CDEV对象result = cdev_add(&rpmsg_eptdev->cdev, devt, 1);if(result < 0){printk("add cdev failed\r\n");goto error1;}//合成设备名称snprintf(device_name, sizeof(device_name), "rpmsg_packet%d", rpdev->dst);printk("device major %d, device minor %d, device file name = %s\r\n", MAJOR(devt), MINOR(devt), device_name);//创建设备文件,将ID作为此设备的次设备号device = device_create(rpmsg_class, NULL, devt, NULL, device_name);if(IS_ERR(device)){printk("device create failed");result = PTR_ERR(device);goto error2;}//发送一次数据,使对方得到端点地址rpmsg_send(rpmsg_eptdev->ept, device_name, (sizeof(device_name) > RPMSG_BUFFER_SIZE) ? RPMSG_BUFFER_SIZE : sizeof(device_name));//设置rpmsg_device私有数据dev_set_drvdata(&rpdev->dev, rpmsg_eptdev);return 0;error2:cdev_del(&rpmsg_eptdev->cdev);
error1:ida_simple_remove(&rpmsg_minor_ida, device_id);
error0:return result;
}static void _rpmsg_drv_remove(struct rpmsg_device *rpdev)
{dev_t devt;struct _rpmsg_eptdev *rpmsg_eptdev = dev_get_drvdata(&rpdev->dev);printk("rpmsg_packet_remove\r\n");if(!rpmsg_eptdev)return;//获取设备号devt = rpmsg_eptdev->cdev.dev;//删除设备文件device_destroy(rpmsg_class, devt);//删除cdevcdev_del(&rpmsg_eptdev->cdev);//释放IDida_simple_remove(&rpmsg_minor_ida, MINOR(devt));
}//匹配列表,与核1中rpmsg_create_ept函数的const char *name参数对应
static struct rpmsg_device_id rpmsg_id_table[] = {{ .name = "rpmsg-openamp-demo-channel" },{},
};static struct rpmsg_driver rpmsg_user_dev_drv = {.drv.name = KBUILD_MODNAME,.drv.owner = THIS_MODULE,.id_table = rpmsg_id_table,.probe = _rpmsg_drv_probe,.remove = _rpmsg_drv_remove,.callback = _rpmsg_drv_cb,
};static int __init _rpmsg_packet_init(void)
{int result;printk("_rpmsg_packet_init\r\n");//根据次设备号起始值动态分配并注册字符设备号result = alloc_chrdev_region(&rpmsg_dev_major, 0, RPMSG_DEV_MAX_MINORS, "rpmsg_packet");if(result){printk("alloc_chrdev_region failed: %d\n", result);goto unreg_region0;}//创建class对象rpmsg_class = class_create(THIS_MODULE, KBUILD_MODNAME);if(IS_ERR(rpmsg_class)){result = PTR_ERR(rpmsg_class);printk("class_create failed: %d\n", result);goto unreg_region1;}//注册rpmsg驱动result = register_rpmsg_driver(&rpmsg_user_dev_drv);if(result){printk("register rpmsg driver failed: %d\n", result);goto unreg_region2;}return 0;unreg_region2:class_destroy(rpmsg_class);
unreg_region1:unregister_chrdev_region(rpmsg_dev_major, RPMSG_DEV_MAX_MINORS);
unreg_region0:return result;
}static void __exit _rpmsg_packet_deinit(void)
{printk("_rpmsg_packet_deinit\r\n");unregister_rpmsg_driver(&rpmsg_user_dev_drv);class_destroy(rpmsg_class);unregister_chrdev_region(rpmsg_dev_major, RPMSG_DEV_MAX_MINORS);
}module_init(_rpmsg_packet_init);
module_exit(_rpmsg_packet_deinit);MODULE_DESCRIPTION("rpmsg_packet_driver");
MODULE_LICENSE("GPL v2");
把回环测试程序添加到petalinux工程中
- 在petalinux中创建应用程序,用于测试RPMsg驱动
在这里插入代码片
- 编辑project-spec/meta-user/recipes-apps/openamp-echo/files/openamp-echo.c文件
gedit project-spec/meta-user/recipes-apps/openamp-echo/files/openamp-echo.c
文件内容如下:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <pthread.h>static int amp_fd;//openamp单次读写最大为496byte
static uint8_t rbuffer[496];
static uint8_t wbuffer[496];static volatile ssize_t read_total = 0, write_total = 0;
static volatile int cnt = 0;void *amp_thread(void *arg)
{int32_t result;ssize_t write_size, read_size;int32_t lenght = sizeof(rbuffer);int32_t magic = 0;while(1){//填充数据magic++;for(int i=0; i<lenght; i++)wbuffer[i] = (uint8_t)(magic + i);//发送到CPU1result = write(amp_fd, wbuffer, lenght);if(result != lenght){printf("linux write amp failed\r\n");continue;}write_size = result;write_total += write_size;//读取CPU1返回的数据memset(rbuffer, 0, lenght);for(read_size = 0; read_size < write_size; ){result = read(amp_fd, &rbuffer[read_size], lenght-read_size);if(result < 0)break;read_size += result;}if(read_size != lenght){printf("linux read amp failed\r\n");continue;}read_total += read_size;//校验数据是否一致for(int i=0; i<lenght; i++){if(wbuffer[i] != rbuffer[i]){printf("check out failed\r\n");break;}}cnt++;}
}int main(int argc, char *argv[])
{int err;pthread_t thread;char *rpmsg_dev="/dev/rpmsg0";if(argc > 1)rpmsg_dev = argv[1];printf("amp test\r\n");amp_fd = open(rpmsg_dev, O_RDWR);if(amp_fd < 0){perror("error");return -1;}err = pthread_create(&thread, NULL, amp_thread, NULL);if(err != 0){printf("create thread failed, error code %d\r\n", err);return -1;}err = pthread_detach(thread);if(err != 0){printf("thread detach failed, error code %d\r\n", err);return -1;}while(1){sleep(1);printf("read speed = %fMB/s\r\n", read_total/1.0f/1024.0f/1024.0f);read_total = 0;printf("write speed = %fMB/s\r\n", write_total/1.0f/1024.0f/1024.0f);write_total = 0;printf("echo count = %d\r\n", cnt);cnt = 0;}
}
- 编辑project-spec/meta-user/recipes-apps/openamp-echo/files/Makefile文件
gedit project-spec/meta-user/recipes-apps/openamp-echo/files/Makefile
文件内容如下:
APP = openamp-echo# Add any other object files to this list below
APP_OBJS = openamp-echo.oall: buildbuild: $(APP)$(APP): $(APP_OBJS)$(CC) -o $@ $(APP_OBJS) $(LDFLAGS) $(LDLIBS) -l pthread
clean:rm -f $(APP) *.o
编译测试
- 编译petalinux工程
#进行一次全编译
petalinux-build
- 打包 BOO.BIN
petalinux-package --boot --fsbl --fpga --u-boot --force
- 找一张SD卡,格式化为FAT32格式,将petalinux编译生成的boot.scr、BOOT.BIN、image.ub和Vitis编译生成的openamp_echo.elf拷贝到SD卡中(petalinux编译生成文件位于images/linux目录),然后将SD查到开发板的SD0接口,再将开发板设置为SD启动即可,若串口配置正确此时便可通过串口看到启动信息
- 加载rpms驱动
#驱动程序在编译时自动打包到根文件系统的/lib/modules/5.4.0-xilinx-v2020.2/extra/目录中,提供modprobe目录即可完成加载
modprobe rpmsg-packet-driver.ko
- 在Linux根文件系统中创建/lib/firmware/目录,然后将vitis生成的固件拷贝到/lib/firmware目录中,然后启动核1的固件
#创建/lib/firmware/目录
mkdir -p /lib/firmware
#将vitis生成的固件拷贝到/lib/firmware目录中
cp /mnt/sd-mmcblk0p1/openamp_echo.elf /lib/firmware
#启动核1的固件
echo openamp_echo.elf > /sys/class/remoteproc/remoteproc0/firmware
echo start > /sys/class/remoteproc/remoteproc0/state
- 运行回环测试程序
#测试程序在编译时自动打包到跟文件系统的/usr/bin/目录中,所以可直接在命令行执行
openamp-echo /dev/rpmsg_packet0
双核AMP模式下的cache问题
增加-DUSE_AMP=1编译选项后调用Xil_DCacheFlushRange函数时只会操作L1 cache,而不会对L2 cache进行操作,这可能会导致DMA之类的外设传输数据不正确,此时可以对ps7_cortexa9_1/freertos10_xilinx_ps7_cortexa9_1/libsrc/standalone_v7_3/src/xil_cache.c文件进行如下修改:
- 修改Xil_DCacheFlushRange函数,使能对L2的操作
void Xil_DCacheFlushRange(INTPTR adr, u32 len)
{u32 LocalAddr = adr;const u32 cacheline = 32U;u32 end;u32 currmask;volatile u32 *L2CCOffset = (volatile u32 *)(XPS_L2CC_BASEADDR +XPS_L2CC_CACHE_INV_CLN_PA_OFFSET);currmask = mfcpsr();mtcpsr(currmask | IRQ_FIQ_MASK);if (len != 0U) {/* Back the starting address up to the start of a cache line* perform cache operations until adr+len*/end = LocalAddr + len;LocalAddr &= ~(cacheline - 1U);while (LocalAddr < end) {/* Flush L1 Data cache line */
#if defined (__GNUC__) || defined (__ICCARM__)asm_cp15_clean_inval_dc_line_mva_poc(LocalAddr);
#else{ volatile register u32 Reg__asm(XREG_CP15_CLEAN_INVAL_DC_LINE_MVA_POC);Reg = LocalAddr; }
#endif
//#ifndef USE_AMP/* Flush L2 cache line */*L2CCOffset = LocalAddr;Xil_L2CacheSync();
//#endifLocalAddr += cacheline;}}dsb();mtcpsr(currmask);
}
- 使能Xil_L2CacheSync函数,将147行的#endif复制到127行
提示
重新生成platfrom时会覆盖修改内容,为避免重复修改可以对安装路径下的对于文件进行相同修改,文件路径位于Vitis\2020.2\data\embeddedsw\lib\bsp\standalone_v7_3\src\arm\cortexa9\xil_cache.c。
双核AMP模式下的外设中断问题
对于共享中断xscugic驱动默认将其映射到核0,这会导致核1无法收到相应中断,而核0收到了自己不需要的中断,针对此问题可以在使能中断前调用XScuGic_InterruptMaptoCpu函数将中断映射到核1。
FreeRTOS使用浮点运算问题
任务中的浮点运算
ZYNQ的FreeRTOS默认任务切换过程中不保护浮点运算器的寄存器,若此时任务使用了浮点运算则可能会导致浮点运算出错,此时可以采用如下两种办法进行处理:
- 在需要使用浮点的任务中调用vPortTaskUsesFPU函数,设置任务的浮点寄存器保护标志
- 修改platfrom的浮点使用标志,将其修改为2(默认是1)
中断中的浮点运算
在发生中断后FreeRTOS对通用寄存器进行保护后便去调用C语言实现vApplicationIRQHandler函数,直接进入用户中断处理函数,若此时在中断中进行了浮点运算可能会出现浮点运算结果不正确,此时可以对ps7_cortexa9_1/freertos10_xilinx_ps7_cortexa9_1/libsrc/freertos10_xilinx_v1_7/src/portZynq7000.c文件的第129行进行修改,将vApplicationIRQHandler函数的名称修改为vApplicationFPUSafeIRQHandlerConst,这样在汇编中采用若定义实现的vApplicationIRQHandler就会生效,而vApplicationIRQHandler函数对浮点寄存器进行了保护核恢复,从而解决了中断不能使用浮点运算的问题。
提示
重新生成platfrom时会覆盖修改内容,为避免重复修改可以对安装路径下的对于文件进行相同修改,文件路径位于Vitis\2020.2\data\embeddedsw\ThirdParty\bsp\freertos10_xilinx_v1_7\src\Source\portable\GCC\ARM_CA9\portZynq7000.c。