I am working in a library which is an interface to other libraries with several defined macros such as:
#define GPIOx some stuff
#define __HAL_RCC_GPIOx_CLK_ENABLE() some other stuff
where x is a letter (A,B,C,...).
I can not alter these macros (or I should not alter them, because they are used by other components).
In the library I am working, I am trying to define some other macros which user can modify, such as:
#define DHT_GPIO_Port GPIOx
And I want to define a macro which uses such definition to generate the other macro name:
#define __HAL_DHT_CLK_ENABLE(DHT_GPIO_Port) __HAL_RCC_## DHT_GPIO_Port ##_CLK_ENABLE()
This is because I want to use the macro for my library instead of the __HAL_RCC_GPIOx_CLK_ENABLE() as it will be different depending on which GPIO is defined by the user.
However, when I try to use my __HAL_DHT_CLK_ENABLE(DHT_GPIO_Port) macro it expands to __HAL_RCC_DHT_GPIO_Port_CLK_ENABLE(), not to __HAL_RCC_GPIOx_CLK_ENABLE().
On the other side, I've tried putting it inside another macro, however it expands GPIOx as well which I do not require.
Perhaps, it is a beginner question, but could you help to expand only the first level of the macro DHT_GPIO_Port in the other macro concatenation?
It does not make too much sense. instead of useless hard to read macros use inline functions
#define SINLINE static inline __attribute__((always_inline))
SINLINE void __HAL_DHT_CLK_ENABLE(GPIO_TypeDef * const gpio)
{
switch((uint32_t)gpio)
{
case (uint32_t)GPIOA:
__HAL_RCC_GPIOA_CLK_ENABLE();
break;
case (uint32_t)GPIOB:
__HAL_RCC_GPIOA_CLK_ENABLE();
break;
case (uint32_t)GPIOC:
__HAL_RCC_GPIOA_CLK_ENABLE();
break;
}
}
When you enable optimizations and call it with the constant expression (like GPIOA. GPIOB etc switch ... case will be optimized. You will be also able to call it with non constant parameter - which you cant do with the macro.
Avoid such a macros as a plaque.