Hello,
I need to make a simple TCP server with up to 5 clients connection on the base of the NUCLEO-H743ZI2 board. I use the STM32_Nucleo_H723ZG_FreeRTOS_TCP project as starting point. I was able to compile it and run in STM32Cube IDE. I added the blinking LED to this code and replaced vStartTCPEchoClientTasks_SingleTasks by my vStartTCPServerTask_SingleTask which is defined in tcp_server.c and looks next:
#include "FreeRTOS.h"
#include "FreeRTOS_IP.h"
#define MAX_NUM_CLIENTS 5
#define TCP_SERVER_PORT 5000
/* Stores the stack size passed into vStartSimpleTCPServerTasks() so it can be
reused when the server listening task creates tasks to handle connections. */
static uint16_t usUsedStackSize = 0;
/*
* Uses FreeRTOS+TCP to listen for incoming TCP connections, creating a task
* to handle each connection.
*/
void prvTcpServerTask( void *pvParameters );
/*
* Created by the connection listening task to handle a single connection.
*/
void prvServerConnectionInstance( void *pvParameters );
/*-----------------------------------------------------------*/
static uint32_t ulConnectionCount = 0; // Number of clients connected to server
/*-----------------------------------------------------------*/
void vStartTCPServerTask_SingleTask( uint16_t usTaskStackSize, UBaseType_t uxTaskPriority )
{
xTaskCreate( prvTcpServerTask, /* The function that implements the task. */
"TcpSrv", /* Just a text name for the task to aid debugging. */
usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
NULL, /* The task parameter, not used in this case. */
uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
NULL ); /* The task handle is not used. */
usUsedStackSize = usTaskStackSize;
}
/*-----------------------------------------------------------*/
void prvTcpServerTask( void *pvParameters )
{
struct freertos_sockaddr xClient, xBindAddress;
Socket_t xListeningSocket, xConnectedSocket;
socklen_t xSize = sizeof( xClient );
static const TickType_t xRxTimeOut = portMAX_DELAY;
static const TickType_t xTxTimeOut = portMAX_DELAY;
const BaseType_t xBacklog = 10;
/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET4, /* Or FREERTOS_AF_INET6 for IPv6. */
FREERTOS_SOCK_STREAM, /* SOCK_STREAM for TCP. */
FREERTOS_IPPROTO_TCP );
/* Check the socket was created. */
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
/* If FREERTOS_SO_RCVBUF or FREERTOS_SO_SNDBUF are to be used with
FreeRTOS_setsockopt() to change the buffer sizes from their default then do
it here!. (see the FreeRTOS_setsockopt() documentation. */
/* If ipconfigUSE_TCP_WIN is set to 1 and FREERTOS_SO_WIN_PROPERTIES is to
be used with FreeRTOS_setsockopt() to change the sliding window size from
its default then do it here! (see the FreeRTOS_setsockopt()
documentation. */
/* Set a time out so accept() will just wait for a connection. */
/* Set send and receive time outs. */
FreeRTOS_setsockopt( xListeningSocket,
0,
FREERTOS_SO_RCVTIMEO,
&xRxTimeOut,
sizeof( xRxTimeOut ) );
/* Bind the socket to the port that the client task will send to, then
listen for incoming connections. */
memset( &xBindAddress, 0, sizeof(xBindAddress) );
xBindAddress.sin_port = FreeRTOS_htons(TCP_SERVER_PORT);
xBindAddress.sin_family = FREERTOS_AF_INET4; /* FREERTOS_AF_INET6 to be used for IPv6 */
/* Bind the socket to the port that the client RTOS task will send to. */
FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
/* Set the socket into a listening state so it can accept connections.
The maximum number of simultaneous connections is limited to 20. */
FreeRTOS_listen( xListeningSocket, xBacklog );
for( ;; )
{
/* Wait for incoming connections. */
xConnectedSocket = FreeRTOS_accept( xListeningSocket, &xClient, &xSize );
configASSERT( xConnectedSocket != FREERTOS_INVALID_SOCKET );
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
/* Spawn a RTOS task to handle the connection. */
xReturned = xTaskCreate( prvServerConnectionInstance,
"TCPServer",
usUsedStackSize,
( void * ) xConnectedSocket,
tskIDLE_PRIORITY,
&xHandle );
configASSERT( xReturned != pdPASS );
if( xReturned == pdPASS )
{
/* The task was created. Use the task's handle to delete the task. */
// configPRINTF( ( "xTaskCreate Error \n" ) );
vTaskDelete( xHandle );
}
}
}
/*-----------------------------------------------------------*/
void prvServerConnectionInstance( void *pvParameters )
{
...
}
I just removed some code which not important.
And it is worked - I was able to connect by 2 clients and even send receive some data from them.
But at some point, my microcontroller started to hang up when the server trying to create a task to handle the connection. And I do not understand why this happened. My code was worked before and as I remember I did not change configuration.
So any helpful idea or recommendation is welcome!