printf() and portable code

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use printf_P today. Will that function compile in GCC for other MCUs? And will it compile in other compilers like Keil and IAR? If it will not, how should I rather do my printf's for maximal portability?

Also, is PSTR something AVR-specific?

Typical example:
printf_P (PSTR ("(%02X) "), startadr);

Greetings,

Børge

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

These aren't "standard" (or to my knowledge even "commonly used") forms, but
they aren't syntactically nor semantically tricky, and so can be adapted quite easily:

#define PSTR(_string)   (_string)
#define printf_P     printf

These definitions will even be fine with WinAVR, you just won't get as "nice" a result.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I didn't verify it, but I assume IAR would supply functions like
printf_f() which serve the same purpose (the _f suffix indicating the
format string is expected to be in flash). As they can partially
automatically handle flash strings, they don't necessarily need the
PSTR() macro, so you could replace it by a null macro there:

#define PSTR(s) (s)

However, to the best of my knowledge, this requires to turn the
compiler into a mode that is no longer conforming to the C standard,
where it puts all string literals into flash ROM. For the more
portable way, you'd need a similar macro as PSTR as well, but you need
to replace the GCC-style »__attribute__((progmem))« by the
IAR-style »__flash« keyword.

Note that Harvard still sends its greetings by the need of separately
implemented functions to pass these strings down (suffix _P in
avr-libc, suffix _f in IAR) in order to tell the called functions the
string is to be fetched by means of LPM as opposed to LDxx
instructions. That's the point where even clever implementations like
IAR (that normally could handle the LPM access transparently) stumble
across Harvard architecture CPUs.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, for convenience, am I better off using a plain printf() instead? Not everybody porting my program to a different MCU is an experienced C programmer. (Neither am I.)

Thanks!

Børge

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sure, if a simple printf() suffices, use it.

Otherwise, if you want to be portable, encapsulate the printf_P in a
macro, say PRINTF_ROM. Note that the historic unability to use
preprocessor macros with variable argument lists has been overcome by
C99, you can now officially write something like:

#define PRINTF_ROM(...) printf_P(__VA_ARGS__)

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks! Tera Term had me fooled for a while, it screwed up random characters until I restarted it.

Do you happen to have any experience in porting to pic controllers? I prefer AVR, but who knows what the customers want to use.

Børge

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, I did my first microcontroller project after some 10+ years of
abstinence (using Zilog Z8s before) using a PIC. As there was no
decent compiler available for my small hobby project, I used
assembler. This made me swear to never volunteerely touch PICs again,
and go for AVR (AVR-GCC being in its infant age by that time).

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jörg,

My short flirt with printf() didn't work out. Could you have a look at these defines for me? What I would like the most is to have no reference to PSTR in my code, only define it in once. But my experience with C macros is non-existant, I'm afraid.

// If your compiler accepts printf_P (PSTR ("text"), arguments);
#define MCU_PRINTF(...) printf_P(__VA_ARGS__)

// If your compiler accepts printf ("text", arguments);
// #define PSTR(_string) (_string)
// #define MCU_PRINTF(...) printf

Does this look okay?

Thanks,
Børge