Is there a replacement for PROGMEM and its access functions?

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

I thought I read recently that there is a nicer way to declare and access flash-based data. Did I dream that, misunderstand, or is it true? Didn't find anything with some searches. Thanks.

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

kk6gm wrote:
Didn't find anything with some searches.
Search for "__flash".

Stefan Ernst

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

The user manual for it:

http://gcc.gnu.org/onlinedocs/gc...

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

Thanks, guys, this is SO much nicer! Anybody know when it was added?

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

As a silly question.

Is there a mechanism for sensible handling of anonymous strings?

Or do you have to continue with the PSTR() abortion?

David.

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

The problem with PSTR is that pointers to it will point to the generic space (RAM). PSTR locates in flash but that does not change the address space to flash.

If you use a consumer function that uses const __flash* then it will still read from flash and you get the desired behavior.

You can read the Embedded C extension ISO/IEC TR 18037 for the gory details. IMO there are some shortcomings regarding string literals. PSTR should cast the outgoing address to const __flash char* provided the built-in macro __FLASH is defined. PSTR only works locally whereas ((const __flash char[]) { "foo" }) will only work globally.

avrfreaks does not support Opera. Profile inactive.

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

The compiler can recognise a named literal or an anonymous literal.

CodeVision and ImageCraft handle it very well.
8051 compilers have always put anonymous strings in __code memory.

Yes, I agree that you do occasionally get anomalies. And after many years of wrapping strings with PSTR(), there is no great problem just continuing.

OTOH, I am sure that the whole subject could be handled more elegantly.

David.

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

kk6gm wrote:
Anybody know when it was added?
Formally, 2012-Mar-22.
http://gcc.gnu.org/gcc-4.7/changes.html (search for named)

"Dare to be naïve." - Buckminster Fuller

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

Better: search for avr to find *all* relevant changes.

avrfreaks does not support Opera. Profile inactive.

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

SprinterSB wrote:
PSTR only works locally whereas ((const __flash char[]) { "foo" }) will only work globally.
I've used __flash locally:
void foo(void) {
  static const __flash char bar[] = { "foobar" };
...

Or did you mean something else?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Consider code like that:

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

const __flash char *pstr = FSTR ("foo");

avrfreaks does not support Opera. Profile inactive.