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

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

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


From: address@hidden
Subject: [avr-gcc-list] Access to array of struct in flash
Date: Wed, 30 Jul 2003 15:46:02 +0200

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




reply via email to

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