Skip to main content
amoore
Associate
September 26, 2014
Question

Unusual GPIO Initialization

Posted on September 26, 2014 at 23:02

I'm maintaining some software for a STM32F103 and came across the following GPIO initialization code.

    /* Configure PA5 as push-pull output for LED */

    GPIO_WriteBit( GPIOA, GPIO_Pin_5, Bit_SET );

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_WriteBit( GPIOA, GPIO_Pin_5, Bit_SET );

    GPIO_WriteBit( GPIOA, GPIO_Pin_5, Bit_RESET );

    GPIO_WriteBit( GPIOA, GPIO_Pin_5, Bit_SET );

    GPIO_WriteBit( GPIOA, GPIO_Pin_5, Bit_RESET );

    GPIO_WriteBit( GPIOA, GPIO_Pin_5, Bit_SET );

    GPIO_WriteBit( GPIOA, GPIO_Pin_5, Bit_RESET );

    GPIO_WriteBit( GPIOA, GPIO_Pin_5, Bit_SET );

    GPIO_WriteBit( GPIOA, GPIO_Pin_5, Bit_RESET );

The first five lines are what I would expect. However the Bit_SET/Bit_RESET pairs are puzzling, and I can't imagine what purpose they would serve. I couldn't find anything in the device reference manual to suggest this was necessary. Any guesses?

    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    September 26, 2014
    Posted on September 27, 2014 at 01:26

    The first five lines are what I would expect. However the Bit_SET/Bit_RESET pairs are puzzling, and I can't imagine what purpose they would serve. I couldn't find anything in the device reference manual to suggest this was necessary. Any guesses?

    Someone wanted to trigger an external scope/analyzer, or test equipment, in a manner that indicated the device initialized and reached this point?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    megahercas6
    Associate III
    September 27, 2014
    Posted on September 27, 2014 at 10:28

    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIOA->BSRRH=GPIO_Pin_5; //set low
    GPIOA->BSRRL=GPIO_Pin_5;// set high