STM32 GPIO, or Input/Output Ports.

Each STM32 port consists of 16 pins, and each pin can be configured in one of 8 ways.
STM32 GPIO, or Input/Output Ports.

Below is the structure of an I/O port.
STM32 GPIO, or Input/Output Ports.

For a port to work, it must be connected to the APB2 bus by setting the corresponding IOPxEN bit in the peripheral clock enable register RCC_APB2ENR.

RCC->APB2ENR |= RCC_APB2ENR_IOPxEN; // Enable PORTx clocking.

After power-up, all pins are in the floating input state, also known as high-impedance input, Hi-Z or the third state.
  • The output driver is disabled
  • The Schmitt trigger is disabled
  • The pull-up/pull-down resistors are disabled
  • The IDR register always reads "0"


In input mode
  • The output driver is disabled
  • The input Schmitt trigger is enabled
  • Depending on the configuration, the pull-up/pull-down resistors are enabled
  • On every APB2 bus clock, the data from the input goes into the IDR register; by reading this register you can find out the pin state


In output mode
  • In Open Drain mode, writing "0" turns on the lower transistor, and writing "1" leaves the line unconnected
  • In Push Pull mode, writing "1" turns on the upper transistor, and writing "0" the lower one
  • The input Schmitt trigger is enabled
  • The pull-up/pull-down resistors are disabled
  • On every APB2 bus clock, the data from the output is transferred to the IDR register, from which it can be read in Open Drain mode
  • Reading the ODR register returns the last written value in Push Pull mode

 
In alternate function mode
  • The driver is enabled in Push Pull or Open Drain mode, depending on the configuration
  • The output driver is controlled by the peripheral signals, not by the ODR register
  • The input Schmitt trigger is enabled
  • The pull-up/pull-down resistors are disabled
  • On every APB2 bus clock, the data from the output is transferred to the IDR register, from which it can be read in Open Drain mode
  • Reading the ODR register returns the last written value in Push Pull mode

 
The table shows that two configurations are possible in alternate function mode: Push Pull and Open Drain. For example, suppose we want to configure the pin responsible for receiving data over USART in alternate function mode. For this, the Reference Manual RM0008, starting from page 161, has tables showing how to configure a pin for various peripherals.
STM32 GPIO, or Input/Output Ports.

Input floating or Input pull-up will suit us.
 
Pin configuration is set in the GPIOx_CRL and GPIOx_CRH registers; in these registers 4 bits are allocated for configuring each pin, MODE[1:0] and CNF[1:0]. GPIOx_CRL configures pins 0 to 7, and GPIOx_CRH pins 8 to 15.
STM32 GPIO, or Input/Output Ports.

STM32 GPIO, or Input/Output Ports.

If MODE[1:0] = 00, the pin is configured as an input, and the input configuration is then set by the CNF[1:0] bits. If MODE[1:0] is not 00, the pin is configured as an output, and the MODE [1:0] value sets the maximum frequency at which it can switch.
 

//We assume the pins are in floating input mode after reset
       
       //enable port A clocking
	RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
		
	//input with pull-up
	GPIOA->CRL &= ~GPIO_CRL_CNF0;
	GPIOA->CRL |= GPIO_CRL_CNF0_1;
	GPIOA->ODR |= GPIO_ODR_ODR0; 
	
	//input with pull-down
	GPIOA->CRL &= ~GPIO_CRL_CNF1;
	GPIOA->CRL |= GPIO_CRL_CNF1_1;
	GPIOA->ODR &= ~GPIO_ODR_ODR1; 
	
	//analog mode
	GPIOA->CRL &= ~GPIO_CRL_CNF2;
	
	//open-drain output 2MHz
	GPIOA->CRL &= ~GPIO_CRL_CNF3;
	GPIOA->CRL |= GPIO_CRL_CNF3_0;
	GPIOA->CRL |= GPIO_CRL_MODE3_1;
	
	//push-pull output 10MHz
	GPIOA->CRL &= ~GPIO_CRL_CNF4;
	GPIOA->CRL |= GPIO_CRL_MODE4_0;
	
	//alternate function, push-pull output, 50 MHz
	GPIOA->CRL &= ~GPIO_CRL_CNF5;
	GPIOA->CRL |= GPIO_CRL_CNF5_1;
	GPIOA->CRL |= GPIO_CRL_MODE5;
	
	//alternate function, open-drain output, 50 MHz
	GPIOA->CRL |= GPIO_CRL_CNF6;
	GPIOA->CRL |= GPIO_CRL_MODE6;

