cancel
Showing results for 
Search instead for 
Did you mean: 

How to ping a STM32F107 using Lwip ?

Romano1
Associate III

Hello I wan't to use the ethernet of my MCU with using Lwip module. For the moment I've connect a etehernet cable between my computer and my ethernet board I've used some example of ethernet example for test the ethernet communication. But I don't arrive to ping my board after settings the differnts parameters (GW address, Mask Address, IP Address).

I've also debug my program for try to understand why I can't ping my card and I've remark that when I run my code and when I enter in my function MX_LWIP_Init() it's the function netif_set_down wich is called and not the function netif_set_up. I've the impression that netif is not well configured.

Here is my Main.c file.

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 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 "main.h"
#include "lwip.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* 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 -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
extern struct netif gnetif;
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
	u32_t local_IP;
	uint32_t local_IP_t;
	uint32_t local_IP_tab[4]={0};
  /* 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_LWIP_Init();
  /* USER CODE BEGIN 2 */
  local_IP = gnetif.ip_addr.addr;
  local_IP_tab[0] = local_IP & 0xff;
  local_IP_tab[1] = (local_IP >> 8) & 0xff;
  local_IP_tab[2] = (local_IP >> 16) & 0xff;
  local_IP_tab[3] = local_IP >> 24;
  local_IP_t = ip4_addr_get_u32(netif_ip4_addr(&gnetif));
 
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  /* Read a received packet from the Ethernet buffers and send it
	         to the lwIP for handling */
	  ethernetif_input(&gnetif);
 
	  /* Handle timeouts */
	  sys_check_timeouts();
  }
  /* USER CODE END 3 */
}
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure the Systick interrupt time
  */
  __HAL_RCC_PLLI2S_ENABLE();
}
 
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
 
}
 
/* USER CODE BEGIN 4 */
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

And here is my Lwip.c file with the function MX_Lwip_Init :

/**
  * LwIP initialization function
  */
void MX_LWIP_Init(void)
{
  /* IP addresses initialization */
  IP_ADDRESS[0] = 10;
  IP_ADDRESS[1] = 11;
  IP_ADDRESS[2] = 132;
  IP_ADDRESS[3] = 70;
  NETMASK_ADDRESS[0] = 255;
  NETMASK_ADDRESS[1] = 255;
  NETMASK_ADDRESS[2] = 252;
  NETMASK_ADDRESS[3] = 0;
  GATEWAY_ADDRESS[0] = 10;
  GATEWAY_ADDRESS[1] = 11;
  GATEWAY_ADDRESS[2] = 135;
  GATEWAY_ADDRESS[3] = 254;
 
/* USER CODE BEGIN IP_ADDRESSES */
/* USER CODE END IP_ADDRESSES */
 
  /* Initilialize the LwIP stack without RTOS */
  lwip_init();
 
  /* IP addresses initialization without DHCP (IPv4) */
  IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
 
  /* add the network interface (IPv4/IPv6) without RTOS */
  netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
 
  /* Registers the default network interface */
  netif_set_default(&gnetif);
 
  if (netif_is_link_up(&gnetif))
  {
    /* When the netif is fully configured this function must be called */
    netif_set_up(&gnetif);
  }
  else
  {
    /* When the netif link is down this function must be called */
    netif_set_down(&gnetif);
  }
 
  /* Set the link callback function, this function is called on change of link status*/
  netif_set_link_callback(&gnetif, ethernetif_update_config);
 
  /* Create the Ethernet link handler thread */
 
/* USER CODE BEGIN 3 */
 
/* USER CODE END 3 */
}
 
 

1 ACCEPTED SOLUTION

Accepted Solutions
Romano1
Associate III

I found the problem, like block comments say : "if The SWR is not performed if the ETH_RX_CLK or the ETH_TX_CLK are not available, please check your external PHY or the IO configuration". I wanted to use MII but my development board (EasyMx PRO V7) doesn't support it, cause of the ethernet controller which is use (LAN8720A). Effectively this ethernet controller support only RMII and not MII so the RX_CLK and TX_CLK does'nt exist in RMII and that's why my the code below doesn't go ahead and return me HAL_TIMEOUT. It was a hardware problem.

View solution in original post

3 REPLIES 3
Imen.D
ST Employee

Hello @Romano​,

You can check your application with the LwIP example available in the STM32CubeF1 MCU package: STM32Cube_FW_F1_Vx.x.x\Projects\STM3210C_EVAL\Applications\LwIP\

Please follow the instructions in the paragraph ''B.3.6 LwIP'' in this User manual, and check this post with fixed issues: how-to-make-ethernet-and-lwip-working-on-stm32

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Romano1
Associate III

Thanks for your answer @Imen DAHMEN​ but I couldn't find my problem on your posts. My problem come from the function HAL_ETH_Init wich return me HAL_TIMEOUT and so the ethernet can't set up.

This loop return me HAL_TIMEOUT :

/* Wait for software reset */
  while (((heth->Instance)->DMABMR & ETH_DMABMR_SR) != (uint32_t)RESET)
  {
    /* Check for the Timeout */
    if ((HAL_GetTick() - tickstart) > ETH_TIMEOUT_SWRESET)
    {
      heth->State = HAL_ETH_STATE_TIMEOUT;
 
      /* Process Unlocked */
      __HAL_UNLOCK(heth);
 
      /* Note: The SWR is not performed if the ETH_RX_CLK or the ETH_TX_CLK are
         not available, please check your external PHY or the IO configuration */
      return HAL_TIMEOUT;
    }
  }

Romano1
Associate III

I found the problem, like block comments say : "if The SWR is not performed if the ETH_RX_CLK or the ETH_TX_CLK are not available, please check your external PHY or the IO configuration". I wanted to use MII but my development board (EasyMx PRO V7) doesn't support it, cause of the ethernet controller which is use (LAN8720A). Effectively this ethernet controller support only RMII and not MII so the RX_CLK and TX_CLK does'nt exist in RMII and that's why my the code below doesn't go ahead and return me HAL_TIMEOUT. It was a hardware problem.