Question

Basicly I am getting data from another thread in the RTOS. This data is a pin on the board. All the IO ports are in structures / unions in an iodefine.h file. See this for example on how Micrium made it:

struct st_portd {
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} DDR;
unsigned char wk0[31];
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} DR;
unsigned char wk1[31];
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} PORT;
unsigned char wk2[31];
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} ICR;
unsigned char wk3[95];
union {
    unsigned char BYTE;
    struct {
        unsigned char B0:1;
        unsigned char B1:1;
        unsigned char B2:1;
        unsigned char B3:1;
        unsigned char B4:1;
        unsigned char B5:1;
        unsigned char B6:1;
        unsigned char B7:1;
    } BIT;
} PCR;
};

Very clever way if you ask me. So I save this pin as 2 chars in a struct, called Port and Pin.

struct StepperMotor {
  CPU_INT32U ID;
  CPU_CHAR *EnablePort;
  CPU_CHAR EnablePin;
  CPU_CHAR *DirectionPort;
  CPU_CHAR DirectionPin;
  CPU_CHAR *PulsePort;
  CPU_CHAR PulsePin;
};

I would like to use the pin in this way:

(struct st_portd)(steppermotor->PulsePort)->DR.BYTE ^= (1 << steppermotor->PulsePin);

steppermotor is the struct. Only with this way I get an error saying

request for member 'DR' in something not a structure or union

How am I able to use the steppermotor->PulsePort->DR.BYTE without making a new variable for it? I hope anyone can help me!

Was it helpful?

Solution

Since you are casting a pointer, you should be casting it to a pointer to a structure, rather than a structure itself, like this:

((struct st_portd*)steppermotor->PulsePort)->DR.BYTE ^= (1 << steppermotor->PulsePin);

Also note that your parentheses are in a wrong place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top