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

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

Re: [avr-gcc-list] array of structures with variables in flash


From: Brano Katreniak
Subject: Re: [avr-gcc-list] array of structures with variables in flash
Date: Fri, 10 Jun 2005 12:46:29 +0200
User-agent: Mutt/1.5.6+20040907i

> I have an array of structures in global space and would like access 
> these structures from within my code. The structure is declared as 
> follows:
> 
> struct {
>       PGM_P name;
>       const uint8_t no_of_param PROGMEM;
>       PGM_P parameters;
> }command[256];
> 
> On compile i get the warning;
> "warning: `__progmem__' attribute ignored"
> 
> How do i declare variables (not strings) to be placed in ROM?

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.

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},  };
 
> 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};
 
//shared and not_shared variables are accessible


other_file.c:

#include "globa.h"

//shared variables are accessible

-- 
Branislav Katreniak
mailto: address@hidden




reply via email to

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