[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] array of structures with variables in flash
From: |
Kitts |
Subject: |
Re: [avr-gcc-list] array of structures with variables in flash |
Date: |
Fri, 10 Jun 2005 17:53:01 +0530 |
User-agent: |
KMail/1.8 |
On Friday 10 Jun 2005 4:16 pm IST, Brano Katreniak wrote:
> You have to place the array of structures to the flash not the members
> of the structure. PGM_P type is a pointer to the string supposed to be
> in flash. It says nothing about the fact, where the pointer itself is
> stored.
Oops! missed that. Thanks for pointing out. And how do i declare a
variable to be in
> This should work. (Althought I didn't try to compile it)
>
> char const name0[] PROGMEM = "name0";
> char const param0[] PROGMEM = "par01 par02 par03";
>
> struct {
> PGM_P name;
> const uint8_t no_of_param;
> PGM_P parameters;
> } const command[256] PROGMEM = { {name0, 3, param0}, };
Got it. But is there a way so i don't have to initialize at the time of
declaration but later? I guess this would not be possible(?)
I found a similar thing in the FAQ of avr-libc but dont understand why
cant do something like;
struct {
PGM_P name;
const uint8_t no_of_param;
PGM_P parameters;
} const command[256] PROGMEM = { {"name0", 3, "param0"}, };
but have to have had declared this earlier;
char const name0[] PROGMEM = "name0";
char const param0[] PROGMEM = "par01 par02 par03";
In my code an array of 256 such structures, this is going to be a pain
and i just might run out of names to give! ;-)
And can i have a structure like the above with members located in
different locations? For example, the variable "no_of_param" located in
RAM while the other 2 are located in flash?.
Maybe i should rather have a pointer located in flash to a global
variable located in RAM?
> > Now for the second part of my query (which is probably not avr
> > specific but C in general), how do i declare the prototype for the
> > above structure to be included all my project files?
> >
> > I would like to use the index variable as an identification and do
> > a one time initialisation. something like this;
> >
> > #define XYZ 3
> >
> > command[XYZ].name = PSTR("Something");
> > command[XYZ].no_of_param = 5;
> >
> > While the above statements are going to be in the same file as the
> > structure, say global.c, i want the structure to be accessible in
> > other files like this;
> >
> > length = strlen_P(command[XYZ].name);
>
> In other files you have to declare the structure (or any other
> variable) as extern, preferable place it into a header file. Extern
> says to the compliler that you want to use the given varibale but the
> variable itself is declared in another module (.c file). Every
> variable not declared as static can be accesed from other modules.
> Headers files come very handy. For detailed explanation look at some
> C book. Short example:
>
> global.h:
>
> #ifndef __GLOBAL_H__
> #define __GLOBAL_H__
>
> extern int shared_int;
> struct S {
> int a,b;
> };
> extern struct S shared_struct;
> #endif //__GLOBAL_H__
>
>
>
> global.c:
>
> int shared_int = 7;
> static int not_shared_int = 5;
> struct S shared_struct = {3, 7};
> static struct S not_shared_struct = {2, 6};
Yes, but i wanted to know how i do it for the stucture which has no name
as above. Stupid doubt i guess... All i had to do was give the
structure a name like "command_proto".
Sorry! When i fall short of sleep... i come up with stupid ideas /
doubts! ;-)
> //shared and not_shared variables are accessible
>
>
> other_file.c:
>
> #include "globa.h"
>
> //shared variables are accessible
--
Cheers!
Kitts