ZYNQ连载08-Lwip网络组件

1. 添加Lwip包

ZYNQ连载08-Lwip网络组件-LMLPHP

2. Lwip配置

我这里关闭ipv6和dhcp。
ZYNQ连载08-Lwip网络组件-LMLPHP

3. tcp客户端

#include "include/my_tcp.h"
#include "lwip/ip.h"

#define THREAD_STACKSIZE 1024
#define SERVER_PORT 8000
#define SERVER_ADDR "192.168.3.190"

#include "../easylogger/include/my_elog.h"
#define LOG_TAG "my_tcp"


static void tcp_client_test(void *pvParameters)
{
	int fd;
	struct sockaddr_in server_addr;
	char msg[] = "tcp client test!\n";

	/* 1. 创建套接字 */
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd < 0) {
		log_e("socket fail");
        return;
	}

	/* 2. 设置 */
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(SERVER_PORT);
	server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR);

	/* 3. 连接 */
	connect(fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));

	while(1) {
		send(fd, (char *)msg, sizeof(msg), 0);
		vTaskDelay(200);
	}

	return;
}


void my_lwip_thread(void *pvParameters)
{
    struct netif *netif;
    unsigned char mac_ethernet_address[] = { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };
    ip_addr_t ipaddr, netmask, gw;

    xil_printf("\r\n\r\n");

	/* 1. LWIP初始化  */
	lwip_init();

    /* 2. 设置静态IP */
    IP4_ADDR(&ipaddr, 192, 168, 3, 10);
    IP4_ADDR(&netmask, 255, 255, 255,  0);
    IP4_ADDR(&gw, 192, 168, 3, 1);
	log_d("IP: %d.%d.%d.%d\n\r", ip4_addr1(&ipaddr), ip4_addr2(&ipaddr), ip4_addr3(&ipaddr), ip4_addr4(&ipaddr));
	log_d("MASK: %d.%d.%d.%d\n\r", ip4_addr1(&netmask), ip4_addr2(&netmask), ip4_addr3(&netmask), ip4_addr4(&netmask));
	log_d("GW: %d.%d.%d.%d\n\r", ip4_addr1(&gw), ip4_addr2(&gw), ip4_addr3(&gw), ip4_addr4(&gw));


    /* 3. 添加网络接口 */
    if (!xemac_add(netif, &ipaddr, &netmask, &gw, mac_ethernet_address, PLATFORM_EMAC_BASEADDR)) {
    	log_e("xemac_add error");
    	return;
    }
    netif_set_default(netif);

    /* 4. 打开网络接口 */
    netif_set_up(netif);

    /* 5. 启动包接收线程 */
    sys_thread_new("xemacif_input_thread", (void(*)(void*))xemacif_input_thread, netif, THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);

    /* 6. 创建TCP线程 */
    sys_thread_new("tcp_client_test", tcp_client_test, 0, THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);

    /* 7. 删除自己 */
    vTaskDelete(NULL);

    return;
}
/* main.c */
sys_thread_new("my_lwip_thread", my_lwip_thread, NULL, 1024, DEFAULT_THREAD_PRIO);

4. 测试

ZYNQ连载08-Lwip网络组件-LMLPHP

10-29 22:17