代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <string.h>
#include "lwip/err.h"
#include "lwip/tcp.h"
#include "lwipopts.h"
#include "xil_cache.h"
#include "xil_printf.h"
#include "sleep.h"
#include "gpio_intr.h"
#define SEND_SIZE 512
#define PAKET_LENGTH 1024
#define local_port 8080
#define DATA_ADDR XPAR_BRAM_0_BASEADDR
static struct tcp_pcb *connected_pcb = NULL;
volatile unsigned tcp_client_connected = 0;
static int tcp_trans_done = 0;
extern u8_t GpioIntrFlag;
volatile u32 internalTimeNumber;
int tcp_poll_cnt = 0;
u8 tcp_rx_buf[20] = "";
u16_t copynum;
void send_received_data() {
#if __arm__
int copy = 3;
#else
int copy = 0;
#endif
err_t err;
int Status;
struct tcp_pcb *tpcb = connected_pcb;
if (GpioIntrFlag) {
if (!connected_pcb)
return;
/* if tcp send buffer has enough space to hold the data we want to transmit from PL, then start tcp transmission*/
if (tcp_sndbuf(tpcb) > SEND_SIZE) {
/*transmit received data through TCP*/
err = tcp_write(tpcb, DATA_ADDR, SEND_SIZE, copy);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_write: %d\r\n", err);
connected_pcb = NULL;
return;
}
err = tcp_output(tpcb);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_output: %d\r\n", err);
return;
}
}
if (tcp_sndbuf(tpcb) > SEND_SIZE) {
/*transmit received data through TCP*/
err = tcp_write(tpcb, DATA_ADDR + SEND_SIZE, SEND_SIZE, copy);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_write: %d\r\n", err);
connected_pcb = NULL;
return;
}
err = tcp_output(tpcb);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_output: %d\r\n", err);
return;
}
}
if (tcp_sndbuf(tpcb) > SEND_SIZE) {
/*transmit received data through TCP*/
err = tcp_write(tpcb, DATA_ADDR + SEND_SIZE * 2, SEND_SIZE, copy);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_write: %d\r\n", err);
connected_pcb = NULL;
return;
}
err = tcp_output(tpcb);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_output: %d\r\n", err);
return;
}
}
if (tcp_sndbuf(tpcb) > SEND_SIZE) {
/*transmit received data through TCP*/
err = tcp_write(tpcb, DATA_ADDR + SEND_SIZE * 3, SEND_SIZE, copy);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_write: %d\r\n", err);
connected_pcb = NULL;
return;
}
err = tcp_output(tpcb);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_output: %d\r\n", err);
return;
}
}
/*clear the gpio intr flag*/
GpioIntrFlag = 0;
}
}
err_t recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
int copy = 3;
/* do not read the packet if we are not in ESTABLISHED state */
if (!p) {
tcp_close(tpcb);
tcp_recv(tpcb, NULL);
return ERR_OK;
}
/* indicate that the packet has been received */
tcp_recved(tpcb, p->len);
xil_printf("Received %d byte data.\r\n",p->len);
copynum = pbuf_copy_partial(p, (void *) tcp_rx_buf, p->len, 0);
internalTimeNumber = tcp_rx_buf[0]<<24 | tcp_rx_buf[1]<<16 | tcp_rx_buf[2]<<8 | tcp_rx_buf[3];
xil_printf("internalTimeNumber is %x\r\n",internalTimeNumber);
pbuf_free(p);
return ERR_OK;
}
//--------------------------------------------------
// TCP轮询的回调函数
//--------------------------------------------------
err_t tcp_poll_callback(void * arg, struct tcp_pcb * tpcb) {
tcp_poll_cnt++; //统计发送数据的次数
xil_printf("poll int:%d\r\n", tcp_poll_cnt);
return ERR_OK;
}
/*this fuction just used to count the tcp transmission times*/
static err_t tcp_sent_callback(void *arg, struct tcp_pcb *tpcb, u16_t len) {
tcp_trans_done++;
return ERR_OK;
}
static err_t tcp_connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err) {
xil_printf("tcp_server: Connection Accepted\r\n");
connected_pcb = tpcb; //存储连接的TCP状态
/* set the receive callback for this connection */
tcp_recv(connected_pcb, recv_callback);
tcp_client_connected = 1;
tcp_nagle_disable(connected_pcb);
//设置轮询回调函数tcp_sent_callback
tcp_poll(connected_pcb, tcp_poll_callback, 2);
xil_printf("Connect Success.\r\n");
return ERR_OK;
}
int tcp_server_init() {
struct tcp_pcb *pcb;
err_t err;
/* 创建新的TCP PCB */
pcb = tcp_new();
if (!pcb) {
xil_printf("txperf: Error creating PCB. Out of Memory\r\n");
return -1;
}
/* 绑定本地端口 */
err = tcp_bind(pcb, IP_ADDR_ANY, local_port);
if (err != ERR_OK) {
xil_printf("tcp_server: Unable to bind to port %d: err = %d\r\n",
local_port, err);
return -2;
}
tcp_client_connected = 0;
GpioIntrFlag = 0;
/* 监听连接 */
tcp_arg(pcb, NULL);
pcb = tcp_listen(pcb);
if (!pcb) {
xil_printf("tcp_server: Out of memory while tcp_listen\r\n");
return -3;
}
/* 设置accept回调函数 */
tcp_accept(pcb, tcp_connected_callback);
xil_printf("err value is:%d\r\n", err);
xil_printf("tcp connect is done.\r\n");
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。