What you are trying to do does not make sense.
#define GLUER(x,y,z) x##y##z #define PxDIR(x) GLUER(P,x,DIR) int main() { int port; port = 2; PxDIR(port) |= 0x01; }
The preprocessor starts at (before) compile time. Therefore, he does not know anything about the contents of the port variable. The preprocessor requires that any values passed as arguments to macros be constants. For example, you can do the following:
#define GLUER(x,y,z) x##y##z #define PxDIR(x) GLUER(P,x,DIR) int main() { PxDIR(2) |= 0x01;
Otherwise, if you want to pass a variable to this macro, there really is the only way to make sure that the code for this is explicitly generated:
#define GLUER(x,y,z) x##y##z #define PxDIR(x) GLUER(P,x,DIR) uint16_t* get_port_pointer(uint8_t port_id) { if (port == 0) { return &PxDIR(0); } else if (port == 1) { return &PxDIR(1); } else if (port == 2) { return &PxDIR(2); } else if (port == 3) { return &PxDIR(3); } else { return &0; } } int main() { int port; port = 2; *(get_port_pointer(port)) |= 0x01; }
Thus, we provide access to all ports from 0 to 3. In addition, now we must monitor the return of null pointers from the get_port_pointer function.
Mattclims
source share