(SOLVED) Sprintf with Floating Point Number enters infinitive loop in Bare-Metal Programming
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 06:07 AM - edited ‎2023-11-20 04:06 AM
The problem is about Sprintf with floating point number on STM32L452RET6P.
Although all settings are correct, sprintf doesn't work with floating point number only. Although it works on stm32cubeMX, it doesn't work on bare-metal program. It loops endlessly in the Default_Handler function. I'm about to go crazy.
First sprintf properly works. But the second one does not. After second sprintf, I've traced back the code on the debugger and I can see that the DefaultHandler executes and traps the board in an Infinite_Loop as a result of WWDG_IRQHandler misfiring.
Error message tells me "No source available for "<signal handler called>() at 0xfffffff9" , No source available for "_printf_float() at 0x8001124", No source available for "_svfprintf_r() at 0x8004c8e", No source available for "sprintf() at 0x8001d08" . I can't seem to solve this problem, do you have any idea?
Program is below:
Here is linker flags.
-mcpu=cortex-m4 -T"/Users/fatih/STM32CubeIDE/workspace_1.2.0/microprocessors/2023/Bare_Metal/Bare_Metal_HCSR04/STM32L452RETXP_FLASH.ld" --specs=nosys.specs -Wl,-Map="${BuildArtifactFileBaseName}.map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -u _printf_float -Wl,--start-group -lc -lm -Wl,--end-group
Compiler Settings:
-mcpu=cortex-m4 -std=gnu11 -DDEBUG -DSTM32L452RETxP -DSTM32L4 -DSTM32 -DSTM32L452xx -c -I../Inc -I"/Users/fatih/STM32CubeIDE/workspace_1.2.0/microprocessors/2023/Bare_Metal/Bare_Metal_HCSR04/Drivers/CMSIS/Core/Include" -I"/Users/fatih/STM32CubeIDE/workspace_1.2.0/microprocessors/2023/Bare_Metal/Bare_Metal_HCSR04/Drivers/CMSIS/Device/ST/STM32L4xx/Include" -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -fcyclomatic-complexity --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb
Here's a screenshot of the debug window.
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 01:08 PM
Typically done in SystemInit() from system_stm32l4xx
void SystemInit(void)
{
/* FPU settings ------------------------------------------------------------*/
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
SCB->CPACR |= ((3UL << 20U)|(3UL << 22U)); /* set CP10 and CP11 Full Access */
#endif
....
.c
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 08:01 AM
Maybe this has nothing to do with printf() as such, and it ends up in default signal handler because of unhandled interrupt?
What's the content of SCB->ICSR register at that moment?
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 08:12 AM
repeat the first sprintf after the first one and comment out the second and see if it works.
i mean like that:
sprintf(str, "i=%d", i);
sprintf(str, "i=%d", i);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 09:00 AM - edited ‎2023-11-20 04:06 AM
It works with integer numbers properly like what i said before.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 09:11 AM - edited ‎2023-11-20 04:07 AM
I couldnt understand why same program in stm32cubemx works properly.
Before:
After:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 10:29 AM - edited ‎2023-11-20 04:07 AM
when I disable FPU, it works:)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 12:13 PM
Okay so VECTACTIVE is 3, which is HardFault.
> when I disable FPU, it works:)
That would then point to FPU not being switched on in the startup code.
I don't know how this is accomplished through the magic of IDEs - maybe it selects a different startup source file (.s)? Snippet of startup file I see in the screenshot above doesn't look like there is any code (conditional or function call) to do that.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 01:08 PM
Typically done in SystemInit() from system_stm32l4xx
void SystemInit(void)
{
/* FPU settings ------------------------------------------------------------*/
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
SCB->CPACR |= ((3UL << 20U)|(3UL << 22U)); /* set CP10 and CP11 Full Access */
#endif
....
.c
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 03:09 PM
Thanks, you are great one!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-02 03:10 PM
yeap you are right, the problem is gone!
