Electronic – first need to set a value and then direction of the GPIO in an embedded processor

embeddedgpio

I have a full time job as a firmware engineer. I've recently been given a task to review GPIO configurations and change the settings as needed. I found a few pins that were incorrectly configured so naturally I reconfigured them, however I was told I did it in the wrong order. Here is what I'm talking about:

Before:
GPIO1.direction = INPUT;

After:
GPIO1.direction = OUTPUT;
GPIO1.value = 0;

However during the code review I've been told that I need to change the order of initialization to the following:

GPIO1.value = 0;
GPIO1.direction = OUTPUT;

In other words set the value first and then set direction of the pin. I've also been told that this is how it needs to be on the modern processors because they use two registers, one for input and one for output, however old processors use only one register, so the order of operations wouldn't matter.
(Note: Modern = ARM Cortex M3 and above, Old = Intel 8051)

I asked for a better explanation at work, but I couldn't get a good answer. That's why I decided to ask here.

So here are my questions:

  1. Why does the order of initialization matter on the new processors?
  2. Why does the order of initialization not matter on the old processors?
  3. What two registers are they talking about in the modern processors?
  4. What single register are they talking about on the old processors?

If someone could provide some sort of a diagram, that would be even better.

Best Answer

The original 8051 used so-called pseudo-bidirectional output ports (open-drain with pullups), so there was really no port direction setting.

Of course for modern true bidirectional output ports it's better to have a known value set before enabling the port pin for output, because otherwise you could have a transient on the output that could do something undesirable.

See my answer here, for example.

Edit: Here is the I/O pin structure for a (relatively) modern CMOS microcontroller:

enter image description here

TRIS (TRIState) is called DDR (Data Direction Register) in many other micros. In this case, if the TRIS latch output is high then both transistors are 'off', but the port can still be read.

Here is a slightly more complex I/O pin structure for a newer Microchip micro.

enter image description here

Again, the TRIS latch disables the output. This one includes a LAT latch that helps avoid read-modify-write issues. On the PIC series you should write to the LAT register only (and read from the PORT register).

Here is the original 8051 and CMOS 8051 classic I/O port pin internal circuitry (from this source):

enter image description here

There's a bit of extra complexity in that there is a speed-up transistor in parallel with the pull-up that is briefly turned on to overcome external capacitance. As you can see, there is no TRIS/DDR control at all. The pull-up MOSFETs used in normal operation are 'weak'- they are small enough (low Idss) that an external output connected to the pin can pull the pseudo-bidirectional port line low.