CUBEMX+探索者STM32F407+LAN8720A+FreeRTOS+LWIP 回显例程(二)
一、前言
二、修改源码
1、添加‘tcpecho.c’与‘udpecho.c’文件
tcpecho.c:
#include "lwip/opt.h"#if LWIP_NETCONN#include "lwip/sys.h"
#include "lwip/api.h"#define TCPECHO_THREAD_PRIO ( tskIDLE_PRIORITY + 4 )/*-----------------------------------------------------------------------------------*/
static void tcpecho_thread(void *arg)
{struct netconn *conn, *newconn;err_t err, accept_err;struct netbuf *buf;void *data;u16_t len;LWIP_UNUSED_ARG(arg);/* Create a new connection identifier. */conn = netconn_new(NETCONN_TCP);if (conn!=NULL){ /* Bind connection to well known port number 7. */err = netconn_bind(conn, NULL, 7);if (err == ERR_OK){/* Tell connection to go into listening mode. */netconn_listen(conn);while (1) {/* Grab new connection. */accept_err = netconn_accept(conn, &newconn);/* Process the new connection. */if (accept_err == ERR_OK) {while (netconn_recv(newconn, &buf) == ERR_OK) {do {netbuf_data(buf, &data, &len);netconn_write(newconn, data, len, NETCONN_COPY);} while (netbuf_next(buf) >= 0);netbuf_delete(buf);}/* Close connection and discard connection identifier. */netconn_close(newconn);netconn_delete(newconn);}}}else{netconn_delete(newconn);printf(" can not bind TCP netconn\r\n");}}
}
/*-----------------------------------------------------------------------------------*/void tcpecho_init(void)
{
// sys_thread_t n;sys_thread_new("tcpecho_thread", tcpecho_thread, NULL, DEFAULT_THREAD_STACKSIZE, TCPECHO_THREAD_PRIO);
// printf("return=%d \r\n",n);
}
/*-----------------------------------------------------------------------------------*/#endif /* LWIP_NETCONN */
udpecho.c:
#include "lwip/opt.h"#if LWIP_NETCONN#include "lwip/api.h"
#include "lwip/sys.h"//#define UDPECHO_THREAD_PRIO ( tskIDLE_PRIORITY + 4 )
#define UDPECHO_THREAD_PRIO ( tskIDLE_PRIORITY + 6 )static struct netconn *conn;
static struct netbuf *buf;
static ip_addr_t *addr;
static unsigned short port;
/*-----------------------------------------------------------------------------------*/
static void udpecho_thread(void *arg)
{err_t err, recv_err;LWIP_UNUSED_ARG(arg);conn = netconn_new(NETCONN_UDP);if (conn!= NULL){err = netconn_bind(conn, IP_ADDR_ANY, 7);if (err == ERR_OK){while (1) {recv_err = netconn_recv(conn, &buf);if (recv_err == ERR_OK) {addr = netbuf_fromaddr(buf);port = netbuf_fromport(buf);netconn_connect(conn, addr, port);buf->addr.addr = 0;netconn_send(conn,buf);netbuf_delete(buf);}}}else{netconn_delete(conn);}}
}
/*-----------------------------------------------------------------------------------*/
void udpecho_init(void)
{sys_thread_new("udpecho_thread", udpecho_thread, NULL, DEFAULT_THREAD_STACKSIZE,UDPECHO_THREAD_PRIO );
}#endif /* LWIP_NETCONN */
2、修改‘freertos.c’文件
2.1、添加
/* USER CODE BEGIN PTD */
extern void tcpecho_init(void);
extern void udpecho_init(void);
/* USER CODE END PTD */
void StartDefaultTask(void const * argument)
{/* init code for LWIP */MX_LWIP_Init();/* USER CODE BEGIN StartDefaultTask */tcpecho_init();/* Infinite loop */for(;;){osDelay(1);}/* USER CODE END StartDefaultTask */
}
三、测试
四、工程文件
工程文件:链接:https://pan.baidu.com/s/1QbIaZ1pLTL3H5DUDJohBkA 提取码:lsb7