Electronic – Can you simulate a GPIO input on STM32F0


I'm trying a variety of things out with input pins and customized timers on the STM32F0. All timer functions are alternate functions on the pins and therefore you cannot use the user button (PC13 on the NUCLEO) as it doesn't have any timer in its alternate functions.

So, the question is:
Can you (in the software) "simulate" a GPIO pin input going high? This would make writing code easier as you won't have to hook up the NUCLEO test board, header wires, etc. I was thinking of manually toggling bits in GPIO's IDR register but it's read-only. And the BSRR register only affects outputs. Is there a way to achieve this?

ST should have put the User Button on a different pin with more functions…


Best Answer

Use the internal pullup and pulldown resistors.

As long as a pin has nothing connected to it, and it's an input pin, setting the corresponding bits in GPIOx_PUPDR will activate pullup or pulldown resistors, directly affecting the pin state. It will not only simulate the pin going high or low, but the voltage will actually appear on the pin. The current might be too weak to drive anything else, but the pin state would change. It should work even when the pin has an alternate input function, like the external trigger or capture input of a timer.

For example, setting PA12 (TIM1 external trigger) to high

GPIOA->PUPDR=(GPIOA->PUPDR & ~(3 << (2 * 12))) | (1 << 2 * 12);

Setting the same pin to low

GPIOA->PUPDR=(GPIOA->PUPDR & ~(3 << (2 * 12))) | (2 << 2 * 12);