2025-01-27 6:24 AM - last edited on 2025-01-27 6:30 AM by Andrew Neil
Good morning, I made a project using stm32f407vet6, configuring the stm32f407vet6 to use ethernet network without freertos, I can ping it and it responds immediately, follow the code:
#include "main.h"
#include "lwip.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_RTC_Init(void);
/* USER CODE BEGIN 0 */
extern struct netif gnetif;
struct pbuf *p;
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init(); MX_LWIP_Init();
MX_RTC_Init();
while (1)
{
ethernet_input(p, &gnetif);
sys_check_timeouts();
}
/* USER CODE END 3 */
}
but now I want to use freertos and have a UDp server, but when configuring freertos, stm32f407vet6 gets stuck inside MX_LWIP_Init();, the firmware version for STmf4 is 1.28.1, here is the code:
#include "main.h"
#include "cmsis_os.h"
#include "lwip.h"
osThreadId_t defaultTaskHandle; const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for task_2 */
osThreadId_t task_2Handle;
const osThreadAttr_t task_2_attributes = {
.name = "task_2",
.stack_size = 1024 * 4,
.priority = (osPriority_t) osPriorityLow,
};
/* Definitions for task_3 */
osThreadId_t task_3Handle;
const osThreadAttr_t task_3_attributes = {
.name = "task_3",
.stack_size = 1024 * 4,
.priority = (osPriority_t) osPriorityLow,
};
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void StartDefaultTask(void *argument);
void StartTask02(void *argument);
void StartTask03(void *argument);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
osKernelInitialize();
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
task_2Handle = osThreadNew(StartTask02, NULL, &task_2_attributes);
task_3Handle = osThreadNew(StartTask03, NULL, &task_3_attributes);
osKernelStart();
while (1)
{
}
}
/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 50;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
}
void StartDefaultTask(void *argument)
{
/* init code for LWIP */
MX_LWIP_Init();
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
{
osDelay(5);
}
/* USER CODE END 5 */
}
void StartTask02(void *argument)
{
for(;;)
{
osDelay(10);
}
}
/
void StartTask03(void *argument)
{
for(;;)
{
osDelay(10);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM6) {
HAL_IncTick();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
2025-01-27 6:29 AM
welcome to the forum.
Please see How to write your question to maximize your chances to find a solution for how to properly post source code, and other tips.
@Mauricio_Vieira wrote:but now I want to use freertos and have a UDp server, but when configuring freertos, stm32f407vet6 gets stuck inside MX_LWIP_Init()
So where, exactly, does it get stuck?
2025-01-27 6:37 AM - last edited on 2025-01-27 6:41 AM by Andrew Neil
using the debugger, it gets stuck when executing this line:
netif_add( &gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input );
2025-01-27 6:44 AM
Again, please see How to insert source code.
So, again, step into that function to see where, exactly, it gets stuck.
Also see this on getting debug info out of LwIP:
Compare & contrast the working & non-working cases.
2025-07-16 9:55 AM
Hello @Mauricio_Vieira,
In your case, the application is stuck during the LwIP initialization due to insufficient stack size. You should increase the stack size to at least 256 words, preferably 512 words.
Regarding the UDP server application, you can apply the following modifications to your main file:
/* USER CODE BEGIN Includes */
#include "lwip/udp.h"
#include <string.h>
/* USER CODE END Includes */
...
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
/* init code for LWIP */
MX_LWIP_Init();
/* USER CODE BEGIN 5 */
const char* message = "Hello UDP message!\n\r";
osDelay(1000);
ip_addr_t PC_IPADDR;
IP_ADDR4(&PC_IPADDR, 192, 168, 0, 20);
struct udp_pcb* my_udp = udp_new();
udp_connect(my_udp, &PC_IPADDR, 55151);
struct pbuf* udp_buffer = NULL;
/* Infinite loop */
for (;;) {
osDelay(1000);
/* !! PBUF_RAM is critical for correct operation !! */
udp_buffer = pbuf_alloc(PBUF_TRANSPORT, strlen(message), PBUF_RAM);
if (udp_buffer != NULL) {
memcpy(udp_buffer->payload, message, strlen(message));
udp_send(my_udp, udp_buffer);
pbuf_free(udp_buffer);
}
}
/* USER CODE END 5 */
}
Let me know if you need further assistance.
Best regards,