libopencm3 STM32G0: GPIO inputs don't work

343 Views Asked by At

My code does not set the GPIOs as inputs on my Nucleo-G071RB. The MODER register ist completly set (0xffffffff) and the GPIOs work as outputs.

What did I wrong?

Code:

#include <libopencm3/stm32/gpio.h>

void setupGpio(void);


void setupGpio(void) {
    // set input
    gpio_mode_setup(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_ALL);
}

int main(void){
    setupGpio();

    while (1)
    {
        // Loop with pin read 
    }
}
1

There are 1 best solutions below

1
On BEST ANSWER

You need to enable peripheral clock first. Not only GPIO but almost all peripherals need this.

Modify your function as below:

void setupGpio(void) {
    rcc_periph_clock_enable(RCC_GPIOB); // Enable GPIOB clock
    gpio_mode_setup(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_ALL);
}