Using Printf to display on serial port of an ARM microcontroller

2

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.

embedded
serial-port
printf
microcontroller
asked on Stack Overflow Nov 9, 2009 by Alex Xander • edited Nov 10, 2009 by Steve Melnikoff

3 Answers

7

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 */
U1LCR   = 0x00000083; /*8 bits, 1 Stop bit */

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:

PINSEL0 = PICSEL0_RXD1EN | PICSEL0_TXD1EN;
U1LCR   = U1LCR_8BITS | U1LCR_1STOPBIT;

..and so on.)

answered on Stack Overflow Nov 10, 2009 by Steve Melnikoff • edited Nov 10, 2009 by Steve Melnikoff
4

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:

answered on Stack Overflow Nov 11, 2009 by Craig McQueen • edited Nov 12, 2009 by Craig McQueen
0

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.

answered on Stack Overflow Nov 13, 2009 by ArielP

User contributions licensed under CC BY-SA 3.0