zzx1997 发表于 2021-3-16 21:48

【米尔边缘AI计算盒FZ5测评】PYNQ框架移植(长文预警)

<div class='showpostmsg'><p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">本次将进行</span>PYNQ<span style="font-family:宋体">框架的移植,本次将继续使用之前搭建的硬件平台。</span>PYNQ<span style="font-family:宋体">为</span>xilinx<span style="font-family:宋体">官方推出的开源框架,其有效的降低了异构系列</span>FPGA<span style="font-family:宋体">的开发难度,并且灵活性得到了极大的提高。下面我们尝试将</span>PYNQ<span style="font-family:宋体">框架移植至米尔电子的开发板上。首先需要根据开发工具的版本选择合适的</span>pynq<span style="font-family:宋体">镜像移植版本:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">笔者使用的是</span>2018.3<span style="font-family:宋体">的</span>vivdao<span style="font-family:宋体">,所以选择</span>2.4<span style="font-family:宋体">版本的</span>PYNQ<span style="font-family:宋体">镜像,首先去</span>xilinx<span style="font-family:宋体">官网上下载</span>PYNQ<span style="font-family:宋体">的离线镜像,其为与底层无关的预编译镜像,可以有效加快</span>PYNQ<span style="font-family:宋体">的构建速度。其下载网址如下:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">https://www.xilinx.com/member/forms/download/xef.html?filename=pynq_rootfs_aarch64_v2.4.zip</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">官网已经提供了大量的官方开发板的镜像:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">由于</span>Edgeboard<span style="font-family:宋体">是第三方开发板所以需要进行移植</span>,<span style="font-family:宋体">下面为方便第三方开发板移植的预编译镜像:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">其中</span>aarch64<span style="font-family:宋体">版本主要面向</span>ultra<span style="font-family:宋体">系列的</span>A53<span style="font-family:宋体">内核,</span>arm<span style="font-family:宋体">版本主要面向</span>ZYNQ<span style="font-family:宋体">系列的</span>A9<span style="font-family:宋体">内核,我们需要的是</span>A53<span style="font-family:宋体">内核的</span>aarch64<span style="font-family:宋体">版本,镜像较大,下载周期较长。如果不想去官网下载,可以使用作者在百度网盘上下载好的预编译镜像,镜像链接在</span>Github<span style="font-family:宋体">中</span>:</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">https://github.com/buaa-zzx/Edgeboard-MPSOC-FPGA</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">然后下载</span>PYNQ<span style="font-family:宋体">软件包:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">git clone https://github.com/Xilinx/PYNQ.git</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">等待下载完成,打开文件夹:</span>cd PYNQ<span style="font-family:宋体">。然后检出版本号</span>:</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">git checkout v2.4</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">然后建立新的分支:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">git checkout -b Edgeboard_zu5</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">打开脚本路径:</span>cd ./PYNQ/sdbuild/scripts<span style="font-family:宋体">,</span> <span style="font-family:宋体">运行自检脚本:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">./setup_host.sh</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">自检完成后,进行</span>shell<span style="font-family:宋体">文件的修改,主要是为了添加</span>SDK<span style="font-family:宋体">等的安装目录:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">sudo vi ~/.bashrc </span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">source /opt/pkg/petalinux/2018.3/settings.sh</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">source /tool/Xilinx/Vivado/2018.3/settings64.sh</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">source /tool/Xilinx /SDK/2018.3/settings64.sh</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">export PATH=/opt/qemu/bin:/opt/crosstool-ng/bin:$PATH</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">然后进入</span>crosstool <span style="font-family:宋体">的安装目录:</span>/opt/crosstool-ng/lib/crosstool-ng-1.22.0/scripts/build/libc/<span style="font-family:宋体">,打开文件</span>newlib.sh<span style="font-family:宋体">:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">sudo vi newlib.sh</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">将文件内的花括号和多余网址删除,改成如下形式:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">进入</span>/PYNQ/sdbuild<span style="font-family:宋体">路径修改</span>makefile<span style="font-family:宋体">:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">sudo vi Makefile</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">注释或者删除</span>sdx<span style="font-family:宋体">一行:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">完成后,需要在</span>/PYNQ/sdbuild/<span style="font-family:宋体">路径下新建</span>prebuilt<span style="font-family:宋体">文件夹,并且把预编译镜像拷贝进去,同时需要在</span>/PYNQ/boards<span style="font-family:宋体">路径下新建</span>Edgeboard<span style="font-family:宋体">的文件夹:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">首先新建文件夹</span>Edgeboard_PYNQ<span style="font-family:宋体">;</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">然后新建</span>base<span style="font-family:宋体">和</span>petalinux_bsp<span style="font-family:宋体">文件夹;</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">并且在</span>petalinux_bsp<span style="font-family:宋体">文件夹文件夹中再新建</span>hardware_project<span style="font-family:宋体">文件夹;</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">将</span>base.bit<span style="font-family:宋体">拷贝到</span>base<span style="font-family:宋体">;</span>system.hdf<span style="font-family:宋体">拷贝到</span>hardware_project<span style="font-family:宋体">;</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">复制</span>/boards/ZCU104<span style="font-family:宋体">中的</span> .spec<span style="font-family:宋体">文件至</span>Edgeboard_PYNQ<span style="font-family:宋体">下并修改文件夹名为</span>Edgeboard_PYNQ.spec<span style="font-family:宋体">,修改内容为:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">ARCH_${BOARDS} := aarch64</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">BSP_${BOARDS} :=</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">BITSTREAM_${BOARDS} := base/base.bit</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">STAGE4_PACKAGES_${BOARDS} := pynq ethernet</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">完成后进入</span>/PYNQ/sdbuild/ <span style="font-family:宋体">打开终端,进行镜像编译:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">make BOARDS=Edgeboard_zu5 \</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">PREBUILT=./prebuilt/bionic.aarch64.2.4.img</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">等待镜像编译完成,如果出问题可以使用</span> sudo make clean<span style="font-family:宋体">清理工程,再进行重新编译。</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">此时编译得到的镜像会发现串口终端没有输出,这是因为默认的硬件配置与实际的硬件配置不符,所以需要进入</span>petalinux<span style="font-family:宋体">工程进行适当修改,</span>petalinux<span style="font-family:宋体">工程的路径为:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">/PYNQ/sdbuild/build/Edgeboard_PYNQ/petalinux_project</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">在此路径下打开</span>petalinux<span style="font-family:宋体">工程:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">petalinux-config</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">完成后需要对设备树文件进行简单修改,其路径为:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">配置文件:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">sudo vi system-user.dtsi</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">添加如下内容:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">/* SD */</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">&amp;sdhci1 {</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">disable-wp;</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">no-1-8-v;</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">};</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">设置完成后,重新</span>make<span style="font-family:宋体">一下工程。编译完成后烧写镜像,插入</span>SD<span style="font-family:宋体">卡,以</span>SD<span style="font-family:宋体">卡模式启动,可以看到串口终端开始输出数据,并且自动进入登录界面:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">可以看到</span>Python<span style="font-family:宋体">可以正常工作,并导入</span>OpenCV<span style="font-family:宋体">,其实</span>pynq<span style="font-family:宋体">框架已经帮我们预先移植好了</span>OpenCV<span style="font-family:宋体">等常用外设库,移植完成后便可以直接使用,并且可以查询开发板</span>IP<span style="font-family:宋体">,可以发现与</span>PYNQ<span style="font-family:宋体">的静态</span>IP<span style="font-family:宋体">一致。移植成功后,可以打开</span>jupyter notebook:</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">登录密码为</span>xilinx<span style="font-family:宋体">,然后进入界面:</span></span></span></span></p>

