参考源码
1,最小系统工程;
2,FreeRTOS官方代码包:
FreeRTOSv202210.01-LTS.zip
还有,官方支持包:FreeRTOSv202212.01.zip
FreeRTOSv202212.01.zip\FreeRTOSv202212.01\FreeRTOS\Demo\CORTEX_M4F_STM32F407ZG-SK
3,LWIP官方代码包:
lwip-2.1.3.zip
官方支持包:contrib-2.1.0.zip
4,GD官方例程:
GD32F4xx_Demo_Suites_V2.6.2.rar
参考工程:GD32F4xx_Demo_Suites_V2.6.2\GD32470Z_EVAL_Demo_Suites\Projects\26_ENET\Projects\FreeRTOS_tcpudp\src
移植FreeRTOS操作系统
核心代码
从GD32F4xx_Demo_Suites_V2.6.2.rar\GD32F4xx_Demo_Suites_V2.6.2\GD32470Z_EVAL_Demo_Suites\Projects\26_ENET\Projects\FreeRTOS_tcpudp\inc 拷贝FreeRTOSConfig.h文件。
跑个简单的程序:
int main(void)
{
/* configure 4 bits pre-emption priority */
nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
/* start toogle LED task every 250ms */
xTaskCreate(led_task, "LED", configMINIMAL_STACK_SIZE, NULL, LED_TASK_PRIO, NULL);
/* start scheduler */
vTaskStartScheduler();
while(1){
}
}
void led_task(void * pvParameters)
{
for( ;; ){
/* toggle LED3 each 250ms */
gd_eval_led_toggle(LED3);
vTaskDelay(250);
}
}
将代码添加到工程中,并配置文件包含路径,可以正常闪灯。
移植LWIP
核心代码
初始化过程及驱动
参见上一篇LWIP移植过程文档。
移植TcpEchoServer
核心代码
参考:LWIP例程:
\contrib-2.1.0\apps\tcpecho
#include "tcpecho.h"
#include "lwip/opt.h"
#if LWIP_NETCONN
#include "lwip/sys.h"
#include "lwip/api.h"
/*-----------------------------------------------------------------------------------*/
static void tcpecho_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err;
LWIP_UNUSED_ARG(arg);
/* Create a new connection identifier. */
/* Bind connection to well known port number 7. */
conn = netconn_new(NETCONN_TCP);
netconn_bind(conn, IP_ADDR_ANY, 7);
LWIP_ERROR("tcpecho: invalid conn", (conn != NULL), return;);
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1) {
/* Grab new connection. */
err = netconn_accept(conn, &newconn);
/*printf("accepted new connection %p\n", newconn);*/
/* Process the new connection. */
if (err == ERR_OK) {
struct netbuf *buf;
void *data;
u16_t len;
while ((err = netconn_recv(newconn, &buf)) == ERR_OK) {
/*printf("Recved\n");*/
do {
netbuf_data(buf, &data, &len);
err = netconn_write(newconn, data, len, NETCONN_COPY);
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
/*printf("Got EOF, looping\n");*/
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
/*-----------------------------------------------------------------------------------*/
void tcpecho_init(void)
{
sys_thread_new("tcpecho_thread", tcpecho_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
}
或者ST例程:
STSW_STM32070_V1.1.1.zip\STM32F4x7_ETH_LwIP_V1.1.1\Project\FreeRTOS\udptcp_echo_server_netconn\src\tcpecho.c
#include "lwip/opt.h"
#if LWIP_NETCONN
#include "lwip/sys.h"
#include "lwip/api.h"
#define TCPECHO_THREAD_PRIO ( tskIDLE_PRIORITY + 5 )
/*-----------------------------------------------------------------------------------*/
static void tcpecho_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err, accept_err;
struct netbuf *buf;
void *data;
u16_t len;
err_t recv_err;
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)
{
recv_err = netconn_recv(newconn, &buf);
while ( recv_err == ERR_OK)
{
do
{
netbuf_data(buf, &data, &len);
netconn_write(newconn, data, len, NETCONN_COPY);
}
while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
recv_err = netconn_recv(newconn, &buf);
}
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
else
{
netconn_delete(newconn);
printf(" can not bind TCP netconn");
}
}
else
{
printf("can not create TCP netconn");
}
}
/*-----------------------------------------------------------------------------------*/
void tcpecho_init(void)
{
sys_thread_new("tcpecho_thread", tcpecho_thread, NULL, DEFAULT_THREAD_STACKSIZE, TCPECHO_THREAD_PRIO);
}
/*-----------------------------------------------------------------------------------*/