-2

I am programming with Xmega and I need some flags used in more than one file. So I declared the flag as extern in the h-file and initialized it global in the main-file.

global.h:

#ifndef GLOBAL_H_
#define GLOBAL_H_

typedef struct GLOBAL_FLAGS {
    volatile uint8_t pidTimer:1;
    volatile uint8_t dummy:7;
}GLOBAL_FLAGS;

// declaration
extern GLOBAL_FLAGS gFlags;

#endif

main.c:

#include <avr/io.h>
#include <avr/interrupt.h>
#include "global.h"
#include "hv.h"
#include "pid.h"

// init
gFlags = {.pidTimer = 0, .dummy = 0};

// code....
int main(void){
// code....
// example use of flag
if(gFlags.pidTimer){
        hv_run_pid();
        gFlags.pidTimer = 0;
    }

I get some of Errors when im doing this.

Where I initilize it I get this:

Errors:

  • conflicting types for gFlags

  • field Name not in record or Union initializer

Warnings:

  • data Definition has no type or storage class

  • type Defaults to 'int' in declaration of 'gflags'

Where I want to use it i get this:

Error:

  • request for member 'pidTimer' in something not a structure or Union

I use Atmel Studio 7.

dandan78
  • 13,328
  • 13
  • 64
  • 78
puzzled
  • 69
  • 1
  • 8
  • Use `typedef struct _GLOBAL_FLAGS` then recompile and update your issue, it's a bit too much at once. – yacc Sep 07 '17 at 08:00
  • 2
    `gFlags = {.pidTimer = 0, .dummy = 0};` this is not initialization but assignment and does not make sense out of the function scope – Ajay Brahmakshatriya Sep 07 '17 at 08:02
  • 1
    You never define the actual variable anywhere. Voting to close this as simple typo. You could avoid issues like this by never using spaghetti globals with `extern`. – Lundin Sep 07 '17 at 08:08
  • @AjayBrahmakshatriya isn't it a definition of `int` variable with (incorrect) initialization? – Gerhardh Sep 07 '17 at 09:09
  • @Gerhardh since the declaration already sits at `extern GLOBAL_FLAGS gFlags;`, this is assignment. But not a correct assignment anyway. – Ajay Brahmakshatriya Sep 07 '17 at 10:14

1 Answers1

0

Thanks!

I switched

// init
gFlags = {.pidTimer = 0, .dummy = 0};

to

// init
GLOBAL_FLAGS gFlags;

and it works.

puzzled
  • 69
  • 1
  • 8
  • 1
    You can still initialize the variable though, you just have to actually define it. `GLOBAL_FLAGS gFlags = {.pidTimer = 0, .dummy = 0};`. But note that global variables and bit-fields are both very bad ideas that should be avoided. – Lundin Sep 07 '17 at 08:19
  • Thank you, I see your Point. When i dont use globals i would have a big main-file because of some different interrupt service routines which need this flag. I am new to this whole stuff. So do you have a recommendation to this problem? – puzzled Sep 07 '17 at 08:25
  • 1
    Your ISRs should only interact with the driver where they are located. You can share data between the ISR and the rest of the driver code through `static` file scope variables - but these should not get directly exposed outside the driver. If other files need to know the status, implement setter/getter functions in the driver. Private encapsulation - bread & butter program design. – Lundin Sep 07 '17 at 08:30
  • Thank you! I will try this. – puzzled Sep 07 '17 at 08:49