里面提到的资源和写完的例程放出来,后续我会放到百度云里
https://download.csdn.net/download/qq_27620407/85443546
1、CubeMX配置
ETH 选择RMII模式 PHY Address设置 0 RX Mode:Polling Mode 使用的不是官方芯片的话就设置 user PHY 自己命个名,无所谓是啥 下面Extened是抄的别人的,不知道啥意思 打开中断 使能LWIP 版本应该默认是最新的 关闭 TCP_QUEUE_OOSEQ
2、验证
当生成模版后,给ethernetif.c中的 HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle) 中添加LAN8720A的使能代码,然后在主函数的死循环中添加 MX_LWIP_Process()函数就可以实现在电脑上ping通STM32了
MX_LWIP_Process一定要加!!!
HAL_GPIO_WritePin(ETH_RST_GPIO_Port,ETH_RST_Pin,GPIO_PIN_RESET);
HAL_Delay(50);
HAL_GPIO_WritePin(ETH_RST_GPIO_Port,ETH_RST_Pin,GPIO_PIN_SET);
while (1)
{
MX_LWIP_Process();
}
3、让网络连接更实用
都有注释,看看和生成的程序里不一样的地方就行, 主要就是完成等待DHCP获取IP地址并打印出来
int main(void)
{
static uint8_t DHCP_Dait_Time=0,DHCP_Dait_Time_MAX=20;
struct netif gnetif;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_TIM6_Init();
MX_USART1_UART_Init();
MX_LWIP_Init();
HAL_TIM_Base_Start(&htim6);
__HAL_TIM_ENABLE_IT(&htim6,TIM_IT_UPDATE);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
HAL_UART_Receive_DMA(&huart1, USART1_DMA_RBUFF, USART1_DMA_RBUFF_LENTH);
printf("F107_ETH_LAN8720A %s %s\r\n",__DATE__,__TIME__);
printf("lwIP Initing...\r\n");
while(Netif_Init_Flag==0)
{
printf("lwIP Init failed!\r\n");
MX_LWIP_Init();
HAL_Delay(1200);
printf("Retrying...\r\n");
}
printf("lwIP Inited\r\n");
wait_DHCP:
printf("等待DHCP分配\r\n");
if(gnetif.ip_addr.addr!=0)
{
printf("网卡en的MAC地址为:................%.2X:%.2X:%.2X:%.2X:%.2X:%.2X\r\n",gnetif.hwaddr[0],gnetif.hwaddr[1],gnetif.hwaddr[2],gnetif.hwaddr[3],gnetif.hwaddr[4],gnetif.hwaddr[5]);
lwipdev.ip[3]=(uint8_t)(gnetif.ip_addr.addr>>24);
lwipdev.ip[2]=(uint8_t)(gnetif.ip_addr.addr>>16);
lwipdev.ip[1]=(uint8_t)(gnetif.ip_addr.addr>>8);
lwipdev.ip[0]=(uint8_t)(gnetif.ip_addr.addr);
printf("通过DHCP获取到IP地址..............%d.%d.%d.%d\r\n",lwipdev.ip[0],lwipdev.ip[1],lwipdev.ip[2],lwipdev.ip[3]);
lwipdev.netmask[3]=(uint8_t)(gnetif.netmask.addr>>24);
lwipdev.netmask[2]=(uint8_t)(gnetif.netmask.addr>>16);
lwipdev.netmask[1]=(uint8_t)(gnetif.netmask.addr>>8);
lwipdev.netmask[0]=(uint8_t)(gnetif.netmask.addr);
printf("通过DHCP获取到子网掩码............%d.%d.%d.%d\r\n",lwipdev.netmask[0],lwipdev.netmask[1],lwipdev.netmask[2],lwipdev.netmask[3]);
lwipdev.gateway[3]=(uint8_t)(gnetif.gw.addr>>24);
lwipdev.gateway[2]=(uint8_t)(gnetif.gw.addr>>16);
lwipdev.gateway[1]=(uint8_t)(gnetif.gw.addr>>8);
lwipdev.gateway[0]=(uint8_t)(gnetif.gw.addr);
printf("通过DHCP获取到的默认网关..........%d.%d.%d.%d\r\n",lwipdev.gateway[0],lwipdev.gateway[1],lwipdev.gateway[2],lwipdev.gateway[3]);
}else{
DHCP_Dait_Time++;
if(DHCP_Dait_Time<DHCP_Dait_Time_MAX)
{
MX_LWIP_Process();
HAL_Delay(500);
goto wait_DHCP;
}else{
printf("DHCP 获取IP失败\r\n");
}
}
while (1)
{
4、app
这里server、Client和UDP是在官方基础上和原子例程中改的 https://www.stmcu.com.cn/design_resource/ 搜索TCP 选择 STM32 MCU STSW_STM32070_LwIP_TCP_IP_STM32F4x7堆栈示例
下载后名为 STSW_STM32070_V1.1.1 STM32F4x7_ETH_LwIP_V1.1.1\Project\Standalone目录下有各种例程,都是放进工程稍微改一下变量命名就能直接用的 里面的
lwipopts.h文件在移植LWIP时也很有用 后续怎么移植的我不再赘述,其实就是改完编译错误后加一些自己需要的功能逻辑,目前写的也不是很完善,也够用了 以下是我改的一些app 首先是一个前置文件 LWIP_COM
LWIP_COM
.c
#include "LWIP_COM.h"
uint8_t Netif_Init_Flag=0;
__lwip_dev lwipdev;
uint32_t Remote_IP_ADDR0=192,
Remote_IP_ADDR1=168,
Remote_IP_ADDR2=2,
Remote_IP_ADDR3=96;
uint16_t Remote_PORT=8089;
uint16_t UDP_Server_PORT=8081;
uint16_t UDP_Remote_PORT=8082;
uint16_t UDP_Client_PORT=8083;
.h
#ifndef __LWIP_COM_H__
#define __LWIP_COM_H__
#include "User_include.h"
typedef struct
{
uint8_t mac[6];
uint8_t remoteip[4];
uint16_t remoteport;
uint8_t ip[4];
uint8_t netmask[4];
uint8_t gateway[4];
volatile uint8_t dhcpstatus;
}__lwip_dev;
extern __lwip_dev lwipdev;
extern uint32_t Remote_IP_ADDR0,
Remote_IP_ADDR1,
Remote_IP_ADDR2,
Remote_IP_ADDR3;
extern uint16_t Remote_PORT;
extern uint16_t UDP_Remote_PORT;
extern uint16_t UDP_Server_PORT;
extern uint16_t UDP_Client_PORT;
#endif
tcp_Server
.c文件
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include <stdio.h>
#include <string.h>
#include "tcp_echoserver.h"
uint8_t tcp_server_recvbuf[TCP_SERVER_RX_BUFSIZE];
uint8_t tcp_server_sendbuf[TCP_SERVER_RX_BUFSIZE];
uint16_t tcp_server_rec_STA=0;
uint8_t tcp_server_flag;
#if LWIP_TCP
static struct tcp_pcb *tcp_echoserver_pcb;
static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t err);
static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
static void tcp_echoserver_error(void *arg, err_t err);
static err_t tcp_echoserver_poll(void *arg, struct tcp_pcb *tpcb);
static err_t tcp_echoserver_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
static void tcp_echoserver_send(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es);
static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es);
extern void tcp_pcb_purge(struct tcp_pcb *pcb);
extern struct tcp_pcb *tcp_active_pcbs;
extern struct tcp_pcb *tcp_tw_pcbs;
void Tcp_Sever_Cloose(void)
{
printf("未完善,需要客户端断开才能关闭,否则还能收发数据\r\n");
tcp_echoserver_connection_close(tcp_echoserver_pcb,0);
}
void TCP_Server_Send(uint8_t *Data_Dend,uint32_t Len)
{
int i=0;
for(i=0; i<Len; i++)
{
tcp_server_sendbuf[i]=Data_Dend[i];
}
tcp_server_flag|=1<<7;
}
void tcp_echoserver_init(void)
{
tcp_echoserver_pcb = tcp_new();
if (tcp_echoserver_pcb != NULL)
{
err_t err;
err = tcp_bind(tcp_echoserver_pcb, IP_ADDR_ANY, 7);
if (err == ERR_OK)
{
tcp_echoserver_pcb = tcp_listen(tcp_echoserver_pcb);
tcp_accept(tcp_echoserver_pcb, tcp_echoserver_accept);
}
else
{
memp_free(MEMP_TCP_PCB, tcp_echoserver_pcb);
printf("Can not bind pcb\n");
}
}
else
{
printf("Can not create new pcb\n");
}
}
static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{
err_t ret_err;
struct tcp_echoserver_struct *es;
LWIP_UNUSED_ARG(arg);
LWIP_UNUSED_ARG(err);
tcp_setprio(newpcb, TCP_PRIO_MIN);
es = (struct tcp_echoserver_struct *)mem_malloc(sizeof(struct tcp_echoserver_struct));
if (es != NULL)
{
es->state = ES_ACCEPTED;
es->pcb = newpcb;
es->p = NULL;
tcp_arg(newpcb, es);
tcp_recv(newpcb, tcp_echoserver_recv);
tcp_err(newpcb, tcp_echoserver_error);
tcp_poll(newpcb, tcp_echoserver_poll, 1);
lwipdev.remoteip[0]=newpcb->remote_ip.addr&0xff;
lwipdev.remoteip[1]=(newpcb->remote_ip.addr>>8)&0xff;
lwipdev.remoteip[2]=(newpcb->remote_ip.addr>>16)&0xff;
lwipdev.remoteip[3]=(newpcb->remote_ip.addr>>24)&0xff;
ret_err = ERR_OK;
}
else
{
tcp_echoserver_connection_close(newpcb, es);
ret_err = ERR_MEM;
}
return ret_err;
}
static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
err_t ret_err;
uint32_t data_len = 0;
struct pbuf *q;
struct tcp_echoserver_struct *es;
LWIP_ASSERT("arg != NULL",arg != NULL);
es=(struct tcp_echoserver_struct *)arg;
if(p==NULL)
{
es->state=ES_CLOSING;
es->p=p;
ret_err=ERR_OK;
} else if(err!=ERR_OK)
{
if(p)pbuf_free(p);
ret_err=err;
} else if((es->state==ES_ACCEPTED)||(es->state==ES_RECEIVED))
{
if(p!=NULL)
{
memset(tcp_server_recvbuf,0,TCP_SERVER_RX_BUFSIZE);
for(q=p; q!=NULL; q=q->next)
{
if(q->len > (TCP_SERVER_RX_BUFSIZE-data_len)) memcpy(tcp_server_recvbuf+data_len,q->payload,(TCP_SERVER_RX_BUFSIZE-data_len));
else memcpy(tcp_server_recvbuf+data_len,q->payload,q->len);
data_len += q->len;
if(data_len > TCP_SERVER_RX_BUFSIZE) break;
}
tcp_server_flag|=1<<6;
lwipdev.remoteip[0]=tpcb->remote_ip.addr&0xff;
lwipdev.remoteip[1]=(tpcb->remote_ip.addr>>8)&0xff;
lwipdev.remoteip[2]=(tpcb->remote_ip.addr>>16)&0xff;
lwipdev.remoteip[3]=(tpcb->remote_ip.addr>>24)&0xff;
tcp_recved(tpcb,p->tot_len);
#if TCP_Server_RX_Debug
printf("TCP_Sever_Re %d.%d.%d.%d:port %d > ",lwipdev.remoteip[0],lwipdev.remoteip[1],lwipdev.remoteip[2],lwipdev.remoteip[3],tpcb->remote_port&0xFFFF);
USART1_Send_Len(tcp_server_recvbuf,data_len);
printf("\r\n");
#endif
tcp_server_rec_STA=data_len;
pbuf_free(p);
ret_err=ERR_OK;
}
} else
{
tcp_recved(tpcb,p->tot_len);
es->p=NULL;
pbuf_free(p);
ret_err=ERR_OK;
}
return ret_err;
}
static void tcp_echoserver_error(void *arg, err_t err)
{
struct tcp_echoserver_struct *es;
LWIP_UNUSED_ARG(err);
es = (struct tcp_echoserver_struct *)arg;
if (es != NULL)
{
mem_free(es);
}
}
static err_t tcp_echoserver_poll(void *arg, struct tcp_pcb *tpcb)
{
err_t ret_err;
struct tcp_echoserver_struct *es;
es=(struct tcp_echoserver_struct *)arg;
if(es!=NULL)
{
if(tcp_server_flag&(1<<7))
{
es->p=pbuf_alloc(PBUF_TRANSPORT,strlen((char*)tcp_server_sendbuf),PBUF_POOL);
pbuf_take(es->p,(char*)tcp_server_sendbuf,strlen((char*)tcp_server_sendbuf));
tcp_echoserver_sent(es,tpcb,es->p->tot_len);
tcp_server_flag&=~(1<<7);
if(es->p!=NULL)pbuf_free(es->p);
} else if(es->state==ES_CLOSING)
{
tcp_echoserver_connection_close(tpcb,es);
}
ret_err=ERR_OK;
} else
{
tcp_abort(tpcb);
ret_err=ERR_ABRT;
}
return ret_err;
}
static err_t tcp_echoserver_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
struct tcp_echoserver_struct *es;
LWIP_UNUSED_ARG(len);
es = (struct tcp_echoserver_struct *)arg;
if(es->p != NULL)
{
tcp_echoserver_send(tpcb, es);
}
else
{
if(es->state == ES_CLOSING)
tcp_echoserver_connection_close(tpcb, es);
}
return ERR_OK;
}
static void tcp_echoserver_send(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es)
{
struct pbuf *ptr;
err_t wr_err = ERR_OK;
while ((wr_err == ERR_OK) &&
(es->p != NULL) &&
(es->p->len <= tcp_sndbuf(tpcb)))
{
ptr = es->p;
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);
#if TCP_Server_TX_Debug
printf("TCP Server Send:");
USART1_Send_Len(ptr->payload, ptr->len);
printf("\r\n");
#endif
if (wr_err == ERR_OK)
{
u16_t plen;
plen = ptr->len;
es->p = ptr->next;
if(es->p != NULL)
{
pbuf_ref(es->p);
}
pbuf_free(ptr);
tcp_recved(tpcb, plen);
}
else if(wr_err == ERR_MEM)
{
es->p = ptr;
}
else
{
}
}
}
static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es)
{
tcp_arg(tpcb, NULL);
tcp_sent(tpcb, NULL);
tcp_recv(tpcb, NULL);
tcp_err(tpcb, NULL);
tcp_poll(tpcb, NULL, 0);
if (es != NULL)
{
mem_free(es);
}
tcp_close(tpcb);
}
#endif
.h
#ifndef __TCP_ECHOSERVER_H__
#define __TCP_ECHOSERVER_H__
enum tcp_echoserver_states
{
ES_NONE = 0,
ES_ACCEPTED,
ES_RECEIVED,
ES_CLOSING
};
struct tcp_echoserver_struct
{
u8_t state;
struct tcp_pcb *pcb;
struct pbuf *p;
};
void TCP_Server_Send(uint8_t *Data_Dend,uint32_t Len);
void tcp_echoserver_init(void);
void Tcp_Sever_Cloose(void);
#define TCP_SERVER_RX_BUFSIZE 200
extern uint8_t tcp_server_recvbuf[TCP_SERVER_RX_BUFSIZE];
extern uint16_t tcp_server_rec_STA;
extern uint8_t tcp_server_flag;
#define Transparent_transmission_mode 0
#define TCP_Server_RX_Debug 1
#define TCP_Server_TX_Debug 0
#endif
CLient
.c
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "main.h"
#include "memp.h"
#include <stdio.h>
#include <string.h>
#include "tcp_echoclient.h"
#if LWIP_TCP
uint8_t tcp_client_recvbuf[TCP_CLIENT_RX_BUFSIZE];
uint8_t *tcp_client_sendbuf="Apollo STM32F4/F7 TCP Client send data\r\n";
uint8_t tcp_client_flag;
u8_t recev_buf[50];
struct tcp_pcb *echoclient_pcb;
enum echoclient_states
{
ES_NOT_CONNECTED = 0,
ES_CONNECTED,
ES_RECEIVED,
ES_CLOSING,
};
struct echoclient
{
enum echoclient_states state;
struct tcp_pcb *pcb;
struct pbuf *p_tx;
};
static err_t tcp_echoclient_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
static void tcp_echoclient_connection_close(struct tcp_pcb *tpcb, struct echoclient * es);
static err_t tcp_echoclient_poll(void *arg, struct tcp_pcb *tpcb);
static err_t tcp_echoclient_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
static void tcp_echoclient_send(struct tcp_pcb *tpcb, struct echoclient * es);
static err_t tcp_echoclient_connected(void *arg, struct tcp_pcb *tpcb, err_t err);
void TCP_Client_Close(void)
{
struct echoclient *es = NULL;
es = (struct echoclient *)mem_malloc(sizeof(struct echoclient));
if (es != NULL)
{
es->state = ES_CLOSING;
es->pcb = echoclient_pcb;
}
tcp_echoclient_connection_close(echoclient_pcb,es);
}
void tcp_echoclient_connect(void)
{
ip_addr_t DestIPaddr;
echoclient_pcb = tcp_new();
if (echoclient_pcb != NULL)
{
IP4_ADDR(&DestIPaddr, Remote_IP_ADDR0, Remote_IP_ADDR1, Remote_IP_ADDR2, Remote_IP_ADDR3 );
tcp_connect(echoclient_pcb,&DestIPaddr,Remote_PORT,tcp_echoclient_connected);
}
else
{
memp_free(MEMP_TCP_PCB, echoclient_pcb);
#ifdef SERIAL_DEBUG
printf("\n\r can not create tcp pcb");
#endif
}
}
void Tcp_Client_Send(uint8_t *Data,uint32_t Data_Len)
{
struct echoclient *es = NULL;
es = (struct echoclient *)mem_malloc(sizeof(struct echoclient));
es->state = ES_CONNECTED;
es->pcb = echoclient_pcb;
es->p_tx = pbuf_alloc(PBUF_TRANSPORT, Data_Len, PBUF_POOL);
if (es->p_tx)
{
pbuf_take(es->p_tx, (char*)Data, strlen((char*)Data));
}
tcp_echoclient_send(es->pcb,es);
}
static err_t tcp_echoclient_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
struct echoclient *es = NULL;
if (err == ERR_OK)
{
es = (struct echoclient *)mem_malloc(sizeof(struct echoclient));
if (es != NULL)
{
es->state = ES_CONNECTED;
es->pcb = tpcb;
es->p_tx = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_POOL);
if (es->p_tx)
{
tcp_arg(tpcb, es);
tcp_recv(tpcb, tcp_echoclient_recv);
tcp_sent(tpcb, tcp_echoclient_sent);
tcp_poll(tpcb, tcp_echoclient_poll, 1);
return ERR_OK;
}
}
else
{
tcp_echoclient_connection_close(tpcb, es);
return ERR_MEM;
}
}
else
{
tcp_echoclient_connection_close(tpcb, es);
}
return err;
}
static err_t tcp_echoclient_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
struct echoclient *es;
err_t ret_err;
uint32_t data_len=0;
struct pbuf *q;
LWIP_ASSERT("arg != NULL",arg != NULL);
es = (struct echoclient *)arg;
if (p == NULL)
{
es->state = ES_CLOSING;
if(es->p_tx == NULL)
{
tcp_echoclient_connection_close(tpcb, es);
printf("we're done sending, close connection\r\n");
}
else
{
tcp_echoclient_send(tpcb, es);
}
ret_err = ERR_OK;
}
else if(err != ERR_OK)
{
pbuf_free(p);
ret_err = err;
}
else if(es->state == ES_CONNECTED)
{
if(p!=NULL)
{
memset(tcp_client_recvbuf,0,TCP_CLIENT_RX_BUFSIZE);
for(q=p; q!=NULL; q=q->next)
{
if(q->len > (TCP_CLIENT_RX_BUFSIZE-data_len)) memcpy(tcp_client_recvbuf+data_len,q->payload,(TCP_CLIENT_RX_BUFSIZE-data_len));
else memcpy(tcp_client_recvbuf+data_len,q->payload,q->len);
data_len += q->len;
if(data_len > TCP_CLIENT_RX_BUFSIZE) break;
}
tcp_client_flag|=1<<6;
tcp_recved(tpcb,p->tot_len);
lwipdev.remoteip[0]=tpcb->remote_ip.addr&0xff;
lwipdev.remoteip[1]=(tpcb->remote_ip.addr>>8)&0xff;
lwipdev.remoteip[2]=(tpcb->remote_ip.addr>>16)&0xff;
lwipdev.remoteip[3]=(tpcb->remote_ip.addr>>24)&0xff;
lwipdev.remoteport=tpcb->remote_port;
printf("TCP_Client Recved: %d.%d.%d.%d:port %d >> ",lwipdev.remoteip[0],lwipdev.remoteip[1],lwipdev.remoteip[2],lwipdev.remoteip[3],lwipdev.remoteport);
USART1_Send_Len(tcp_client_recvbuf,data_len);
printf("\r\n");
pbuf_free(p);
ret_err=ERR_OK;
}
}
else
{
tcp_recved(tpcb, p->tot_len);
pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;
}
static void tcp_echoclient_send(struct tcp_pcb *tpcb, struct echoclient * es)
{
struct pbuf *ptr;
err_t wr_err = ERR_OK;
while ((wr_err == ERR_OK) &&
(es->p_tx != NULL) &&
(es->p_tx->len <= tcp_sndbuf(tpcb)))
{
ptr = es->p_tx;
wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);
if (wr_err == ERR_OK)
{
es->p_tx = ptr->next;
if(es->p_tx != NULL)
{
pbuf_ref(es->p_tx);
}
pbuf_free(ptr);
}
else if(wr_err == ERR_MEM)
{
es->p_tx = ptr;
}
else
{
}
}
}
static err_t tcp_echoclient_poll(void *arg, struct tcp_pcb *tpcb)
{
err_t ret_err;
struct echoclient *es;
es = (struct echoclient*)arg;
if (es != NULL)
{
if (es->p_tx != NULL)
{
tcp_echoclient_send(tpcb, es);
}
else
{
if(es->state == ES_CLOSING)
{
tcp_echoclient_connection_close(tpcb, es);
}
}
ret_err = ERR_OK;
}
else
{
tcp_abort(tpcb);
ret_err = ERR_ABRT;
}
return ret_err;
}
static err_t tcp_echoclient_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
struct echoclient *es;
LWIP_UNUSED_ARG(len);
es = (struct echoclient *)arg;
if(es->p_tx != NULL)
{
tcp_echoclient_send(tpcb, es);
}
return ERR_OK;
}
static void tcp_echoclient_connection_close(struct tcp_pcb *tpcb, struct echoclient * es )
{
tcp_recv(tpcb, NULL);
tcp_sent(tpcb, NULL);
tcp_poll(tpcb, NULL,0);
if (es != NULL)
{
mem_free(es);
}
tcp_close(tpcb);
}
#endif
.h
#ifndef __UDP_ECHOCLIENT_H__
#define __UDP_ECHOCLIENT_H__
void udp_echoclient_connect(void);
#endif
UDP
.c
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#include "lwip/tcp.h"
#include <string.h>
#include <stdio.h>
#include "LWIP_COM.h"
#include "udp_echoserver.h"
uint8_t udp_demo_recvbuf[UDP_DEMO_RX_BUFSIZE];
struct udp_pcb *upcb;
ip_addr_t Remote_addr;
void udp_echoserver_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p,const ip_addr_t *addr, u16_t port);
void udp_echoserver_init(void)
{
err_t err;
upcb = udp_new();
printf("udp_new IP_ADDR_ANY Port:%d\r\n",UDP_Server_PORT);
if (upcb)
{
err = udp_bind(upcb, IP_ADDR_ANY, UDP_Server_PORT);
if(err == ERR_OK)
{
udp_recv(upcb, udp_echoserver_receive_callback, NULL);
}
else
{
udp_remove(upcb);
printf("can not bind pcb");
}
}
else
{
printf("can not create pcb");
}
}
void udp_echoserver_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
uint8_t UDP_Server_remoteip[4];
uint32_t data_len = 0;
struct pbuf *q;
printf("udp_server_receive:");
UDP_Server_remoteip[0]=addr->addr&0xff;
UDP_Server_remoteip[1]=(addr->addr>>8)&0xff;
UDP_Server_remoteip[2]=(addr->addr>>16)&0xff;
UDP_Server_remoteip[3]=(addr->addr>>24)&0xff;
printf("from %d.%d.%d.%d:%d >",UDP_Server_remoteip[0],UDP_Server_remoteip[1],UDP_Server_remoteip[2],UDP_Server_remoteip[3],port);
IP4_ADDR(&Remote_addr, UDP_Server_remoteip[0], UDP_Server_remoteip[1], UDP_Server_remoteip[2], UDP_Server_remoteip[3] );
if(p!=NULL)
{
memset(udp_demo_recvbuf,0,UDP_DEMO_RX_BUFSIZE);
for(q=p;q!=NULL;q=q->next)
{
if(q->len > (UDP_DEMO_RX_BUFSIZE-data_len)) memcpy(udp_demo_recvbuf+data_len,q->payload,(UDP_DEMO_RX_BUFSIZE-data_len));
else memcpy(udp_demo_recvbuf+data_len,q->payload,q->len);
data_len += q->len;
if(data_len > UDP_DEMO_RX_BUFSIZE) break;
}
USART1_Send_Len(udp_demo_recvbuf,data_len);
printf(" Len %d\r\n",p->len);
}
pbuf_free(p);
}
void UDP_Send(uint8_t *Data,uint8_t Len)
{
struct pbuf *ptr;
ptr=pbuf_alloc(PBUF_TRANSPORT,Len,PBUF_POOL);
if(Remote_addr.addr)
{
printf("Send %d.%d.%d.%d:%d > ",(Remote_addr.addr>>0)&0xFF,(Remote_addr.addr>>8)&0xFF,(Remote_addr.addr>>16)&0xFF,(Remote_addr.addr>>24)&0xFF,UDP_Remote_PORT);
USART1_Send_Len(Data,Len);
printf("\r\n");
udp_connect(upcb, &Remote_addr, UDP_Remote_PORT);
if(ptr)
{
ptr->payload=(void*)Data;
udp_send(upcb,ptr);
pbuf_free(ptr);
}
udp_disconnect(upcb);
}else{
pbuf_free(ptr);
printf("先用远端主机给单片机发送一条信息\r\n");
}
}
void UDP_Close(void)
{
udp_disconnect(upcb);
udp_remove(upcb);
}
.h
#ifndef __ECHO_H__
#define __ECHO_H__
#define UDP_DEMO_RX_BUFSIZE 200
extern struct udp_pcb *upcb;
extern uint8_t udp_demo_recvbuf[UDP_DEMO_RX_BUFSIZE];
void udp_echoserver_init(void);
void UDP_Send(uint8_t *Data,uint8_t Len);
void UDP_Close(void);
#endif
|