GPIO: What Happens at the MOSFET Level When You Run Your Code? - Part 1

:warning:Note: The internal GPIO structure has been simplified for better conceptual understanding. (Components such as
ESD protection diodes, pull-up and pull-down resistors, etc., are not shown.)
:warning:Warning / Disclaimer This analysis is based on my personal study and interpretation of technical documents. Therefore, I cannot guarantee that it fully represents the actual internal behavior of all STM32 microcontrollers. This is a simplified conceptual model for educational purposes.
INTRODUCTION
In this document, we will examine how our code affects GPIO units at the MOSFET level. Our analysis focuses on the MOSFET switching logic in General Purpose Output (Push-Pull, Open-Drain) and Input Mode (Analog and Alternate Function modes are not included in this analysis). I used the following documents as references for this analysis and the visuals:
- AN4899 Application Note (STMicroelectronics)
- RM0365 Reference Manual (STM32F3 series)
Since this analysis is based on the STM32 architecture, we will focus on MODER, OTYPER, and ODR registers.
I would be very happy if ST employees or experienced engineers could check my work. Please let me know if you find any mistakes or if there is anything I can improve. I am eager to learn from your feedback!
How to read the table?
-
Register Selection: Select MODER, OTYPER, and ODR values in order.
-
Output Control: The Output Controller sends the resulting Signal 1 and Signal 2 to the MOSFET Gates.
-
Note: "X" indicates that the selection is not available or has no effect for that mode.
PIN MODES
1. OUTPUT MODE
1.1 What is Push-pull?
Briefly, it is a MOSFET pair that forces the line to a specific logic level. It is easier to understand with an example:
- To make the line HIGH: PMOS is ON (conducting), NMOS is OFF (cut-off).
- To make the line LOW: NMOS is ON (conducting), PMOS is OFF (cut-off).
1.2 What is Open-drain?
Briefly, the PMOS is always OFF (cut-off). Only the NMOS switches between ON and OFF states. This is why we use this mode for I2C. As you may remember, in I2C, the line is either released (floats/HIGH with pull-up) or pulled LOW.
2. INPUT MODE
In input mode, both MOSFETs (PMOS and NMOS) are in the OFF (cut-off) state. This allows the external signal to be read correctly by the processor (via the Input Data Register).
QUICK NOTES ON REGISTER STRUCTURES
- The two most fundamental modes of a GPIO are Input and Output. We use the MODER register to select between these modes.
- If Input Mode is selected: This step does not apply. If General Purpose Output Mode is selected: We use the OTYPER (Output Type Register) to choose between:
- Push-pull
- Open-drain
- ODR (Output Data Register) register stores the output data. Example: When the pin is in Push-pull mode, if the relevant bit of the pin in the ODR structure is 1, a HIGH signal is generated; if it is 0, a LOW signal is generated.
