Skip to main content
Graduate
January 13, 2025
Solved

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

  • January 13, 2025
  • 2 replies
  • 3282 views

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

    This topic has been closed for replies.
    Best answer by OskarP

     

    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

    2 replies

    Technical Moderator
    January 13, 2025

    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?

    OskarPAuthorAnswer
    Graduate
    January 13, 2025

     

    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

    Technical Moderator
    January 13, 2025

    @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();

     

    Super User
    January 13, 2025

    __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

     

    OskarPAuthor
    Graduate
    January 13, 2025

    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

    Technical Moderator
    January 14, 2025

    Yes I got the Hardfault with your latest software but without srand() call (I commented it). So the question is: are you sure srand() is what causing your original issue?

    OskarPAuthor
    Graduate
    January 15, 2025

    I think the srand problem and the new hard fault are two separate issues. The srand issue was caused by the linker script. I feel like the hard fault is a software bug where the &hdr->sipaddr gets placed unaligned in the microcontroller's memory.

    Should I mark this problem as solved and start a new thread for the hard fault issue?

    My Nucleo should arrive tomorrow. Then I can dive deeper into this problem.

    Technical Moderator
    January 15, 2025

    As they are a separate issue need to close this thread by Accepting as Solution the comment that guided you to the answer of the subject/question : "Issue with srand() causing exit in STM32H747 Project with FreeRTOS and lwIP"

    and open a new thread for this new issue with a new description.

    Thank you