[Top][All Lists]

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

RE: AW: [avr-gcc-list] Pgmspace.h Use

From: Dave Hansen
Subject: RE: AW: [avr-gcc-list] Pgmspace.h Use
Date: Tue, 09 Aug 2005 08:58:01 -0400

From: "Haase Bjoern (PT-BEU/EMT) *" <address@hidden>

address@hidden wrote:

>for code portability reasons, I would like to avoid to call explicity the
>function pgm_read_byte() like in the following example :
>const prog_char array[7] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70};
>y = pgm_read_byte(&array[2]);
>// I would like to to keep the syntax y = array[2]; without having the array
>copied in the SRAM
>Is there a way to do it ?
No. The declaration you give above *is* how you put the array in Flash,
and how you access the array in Flash. GCC requires a new feature patch
for it to know that the expression, y = array[2], means that array is in
Flash and to use the correct assembly sequence. There is a person
working on such a feature, however, it is very non-trivial to implement
in GCC. And currently there is not a deadline when this work will be
completed as it being written by a volunteer.

Eric Weddington

IMO, it is not a problem of gcc, it is a problem of the C programming language that lacks support for Harvard architectures. In order to add support, you are forced to leave the ANSI standard, IIUC.

There is ISO/IEC WDTR 18037, which addresses multiple memory spaces ("Harvard architectures"), fixed-point arithemetic, and hardware IO addressing. I'm not sure of what the status is, but you should (soon? now?) be able to support this stuff without leaving the standard.

FWIW, to maintain portability between the two compilers I use, my current code has a "common.h" file that all the .c files #include. One of the fetures of common.h is a set of preprocessor directives something like

  #if defined(__GNUC__)
  #  include <avr/pgmspace.h>
  #  define flash const
  #  define pgm_read_byte(p)   (*((unsigned char flash *)(p)))
  #  define pgm_read_word(p)   (*((unsigned short flash *)(p)))
  #  define PROGMEM

So I can define data like

  unsigned char flash array[2] PROGMEM = {3,5};

and access it with something like

  x = pgm_read_byte(array+idx);

Maybe the syntax isn't as pretty, but it works.


reply via email to

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