The input state can be read using the Port input data register, or GPIOx_IDR for short, where x is the port name, from A to G. The state of any pin can be read from the lower 16 bits; the upper 16 bits are not used.
STM32 GPIO, or Input/Output Ports.


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

If the port is configured as an output, its state can be controlled with the Port output data register, or GPIOx_ODR. The value we write to this register will appear on the corresponding port pins. The lower 16 bits are used to set the port state; the upper 16 bits are not used.
STM32 GPIO, or Input/Output Ports.


//if the pin is in input mode, the pull-up is activated
GPIOA->ODR |= GPIO_ODR_ODR0;
//or the pull-down
GPIOA->ODR &= ~GPIO_ODR_ODR0;
//if it is in output mode, the corresponding logic level will be set on it
//for example, this is how you can set all port outputs to 1
GPIOA->ODR = 0xFFFF;

STM32 lets you atomically control individual port bits using the GPIOx_BSRR (Port Bit Set/Reset Register) and GPIOx_BRR (Port Bit Reset Register) registers.
To set an individual port bit manually, you need to read the port value, change the required bit using a mask and write the result back to GPIOx_ODR. Since there are three actions, an interrupt occurring between them can corrupt the data. With the registers described above, this is done in one action.
To reset a bit, write a one to bit zero of GPIOx_BRR, and a 0 will be written to bit zero of GPIOx_ODR; the lower 16 bits are used for this operation, and the upper 16 bits are not used.
STM32 GPIO, or Input/Output Ports.


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

With GPIOx_BSRR it's a bit more interesting: the lower 16 bits are responsible for setting to 1, and the upper 16 bits for resetting to 0. To set bit zero to 1, write 1 to bit zero of GPIOx_BSRR. To set bit zero to 0, set bit 16 to 1.
STM32 GPIO, or Input/Output Ports.


//reset bit zero 
GPIOA->BSRR = GPIO_BSRR_BR0;
//set bit zero
GPIOA->BSRR = GPIO_BSRR_BS0;

STM32 lets you protect a port's configuration from being changed; the GPIOx_LCKR register is used for this. The lower 16 bits are used to select the pin you want to lock (a bit is selected by setting it to one), and then the lock is applied by a special sequence of writes to bit 16 (LCKK).
STM32 GPIO, or Input/Output Ports.

The sequence is as follows: write 1 to LCKK, write 0, write 1, then read 0 from the LCKR register, read 1. The last one read means that the pin is locked. The pin will only be unlocked after the controller is reset.
 

#include "stm32f10x.h"

uint32_t temp;
  	
int main(void)
{
        //enable port clocking
	RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
        //configure as a push-pull output
	GPIOA->CRL &= ~GPIO_CRL_CNF0;
        //with a maximum frequency of 50MHz
	GPIOA->CRL |= GPIO_CRL_MODE0;
	
        //select the pin we want to lock
	GPIOA->LCKR |= GPIO_LCKR_LCK0;
        //write 1
	GPIOA->LCKR |= GPIO_LCKR_LCKK;
        //write 0
	GPIOA->LCKR &= ~GPIO_LCKR_LCKK;
        //write 1
	GPIOA->LCKR |= GPIO_LCKR_LCKK;
        //read twice
	temp = GPIOA->LCKR;
	temp = GPIOA->LCKR;
}

For more detailed information, see the Reference Manual RM0008, section General-purpose and alternate-function I/Os (GPIOs and AFIOs).