Electronic – What actually happens when we set direction on GPIOs in controllers

Architecturebidirectionalconfigurationgpio

Generally, while working on GPIO pins of any controller/device, we set the directions for the GPIO pins. Let's say in LPC2148(ARM7 core) controller, we have IODIR to configure the direction of Pin. We set the IODIR for output and clear for input.

IODIR0 |= (1 << 26); //output
IODIR0&= ~(1 << 10); //input

How setting/clearing the pin makes it as output/input. I think it is due to pull-up and pull-down transistors but I couldn't conclude a specific reason.

And another question, Is configuration of the direction of IO varies depending on the architecture/CPU we use?

Best Answer

I couldn't find the specific internal schematic for NXP's part in the User Manual, so I will explain using the one found in a ATMEL ATMega328, but they should look similar.

enter image description here

The picture shows the internal schematic for a single pin. The highlight part is the port direction pin part. When The port is set to output (via the databus, where one can set each pin individually as Input or Output; and via the Internal control signal WDx, which "strobes" the data on the data bus to the flip-flop), the buffer is activated and begins to drive the pin to the logic levels (voltages) on the output pin*.

When the pin is set as input, the enable pin on that buffer is configured so the buffer is a High Impedance output, thus not driving the pin to any specific voltage level. That becomes a task for the circuitry connected to that pin in the circuit outside the microcontroller. Note, in the Atmel's case, that reading the logic state of a pin (bottom part of schematic) always reads the actual state of that pin, doesn't matter if it is a input or output pin. Configuring a pin as input also enables the user to enable the pull-up resistor inside the microcontroller.

*: I am ignoring open colector and other types of pins because I think it is not relevant to this question. The workings should be similar, though.

How setting/clearing the pin makes it as output/input. I think it is due to pull-up and pull-down transistors but I couldn't conclude a specific reason.

Note that you set the direction register, not the pin state itself. They are different things. Setting/clearing the pins tells the hardware which voltage is to be put on the output of the output buffer (when it is enabled by the enable pin), while setting/clearing the direction of the pin tells the micro if the output buffer will be enabled or disabled at all.