Skip to main content
VTara
Associate
February 6, 2019
Solved

How to configure Push-Pull/Open drain and Pull up/down for GPIO configured as AF?

  • February 6, 2019
  • 3 replies
  • 5464 views

I can not find any instruction in documents about gpio configuration in alternate function mode. For example I want to configure SPI GPIO. Examples found in the internet have different configuration. Is it unimportant to configure push pull and pull up/down parameters?

Best answer by Andreas Bolsch

You can't "get" such information magically from somewhere but you have to think about *your* requirements: E. g. SPI clock speed determines the slew rate setting, I2C usually requires open-drain, for an input connected to a simple switch you would use a pull-up if the switch's is connected to GND etc. (however, in a noisy environment with a remote switch the internal pull-up might be to weak and some sort of protection necessary).

3 replies

Andreas Bolsch
Lead III
February 6, 2019

The alternate function settings have litte impact on the port configuration: output state is taken from the "alternate" peripheral rather than from ODR. The output configuration (pp vs. od, pull-up/-down) is still handled in the very same was as for normal GPIO, i e. via OTYPER, OSPEEDR, PUPDR., see 6.3.2 im RM:

"Peripheral alternate function:

– Connect the I/O to the desired AFx in one of the GPIOx_AFRL or GPIOx_AFRH register.

– Select the type, pull-up/pull-down and output speed via the GPIOx_OTYPER,

GPIOx_PUPDR and GPIOx_OSPEEDER registers, respectively.

– Configure the desired I/O as an alternate function in the GPIOx_MODER register."

and the following paragraph.

The only exception are "special" pins, e. g. oscillator, where the activation overrides GPIO settings.

G0 family is not different to others in this respect.

VTara
VTaraAuthor
Associate
February 6, 2019

OK, I understand that I need to configure GPIOx_MODER, GPIOx_OTYPER, GPIOx_PUPDR and GPIOx_OSPEEDER. But which values should be programmed? Where can I get such information? And this question is not about G0 family only or SPI GPIO only. I really do not understand where to get particular values for programming GPIO registers for GPIO in alternate function mode.

Andreas Bolsch
Andreas BolschBest answer
Lead III
February 6, 2019

You can't "get" such information magically from somewhere but you have to think about *your* requirements: E. g. SPI clock speed determines the slew rate setting, I2C usually requires open-drain, for an input connected to a simple switch you would use a pull-up if the switch's is connected to GND etc. (however, in a noisy environment with a remote switch the internal pull-up might be to weak and some sort of protection necessary).

VTara
VTaraAuthor
Associate
February 6, 2019

Thank you. It seems I need to improve my knowledge in electrical details of MCU peripherals :) Thank you. The problem is solved.

In fact, I saw some recomendations for f103 family. That's why I asked this question.

Andrew Neil
Super User
July 31, 2024

@VTara wrote:

It seems I need to improve my knowledge in electrical details of MCU peripherals


Indeed - and standard interfaces such as I2C and SPI.

The MCU peripheral just implements the requirements of the standard.

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Graduate II
July 31, 2024

Hi

I highly recommend the 'STM32F0 Arm Cortex M0' book which also covers this topic. Author i.a. Muhammad Ali Mazidi. It can be bought online.