ZYNQ

您所在的位置:网站首页 fil测试网 ZYNQ

ZYNQ

2023-07-09 02:50| 来源: 网络整理| 查看: 265

硬件平台搭建

新建工程,创建 block design。添加ZYNQ7 IP,对zynq进行初始化配置,对应板卡配置勾选SD,UART以及ENET资源,

编辑

如使用相同型号的板卡,可设置该部分为相同配置。

编辑

勾选DDR,并设置为PYNQZ2板卡的DDR的信息,

编辑

取消勾选多余资源,点击OK,完成硬件设计。如下图:

编辑

然后我们进行generate output product 然后生成HDL封装。这里没有进行使用PL资源,也不需要进行综合布局,在导出硬件时也不用包含bit流文件。

SDK软件部分

打开SDK后,新建application project,这里为了方便lwip设置,可选用使用lwip的相关模板,这里选择lwip tcp回环测试模板,保存新建工程。

编辑

选中新建好的工程,选择右击选中设置板载支持包,除了勾选lwip的板级支持包外,还需勾选sd卡需要的文件模式支持包。

编辑

点击standalone下的xilffs,可以对文件系统进行配置,这里可以使能长文件名有效,改变勾选为true。

保留模板例程的中的platform配置文件,删除其余文件。

编辑

修改main.c文件

修改main.c文件为如下:

#include

#include "xparameters.h"

#include "netif/xadapter.h"

#include "platform.h"

#include "platform_config.h"

#include "lwipopts.h"

#include "xil_printf.h"

#include "sleep.h"

#include "lwip/priv/tcp_priv.h"

#include "lwip/init.h"

#include "lwip/inet.h"

#if LWIP_IPV6==1

#include "lwip/ip6_addr.h"

#include "lwip/ip6.h"

#else

#if LWIP_DHCP==1

#include "lwip/dhcp.h"

extern volatile int dhcp_timoutcntr;

#endif

#define DEFAULT_IP_ADDRESS "192.168.1.10"

#define DEFAULT_IP_MASK "255.255.255.0"

#define DEFAULT_GW_ADDRESS "192.168.1.1"

#endif /* LWIP_IPV6 */

extern volatile int TcpFastTmrFlag;

extern volatile int TcpSlowTmrFlag;

void platform_enable_interrupts(void);

void start_application(void);

void print_app_header(void);

int transfer_data();

struct netif server_netif;

#if LWIP_IPV6==1

static void print_ipv6(char *msg, ip_addr_t *ip)

{

print(msg);

xil_printf(" %s\n\r", inet6_ntoa(*ip));

}

#else

static void print_ip(char *msg, ip_addr_t *ip)

{

print(msg);

xil_printf("%d.%d.%d.%d\r\n", ip4_addr1(ip), ip4_addr2(ip),

ip4_addr3(ip), ip4_addr4(ip));

}

static void print_ip_settings(ip_addr_t *ip, ip_addr_t *mask, ip_addr_t *gw)

{

print_ip("Board IP: ", ip);

print_ip("Netmask : ", mask);

print_ip("Gateway : ", gw);

}

static void assign_default_ip(ip_addr_t *ip, ip_addr_t *mask, ip_addr_t *gw)

{

int err;

xil_printf("Configuring default IP %s \r\n", DEFAULT_IP_ADDRESS);

err = inet_aton(DEFAULT_IP_ADDRESS, ip);

if (!err)

xil_printf("Invalid default IP address: %d\r\n", err);

err = inet_aton(DEFAULT_IP_MASK, mask);

if (!err)

xil_printf("Invalid default IP MASK: %d\r\n", err);

err = inet_aton(DEFAULT_GW_ADDRESS, gw);

if (!err)

xil_printf("Invalid default gateway address: %d\r\n", err);

}

#endif /* LWIP_IPV6 */

int main(void)

{

struct netif *netif;

//设置开发板的MAC地址

unsigned char mac_ethernet_address[] = {

0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };

netif = &server_netif;

init_platform();

print_app_header();

//初始化lwIP

lwip_init();

//将网络接口添加到netif,并将其设置为默认值

if (!xemac_add(netif, NULL, NULL, NULL, mac_ethernet_address,

PLATFORM_EMAC_BASEADDR)) {

xil_printf("Error adding N/W interface\r\n");

return -1;

}

#if LWIP_IPV6==1

netif->ip6_autoconfig_enabled = 1;

netif_create_ip6_linklocal_address(netif, 1);

netif_ip6_addr_set_state(netif, 0, IP6_ADDR_VALID);

print_ipv6("\n\rlink local IPv6 address is:", &netif->ip6_addr[0]);

#endif /* LWIP_IPV6 */

netif_set_default(netif);

//使能中断

platform_enable_interrupts();

//指定网络是否已启动

netif_set_up(netif);

#if (LWIP_IPV6==0)

#if (LWIP_DHCP==1)

//创建新的DHCP客户端

dhcp_start(netif);

dhcp_timoutcntr = 2;

while (((netif->ip_addr.addr) == 0) && (dhcp_timoutcntr > 0))

xemacif_input(netif);

if (dhcp_timoutcntr ip_addr.addr) == 0) {

xil_printf("ERROR: DHCP request timed out\r\n");

assign_default_ip(&(netif->ip_addr),

&(netif->netmask), &(netif->gw));

}

}

#else

assign_default_ip(&(netif->ip_addr), &(netif->netmask), &(netif->gw));

#endif

print_ip_settings(&(netif->ip_addr), &(netif->netmask), &(netif->gw));

#endif /* LWIP_IPV6 */

//启动应用程序

start_application();

while (1) {

if (TcpFastTmrFlag) {

tcp_fasttmr();

TcpFastTmrFlag = 0;

}

if (TcpSlowTmrFlag) {

tcp_slowtmr();

TcpSlowTmrFlag = 0;

}

xemacif_input(netif);

transfer_data();

}

cleanup_platform();

return 0;

}

添加remote_update.h文件

#ifndef REMOTE_UPDATE_H_

#define REMOTE_UPDATE_H_

#include "xparameters.h"

#include "xtime_l.h"

#include "xstatus.h"

#include

//服务器端口

#define SER_PORT 5678

//接收的最大文件大小16MB

#define MAX_FLASH_LEN 16*1024*1024

void sent_msg(const char *msg);

#endif

添加remote_update.c文件

#include "remote_update.h"

#include "xparameters.h"

#include "ff.h"

#include "string.h"

#include

#include "lwip/err.h"

#include "lwip/tcp.h"

#include "xil_printf.h"

u8 start_update_flag = 0;

u8 rxbuffer[MAX_FLASH_LEN];

u32 total_bytes = 0;

#define FILE_NAME "BOOT.bin"

struct tcp_pcb *c_pcb;

FATFS fs;

void print_app_header()

{

xil_printf("-----SD remote update demo------\n");

}

//挂载sd卡

void sd_mount(){

FRESULT status;

BYTE work[FF_MAX_SS];

//挂载sd卡,注册文件系统对象

status=f_mount(&fs,"",1);

if(status != FR_OK){

printf("%d\n",status);

printf("It isn't FAT format\n");

f_mkfs("",FM_FAT32,0,work,sizeof work);

f_mount(&fs,"",1);

}

}

//写数据

void sd_write_data(u8 wr_dat[], u32 wr_len){

FIL fil;

UINT bw;

//创建或者打开文件

f_open(&fil,FILE_NAME,FA_CREATE_ALWAYS | FA_WRITE | FA_READ);

//移动读写指针

f_lseek(&fil, 0);

//写数据

f_write(&fil,wr_dat,wr_len,&bw);

//关闭文件

f_close(&fil);

}

//将接收到的BOOT.bin文件写入到SD中

int transfer_data()

