2025-06-24 5:08 AM - last edited on 2025-06-24 8:40 AM by Imen.D
Hello everyone,
I'm currently working on Ethernet communication between my STM32H563ZI and a TP-Link router. They are connected by Ethernet, and I'm trying to ping the router with my STM32.
All is going on, except the DHCP who don't want to connect (I keep the code because my be it can be a solution)
But, when I see the status of icmp_ping, it tells me 0x29 (NX_NOT_SUCCESSFUL). I'v tried a lot of things, changed the Ethernet cable, nothing changed.
Do you have any idea form where it comes ?
Here is my app_threadx, app_netxduo and my main code.
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file app_threadx.c
* @author MCD Application Team
* @brief ThreadX applicative file
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "app_threadx.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "tx_api.h"
#include "tx_thread.h"
#include "app_netxduo.h"
#include "main.h"
#include "nx_api.h"
#include "nx_stm32_eth_driver.h"
#include <stdio.h>
#include <string.h>
#include "nxd_dhcp_client.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
// Configuration du thread principal
#define MAIN_THREAD_STACK_SIZE 4096
#define MAIN_THREAD_PRIORITY 10
#define PACKET_POOL_SIZE (1536 * 10)
#define PHY_BSR ((uint16_t)0x01)
#define PHY_LINKED_STATUS ((uint16_t)0x0004)
#define LAN8742A_PHY_ADDRESS 0x01
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
TX_THREAD main_thread;
ULONG main_thread_stack[MAIN_THREAD_STACK_SIZE / sizeof(ULONG)];
TX_BYTE_POOL byte_pool;
UCHAR byte_pool_buffer[18 * 1024];
NX_DHCP dhcp_client;
NX_IP ip_instance;
NX_PACKET_POOL packet_pool;
UCHAR ip_stack[2048];
extern UART_HandleTypeDef huart3;
extern ETH_HandleTypeDef heth;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
VOID MainThread_Entry(ULONG thread_input);
/* USER CODE END PFP */
/**
* @brief Application ThreadX Initialization.
* memory_ptr: memory pointer
* @retval int
*/
UINT App_ThreadX_Init(VOID *memory_ptr)
{
UINT ret = TX_SUCCESS;
/* USER CODE BEGIN App_ThreadX_MEM_POOL */
/* USER CODE END App_ThreadX_MEM_POOL */
/* USER CODE BEGIN App_ThreadX_Init */
ret = tx_byte_pool_create(&byte_pool, "byte_pool", byte_pool_buffer, sizeof(byte_pool_buffer));
if (ret != TX_SUCCESS)
{
HAL_UART_Transmit(&huart3, (uint8_t *)"Erreur byte_pool_create\r\n", 26, HAL_MAX_DELAY);
return ret;
}
/* creation of main thread */
ret = tx_thread_create(&main_thread, "Main Thread",
MainThread_Entry, 0,
main_thread_stack, sizeof(main_thread_stack),
MAIN_THREAD_PRIORITY, MAIN_THREAD_PRIORITY,
TX_NO_TIME_SLICE, TX_AUTO_START);
if (ret != TX_SUCCESS)
{
HAL_UART_Transmit(&huart3, (uint8_t *)"Erreur thread_create\r\n", 23, HAL_MAX_DELAY);
}
return ret;
/* USER CODE END App_ThreadX_Init */
return ret;
}
/**
* @brief Function that implements the kernel's initialization.
* None
* @retval None
*/
void MX_ThreadX_Init(void)
{
/* USER CODE BEGIN Before_Kernel_Start */
if (App_ThreadX_Init(NULL) != TX_SUCCESS)
{
HAL_UART_Transmit(&huart3, (uint8_t *)"Erreur App_ThreadX_Init\r\n", 26, HAL_MAX_DELAY);
}
/* USER CODE END Before_Kernel_Start */
tx_kernel_enter();
/* USER CODE BEGIN Kernel_Start_Error */
/* USER CODE END Kernel_Start_Error */
}
/* USER CODE BEGIN 1 */
VOID MainThread_Entry(ULONG thread_input)
{ HAL_UART_Transmit(&huart3, (uint8_t *)"Demarrage Thread\r\n", 19, HAL_MAX_DELAY);
nx_system_initialize(); // Initialise all NetX components
UINT status;
ULONG ip_address = 0;
ULONG netmask = 0;
NX_PACKET *response = NULL;
UCHAR *pointer = NULL;
char msg[128];
HAL_Delay(500);
// Vérification link PHY
uint32_t phy_status = 0;
HAL_ETH_ReadPHYRegister(&heth, LAN8742A_PHY_ADDRESS, PHY_BSR, &phy_status);
if (!(phy_status & PHY_LINKED_STATUS))
{
HAL_UART_Transmit(&huart3, (uint8_t *)"Link PHY DOWN\r\n", 16, HAL_MAX_DELAY);
return;
}
HAL_UART_Transmit(&huart3, (uint8_t *)"Link PHY OK\r\n", 14, HAL_MAX_DELAY);
// memory allocation for pool packet
if (tx_byte_allocate(&byte_pool, (VOID **)&pointer, PACKET_POOL_SIZE, TX_NO_WAIT) != TX_SUCCESS)
{
HAL_UART_Transmit(&huart3, (uint8_t *)"Error alloc packet pool\r\n", 27, HAL_MAX_DELAY);
return;
}
// Creation of packet pool
status = nx_packet_pool_create(&packet_pool, "NetX Packet Pool", 1536, pointer, PACKET_POOL_SIZE);
if (status != NX_SUCCESS)
{
HAL_UART_Transmit(&huart3, (uint8_t *)"Error creation packet_pool\r\n", 30, HAL_MAX_DELAY);
return;
}
// Creation of IP Instance
status = nx_ip_create(&ip_instance, "IP Instance", 0, 0, &packet_pool,
nx_stm32_eth_driver, ip_stack, sizeof(ip_stack), 1);
if (status != NX_SUCCESS)
{
HAL_UART_Transmit(&huart3, (uint8_t *)"nx_ip_create failed\r\n", 22, HAL_MAX_DELAY);
return;
}
else{
HAL_UART_Transmit(&huart3, (uint8_t *)"nx_ip_create succeed\r\n", 22, HAL_MAX_DELAY);
}
nx_ip_interface_attach(&ip_instance, "eth0", 0, 0, nx_stm32_eth_driver);
ULONG link_status;
status = nx_ip_interface_status_check(&ip_instance, 0, NX_IP_INTERFACE_LINK_ENABLED, &link_status, 10 * TX_TIMER_TICKS_PER_SECOND);
if (status != NX_SUCCESS) {
HAL_UART_Transmit(&huart3, (uint8_t *)"Interface DOWN\r\n", 17, HAL_MAX_DELAY);
return;
}
// Activation protocole network
static UCHAR arp_memory_area[1024];
nx_arp_enable(&ip_instance, arp_memory_area, sizeof(arp_memory_area));
nx_udp_enable(&ip_instance);
nx_icmp_enable(&ip_instance);
nx_tcp_enable(&ip_instance);
// DHCP
UCHAR *dhcp_stack;
status = tx_byte_allocate(&byte_pool, (VOID **)&dhcp_stack, 2048, TX_NO_WAIT);
if ( status != TX_SUCCESS)
{
snprintf(msg, sizeof(msg), "Error alloc DHCP 0x%X\r\n", status);
HAL_UART_Transmit(&huart3, (uint8_t *)msg, strlen(msg), HAL_MAX_DELAY);
return;
}
status = nx_dhcp_create(&dhcp_client, &ip_instance, "DHCP Client");
if (status != NX_SUCCESS) {
HAL_UART_Transmit(&huart3, (uint8_t *)"Error DHCP create\r\n", 21, HAL_MAX_DELAY);
return;
}
status = nx_dhcp_start(&dhcp_client);
if (status != NX_SUCCESS) {
HAL_UART_Transmit(&huart3, (uint8_t *)"Error DHCP start\r\n", 20, HAL_MAX_DELAY);
return;
}
else{
HAL_UART_Transmit(&huart3, (uint8_t *)"DHCP OK\r\n", 20, HAL_MAX_DELAY);
}
HAL_Delay(500);
if (dhcp_client.nx_dhcp_thread.tx_thread_id != TX_THREAD_ID)
{
HAL_UART_Transmit(&huart3, (uint8_t *)"Thread DHCP non lancé\r\n", 25, HAL_MAX_DELAY);
}
// Waiting an IP via DHCP
ULONG actual_status;
int timeout = 0;
do {
status = nx_ip_status_check(&ip_instance, NX_IP_ADDRESS_RESOLVED, &actual_status, 5 * TX_TIMER_TICKS_PER_SECOND); // 10 sec
if (status == NX_SUCCESS) break;
HAL_UART_Transmit(&huart3, (uint8_t *)"Waiting IP...\r\n", 16, HAL_MAX_DELAY);
snprintf(msg, sizeof(msg), "Connection failed: 0x%X\r\n", status);
HAL_UART_Transmit(&huart3, (uint8_t *)msg, strlen(msg), HAL_MAX_DELAY);
timeout++;
} while (timeout < 5);
if (status != NX_SUCCESS) {
HAL_UART_Transmit(&huart3, (uint8_t *)"Timeout IP\r\n", 13, HAL_MAX_DELAY);
//return;
}
//If it doesn't work, put a static IP
ip_address = IP_ADDRESS(192,168,0,120);
netmask = IP_ADDRESS(255,255,255,0);
nx_ip_address_set(&ip_instance, ip_address, netmask);
HAL_UART_Transmit(&huart3, (uint8_t *)"Adresse IP statique definie.\r\n", 31, HAL_MAX_DELAY);
status = nx_ip_address_get(&ip_instance, &ip_address, &netmask);
if (status == NX_SUCCESS)
{
snprintf(msg, sizeof(msg), "IP obtained: %lu.%lu.%lu.%lu\r\n",
(ip_address >> 24) & 0xFF,
(ip_address >> 16) & 0xFF,
(ip_address >> 8) & 0xFF,
ip_address & 0xFF);
HAL_UART_Transmit(&huart3, (uint8_t *)msg, strlen(msg), HAL_MAX_DELAY);
}
else
{
HAL_UART_Transmit(&huart3, (uint8_t *)"Erreur IP get\r\n", 16, HAL_MAX_DELAY);
}
nx_ip_gateway_address_set(&ip_instance, IP_ADDRESS(192,168,0,1));
// Ping
ULONG target_ip = IP_ADDRESS(192,168,0,1); // IP routeur
status = nx_icmp_ping(&ip_instance, target_ip, NULL, 0, &response, 5 * NX_IP_PERIODIC_RATE);
if (status == NX_SUCCESS)
{
HAL_UART_Transmit(&huart3, (uint8_t *)"Ping success\r\n", 14, HAL_MAX_DELAY);
nx_packet_release(response);
}
else
{
snprintf(msg, sizeof(msg), "Ping failed: 0x%X\r\n", status);
HAL_UART_Transmit(&huart3, (uint8_t *)msg, strlen(msg), HAL_MAX_DELAY);
}
HAL_UART_Transmit(&huart3, (uint8_t *)"Ethernet active\r\n", 17, HAL_MAX_DELAY);
}
/* USER CODE END 1 */
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file app_netxduo.c
* @author MCD Application Team
* @brief NetXDuo applicative file
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "app_netxduo.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "tx_api.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/**
* @brief Application NetXDuo Initialization.
* memory_ptr: memory pointer
* @retval int
*/
UINT MX_NetXDuo_Init(VOID *memory_ptr)
{
UINT ret = NX_SUCCESS;
TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;
/* USER CODE BEGIN App_NetXDuo_MEM_POOL */
// Vérifie que le pointeur mémoire est valide
if (memory_ptr == NULL)
{
return NX_PTR_ERROR;
}
(void)byte_pool; // Supprime le warning si byte_pool n'est pas encore utilisé
/* USER CODE END App_NetXDuo_MEM_POOL */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* USER CODE BEGIN MX_NetXDuo_Init */
/* USER CODE END MX_NetXDuo_Init */
return ret;
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ICACHE_Init();
MX_USART3_UART_Init();
MX_USART1_UART_Init();
MX_SPI2_Init();
// MX_SDMMC1_SD_Init();
MX_ETH_Init();
MX_RTC_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
MX_ThreadX_Init();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
//Blinking of the green light to say that the code is working
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // Green LED LOW
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Green LED LOW
HAL_Delay(500);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
}