It might have something to do with the internal pull up resistor. According to the SAM3X/A datasheet,
Control of the pull-up resistor is possible regardless of the
configuration of the I/O line.
After reset, all of the pull-ups are enabled.
If you dig through all the include files you find:
Line 124 defines the
pinMode(uint32_t ulPin, uint32_t ulMode) function. Examining the switch/case statement for INPUT vs INPUT_PULLUP vs OUTPUT you see the following:
- INPUT sets a register to
reg = PORT_PINCFG_INEN.
- INPUT_PULLUP sets a register to
reg = (PORT_PINCFG_INEN | PORT_PINCFG_PULLEN)
- OUTPUT sets a register to
reg &= ~PORT_PINCFG_INEN.
The 'register' in each case is the same. I cannot for the life of me find what value PORT_PINCFG_INEN or PORT_PINCFG_PULLEN are defined as, but they're no doubt just 8-bit masks (they're cast to uint8_t when they're assigned to the 'register'). Thus, we can assume that whichever bit controls input/output is active when asserted, as is the pullup bit. For example:
PORT_PINCFG_INEN = b'00000001';
PORT_PINCFG_PULLEN = b'00000010';
~PORT_PINCFG_INEN = b'11111110';
If pull-ups are enabled after reset, we can say that on reset:
reg = b'xxxxxx1x';
Point (3) above strongly implies that the instruction is:
reg = b'xxxxxx1x' & 'b11111110';
reg = b'xxxxxx10'; // pull-up is enabled!
Therefore, if you call pinMode(X, OUTPUT) before anything else you will end up with the pullup resistor enabled. Setting the pin to an input will clear the pullup enable bit, after which you can set the pin to an output and the bit will remain clear.
However, the whole arguement falls down with the simple fact that if you don't call pinMode() at all, the problem doesn't occur...