2025-03-31 5:25 AM
Hello,
I know that STM doesn't manage AZ-RTOS, so this is more of an informational post to help others. In one of my projects, I'm using an STM32H7R3L8H7H MPU, on which I found this behavior very cumbersome. Some settings for my board are stored in a JSON file, which is read at the beginning of the main NetXDuo task.
// reading DCU settings from disk
_ReadStatus = Read_DCU_Settings_From_Disk();
if (_ReadStatus == ACCESSING_FILE_OK) {
_IP_ChangeStatus = Change_IP_And_Port();
if (_IP_ChangeStatus != NX_SUCCESS) {
// doing something here later...
}
}
The function Read_DCU_Settings_From_Disk() reads and parses the JSON file, and the function Change_IP_And_Port() modifies the actual settings.
The Change_IP_And_Port() looks like this:
UINT Change_IP_And_Port(void)
{
UINT status;
// Set the new IP address
status = nx_ip_interface_address_set(&IpInstance, 0, dcu_def.network_settings.ip_addr, dcu_def.network_settings.nw_mask);
if (status != NX_SUCCESS) {
return status;
}
// Stop the HTTP and FTP servers
if (HTTP_Server.nx_web_http_server_tcpserver.nx_tcpserver_listen_session != NX_NULL) {
Enable_HTTP_Server(false);
}
Enable_FTP_Server(false);
// Update the HTTP server port
HTTP_Server.nx_web_http_server_listen_port = dcu_def.network_settings.http_port;
// Update the FTP server control port
// FTP_Server.nx_ftp_server_client_list[0].nx_ftp_client_request_control_socket.nx_tcp_socket_port = dcu_def.network_settings.ftp_port;
// Restart the HTTP and FTP servers
Enable_HTTP_Server(dcu_def.network_settings.http_en);
Enable_FTP_Server(dcu_def.network_settings.ftp_en);
return NX_SUCCESS;
}
I use two additional functions to start or stop the HTTP and FTP servers.
Here is the one for the HTTP server:
UINT Enable_HTTP_Server(bool _enable)
{
UINT status = NX_SUCCESS;
ULONG _EventFlags = 0x00000000UL;
ULONG tmp_actual_events = 0x00000000UL;
_EventFlags |= TAGID_SE01_HTTP_SERVER_OK;
if (_enable) {
// Start the HTTP Server.
STM32_ERROR_CHECK(ERR_Nx_HTTP_ServerStart, nx_web_http_server_start(&HTTP_Server));
// ---------- set TAGID_SE_HTTP_SERVER_OK event flag ----------
status = tx_event_flags_set(&TAGID_status_event_group_01, TAGID_SE01_HTTP_SERVER_OK, TX_OR);
if (status != TX_SUCCESS || status != TX_SUCCESS) {
return status;
}
}
else {
STM32_ERROR_CHECK(ERR_Nx_HTTP_ServerStop, nx_web_http_server_stop(&HTTP_Server));
// ---------- clear TAGID_SE_HTTP_SERVER_OK event flag ----------
status = tx_event_flags_get(&TAGID_status_event_group_01, _EventFlags, TX_OR_CLEAR, &tmp_actual_events, EVENT_FLAG_CLEAR_TO);
if (status != TX_SUCCESS || status != TX_SUCCESS) {
return status;
}
}
return NX_SUCCESS;
}
And here is the one for the FTP server:
UINT Enable_FTP_Server(bool _enable)
{
UINT status = NX_SUCCESS;
ULONG _EventFlags = 0x00000000UL;
ULONG tmp_actual_events = 0x00000000UL;
_EventFlags |= TAGID_SE01_FTP_SERVER_OK;
if (_enable) {
// Start the FTP Server.
STM32_ERROR_CHECK(ERR_Nx_FTP_ServerStart, nx_ftp_server_start(&FTP_Server));
// ---------- set TAGID_SE_FTP_SERVER_OK event flag ----------
status = tx_event_flags_set(&TAGID_status_event_group_01, TAGID_SE01_FTP_SERVER_OK, TX_OR);
if (status != TX_SUCCESS || status != TX_SUCCESS) {
return status;
}
}
else {
STM32_ERROR_CHECK(ERR_Nx_FTP_ServerStop, nx_ftp_server_stop(&FTP_Server));
// ---------- clear TAGID_SE_FTP_SERVER_OK event flag ----------
status = tx_event_flags_get(&TAGID_status_event_group_01, _EventFlags, TX_OR_CLEAR, &tmp_actual_events, EVENT_FLAG_CLEAR_TO);
if (status != TX_SUCCESS || status != TX_SUCCESS) {
return status;
}
}
return NX_SUCCESS;
}
If I try to stop the HTTP server without checking it before, the function will return an error. This error is generated by the function _nx_tcpserver_stop in the nx_tcpserver.c file. You'll find at the very beginning of this function a check for an assigned TCP port:
/* Check whether listen is done. */
if(server_ptr -> nx_tcpserver_listen_port == 0)
return NX_TCPSERVER_FAIL;
It generates the abovementioned error. Since we're trying to stop the listening thread, this error is unnecessary because if no TCP is assigned, the listening thread will not be running.
The other issue I find somewhat strange is that the FTP control port is "hard-coded" with the value of the macro NX_FTP_SERVER_CONTROL_PORT like this:
/* Start listening on the FTP control socket. */
status = nx_tcp_server_socket_listen(ftp_server_ptr -> nx_ftp_server_ip_ptr, NX_FTP_SERVER_CONTROL_PORT,
&(ftp_server_ptr -> nx_ftp_server_client_list[0].nx_ftp_client_request_control_socket),
NX_FTP_MAX_CLIENTS, _nx_ftp_server_connection_present);
This is done at the beginning of the _nx_ftp_server_start function, preventing changes to the FTM server control port without modifying the firmware.
My Friends, stay suspicious and keep in mind:
"Always be yourself unless you can be a pirate. Then, always be a pirate."