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: Daniel Williamson
Subject: RE: [avr-gcc-list] Access to array of struct in flash
Date: Thu, 31 Jul 2003 14:55:40 +0100

Erm,  I have been mostly using these routines blindly,
As far as I can tell, since I'm not an inline assembler Guru, The data is
pulled from flash into a register. The address of the data is in the 16 bit
Z register. The flash is stored as 16 bit words and the lsb selects the
upper or lower word to read.
This way your'e not using SRAM to store entire arrays of constant data.

There is no instruction for chars as that is already implemented in GCC.
(see PRG_RDB in the libc manual)

Note that these will only work for devices up to 64K flash, as the LMP
instruction will only work on the lower 64K, after that you need to use ELPM
and RAMPZ.

I use these on a M128 but I've not yet exceeded 64k.

Maybe they should be modified to allow the M128 address range?
Maybe If I knew more about GCC's assembler and how to tell which 64k page
the data is in, any pointers anyone?

Dan.





-----Original Message-----
From: Reza Naima [mailto:address@hidden
Sent: 30 July 2003 22:17
To: Daniel Williamson
Cc: address@hidden; 'avr-gcc-list'
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



reply via email to

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