cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with srand() causing exit in STM32H747 Project with FreeRTOS and lwIP

OskarP
Associate

Hello everyone,

I’m encountering an issue with the srand() function in my STM32H747 project, and I would appreciate your insights on what might be causing this problem.

Hardware:

  • MCU: STM32H747IIT6
  • Custom board

Software setup:

  • CubeMX-generated project
  • Cube CLT version: 1.17
  • Firmware version: 1.12.1

Problem Description

Whenever I call the srand() function, my code immediately ends up in the exit handler.

To troubleshoot, I created a minimal project with no additional middleware (bare minimum setup), and in that setup, the srand() function works without any issues.

However, in my main project where I need both lwIP and FreeRTOS, calling srand() causes an exit. The project was created following this guide:
How to Create a Project for STM32H7 with Ethernet and lwIP Stack.

I made one modification to the guide: instead of using the default OS wrapper, I selected CMSIS_V2.

Currently, I am only using the M7 core.

What I’ve Tried So Far

  • Minimal project without lwIP and FreeRTOS -> srand(12345) works fine.
  • Project with lwIP and FreeRTOS -> srand(12345) causes an exit.

I’ve attached an image showing the callstack where the issue occurs. I hope this helps in diagnosing the problem.

Here’s a link to my code: GitHub Repository

Does anyone have any ideas on what might be causing this issue? Could it be related to the use of CMSIS_V2, a conflict with FreeRTOS, or possibly a memory issue?

Thank you in advance for your help!

Best regards,
Oskar

Callstack.JPG

18 REPLIES 18
SofLit
ST Employee

Hello @OskarP and welcome to the community,

1- I didn't see any srand() call in the project you shared in github.

2- Could you please try with STM32CubeIDE and check if you have the issue?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
PS: Be polite in your reply. Otherwise, it will be reported as inappropriate and you will be permanently blacklisted from my help/support.
KnarfB
Principal III

__assert_func should (try to) write soemthing to stderr. You may catch the output by setting a breakpoint in your/default _write syscall implementation.

my guess:

NOTES
<<rand>> and <<srand>> are unsafe for multi-threaded applications.
<<rand_r>> is thread-safe and should be used instead.

 

PS: Does ST provide easily obtainable (and buildable) official libc sources to reproduce such potential libc issues?

I use GitHub - bminor/newlib: Unofficial mirror of sourceware newlib repository. Updated daily. which should be close.

hth

KnarfB

 

 

Hello SofLit,

Thank you for your quick response!

I call the srand() function in the main.c file of the CM7, specifically at line 362.
I tried building the project using Cube IDE with the same .ioc file. With this setup, the srand() function works, but I encounter a hard fault, as shown in the attached images.

I noticed that the flash script is slightly different. There is a second definition of ._user_heap_stack added in the flash.ld:

 

._user_heap_stack :  
{  
  . = ALIGN(8);  
  PROVIDE ( end = . );  
  PROVIDE ( _end = . );  
  . = . + _Min_Heap_Size;  
  . = . + _Min_Stack_Size;  
  . = ALIGN(8);  
} > RAM_D1

 

 

After copying the new flash.ld file, the srand() function call works in the project generated by CubeMX as well.

Hardfault Cube IDE.JPG

 

 

 

 

 

 

 

Function Call.JPG

Hello KnarfB,

Thank you for your suggestions. I believe lwIP uses the rand() function, so I need to call srand() to initialize the seed value. I will make sure to call it before starting FreeRTOS. That should resolve the issue with being thread unsafe at least for the srand() call.

Best regards,

OskarP


@OskarP wrote:

I call the srand() function in the main.c file of the CM7, specifically at line 362.

Yes because I regenerated your code and you put srand() outside USER CODE START / USER CODE END location.

Anyway ..

Running your example with CubeIDE I got that hardfault but nothing related to srand() call. The hardfault is caused by  MX_SDMMC1_SD_Init() and may be the subsequent calls.

I commented out these lines and no more hardfalt occured again:

MX_SDMMC1_SD_Init();
MX_USB_OTG_HS_PCD_Init();
MX_FATFS_Init();

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
PS: Be polite in your reply. Otherwise, it will be reported as inappropriate and you will be permanently blacklisted from my help/support.

Sorry SofLit, that one's on me. However, even if I comment out the three mentioned function calls, I still hit the hard fault at the same location. Since this is a new topic for me, do you have any suggestions on how I can debug this further?

Best regards

OskarP.


@OskarP wrote:

However, even if I comment out the three mentioned function calls, I still hit the hard fault at the same location.


Did you use CubeIDE for that?

Just made a change in CubeMx to run the example on Nucleo board. Decreased the system clock to 400MHz, and selected SMPS as power config and I'm reaching the osDelay(1) in:

 

void StartDefaultTask(void *argument)
{
  /* init code for LWIP */
	  srand(12345);
  MX_LWIP_Init();
  /* USER CODE BEGIN 5 */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END 5 */
}

 

I don't think these changes impact the functionality.

I'am attaching the project where I reverted the modifications for you (I just kept the system at 400MHz). Please test again with CubeIDE.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
PS: Be polite in your reply. Otherwise, it will be reported as inappropriate and you will be permanently blacklisted from my help/support.

Yes I'm still in the CubeIDE.

I built the project with your attached .ioc file and commented out the following lines:

 

//MX_SDMMC1_SD_Init();
//MX_USB_OTG_HS_PCD_Init();
//MX_FATFS_Init();

 

However, I’m still encountering the same fault. The Fault Analyzer suggests an "Attempt to perform an unaligned access".

Hardfault Cube IDE 2.JPG

 

 

 

 

 

 

 

FaultAnalyser.JPG

That's weird.. I cannot help you if I couldn't reproduce the behavior.. What CubeIDE version are you using. 1.17.0?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
PS: Be polite in your reply. Otherwise, it will be reported as inappropriate and you will be permanently blacklisted from my help/support.