STM32F3 and STM32F4 GPIO.

An article about configuring STM32 pins was published on this site earlier; it was dedicated to the F1 series microcontrollers, although the article didn't say so explicitly.
This article covers the F3 and F4 series microcontrollers.
The block diagram of a pin looks like this.
STM32F3 and STM32F4 GPIO.

Each MCU pin can be configured in one of the states.
STM32F3 and STM32F4 GPIO.

Just in case, I'll explain which bit field is responsible for what.

MODER sets the pin mode
00: input mode (after reset)
01: output mode
10: alternate function mode
11: analog mode

OTYPER sets the output type
0: push-pull output, PP for short (after reset)
1: open-drain, OD for short

OSPEEDR sets the output switching speed
x0: 2 MHz
01: 10 MHz
11: 50 MHz

PUPDR sets the pin pull-up/pull-down
00: no pull-up or pull-down
01: pull-up to the positive supply, PU for short
10: pull-down to ground, PD for short
11: reserved


//We assume the pins are in floating input mode after reset
    
//enable port A clocking   
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
		
		//push-pull output 10MHz 
		GPIOA->MODER |= GPIO_MODER_MODER0_0;
		GPIOA->OTYPER &= ~GPIO_OTYPER_OT_0;
		GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR0_0;	
		
		//open-drain output 2MHz with pull-up
		GPIOA->MODER |= GPIO_MODER_MODER0_0;
		GPIOA->OTYPER |= GPIO_OTYPER_OT_0;
		GPIOA->OSPEEDR &= ~GPIO_OSPEEDER_OSPEEDR0;	
		GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_0;
		
		//alternate function, push-pull output without pull-up, 50 MHz
		GPIOA->MODER |= GPIO_MODER_MODER0_1;
		GPIOA->OTYPER &= ~GPIO_OTYPER_OT_0;
		GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR0;
		GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR0;
		
		//alternate function, open-drain output with pull-up, 50 MHz
		GPIOA->MODER |= GPIO_MODER_MODER0_1;
		GPIOA->OTYPER |= GPIO_OTYPER_OT_0;
		GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_0;
		GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR0;
		
		//input with pull-up
		GPIOA->MODER &= ~ GPIO_MODER_MODER0;
		GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_0;
		
		//input with pull-down
		GPIOA->MODER &= ~ GPIO_MODER_MODER0;
		GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_1;
		
		//analog mode
		GPIOA->MODER |= GPIO_MODER_MODER0;
		GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR0;


It should be noted that most pins are configured as floating input after reset
(input floating), except for the debug pins, which are configured as inputs with pull-up/pull-down.
PA15: JTDI in pull-up
PA14: JTCK/SWCLK in pull-down
PA13: JTMS/SWDIO in pull-up
PB4: NJTRST in pull-up
PB3: JTDO/TRACESWO

The ODR, BRR and IDR registers work the same way as in the F1 series, while the 32-bit BSRR register is divided into two parts: the lower one is used to set bits — BSRRL, the upper one to reset them — BSRRH.

//set all port outputs to 1
GPIOA->ODR = 0xFFFF;

//reset bit zero of port A
GPIOA->BRR = GPIO_BRR_BR_0;

//set bit zero
GPIOA->BSRRL = GPIO_BSRR_BS_0;
//reset bit zero 
GPIOA->BSRRH = GPIO_BSRR_BS_0;//it most likely worked this way because 
//the register is split into two parts; I thought it would work like this 
//GPIOA->BSRRH = GPIO_BSRR_BR_0;

//check the value of pin zero of port A
if(GPIOA->IDR & GPIO_IDR_0)


The most interesting part is left: figuring out how to configure a pin to perform alternate functions.
On microcontrollers of these series, you have to specify exactly which alternate function corresponds to a given pin. Alternate functions are configured using two registers, AFRL and AFRH. AFRL configures the 8 lower pins of the port, AFRH the 8 upper pins, i.e. pins 8 to 15. The alternate functions of a pin can be found in the MCU datasheet.
STM32F3 and STM32F4 GPIO.

For example, we want to assign the function of channel one of timer four (TIM4_CH1) to pin PD12. To do this we must write the alternate function number to AFRH5. (Since pins 0 to 7 are configured in AFRL, pin 12 is configured by AFRH (12 - 7 = 5).)
The alternate function number corresponds to the column number, in this case AF2, so we need to write 2 to AFRH5.

Before moving on to the implementation, note that 4 bits are allocated in the AFR registers for configuring each pin.
STM32F3 and STM32F4 GPIO.


       //enable port and timer clocking
	RCC->AHBENR |= RCC_AHBENR_GPIODEN; 
	RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
        //alternate function push-pull without pull-up, 50MHz
	GPIOD->MODER |= GPIO_MODER_MODER12_1;// 
        GPIOD->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR12;
	GPIOD->PUPDR &= ~GPIO_PUPDR_PUPDR12;
        //assign the alternate function to pin 12 
	GPIOD->AFR[1] |= (0x02<<4*4);


We've figured out pin configuration; now a little additional information. If the HSE and LSE oscillators are turned off, the pins to which the crystals are connected can be used as ordinary pins. If the MCU is clocked from an external source (which needs only one pin), the free pin can be used for your own purposes. It should also be noted that any pin can be configured for an external interrupt.

All in all, nothing complicated, and in the next article I'll describe how open-drain mode can be used and why push-pull can't replace it.