STM32 gotchas
37.GPIO pin input state can still be read if it's set to Out or AF - and EXTI works, too

As long as a pin is not set as Analog in GPIO_MODER, it is permanently connected to the input Schmitt trigger, and from that to GPIO_IDR, as well as to the EXTI input multiplexer.

That means, that a pin's input state can be read, even if it is set as Out or AF1. This is useful especially when the output is set as Open Drain - in that case, level set in ODR for that pin does not necessarily match the level read in into IDR for the same pin.

Similarly, EXTI can be used, even if pin is set as Out or AF. This allows for example to have an external interrupt together with pin acting as the dedicated alternate-function input for some module. One useful application for this is the NSS pin in SPI set as slave, used with DMA - the AF acts as framing/output enable for the hardware, and the rising edge could trigger an interrupt which in software starts the processing of received data.

These facts come as a surprise mainly to the users of "libraries" such as Cube or SPL,as they tend to avoid reading the RM, and also in "libraries" the GPIO initialization routines usually bind EXTI functionality exclusively to pins being set as In.


1.In fact, a pin being set in GPIO_MODER as In means just that the output drivers are permanently disabled.