Search code examples
gccarmgpiostm32f4

ST32F4 GPIO enabling twice?


I'm starting with arm development on a STM32f4 Discovery board. I am using CooCox with GCC ARM compiler and the STM32 libraries for CMSIS.

I created the below function for configuring my GPIOs. First, I set port A4 as analog for the DAC. This works like a charm as it is. Then, I configure some outputs for the onboard LEDs on Port D and two more on port E that I need for my application. The problem is that these did not work until I (accidentaly) duplicated the line that enables the corresponding periph clock. I am at a lost as to why this may be! Any ideas?

Just to be clear, code compiles either way. It´s just that if I enable the clocks only once, the pins are always at 0 v, regardless if I set or clear them. I have not yet tested the Analog ports and USART defined after the digital outs.

void setupGPIO(void){
    static GPIO_InitTypeDef GPIO_InitStruct;

    GPIO_StructInit(&GPIO_InitStruct);
    RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; //BUTTON
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA,&GPIO_InitStruct);

    GPIO_StructInit(&GPIO_InitStruct);

    //Setup PA4 as Analog
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; //DAC Output
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    //GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;



    GPIO_Init(GPIOA,&GPIO_InitStruct);
    GPIO_SetBits(GPIOA,GPIO_Pin_4);

    //Setup LED pins as Out

    GPIO_StructInit(&GPIO_InitStruct);
    RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //WHY?!?!?
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
    //GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStruct.GPIO_OType = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOD,&GPIO_InitStruct);

    //PE7 and PE8 as digital Outs
    //PE7 ~HC05 VDD
    //PE8 HC05 AT Mode
    GPIO_StructInit(&GPIO_InitStruct);
    RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);// WHY!?!?!
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
    //GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStruct.GPIO_OType = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOE,&GPIO_InitStruct);

     //Setup PC4 and PC5 Analog PIN
     GPIO_StructInit(&GPIO_InitStruct);
     RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
     GPIO_Init(GPIOC,&GPIO_InitStruct);

     //Setup PD5 and PD6 as USART pins
     GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2);
     GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2);
     GPIO_StructInit(&GPIO_InitStruct);
     RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_Init(GPIOD,&GPIO_InitStruct);



}

Solution

  • It looks like two different function calls to me.

    // Enables or disables the Low Speed APB (APB1) peripheral clock.
    // notice the APB1 in the function name.
    RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    
    // Enables or disables the AHB1 peripheral clock.
    // notice the AHB1 in the function name.
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);