AnsweredAssumed Answered

How does printf work?

Question asked by Dorin Petre on Oct 4, 2018
Latest reply on Oct 8, 2018 by jeremyzhou

Hi all,

 

I am working on a LPC804 with MCUXPRESSO.

I am using the Redlib (nohost -nf) - Library support with the following preprocessor defines:

__REDLIB__
PRINTF_FLOAT_ENABLE=0
CR_INTEGER_PRINTF
CPU_LPC804M101JDH24
CPU_LPC804M101JDH24_cm0plus
SDK_DEBUGCONSOLE=0
CR_PRINTF_CHAR
__MCUXPRESSO
__USE_CMSIS
DEBUG

 

I am using the printf - character by character with only interger only implementation.

It is redirected to work with a uart driver to print in a terminal. In the driver I have a software buffer of 20 bytes, and it works fine if I give it a for loop with 200 bytes, it just waits to have space to put the next byte and I can see all of them on the terminal.

 

If I use:

 

1)

printf("Print this sequence that is longer then 20 bytes 012345678910111213141516171819\n\r");

- works just fine

 

2)

printf("Print this sequence that is longer then 20 bytes")

for(uint8_t i = 0; i < 20; i++)

{

   printf("%d, ",  i);

}

printf("\n\r");

- here some times I get HardFault or I get garbage 

 

I saw that in the MCUXpresso_IDE_User_Guide.pdf on 15.5.1 Redlib printf Variants(page 136)

 

"It is possible to switch to using “character-by-character” versions of these functions (which do
not require heap space) by specifying the build define “CR_PRINTF_CHAR” (which should be
set at the project level). This can be useful, for example, if you are retargeting printf() to write
out over a UART (as detailed below)- as in this case it is pointless creating a temporary buffer to
store the whole string, only to then print it out over the UART one character at a time"

 

When the printf has arguments does it still uses malloc() to store the arguments?

Outcomes