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?

  • 3
    \$\begingroup\$ How you need to set up the GPIO port registers is something you'll find in the device datasheet. It varies from device to device, even with the same architecture. \$\endgroup\$ – Matt Young Sep 2 '13 at 5:34
  • \$\begingroup\$ would love some examples. \$\endgroup\$ – Raaj Sep 2 '13 at 5:54
  • \$\begingroup\$ the datasheets/users manuals for the parts are online, and they do show you how to configure, including tables for the different modes (digital input, open drain, digital output, etc.) Just take the values from the table and plug them in the registers \$\endgroup\$ – old_timer Sep 9 '13 at 18:55
  • \$\begingroup\$ Just in case, someone is searching for manual that describes gpio in details: st.com/web/en/resource/technical/document/reference_manual/… \$\endgroup\$ – Alexey Shmalko Aug 30 '14 at 12:01

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);
  • \$\begingroup\$ You may want to include example defines for the different LEDx, since those are not part of the standard peripheral library. Good point showing the clock enabling. That's an easy thing to forget. \$\endgroup\$ – fm_andreas Sep 2 '13 at 21:27
  • \$\begingroup\$ I know this, but I want to mess with the registers. I suppose I could study the library code. :( But it is rather painful \$\endgroup\$ – Raaj Sep 3 '13 at 18:25
  • \$\begingroup\$ If you want to "mess with the registers" then you need to study the datasheet. From the library code you wouldn't know why certain bits are set or cleared. \$\endgroup\$ – fm_andreas Sep 3 '13 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.