void digitalWrite(uint8_t pin, PinStatus val)
{
// omitted
if(port->DIR & bit_mask){
/* Set output to value */
// omitted
/* Input direction */
} else {
/* Old implementation has side effect when pin set as input -
pull up is enabled if this function is called.
Should we purposely implement this side effect?
*/
/* Get bit position for getting pin ctrl reg */
uint8_t bit_pos = digitalPinToBitPosition(pin);
/* Calculate where pin control register is */
volatile uint8_t* pin_ctrl_reg = getPINnCTRLregister(port, bit_pos);
/* Save system status and disable interrupts */
uint8_t status = SREG;
cli();
if(val == LOW){
/* Disable pullup */
*pin_ctrl_reg &= ~PORT_PULLUPEN_bm;
} else {
/* Enable pull-up */
*pin_ctrl_reg |= PORT_PULLUPEN_bm;
}
/* Restore system status */
SREG = status;
}
}
MegaAVR's digitalWrite() does not change the OUT register of PORT when pinMode is not OUTPUT (operation is different from UNO r3 etc.).
However, SoftwareSerial's setTx() function is written to set the OUT register to H level using digitalWrite() before setting pinMode to OUTPUT. As a result, the OUT register is not set, the output becomes L, and it seems that the appropriate output for the UART is not obtained.
I am using Arduino nano every, Board lib: Arduino megaAVR Boards 1.8.8.
I tried the following program:
The expectation is that "START(CR)ABC(CR)DEF(CR)" will be displayed, but what I actually got was the following:
This works fine:
Looking inside SoftwareSerial.cpp, it looks like the following.
and in wiring_digital.c
MegaAVR's digitalWrite() does not change the OUT register of PORT when pinMode is not OUTPUT (operation is different from UNO r3 etc.). However, SoftwareSerial's setTx() function is written to set the OUT register to H level using digitalWrite() before setting pinMode to OUTPUT. As a result, the OUT register is not set, the output becomes L, and it seems that the appropriate output for the UART is not obtained.