<p align="center" style="text-align:center; text-indent:24.0pt"></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">随便打开一个示例文件运行一下:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">可以看到</span>PYNQ<span style="font-family:宋体">框架工作正常。本次我们花费了较大篇幅来完成</span>PYNQ<span style="font-family:宋体">的框架移植,通过此框架我们可以快速的部署各类有意思的项目。如果有些读者觉得移植该框架有难度,可以利用笔者移植好的镜像,直接烧写进</span>SD<span style="font-family:宋体">卡便可以正常使用,镜像的下载地址已经上传至</span>Github<span style="font-family:宋体">,如果您觉得有用,可以给项目个星星哈,地址如下:</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif">https://github.com/buaa-zzx/Edgeboard-MPSOC-FPGA</span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify"><span style="font-size:12pt"><span style="line-height:150%"><span style="font-family:&quot;Times New Roman&quot;,serif"><span style="font-family:宋体">本次测评我们在米尔电子的开发板上实现了</span>PYNQ<span style="font-family:宋体">框架,该框架可以有效简化开发流程,下期教程将在</span>PYNQ<span style="font-family:宋体">框架上跑通一个经典的图像缩放算法,帮助读者熟悉利用</span>PYNQ<span style="font-family:宋体">框架进行各类计算过程加速的流程。</span></span></span></span></p>

<p style="text-indent:24.0pt; text-align:justify">&nbsp;</p>

<p style="text-indent:24.0pt; text-align:justify">&nbsp;</p>

<p style="text-indent:24.0pt; text-align:justify">&nbsp;</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

freebsder 发表于 2021-3-16 22:36

<p>pynq和linux+python+各种cv/ml库 有什么区别?</p>

火辣西米秀 发表于 2021-3-17 07:08

<p>镜像编译完成,如果出问题可以使用 sudo make clean清理工程,再进行重新编译,这个确实繁杂<br />
能不能先编译,再镜像,镜像一般不会破坏编译文件了吧</p>

okhxyyo 发表于 2021-3-17 08:58

<p>赞~~~期待后续呀</p>

w494143467 发表于 2021-3-18 13:03

<p>厉害厉害!!!</p>

dql2016 发表于 2021-3-18 14:26

<p>好厉害<img height="50" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/wanwan21.gif" width="63" /></p>

alshami 发表于 2024-5-29 03:21

hello how can I download the file from Baidu, it's need Account with china phone :(
页: [1]
查看完整版本: 【米尔边缘AI计算盒FZ5测评】PYNQ框架移植(长文预警)