2025-01-13 12:45 AM - edited 2025-01-13 12:46 AM
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:
Software setup:
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.
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
2025-01-13 01:26 AM
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?
2025-01-13 01:47 AM - edited 2025-01-13 01:47 AM
__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:
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
2025-01-13 02:18 AM - edited 2025-01-13 02:19 AM
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.
2025-01-13 02:29 AM
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
2025-01-13 02:54 AM
@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();
2025-01-13 03:08 AM
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.
2025-01-13 03:23 AM - edited 2025-01-13 03:36 AM
@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.