[otw_is sidebar=otw-sidebar-1]

STM32 family provide two options while using a GPIO as output. We could use any GPIO as either in Open Drain mode  or in push pull manner. We can also choose the operating frequency. Which means we can use stm32 GPIO with low frequency output, medium frequency or High Frequency as HAL GPIO header file suggest. What these speed do? Well actually it change slew rate . Further details could be found in their GPIO related application note

three or five volt tolerant GPIO structure.

Here is how HAL header file define the speeds 

 

/*!< IO works at 2 MHz, please refer to the product datasheet */
#define  GPIO_SPEED_FREQ_LOW         ((uint32_t)0x00000000)
  /*!< range 12,5 MHz to 50 MHz, please refer to the product datasheet */ 
#define  GPIO_SPEED_FREQ_MEDIUM      ((uint32_t)0x00000001) 
/*!< range 25 MHz to 100 MHz, please refer to the product datasheet  */
#define  GPIO_SPEED_FREQ_HIGH        ((uint32_t)0x00000002) 
/*!< range 50 MHz to 200 MHz, please refer to the product datasheet  */  
#define  GPIO_SPEED_FREQ_VERY_HIGH   ((uint32_t)0x00000003) 

 

Here you can see that for more clarification you may need to refer the resective part datasheet from manufacturer’s  website. But you may get the idea.

 Why speed is important?

Speed is important because if you want to generate a specific frequency on that output pin or if you are communicating with other device which also very fast then you may face miscommunication if you choose the wrong operating frequency for the respective pin on your STM32 side. So be very clear about the maximum frequency if you are talking to some other device or microcontroller on that GPIO Pin. 

STM32 GPIO OUTPUT as Open Drain

[otw_is sidebar=otw-sidebar-2]

 

You can use the GPIO pin as Output in Open Drain manner if you need to drive high current load. You can provide external pull-up or pull-down resistors. There is a high chance that you leave the GPIO pin floating if you do not provide the external pull-up or pull-down resistors. So always keep in mind to be very sure before you use in this state. Most of the time the need will be fulfilled if you use in Push-Pull state. 

STM32 GPIO Push-Pull Output

To use GPIO as output in Push-Pull manner actually two MOSFETs are used in push pull manner(as shown in figure below). In this manner it is made sure that the pin is not in floating state. So it is either in Logic High or in Logic Low state. Because in any case one of the MOSFET is conducting and other will remain OFF.    

stm32 gpio iNTERNAL Circuit

 

 

GPIO Related Registers

Here are list of the registers involved in any GPIO from port A to H or any number of GPIO port you have in your concerned microcontroller according to the datasheet. The following abbreviations are used in register descriptions (x = A to H): 

GPIOx_MODER: GPIO port mode register 

GPIOx_OTYPER: GPIO output type register 

GPIOx_OSPEEDR: GPIO output speed register 

GPIOx_PUPDR: GPIO port pull-up / pull-down register 

GPIOx_IDR: GPIO port input data register 

GPIOx_ODR: GPIO port output data register 

GPIOx_BSRR: GPIO port it set / reset register 

GPIOx_LCKR: GPIO port configuration lock register 

GPIOx_AFRL: GPIO alternate function low register

 GPIOx_AFRH: GPIO alternate function high register 

GPIOx_ASCR: GPIO port analog switch control register

 

 

So it is clear that we need to configure at least these registers to make sure that our desired settings are configured and we are ready to use that GPIO pin in our desire form. 

GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR: