avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] Access to array of struct in flash


From: Larry Barello
Subject: Re: [avr-gcc-list] Access to array of struct in flash
Date: Wed, 30 Jul 2003 14:44:52 -0700

All access is byte wise on the AVR.  Some of the helpers/macros do return 
words, but
that is done with two accesses.

GCC will copy FLASH to SRAM if you declare the data in RAM to begin with (i.e. 
don't use
the PGM macros): It allocates two slabs of memory: one FLASH with the data and 
one in
RAM, which it copies the data to at boot time.

If you just want data in FLASH so as not to consume RAM, then you declare it 
with the
PGMxx macros and you need to programmatically access it via the macros or some 
other
means.  GCC does not provide *any* native access to EEPROM or FLASH.

I bet the documentation that comes with libc covers all this stuff and more.  
Check out
GCC section on www.avrfreaks.net or your documentation that comes with WinAVR 
(windows
distribution).  Look for a FAQ or "Getting Started" guide.

Cheers!

----- Original Message ----- 
From: "Reza Naima" <address@hidden>
To: "Daniel Williamson" <address@hidden>
Cc: "'avr-gcc-list'" <address@hidden>
Sent: Wednesday, July 30, 2003 2:17 PM
Subject: Re: [avr-gcc-list] Access to array of struct in flash


> I was actually just about to start researching this.  I am generating
> large arrays of uint8_t to store fonts for display on a graphic LCD.   I
> though that by using something like PROGMEM to define it, then it would
> be sufficient to address it normally to retrieve the data -- or will
> that copy it to ram first before returning it to you?  Is that the
> purpose of the assembly functions?
>
> Also, do you have to access flash memeory in words?  What if all that is
> required is a char?
>
>
> Thanks,
> Reza
>
> On Wed, Jul 30, 2003 at 04:03:51PM +0100, Daniel Williamson sent me this...
> > I've been using these Defined functions for reading memory,  It is then as
> > simple as calling PRG_RDW(&ps->min) to pull out a word from flash.
> > there's also PRG_RDL for Longs.
> >
> > I can't remember where they were from it was either this list or AVR FREAKS.
> > However, they do work and that's good enough for me. Shame they are not
> > included in the gcc package.
> >
> > hope this helps.
> >
> > Dan
> >
> > -----Original Message-----
> > From: address@hidden
> > [mailto:address@hidden Behalf Of address@hidden
> > Sent: 30 July 2003 14:46
> > To: avr-gcc-list
> > Subject: [avr-gcc-list] Access to array of struct in flash
> >
> >
> > Hello all,
> >
> > I have a question on how convert C code.
> >
> > In the past I have written a lot of functions with heavy use of array of
> > structure to do the job. The target CPU was Motorola 683XX.
> > All arrays were constat.
> >
> >
> > The code look like
> >
> > ...
> > struct mystruct
> > {
> > char* msg;
> > short Min;
> > short Max;
> > short default;
> > void (*pfunction)( short i );
> > .....
> >
> > };
> > ...
> > const struct mystruct example[ ] =
> > {
> > "Value 1", 10, 500, 100, Control,
> > "Value 2", 50, 2500, 50, Control,
> > "Value 3", 10, 500, 100, Control,
> > "Value 4", 10, 500, 100, Control,
> > ...
> > };
> > ...
> > const struct mystruct * ps = example;
> > ...
> >
> > if( ps->msg != NULL )
> > DisplayMessage( ps->msg );
> > ...
> > if( ( value < ps->Min )||( value > ps->Max ) )
> > value = ps->default;
> > ...
> > if( ps->pfunction != NULL )
> > ( ps->pfunction )( index );
> > ...
> > and so on.
> >
> > Now I'm porting this code to AVR ATmega128.
> >
> > I'have converted some functions in this way:
> > ...
> > const struct mystruct PROGMEM example[ ] =
> > {
> > "Value 1", 10, 500, 100, Control,
> > "Value 2", 50, 2500, 50, Control,
> > "Value 3", 10, 500, 100, Control,
> > "Value 4", 10, 500, 100, Control,
> > ...
> > };
> > ...
> > typedefs unsigned short word;
> >
> > word GetPRGWord( word add )
> > {
> > word w = PRG_RDB( add );
> > w += ( PRG_RDB( ++add ) << 8 );
> > return w;
> > }
> > ....
> > const struct mystruct * ps = example;
> >
> > word msg = GetPRGWord( (word)&ps->msg );
> >
> > if( (void*)msg != NULL )
> > DisplayMessage( (char*)msg );
> >
> > word Min = GetPRGWord( (word)&ps->Min );
> > word Max = GetPRGWord( (word)&ps->Max );
> > word default = GetPRGWord( (word)&ps->default );
> > ...
> > if( ( value < Min )||( value > Max ) )
> > value = default;
> > ...
> > word pfunction = GetPRGWord( (word)&ps->pfunction );
> > if( (void*)pfunction != NULL )
> > ( ( void(*)( short ) ) pfunction )( index );
> > ...
> > and so on.
> >
> > The converted functions work, but it is not good; and the conversion takes
> >
> > a lot of time and can introduce errors.
> >
> > How is possible to convert the code is a simpler and better way, reducing to
> > minimum the modification of original code ?
> >
> > Thanks in advance
> >
> > Patrizio Zelotti
> >
> > address@hidden
> >
> >
> >
> > _______________________________________________
> > avr-gcc-list mailing list
> > address@hidden
> > http://www.avr1.org/mailman/listinfo/avr-gcc-list
>
>
> > _______________________________________________
> > avr-gcc-list mailing list
> > address@hidden
> > http://www.avr1.org/mailman/listinfo/avr-gcc-list
>
>
> _______________________________________________
> avr-gcc-list mailing list
> address@hidden
> http://www.avr1.org/mailman/listinfo/avr-gcc-list
>



reply via email to

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