We have an ATMega2560 with the following, rather simple test application:
#define VARIANT_SERIAL_1_ONLY 1
#define VARIANT_SERIAL_3_ONLY 2
#define VARIANT_SERIAL_1_PLUS_3 3
#define VARIANT_TOGGLE_SERIALS 4
unsigned long g_uLastSendSerial1 = 0;
unsigned long g_uLastSendSerial3 = 0;
unsigned long g_uLastDebugOutput = 0;
unsigned long g_uSerial1Received = 0;
unsigned long g_uSerial3Received = 0;
unsigned char g_uToggle = 0;
unsigned char g_uVariant = VARIANT_TOGGLE_SERIALS;
void setup( void )
{
ConfigurePortC();
// it does not seem to matter whether UART0 is enabled or not
Serial.begin( 115200 );
if( g_uVariant == VARIANT_SERIAL_1_ONLY || g_uVariant == VARIANT_SERIAL_1_PLUS_3 || g_uVariant == VARIANT_TOGGLE_SERIALS )
{
// we need UART1
Serial1.begin( 115200 );
}
// it does not seem to matter whether UART2 is enabled or not
Serial2.begin( 115200 );
if( g_uVariant == VARIANT_SERIAL_3_ONLY || g_uVariant == VARIANT_SERIAL_1_PLUS_3 || g_uVariant == VARIANT_TOGGLE_SERIALS )
{
// we need UART3
Serial3.begin( 115200 );
}
} // setup
void loop()
{
if( (GetMillis() - g_uLastDebugOutput) > 1000 )
{
g_uLastDebugOutput = GetMillis();
Serial.print("Serial 1: ");
Serial.print(g_uSerial1Received);
Serial.println();
Serial.print("Serial 3: ");
Serial.print(g_uSerial3Received);
Serial.println();
if( g_uVariant == VARIANT_TOGGLE_SERIALS )
{
if( g_uToggle )
{
Serial.print("Serial3 end");
Serial.println();
Serial3.end();
}
else
{
Serial.print("Serial3 begin");
Serial.println();
Serial3.begin( 115200 );
}
g_uToggle = !g_uToggle;
}
}
ReadSerial1();
ReadSerial3();
if( (GetMillis() - g_uLastSendSerial1) > 100 )
{
unsigned char aCommand[5] = { 1, 2, 3, 4, 5 };
g_uLastSendSerial1 = GetMillis();
Serial1.write( aCommand, 5 );
}
if( (GetMillis() - g_uLastSendSerial3) > 250 )
{
unsigned char aCommand[5] = { 1, 2, 3, 4, 5 };
g_uLastSendSerial3 = GetMillis();
Serial3.write( aCommand, 5 );
}
} // loop
void ReadSerial1( void )
{
unsigned char uByte;
while( Serial1.available() > 0 )
{
g_uSerial1Received ++;
uByte = Serial1.read();
}
} // ReadSerial1
void ReadSerial3( void )
{
unsigned char uByte;
while( Serial3.available() > 0 )
{
g_uSerial3Received ++;
uByte = Serial3.read();
}
} // ReadSerial3
unsigned long GetMillis( void )
{
return millis();
} // GetMillis
void ConfigurePortC( void )
{
DDRC = 0b00111111;
PORTC = 0b00000100;
} // ConfigurePortC
Depending to how we set g_uVariant, we receive the following outputs:
unsigned char g_uVariant = VARIANT_SERIAL_1_ONLY;
Serial 1: 92
Serial 3: 0
Serial 1: 322
Serial 3: 0
Serial 1: 552
Serial 3: 0
...
unsigned char g_uVariant = VARIANT_SERIAL_3_ONLY;
Serial 1: 0
Serial 3: 66
Serial 1: 0
Serial 3: 154
Serial 1: 0
Serial 3: 242
...
The above cases are clear and expected ... but the next one is bothering us extremely:
unsigned char g_uVariant = VARIANT_SERIAL_1_PLUS_3;
Serial 1: 0
Serial 3: 66
Serial 1: 0
Serial 3: 154
Serial 1: 0
Serial 3: 242
In fact we do not receive any byte from Serial1 in case Serial3 is active (= between Serial3.begin() and Serial3.end().
The final variant seems to prove clearly that Serial1 is working or not working depending to whether Serial3 is active:
unsigned char g_uVariant = VARIANT_TOGGLE_SERIALS;
Serial 1: 0
Serial 3: 66
Serial3 begin
Serial 1: 0
Serial 3: 154
Serial3 end
Serial 1: 230
Serial 3: 154
Serial3 begin
Serial 1: 230
Serial 3: 242
Serial3 end
Serial 1: 460
Serial 3: 242
Serial3 begin
Serial 1: 460
Serial 3: 330
Serial3 end
...
We do not think that the pins of Serial3 and Serial1 are connected but we are going to check the Tx and Rx pins of Serial1 in a next step (at the board of interest, we do not have good access to these lines). We think to have checked everything else at the board already ... and we think that this simple sketch does not contain anything which should cause this effect.
We are glad about any hint about what could be wrong here ... does anybody have any piece of clue about what we could do wrong here and/or about how we could use Serial1 and Serial3 in parallel here?
Thanks in Advance,
Markus