Take the 2-minute tour ×

I would like to use printf to diplay text on a serial port of an ARM microcontroller. I am unable to do so. Any help is appreciated.

My init_serial looks like this

void init_serial (void)
PINSEL0 = 0x00050000; /* Enable RXD1 TxD1 */
U1LCR = 0x00000083; /*8 bits, 1 Stop bit */
U1DLL = 0x000000C2; /*9600 Baud Rate @12MHz VPB Clock */
U1LCR = 0x00000003; /* DLAB=0*/

which is obviously wrong.

share|improve this question
You will probably need to provide more information to get an answer: (a) Which microcontroller, exactly? "ARM" basically just specifies the instruction set. (b) Why is the above code "obviously" wrong? There are a lot of parts that work together to make printf() work, serial port initialisation is just one of them. Do you know this is the wrong part? (c) Do you have any example code that works, perhaps supplied by the manufacturer? (d) Do you have an oscilloscope? –  Greg Hewgill Nov 9 '09 at 8:52
For example LPC213x microcontroller. This code is wrong because it is not working with printf. Do not have any code that works. –  Alex Xander Nov 9 '09 at 9:05
add comment

3 Answers

up vote 7 down vote accepted

For microcontollers, you typically have to define your own putc function to send bytes to whichever UART you're using. print will then call your putc.

Check the documentation for the libraries supplied with your compiler.

Note that this is entirely unrelated to how you intialise your UART. All that matters is which UART you're using.

(On an unrelated issue, rather than saying:

PINSEL0 = 0x00050000; /* Enable RXD1 TxD1 */

there are typically #defines for registers which (usually) aid readability, provide a link to the bit names in the documentation, and reduce the need for comments to be added and maintained on every line like these. For example:


..and so on.)

share|improve this answer
add comment

To make printf(), puts() etc work on an embedded platform, you need to implement some hooks that work with the C library. This is typically dependent on the C libraries provided with your compiler, so is probably compiler-dependent. But in many cases the library just requires you to provide a putc() function (or similar name), which takes a character (generated by the printf() library function) and sends it to your chosen output device. That could be a memory buffer, serial port, USB message, whatever.

From the point of view of the C library, the putc() function would be run-to-completion, so it's up to you whether you implement it to be a simple blocking function (waiting until the serial port is free and sending the character), or non-blocking (putting it into a buffer, to be sent by a background interrupt task; but the buffer might fill up if you output enough bytes fast enough, and then you have to either block or discard characters). You can also make it work properly with your RTOS if you have one, implementing a blocking write that sleeps on a semaphore until the serial port is available.

So, in summary, read the documentation for your compiler and its C library, and it should tell you what you need to do to make printf() work.

Example links for AVR micro with GCC compiler:

ARM GCC compiler using newlib C library:

share|improve this answer
add comment

I'm not sure about ARM in particular...

For some chips, within the IDE, you need to specify that you need a heap to use the printf, and how big it should be. The programmer won't automatically put one on.

Check in the menus of your programmer/IDE and see if there is a place to specify the heap size.

And I agree with Steve, this is only if you can actually use the printf, otherwise write your own little snippet.

share|improve this answer
add comment

Your Answer


