Electronic – STM32 GPIO Configuration

armcortex-mgpiostm32

I am trying to figure out some online resource to figure out how to configure the GPIO Registers on an STM32F4 Cortex M4 board. I only have examples for the Cortex M4

typedef struct
 {
   __IO uint32_t MODER;    /*!< GPIO port mode register,               Address offset: 0x00      */
   __IO uint32_t OTYPER;   /*!< GPIO port output type register,        Address offset: 0x04      */
   __IO uint32_t OSPEEDR;  /*!< GPIO port output speed register,       Address offset: 0x08      */
   __IO uint32_t PUPDR;    /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */
   __IO uint32_t IDR;      /*!< GPIO port input data register,         Address offset: 0x10      */
   __IO uint32_t ODR;      /*!< GPIO port output data register,        Address offset: 0x14      */
   __IO uint16_t BSRRL;    /*!< GPIO port bit set/reset low register,  Address offset: 0x18      */
   __IO uint16_t BSRRH;    /*!< GPIO port bit set/reset high register, Address offset: 0x1A      */
   __IO uint32_t LCKR;     /*!< GPIO port configuration lock register, Address offset: 0x1C      */
   __IO uint32_t AFR[2];   /*!< GPIO alternate function registers,     Address offset: 0x24-0x28 */
 } GPIO_TypeDef;

Here are the registers. But there is nothing online that shows me how to actually configure a pin to say output/input etc. Can someone point me a resource?

Best Answer

If you are using the ST Standard Peripheral library (downloadable from the STM32F4 page, there is also the USB library, plus more stuff), then have a read of the documentation (it's a.chm file at the top level of the zip) and check the example code. All the functions to setup IOs, peripherals, etc are in there.

Here are a couple of snippets showing initialising and using an IO pin from some of my code.

Setting up pins:

#include "stm32f4xx.h"

void LED_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    /* Enable the GPIO_LED Clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

    GPIO_InitStructure.GPIO_Pin = LED1 | LED2 | LED3 | LED4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
}

Toggling LED:

    GPIO_SetBits(GPIOD, LED1);
    GPIO_ResetBits(GPIOD, LED2);