{

char msg[60];

if (start_update_flag) {

xil_printf("\r\nStart SD Update!\r\n");

xil_printf("file size of BOOT.bin is %lu Bytes\r\n", total_bytes);

sprintf(msg, "file size of BOOT.bin is %lu Bytes\r\n",total_bytes);

sent_msg(msg);

sd_write_data(rxbuffer,total_bytes);

xil_printf("SD Update finish!\n");

total_bytes = 0;

}

start_update_flag = 0;

return 0;

}

//向客户端回送信息

void sent_msg(const char *msg)

{

err_t err;

tcp_nagle_disable(c_pcb);

if (tcp_sndbuf(c_pcb) > strlen(msg)) {

err = tcp_write(c_pcb, msg, strlen(msg), TCP_WRITE_FLAG_COPY);

if (err != ERR_OK)

xil_printf("tcp_server: Error on tcp_write: %d\r\n", err);

err = tcp_output(c_pcb);

if (err != ERR_OK)

xil_printf("tcp_server: Error on tcp_output: %d\r\n", err);

} else

xil_printf("no space in tcp_sndbuf\r\n");

}

//接收回调函数

static err_t recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)

{

struct pbuf *q;

if (!p) {

tcp_close(tpcb);

tcp_recv(tpcb, NULL);

xil_printf("tcp connection closed\r\n");

return ERR_OK;

}

q = p;

if (q->tot_len == 6 && !(memcmp("update", p->payload, 6))) {

start_update_flag = 1;

sent_msg("\r\nStart SD Update\r\n");

} else if (q->tot_len == 5 && !(memcmp("clear", p->payload, 5))) {

start_update_flag = 0;

total_bytes = 0;

sent_msg("Clear received data\r\n");

xil_printf("Clear received data\r\n");

} else {

while (q->tot_len != q->len) {

memcpy(&rxbuffer[total_bytes], q->payload, q->len);

total_bytes += q->len;

q = q->next;

}

memcpy(&rxbuffer[total_bytes], q->payload, q->len);

total_bytes += q->len;

}

tcp_recved(tpcb, p->tot_len);

pbuf_free(p);

return ERR_OK;

}

err_t accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err)

{

xil_printf("tcp_server: Connection Accepted\r\n");

c_pcb = newpcb; //保存连接的客户端PCB

//设置接收回调

tcp_recv(c_pcb, recv_callback);

tcp_arg(c_pcb, NULL);

return ERR_OK;

}

int start_application()

{

struct tcp_pcb *pcb;

err_t err;

//挂载SD卡

sd_mount();

xil_printf("Successfully init SD\r\n");

print_app_header();

//创建TCP PCB

pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);

if (!pcb) {

xil_printf("Error creating PCB. Out of Memory\n\r");

return -1;

}

//绑定端口号

err = tcp_bind(pcb, IP_ANY_TYPE, SER_PORT);

if (err != ERR_OK) {

xil_printf("Unable to bind to port %d: err = %d\n\r", SER_PORT, err);

return -2;

}

//此处不需要回调函数的任何参数

tcp_arg(pcb, NULL);

//侦听连接

pcb = tcp_listen(pcb);

if (!pcb) {

xil_printf("Out of memory while tcp_listen\n\r");

return -3;

}

//指定用于传入连接的回调

tcp_accept(pcb, accept_callback);

xil_printf("TCP server started @ port %d\n\r", SER_PORT);

return 0;

}

完成代码编写后,进行烧写验证。

下载验证

打开网络调试助手,选择协议类型为TCP客户端,选择远程主机的IP地址和端口,选择需要加载的应用程序的bin文件,勾选加载文件数据源,点击发送。

发送完成后在发送框选择输入“update”更新SD卡的应用程序。

串口终端中查看调试信息,表示SD卡程序更新完成。

使用读卡器查看贴片SD卡转接卡是否正常存储到SD卡中,读取文件可知已经正常写入。

将板卡启动模式调整至SD卡模式,上电重启板卡程序,观察到板卡程序成功启动。

————————————————

如果看完文章之后还是有疑惑或不懂的地方,找深圳雷龙发展了解产品详情。返回搜狐,查看更多



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3