chipKIT® Development Platform

Inspired by Arduino™

Printf

Created Mon, 05 Nov 2012 05:58:42 +0000 by lafleur


lafleur

Mon, 05 Nov 2012 05:58:42 +0000

How do I link the output of printf to the serial port??

I know about the sprintf option, but would like to make printf work??

thanks


EmbeddedMan

Mon, 05 Nov 2012 15:20:44 +0000

I don't believe that Arduino has a standard way to do printf() directly to the serial ports. Maybe somebody who knows more than I do can correct me if I'm wrong . . . .

I've wanted this same thing in my sketches.

*Brian


guymc

Mon, 05 Nov 2012 23:20:47 +0000

I believe the chipKIT compiler can do this quite easily.

There are a handful of "simple I/O functions" that can be defined to replace stubs in the C runtime library. For printf() output to a hardware UART, try adding this function definition to your sketch:

void _mon_putc (char c)
{
   while (U1STAbits.UTXBF);
   U1TXREG = c;
}

Of course, you will have to initialize the UART for such things as baud rate.

If you give this a try, please post the results. If it doesn't work, we'll dig a little deeper...

Cheers


bperrybap

Mon, 12 Nov 2012 05:25:14 +0000

I don't believe that Arduino has a standard way to do printf() directly to the serial ports. Maybe somebody who knows more than I do can correct me if I'm wrong . . . . I've wanted this same thing in my sketches. *Brian

Yes there is a way to do it. Actually more than one. I do it all the time. But it does depend on some support in the AVR libc library that is not in the pic32 libc. (I'm sure something is there, but it will be different) Here is a link for some examples: [url]http://www.arduino.cc/playground/Main/Printf[/url]

The tricky part on AVR Arduino is that their wimpy IDE hard codes the linker options and by default the AVR libC has the floating point disabled in xxprintf() and you have to link against a different library to get full floating point support. The kludge is to create an altered version of libC that has the floating point versions of the xxprintf() code in them.

As of the 1.5 release of Arduino s/w, you can alter the linker options the same as with mpide. Another issue you run into using printf() on AVR Arduino is that of RAM because string constants are copied to RAM. I create a Printf() macro that plays games so that all the formatting strings are pushed into progmem automatically.

pic32 won't have the ram/progmem issues.

--- bill


drpap

Tue, 18 Dec 2012 23:10:33 +0000

I believe the chipKIT compiler can do this quite easily. There are a handful of "simple I/O functions" that can be defined to replace stubs in the C runtime library. For printf() output to a hardware UART, try adding this function definition to your sketch:

void _mon_putc (char c)
{
while (U1STAbits.UTXBF);
U1TXREG = c;
}

Of course, you will have to initialize the UART for such things as baud rate. If you give this a try, please post the results. If it doesn't work, we'll dig a little deeper... Cheers

Just gave this a try and it did not work. Any chance of checking on what needs to be done to enable the trick?


bperrybap

Wed, 19 Dec 2012 00:49:41 +0000

Ok so I got curious and decided to make it work. It's not difficult.

Arduino uses c++ so you need a wrapper around the _mon_putc() function. I'd also suggest that it is better/easier to stay in the Arduino world rather than touch the UART hardware directly.

So to make it work: Add this to the top of your sketch:

/*
 * For printf() output with pic32 Arduino
 */
extern "C"
{
  void _mon_putc(char s)
  {
	Serial.write(s);
  }
}

Then make sure to initialize the serial port in setup() with something like:

Serial.begin(9600) ;

pick your favorite baud rate.

printf() will now work including with floating point formatting. Enjoy,

--- bill


drpap

Fri, 21 Dec 2012 16:18:47 +0000

This indeed works. Thank you!

An added advantage of this approach is that all debug messages can be disabled at runtime within the _mon_putc function.

Invoking printf seems to add about 8K of code, which is worth it considering the mess of using print/println for complex messages.

Thanks again.

Ok so I got curious and decided to make it work. It's not difficult. Arduino uses c++ so you need a wrapper around the _mon_putc() function. I'd also suggest that it is better/easier to stay in the Arduino world rather than touch the UART hardware directly. So to make it work: Add this to the top of your sketch:

/*
* For printf() output with pic32 Arduino
*/
extern "C"
{
void _mon_putc(char s)
{
Serial.write(s);
}
}

Then make sure to initialize the serial port in setup() with something like:

Serial.begin(9600) ;

pick your favorite baud rate. printf() will now work including with floating point formatting. Enjoy, --- bill