EmbDev.net

Forum: ARM programming with GCC/GNU tools help with c code


von Sevc D. (sevc)


Rate this post
useful
not useful
Hi all.
I need help with easy qestion to guru of c code.
I need declarate structureto flash.

I define:

const struct setting_x {
      float  Vmax[1800];
      float  Amax[600];
      float  Step[400];
      int  Hold_power[20];
      int  Move_power[130];
      int  Hold_les_power[20];
      int  Move_more_power[140];
      };

But I can read value from this structure to double variable.
I know so this is easy , but not for me.

regards.

von Clifford S. (clifford)


Rate this post
useful
not useful
The use of const as you have it has no effect. In fact if you compile
that you get a warning "useless keyword or type name in empty
declaration". const only makes sense for data instances, not data types
(which is what you have declared). const only makes sense when creating
a typedef or declaring data:

typedef const struct {...} tConstSettings ;
tConstSettings settings_x ; // settings_x is a const of type
"tConstSettings"

or

const struct sSettings {...} settings_x ; // settings_x is const of type
"struct sSettings"

or

struct sSettings {...} ;
const struct sSettings settings_x ; // settings_x is const of type
"struct sSettings"

Despite that point, const only specifies that the data may not be
modified by the code. It does not determine or specify any particular
storage location. There is no standard language support for data
location, so to do this requires compiler extensions. In GCC you use the
_attribute_ keyword, specifically the 'section' attribute: Eg:

// settings_x is an anonymous structure located in "FLASH"
const struct
{
    float  Vmax[1800];
    float  Amax[600];
    float  Step[400];
    int  Hold_power[20];
    int  Move_power[130];
    int  Hold_les_power[20];
    int  Move_more_power[140];

} settings_x _attribute_ ((section ("FLASH"))) = { <initialiser list
here> ) ;

Section names are defined in the linker script, so you'll need to create
a suitable named section is one does not already exist. Here I called it
"FLASH", but there is no magic, you need to define such a section.

However there is possibly a problem here. Using a section attribute
requires that the variable is explicitly initialised (and you would have
to do that for non-volatile storage devices in any case). I am not sure
what this structure is for but it has 3110 values - that is some
initialiser! I assume that you will use some sort of code generator to
build this initialiser?


If on the other hand, this data already exists in flash and you merely
want to overlay a structure to it then that is a simple case of creating
a pointer to that structure type and assigning its address, so using the
earlier tConstSettings type, and assuming that the data is at address
0x8000 for example:

tConstSettings* settings_x = (tConstSettings*)0x8000 ;

Now typically you might arrange for the linker script to create a global
symbol at the appropruate address and use that in the assignment
expression.

Clifford

von Clifford S. (clifford)


Rate this post
useful
not useful
... oh, I meant to include a link to the GCC documentation for Variable
Attributes:
http://gcc.gnu.org/onlinedocs/gcc-4.2.2/gcc/Variable-Attributes.html#Variable-Attributes

You may also need documentation for the linker (in order to define a
'section'):
http://www.gnu.org/software/binutils/manual/ld-2.9.1/html_node/ld_toc.html


Sevc Dominik wrote:
> I know so this is easy , but not for me.
No, it is just not easy! ;-)

Sevc Dominik wrote:
> But I can read value from this structure to double variable.
I really have no idea how that statement relates to your question. The
structure is 12440 bytes long, a double is only 8 bytes - it won't fit!

Clifford.

von Sevc D. (sevc)


Rate this post
useful
not useful
Hi Clifford.
in fact its not easy.
I vant create structure in FLash and in structure I want some variable
(constant). In code want read this value. in me code I define :
float Vmax[1800] , so I want define inicial value of Vmax constant, but
I define array with 1800cell and size of 1cell is 8 = 1800*8 ... .
mistake ...
on this poit I have constant and want give this constant to structure
for readable code. maybe is better to create normal structure in ram and
then fill with needed value;

regards;

von Martin T. (mthomas) (Moderator)


Rate this post
useful
not useful
Maybe I do not understand your problem correctly but if you want a
structure with constant values, where are those values? Something like
this:

typedef struct {
int a[2];
float b[3];
} Tfoo;

const Tfoo myfoo = { {1, 2}, {1.1, 2.2, 3.3} };

Here myfoo "lives" in .rodata which is mapped to the flash memory-region
by a "ROM-run" linker-script.

von Sevc D. (sevc)


Rate this post
useful
not useful
Hi Martin.
In my application ,I will have user define variable. This variable well
be defined only sometime, but don't have working code (IAP - save
variable to internal flash) us IAP, then I define this variable like
constant.
I try to understand your code in t_clock_20041129b application.
I use LPC2142 mcu.

regards

von Clifford S. (clifford)


Rate this post
useful
not useful
Sevc Dominik wrote:
> float Vmax[1800] , so I want define inicial value of Vmax constant, but
> I define array with 1800cell and size of 1cell is 8 = 1800*8 ... .
> mistake ...

No sizeof(float) is 4 not 8. Assuming you are using an ARM processor
(which should be a given on this forum).


I still do not understand what you are trying to do or why the answer I
have provided has not helped. I have provided answers to the only two
interpretations of your question I can think make any sense. Please
clarify.

Clifford

von Sevc D. (sevc)


Rate this post
useful
not useful
Hi Clifford
your answer is good for me thanks.
mistake double is 8bytes.
all your sugestion is good fo me.
I'm my project just start and don't know how make solution to store
some variable what wil be change never or only once per month.

regards all.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.