How can I pass macros with commas into other macros?
-
I am writing code for an embedded ARM processor and I am compiling with GNU GCC. I want to do the following:
#define PA0 GPIOA, GPIO_PIN0
#define PIN_DEF(PORT, PIN) \
GPIO_TypeDef* PORT; \
uint16_t PIN;
PIN_DEF(PA0)
However, I get an error message saying PIN_DEF requires 2 arguments but I'm only giving it 1. Does anybody know how to solve this?
-
I am writing code for an embedded ARM processor and I am compiling with GNU GCC. I want to do the following:
#define PA0 GPIOA, GPIO_PIN0
#define PIN_DEF(PORT, PIN) \
GPIO_TypeDef* PORT; \
uint16_t PIN;
PIN_DEF(PA0)
However, I get an error message saying PIN_DEF requires 2 arguments but I'm only giving it 1. Does anybody know how to solve this?
-
I am writing code for an embedded ARM processor and I am compiling with GNU GCC. I want to do the following:
#define PA0 GPIOA, GPIO_PIN0
#define PIN_DEF(PORT, PIN) \
GPIO_TypeDef* PORT; \
uint16_t PIN;
PIN_DEF(PA0)
However, I get an error message saying PIN_DEF requires 2 arguments but I'm only giving it 1. Does anybody know how to solve this?
The generic answer is you can use __VA_ARGS__ its used to pass variable arguments thru macros, something like this is generally how it used to feed a multi variable function
#define LOG_DEBUG(...) printf(__VA_ARGS__)
For what you are doing you pass the arguments thru two macros the first take any number of variables the second macro enforces it is using just two values.
#define PA0 GPIOA, GPIO_PIN0
#define PIN_DEF_A(A, B) \
GPIO_TypeDef* PORT; \
uint16_t PIN;
#define PIN_DEF(...) PIN_DEFA(__VA_ARGS__)Your code doesn't make a lot of sense to me because I can't see the types. I am guessing GPIO_TypeDef* is a volatile to a 16 bit port and what you are trying to do is MACRO the output of a 16 bit value which would usually look actually like this
*((volatile uint16_t*) 0xXXXXXXXXX = 0x????;
// So something like this ... send 0x1234 to port 0x3F000000
*((volatile uint16_t*) 0x3F000000) = 0x1234;Using the above macro form for that would be
#define PA0 0x3F000000,0x1234
#define PIN_DEFA(A, B) *((volatile uint16_t*) (A)) = (B)
#define PIN_DEF(...) PIN_DEFA(__VA_ARGS__)//USING IT
PIN_DEF(PA0); // This expands to *((volatile uint16_t*) 0x3F000000) = 0x1234;In vino veritas