Placing anonymous strings in flash with IAR compiler.

1 post / 0 new
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using the IAR KickStart freebie edition.

The compiler defaults to writing anonymous strings to SRAM, and consequently uses up all the SRAM in a Tiny2313.

printf("this string is anonymous,  yet gets copied to SRAM");

There appears to be a command line option:


but this just produces errors when the project is built from the IDE.

You then have the problem that the library printf() expects a SRAM format string, but comes in several flavours.
The printf_P() expects a FLASH format string, but only comes in one full-featured flavour. ---- far too big for a Tiny.

I have managed to create a workaround by using a tinyprint_P() implementation and a macro. But is there an easier way?

#if  defined(__GNUC__)
#define PRINTF_P(fmt, ...) {tinyprint_P(PSTR(fmt), __VA_ARGS__);}
#elif defined(__CODEVISIONAVR__)
#define PGM_P				flash char *
#define PROGMEM                         flash
#define PSTR(x)				x
#define PRINTF_P(fmt, ...) {tinyprint_P(fmt, __VA_ARGS__);}
#elif defined(__IAR_SYSTEMS_ICC__)
#define PROGMEM                         __flash
#define PRINTF_P(fmt, ...) {static __flash char _1[] = fmt; tinyprint_P((PGM_P)_1, __VA_ARGS__);}
#define PGM_P                           char *
#define PROGMEM                         
#define PSTR(x)				x
#warning Using RAM for tinyprint_P(fmt, ...)
#define PRINTF_P(fmt, ...) {tinyprint_P(fmt, __VA_ARGS__);}

I can then use regular looking PRINTF() instead of printf(). IAR is not as flexible with variadic macros. So I always have to use at least one argument.

avr-gcc is positively simple for anonymous strings. IAR does not seem to work with a simple PSTR() macro.

I am sure that there is a neat and simple solution. There must be many IAR users that can point me in the right direction.