[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] PSTR() in static initialisers

From: Georg-Johann Lay
Subject: Re: [avr-libc-dev] PSTR() in static initialisers
Date: Thu, 1 Feb 2018 17:15:44 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0

On 31.01.2018 21:15, Paul "LeoNerd" Evans wrote:
On Wed, 31 Jan 2018 21:04:55 +0100
Joerg Wunsch <address@hidden> wrote:

As Paul "LeoNerd" Evans wrote:

Does anyone have a suggestion on how I can have a string pointer to
a flash-stored string in a static initialiser?

This only works by allocating each of the string into a variable
(well, constant actually) in flash by itself, and then mention their
names in the struct initializer.

Hrm; that's upsettingly annoying. Is there perhaps a way this can be
convinced into working? I generally dislike the untidiness of having to
define lots of things above the main array definition, just so I can
pull them in by name into it, and use that name nowhere else. Inline
things would be nicer there.

If that static is not function-static, then __flash can be used:

#if !defined __FLASH || (defined __AVR_PM_BASE_ADDRESS__)
// For devices that see flash in RAM address space, resort to vanilla C.
#define __flash // empty
#endif // have __flash

#define FSTR(X) ((const __flash char[]) { X } )

const __flash char* const __flash animals[] =
  FSTR ("mite"), FSTR ("slug"), FSTR ("sloth")

const __flash char flash[] = "flash";
const __flash char *pram_to_flash = FSTR ("pet");
const __flash char * const __flash pflash_to_flash = FSTR ("cat");

const __flash char* ani (int i)
  return animals[i];

Notice that in contrast to progmem, __flash is a qualifier and *not* an attribute. In particular, it matters where it occurs in a declaration, e.g. whether left or right of a "*". gcc will generate accesses as desired, no need for pgm_read macros or inline asm. However gcc rejects FSTR for function-static so that there is no one-fits-all solution:

const __flash char* get_pet (void)
  static const __flash char *pet = FSTR ("pet");
  return pet;

In function 'get_pet':
error: compound literal qualified by address-space qualifier
   return FSTR ("pet");


reply via email to

[Prev in Thread] Current Thread [Next in Thread]