Saturday, 15 April 2017

configuration - What actually happens when we set direction on GPIOs in controllers ?


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?



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.


No comments:

Post a Comment

arduino - Can I use TI&#39;s cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...