cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] Sprintf with floating point number crashing into hard fault on STM32L476, working fine with STM32F091

jaromir sukuba
Associate II

I'm trying to use this C code

#include <stdio.h>
 
 
char arr[50];
int var_int;
float var_float;
 
int main (void)
{
while (1)
	{
	var_int++;
	sprintf (arr,"RES 1 %d",var_int);
	var_float = var_float + 0.1;
	sprintf (arr,"RES2 %d %f",var_int,var_float);
	}
}

on STM32L476 nucleo kit, with STM32CubeIDE (Version: 1.5.0 Build: 8698_20201117_1050 (UTC)), I'm using debugger, stepping the code and observing variables in expression window, however the code always fails at second sprintf call (the one with float variable). Before trying to execute second sprintf call I can see proper output from first sprintf call, as well as float variable with proper value

0693W00000GYFKSQA5.pngbut after second sprintf call I find the MCU executing endless loop after fault. The second sprintf managed to print output up to the floating point part.

0693W00000GYFJyQAP.pngI started fresh project for this, didn't modify linker file nor any project settings, with exception of ticking "Use float with with printf from newlib-nano"

0693W00000GYFMYQA5.pngI suppose hard fault from sprintf is not desired outcome, but I can't find any obvious way to fix this.

For sake of completeness, due to size, map file is pasted here https://pastebin.com/CeyVYpEP and build output follows.

12:32:09 **** Incremental Build of configuration Debug for project test476 ****
make -j2 all 
arm-none-eabi-gcc "../Src/main.c" -mcpu=cortex-m4 -std=gnu11 -g3 -DSTM32L4 -DSTM32 -DNUCLEO_L476RG -DDEBUG -DSTM32L476RGTx -c -I../Inc -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -MMD -MP -MF"Src/main.d" -MT"Src/main.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Src/main.o"
arm-none-eabi-gcc -o "test476.elf" @"objects.list"   -mcpu=cortex-m4 -T"/home/jarin/STM32CubeIDE/workspace_1.5.0/test476/STM32L476RGTX_FLASH.ld" --specs=nosys.specs -Wl,-Map="test476.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
Finished building target: test476.elf
 
arm-none-eabi-objdump -h -S  test476.elf  > "test476.list"
arm-none-eabi-objcopy  -O binary  test476.elf  "test476.bin"
Finished building: test476.bin
Finished building: test476.list
 
arm-none-eabi-size   test476.elf 
 
   text	   data	    bss	    dec	    hex	filename
  12520	    472	   1640	  14632	   3928	test476.elf
Finished building: default.size.stdout
 
 
12:32:10 Build Finished. 0 errors, 0 warnings. (took 830ms)

I tried the same source, with the same project setup modification (single checkbox to use float functions) on STM32F091 nucleo kit, where it works as I expect (second printf doesn't crash and prints desired output to char array).

Perhaps I'm overlooking something very obvious, but I can't see where the problem is.

Also, I'm aware of printf and similar functions not being the most fitting for small embedded systems, but this is probably not the topic here.

21 REPLIES 21

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. Here's a screenshot of the debug window.

Error message tells me "No source available for "<signal handler called>() at 0xfffffff9" , No source available for "_printf_float() at 0x8001418", No source available for "_svfprintf_r() at 0x800347e", No source available for "sprintf() at 0x8001bd0" . I can't seem to solve this problem, does anyone have any ideas?