51
\$\begingroup\$

In STM32 Standard Peripheral library, we need to configure the GPIO.

But there are 3 functions which I not sure how to configure them;

  • GPIO_InitStructure.GPIO_Speed
  • GPIO_InitStructure.GPIO_OType
  • GPIO_InitStructure.GPIO_PuPd

In the GPIO_Speed, there are 4 settings to pick from

GPIO_Speed_2MHz  /*!< Low speed */
GPIO_Speed_25MHz /*!< Medium speed */
GPIO_Speed_50MHz /*!< Fast speed */
GPIO_Speed_100MHz

How do I know which speed do I choose from? Is there any advantage or disadvantages using high speed or low speed? (eg: power consumption?)

In the GPIO_OType, there are 2 settings to pick from

GPIO_OType_PP // Push pull
GPIO_OType_OD // Open drain

How to know which to choose from? and what is open drain and push pull?

In the GPIO_PuPd, there are 3 settings to pick from

GPIO_PuPd_NOPULL // No pull
GPIO_PuPd_UP     // Pull up
GPIO_PuPd_DOWN   // Pull down

I think this settings is related to initial setting of push pull.

\$\endgroup\$
1

2 Answers 2

59
\$\begingroup\$
  • GPIO_PuPd (Pull-up / Pull-down)

In digital circuits, is is important that signal lines are never allowed to "float". That is, they need to always be in a high state or a low state. When floating, the state is undetermined, and causes a few different types of problems.

The way to correct this is to add a resistor from the signal line either to Vcc or Gnd. That way, if the line is not being actively driven high or low, the resistor will cause the potential to drift to a known level.

The STM32 (and other microcontrollers) have built-in circuitry to do this. That way, you don't need to add another part to your circuit. If you choose "GPIO_PuPd_UP", for example, it is equivelent to adding a resistor between the signal line and Vcc.

  • GPIO_OType (Output Type):

Push-Pull: This is the output type that most people think of as "standard". When the output goes low, it is actively "pulled" to ground. Conversely, when the output is set to high, it is actively "pushed" toward Vcc. Simplified, it looks like this:

pushpull

An Open-Drain output, on the other hand, is only active in one direction. It can pull the pin towards ground, but it cannot drive it high. Imagine the previous image, but without the upper MOSFET. When it is not pulling to ground, the MOSFET is simply non-conductive, which causes the output to float:

opendrain

For this type of output, there needs to be a pull-up resistor added to the circuit, which will cause the line to go high when not driven low. You can do this with an external part, or by setting the GPIO_PuPd value to GPIO_PuPd_UP.

The name comes from the fact that the MOSFET's drain isn't internally connected to anything. This type of output is also called "open-collector" when using a BJT instead of a MOSFET.

  • GPIO_Speed

Basically, this controls the slew rate (the rise time and fall time) of the output signal. The faster the slew rate, the more noise is radiated from the circuit. It is good practice to keep the slew rate slow, and only increase it if you have a specific reason.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ thanks! for the great answer ;), do you mind explain a bit more on different problems when it is in floating state? \$\endgroup\$
    – Tim
    Commented Feb 25, 2015 at 2:34
  • 3
    \$\begingroup\$ The idea is that dwelling for any length of time at an intermediate input voltage can partially turn on both the upper and lower FETs in an input block and partially short out the power supply through them, resulting in excessive power consumption or (in especially severe cases) potentially damage. \$\endgroup\$ Commented Feb 25, 2015 at 2:37
  • 3
    \$\begingroup\$ @Tim Also, when the lines are floating, it's very easy to perturb the voltage levels. Just waving your hand around the circuit can change the state of the input, because of capacitive interactions... \$\endgroup\$
    – bitsmack
    Commented Feb 25, 2015 at 2:50
  • \$\begingroup\$ @bitsmack last question! ;), when to use pull up or down? one possible scenario as you mention earlier is to use pull up during open drain, how about pull down? is it dependent on hardware or it doesnt matter pull up or down? \$\endgroup\$
    – Tim
    Commented Feb 25, 2015 at 6:22
  • 1
    \$\begingroup\$ @Tim It really depends on the application. If you're reading a switch (or button), then it doesn't really matter. If you're interfacing to other components, it depends on the interface. For example, for SPI communications, the CS line is active-low. In that case, you'd want a pull-up resistor, so the CS never goes low inadvertently. You might think that this is unnecessary if you will always actively drive the line with a microcontroller. But what about before the microcontroller initializes? Or if it resets? The pull-up resistor removes any ambiguity... \$\endgroup\$
    – bitsmack
    Commented Mar 3, 2015 at 7:17
5
\$\begingroup\$

GPIO Speed is the maximum frequency the GPIO can produce. Lower settings can save power.

Output type is whether the pin asserts highs and lows (push pull), or whether the output turns on the gate of a FET that is attached to the pin at the drain (Open drain). This can be convenient if you need any attached pin to be able to pull a bus low without short in out other pins.

Pull up resistors attach the pin output to the power rail, and pull down attaches it through a resistor to ground. This wiĺl, among other things, control the voltage of the pin even if the bit is in a high impedance state. This is important to do stuff like using an spot switch to change a digital input value. Even with the switch open, the input is predictable.

\$\endgroup\$

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