How does printf work?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How does printf work?

810 Views
dorin_petre
Contributor I

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?

Tags (1)
0 Kudos
Reply
1 Reply

577 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Dorin Petre,

Thank you for your interest in NXP Semiconductor products and for the opportunity to serve you.
To provide the fastest possible support, please refer to the Fig 1.

2018-10-08_15-31-22.jpg

Fig 1


Have a great day,
TIC

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply