0

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 
    }
}
Dinera
  • 85
  • 2
  • 11

1 Answers1

1

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);
}
Tagli
  • 2,412
  • 2
  • 11
  • 14