Skip to main content
Visitor II
July 30, 2021
Solved

Using the Cube: with Visual Studio and Nano lib supporting printing of floats. sprintf crashes if the CDC USB output is not plugged into the PC

  • July 30, 2021
  • 2 replies
  • 892 views

unplug my CDC USB output cable and

sprintf( string, "%.2f ",myFloat); will crash

plug it back in to the PC and without any comms, it works.

could it be a memory leak if the CDC driver is not handshook ?

anyhow the CUBE didn't allocate enough Stack space.

fixed here: in the stm32L4xx_flash.lds file

	.reserved_for_stack (NOLOAD) :
	{
		. = ALIGN(4);
		PROVIDE(__reserved_for_stack_start__ = .);
		KEEP(*(.reserved_for_stack))
		. = . + 0x100; /* <--- fixed here */
		. = ALIGN(4);
		PROVIDE(__reserved_for_stack_end__ = .);
	} > SRAM

    This topic has been closed for replies.
    Best answer by Walid ZRELLI

    Hello @Community member​ ,

    You're correct about the increase of stack. (s)printf (according to implementation) uses more stack if it processes more complex data (like floats for example). So, it is totally normal to have to increase the stack if it isn't already enough.

    Are you using an OS, where the sprintf is located (interrupt, normal code, OS task, etc...)?

    BeST Regards,

    Walid

    2 replies

    Visitor II
    August 5, 2021

    Hello @Community member​ ,

    You're correct about the increase of stack. (s)printf (according to implementation) uses more stack if it processes more complex data (like floats for example). So, it is totally normal to have to increase the stack if it isn't already enough.

    Are you using an OS, where the sprintf is located (interrupt, normal code, OS task, etc...)?

    BeST Regards,

    Walid

    T JAuthor
    Visitor II
    August 11, 2021

    Hello Walid,

    I use my own OS and never print in the background..

    It was my fault in the end.

    I changed the Stack and Heap in the Cube down to 0x800 each, to save RAM, but its not ok.

    putting it back to 0x1000 for both fixed the issue.

    BeST

    Nick

    Visitor II
    August 11, 2021

    Good news. Glad to help you.