Skip to main content
Associate
April 18, 2024
Solved

Variadic functions (like prinf)

Hi community,

I wrote the following function

UINT PrintOut(const char* format, ...)
{
	va_list args;
	va_start(args, format);

	char buffer[1024] = { 0 };
	vsprintf(buffer, format, args);

	va_end(args);

	return PrintString(buffer);
}

No problems at compiling time, but at runtime the execution falls in HardFault.

Any suggestion?

    This topic has been closed for replies.
    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    April 18, 2024

    Probably related to 1KB stack usage.

    See if making buffer static changes failure or results in Linker flagging memory size issue.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..

    7 replies

    Pavel A.
    April 18, 2024

    The code looks OK. Debug as usual. HardFault where? The debugger of CubeIDE has convenient tool to analyze HardFaults.

    Andrew Neil
    Super User
    April 18, 2024

    So where, exactly, does the Hard Fault occur?

    And what Fault is reported?

    Some links here on debugging Cortex-M Hard Faults:

    https://community.arm.com/support-forums/f/embedded-forum/3257/debugging-a-cortex-m0-hard-fault

    See also CubeIDE Fault Analyser:

    AndrewNeil_0-1713427905139.png

     

    https://www.st.com/resource/en/user_manual/um2609-stm32cubeide-user-guide-stmicroelectronics.pdf 

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    alister
    Senior III
    April 18, 2024

    Line 6 is a waste of code. Isn't line 7 copying data over buffer? In embedded development especially, all code should be for a reason and no code should be without a reason.

    Line 7 should use vsnprintf. Else what would happen if the format or its expansions overrun buffer?

    Ways this code could fail are to numerous to list. Let it hard-fault in a debugger and then debug it. Configure your IDE or makefile to generate a link map file and check that for the addresses in the hard-fault information. Check ST's applicable programmer manual for your variety of STM32. Search community for other posts on hard faults.

    Andrew Neil
    Super User
    April 18, 2024

    @diego-z.esse-ti wrote:

    I wrote the following function


    Why?

    Why not just use printf() - you just have to add a simple write() function to support output to whatever device you require; eg, UART.

    The bulk of the code in printf() is  accounted for by vsprintf() anyhow - so you're not saving much (if anything).

    https://shawnhymel.com/1873/how-to-use-printf-on-stm32/

     

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    April 18, 2024

    Probably related to 1KB stack usage.

    See if making buffer static changes failure or results in Linker flagging memory size issue.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Associate
    April 18, 2024

    Thank you @Tesla DeLorean,

       you nailed the problem!

    Pavel A.
    April 18, 2024

    Then you have a worse problem. No detection of stack growing out of limits.