0

When setting up the GPIO on my STM32 Processor I want to check and make sure two different GPIOs are on the same port.

So, I added this macro:

#if (USART2_TX_GPIO_Port != USART2_RX_GPIO_Port)
#error "USART TX and RX on different ports!!!"
#endif

It won't compile, this is the error:

../Drivers/CMSIS/Device/ST/STM32H7xx/Include/stm32h7a3xxq.h:2419:45: error: operator '*' has no right operand
 2419 | #define GPIOD               ((GPIO_TypeDef *) GPIOD_BASE)

No idea what the issue is.

Anyone have an idea?

MattR
  • 85
  • 3
  • The condition of `#if` must expand to an integral constant expression like `42` or `(1+2+3+4+5)/3 == 5` (plus some special things like `defined` and `__has_­include`). `USART2_TX_GPIO_Port` is not defined this way, so no such luck. – n. m. could be an AI Aug 23 '22 at 19:15
  • But USART2_TX_GPIO_Port does expand to a constant. { #define GPIOD_BASE (SRD_AHB4PERIPH_BASE + 0x0C00UL) } and { #define SRD_AHB4PERIPH_BASE (PERIPH_BASE + 0x18020000UL) } and { #define PERIPH_BASE (0x40000000UL) } Is it because there are too many #defines? – MattR Aug 23 '22 at 19:46
  • There is a type cast in the definition. The preprocessor doesn't know anything about those (or pretty much all of the rest of the C language) – Mat Aug 23 '22 at 19:49
  • `GPIO_TypeDef` is probably not a macro name. In this case it is [replaced with 0](https://port70.net/~nsz/c/c11/n1570.html#6.10.1p4) prior to evaluation, so you have `((0 *) ...)`. This is not legal. If it is a macro name that expands to say `int`, and `int` is not a macro name, then `int` is replaced with 0 and you have `((0 *) ...)` again, etc.In any case, a cast in the preprocessor conditional simply doesn't work. On top of that, it is intended to be a cast to a *pointer*, which is illegal in an integral constant expression (preprocessor or not) on its own. – n. m. could be an AI Aug 23 '22 at 20:22
  • Thanks! Very good to know. I've tried this kind of thing many times (you know, better to catch problems in compile than in code) and it's always failed. And I could never figure out why or how to make it work. – MattR Aug 24 '22 at 14:11

0 Answers0