主文件
#include "lwip/opt.h"
#if LWIP_TCP
#include "tcpecho_raw.h"
#include "lwip/timeouts.h"
#include "lwip/init.h"
#include "netif/ethernet.h"
#include "enet_ethernetif.h"
#include "board.h"
#include "fsl_device_registers.h"
#include "pin_mux.h"
#include "clock_config.h"
#define configIP_ADDR0 192
#define configIP_ADDR1 168
#define configIP_ADDR2 0
#define configIP_ADDR3 102
#define configNET_MASK0 255
#define configNET_MASK1 255
#define configNET_MASK2 255
#define configNET_MASK3 0
#define configGW_ADDR0 192
#define configGW_ADDR1 168
#define configGW_ADDR2 0
#define configGW_ADDR3 1
#define configMAC_ADDR \
{ \
0x02, 0x12, 0x13, 0x10, 0x15, 0x11 \
}
#define EXAMPLE_PHY_ADDRESS BOARD_ENET0_PHY_ADDRESS
#define EXAMPLE_CLOCK_NAME kCLOCK_CoreSysClk
#ifndef EXAMPLE_NETIF_INIT_FN
#define EXAMPLE_NETIF_INIT_FN ethernetif0_init
#endif
void SysTick_Handler(void)
{
time_isr();
}
int main(void)
{
struct netif netif;
ip4_addr_t netif_ipaddr, netif_netmask, netif_gw;
#if defined(FSL_FEATURE_SOC_LPC_ENET_COUNT) && (FSL_FEATURE_SOC_LPC_ENET_COUNT > 0)
mem_range_t non_dma_memory[] = NON_DMA_MEMORY_ARRAY;
#endif
ethernetif_config_t enet_config = {
.phyAddress = EXAMPLE_PHY_ADDRESS,
.clockName = EXAMPLE_CLOCK_NAME,
.macAddress = configMAC_ADDR,
#if defined(FSL_FEATURE_SOC_LPC_ENET_COUNT) && (FSL_FEATURE_SOC_LPC_ENET_COUNT > 0)
.non_dma_memory = non_dma_memory,
#endif
};
SYSMPU_Type *base = SYSMPU;
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
base->CESR &= ~SYSMPU_CESR_VLD_MASK;
time_init();
IP4_ADDR(&netif_ipaddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3);
IP4_ADDR(&netif_netmask, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3);
IP4_ADDR(&netif_gw, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3);
lwip_init();
netif_add(&netif, &netif_ipaddr, &netif_netmask, &netif_gw, &enet_config, EXAMPLE_NETIF_INIT_FN, ethernet_input);
netif_set_default(&netif);
netif_set_up(&netif);
tcpecho_raw_init();
PRINTF("\r\n************************************************\r\n");
PRINTF(" TCP Echo example\r\n");
PRINTF("************************************************\r\n");
PRINTF(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&netif_ipaddr)[0], ((u8_t *)&netif_ipaddr)[1],
((u8_t *)&netif_ipaddr)[2], ((u8_t *)&netif_ipaddr)[3]);
PRINTF(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&netif_netmask)[0], ((u8_t *)&netif_netmask)[1],
((u8_t *)&netif_netmask)[2], ((u8_t *)&netif_netmask)[3]);
PRINTF(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&netif_gw)[0], ((u8_t *)&netif_gw)[1],
((u8_t *)&netif_gw)[2], ((u8_t *)&netif_gw)[3]);
PRINTF("************************************************\r\n");
while (1)
{
ethernetif_input(&netif);
sys_check_timeouts();
}
}
#endif
应用层代码
#include "lwip/opt.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "tcpecho_raw.h"
#if LWIP_TCP && LWIP_CALLBACK_API
static struct tcp_pcb *tcpecho_raw_pcb;
enum tcpecho_raw_states
{
ES_NONE = 0, ES_ACCEPTED, ES_RECEIVED, ES_CLOSING
};
struct tcpecho_raw_state
{
u8_t state;
u8_t retries;
struct tcp_pcb *pcb;
struct pbuf *p;
};
static void tcpecho_raw_free(struct tcpecho_raw_state *es)
{
if (es != NULL)
{
if (es->p)
{
pbuf_free(es->p);
}
mem_free(es);
}
}
static void tcpecho_raw_close(struct tcp_pcb *tpcb,
struct tcpecho_raw_state *es)
{
tcp_arg(tpcb, NULL);
tcp_sent(tpcb, NULL);
tcp_recv(tpcb, NULL);
tcp_err(tpcb, NULL);
tcp_poll(tpcb, NULL, 0);
tcpecho_raw_free(es);
tcp_close(tpcb);
}
static void tcpecho_raw_send(struct tcp_pcb *tpcb, struct tcpecho_raw_state *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 (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 tcpecho_raw_error(void *arg, err_t err)
{
struct tcpecho_raw_state *es;
LWIP_UNUSED_ARG(err);
es = (struct tcpecho_raw_state*) arg;
tcpecho_raw_free(es);
}
static err_t tcpecho_raw_poll(void *arg, struct tcp_pcb *tpcb)
{
err_t ret_err;
struct tcpecho_raw_state *es;
es = (struct tcpecho_raw_state*) arg;
if (es != NULL)
{
if (es->p != NULL)
{
tcpecho_raw_send(tpcb, es);
}
else
{
if (es->state == ES_CLOSING)
{
tcpecho_raw_close(tpcb, es);
}
}
ret_err = ERR_OK;
}
else
{
tcp_abort(tpcb);
ret_err = ERR_ABRT;
}
return ret_err;
}
static err_t tcpecho_raw_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
struct tcpecho_raw_state *es;
LWIP_UNUSED_ARG(len);
es = (struct tcpecho_raw_state*) arg;
es->retries = 0;
if (es->p != NULL)
{
tcpecho_raw_send(tpcb, es);
}
else
{
if (es->state == ES_CLOSING)
{
tcpecho_raw_close(tpcb, es);
}
}
return ERR_OK;
}
static err_t tcpecho_raw_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p,err_t err)
{
struct tcpecho_raw_state *es;
err_t ret_err;
LWIP_ASSERT("arg != NULL", arg != NULL);
es = (struct tcpecho_raw_state*) arg;
if (p == NULL)
{
es->state = ES_CLOSING;
if (es->p == NULL)
{
tcpecho_raw_close(tpcb, es);
}
else
{
tcpecho_raw_send(tpcb, es);
}
ret_err = ERR_OK;
}
else if (err != ERR_OK)
{
if (p != NULL)
{
pbuf_free(p);
}
ret_err = err;
}
else if (es->state == ES_ACCEPTED)
{
es->state = ES_RECEIVED;
es->p = p;
tcpecho_raw_send(tpcb, es);
ret_err = ERR_OK;
}
else if (es->state == ES_RECEIVED)
{
if (es->p == NULL)
{
es->p = p;
tcpecho_raw_send(tpcb, es);
}
else
{
struct pbuf *ptr;
ptr = es->p;
pbuf_cat(ptr, p);
}
ret_err = ERR_OK;
}
else
{
tcp_recved(tpcb, p->tot_len);
pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;
}
static err_t tcpecho_raw_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{
err_t ret_err;
struct tcpecho_raw_state *es;
LWIP_UNUSED_ARG(arg);
if ((err != ERR_OK) || (newpcb == NULL))
{
return ERR_VAL;
}
tcp_setprio(newpcb, TCP_PRIO_MIN);
es = (struct tcpecho_raw_state*) mem_malloc(
sizeof(struct tcpecho_raw_state));
if (es != NULL)
{
es->state = ES_ACCEPTED;
es->pcb = newpcb;
es->retries = 0;
es->p = NULL;
tcp_arg(newpcb, es);
tcp_recv(newpcb, tcpecho_raw_recv);
tcp_err(newpcb, tcpecho_raw_error);
tcp_poll(newpcb, tcpecho_raw_poll, 0);
tcp_sent(newpcb, tcpecho_raw_sent);
ret_err = ERR_OK;
}
else
{
ret_err = ERR_MEM;
}
return ret_err;
}
void tcpecho_raw_init(void)
{
tcpecho_raw_pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
if (tcpecho_raw_pcb != NULL)
{
err_t err;
err = tcp_bind(tcpecho_raw_pcb, IP_ANY_TYPE, 7);
if (err == ERR_OK)
{
tcpecho_raw_pcb = tcp_listen(tcpecho_raw_pcb);
tcp_accept(tcpecho_raw_pcb, tcpecho_raw_accept);
}
else
{
}
}
else
{
}
}