0

I have a large amount of constant data that I need to store in a header file. Due to the nature of the data (how its nested), I decided to use a structure that is initialized by the preprocessor.

I am trying to initialize these structs like so:

typedef struct s_PWM {
  uint8_t muxmode;
  char *name;
  char *path;
} PWM;

typedef struct s_PIN {
  char *name;
  uint8_t gpio;
  char *mux;
  uint8_t eeprom;
  PWM *pwm;
} PIN;

This would allow me to do something like

PIN p1 = P8_19;

to, in a sense, assemble a struct with the proper attributes.

Basically, you can send a pin into a function

pinMode(P8_19, OUTPUT)

This is the line in question

#define P8_19 ((PIN){"EHRPWM2A", 22, "gpmc_ad8", 14, (PWM*){4, "EHRPWM2A", "ehrpwm.2:0"}})

This is the warning

src/gpio.c:50:2: warning: excess elements in scalar initializer
src/gpio.c:50:2: warning: (near initialization for '(anonymous)')
src/gpio.c:50:2: warning: excess elements in scalar initializer
src/gpio.c:50:2: warning: (near initialization for '(anonymous)')

Does anyone have any ideas why this is happening? Am I incorrectly nesting the PWM* struct into the PIN struct?

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
Ethan Hayon
  • 346
  • 1
  • 9

3 Answers3

0

Short answer: Yes.

Instead of having a pointer you should use the structure directly:

typedef struct s_PIN {
  char *name;
  uint8_t gpio;
  char *mux;
  uint8_t eeprom;
  PWM pwm;  /* Notice: not a pointer */
} PIN;

Also, you don't need to do the casting:

#define P8_19 {"EHRPWM2A", 22, "gpmc_ad8", 14, {4, "EHRPWM2A", "ehrpwm.2:0"}}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I initially tried to that that, but there are some instances where there won't be a PWM, so I need the ability to set it NULL. How can I make it work where there may be a NULL PWM if it's not a pointer? – Ethan Hayon Aug 08 '12 at 19:03
  • Ok, I basically added a flag to PIN pwm_present and set that to true or false so I never have to do a logical test directly against PIN.pwm -- Thanks! – Ethan Hayon Aug 08 '12 at 19:37
0
#define P8_19 ((PIN){"EHRPWM2A", 22, "gpmc_ad8", 14, (PWM*){4, "EHRPWM2A", "ehrpwm.2:0"}})
                                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The marked section is not a pointer.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

I would tackle this problem slightly differently; I would define the pin names in their own module, as follows.

gpio.h:

typedef struct s_PWM {
  uint8_t muxmode;
  char *name;
  char *path;
} PWM;

typedef struct s_PIN {
  char *name;
  uint8_t gpio;
  char *mux;
  uint8_t eeprom;
  PWM *pwm;
} PIN;

...
extern PIN * P8_19;

gpio.c:

PWM _P8_19_PWM = {4, "EHRPWM2A", "ehrpwm.2:0"};
PIN _P8_19     = {"EHRPWM2A", 22, "gpmc_ad8", 14, &_P8_19_PWM};
PIN *P8_19     = &_P8_19;

Then you can reference it in another file by including gpio.h and using P8_19 normally.

Alex Chamberlain
  • 4,147
  • 2
  • 22
  • 49
  • Let me know if that needs more explanation. – Alex Chamberlain Aug 10 '12 at 07:29
  • Will this have more overhead that using a macro to create instance of the structs? There may be cases where a pin is never used. I don't know, but it looks like in gpio.c this will create a struct for each pin, wasting memory if it is never used. – Ethan Hayon Aug 11 '12 at 14:51