STM32 controller: GPIO_SetBits doesn’t seem to have any effect

cstm32stm32f0

I have some problem with a STM32F030F4P6 MCU. The circuit consists of MCU itself, 3V rail connected to VDD and VDDA pins, ground connected to GND and BOOT0 pin and an LED connected to each of PA0-PA3 pins. Here is a circuit diagram (lines drawn near SV1 and SV2 connectors means external wires going to LED/power/ground/SWCLK/SWDIO pins.

enter image description here

Here is my code in it:

#include "stm32f0xx.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"

const uint16_t PINS = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;

int main(void)
{
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIOa;
    GPIO_StructInit(&GPIOa);

    GPIOa.GPIO_Mode = GPIO_Mode_OUT;
    GPIOa.GPIO_OType = GPIO_OType_PP;
    GPIOa.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIOa.GPIO_Pin = PINS;
    GPIOa.GPIO_Mode = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &GPIOa);

    volatile uint32_t i;

    GPIO_SetBits(GPIOA, PINS);
    volatile uint8_t state = 1;
    while(1)
    {
        // Waste some time
        for (i = 0; i < 500000; i++) {};
        if (state == 1) {
            GPIO_ResetBits(GPIOA, PINS);
            state = 0;
        } else {
            GPIO_SetBits(GPIOA, PINS);
            state = 1;
        }
    }
}

The problem is – LEDs connected to pins doesn't light up. I have connected a voltmeter to the output pins and debugged a program and found out that voltage goes up a little bit (to about 0.1V) each time I do GPIO_SetBits or GPIO_ResetBits, but instantly drops down to zero afterwards. Also to rule out lack-of-current issues, I changed GPIOa.GPIO_PuPd to GPIO_PuPd_UP and all the LEDs light up, but doesn't light down.

I use STM32VLDiscovery board as a debugger and CooCoox IDE.

During debugging, I can see GPIOA_ODR register bits are changing (and no longer changing if I remove the first line of main()), so it seems to be some problem with the output.

Can anyone give me any advise of what am I doing wrong?

Best Answer

Here is the problem:

GPIOa.GPIO_Mode = GPIO_Speed_2MHz;

Should be

GPIOa.GPIO_Speed = GPIO_Speed_2MHz;

Very dumb. 8 hours of life wasted :(