GolamMostafa:
4. From the ISR of Step-3, the MCU calls upon the serialEvent() SUR.
5. After finishing the serialEvent() SUR, the MCU goes back to ISR.
No, you have this wrong. serialEvent() is not called from the ISR in recent versions of Arduino AVR Boards. Years ago it was but it is not now done that way. I haven't looked at how things are implemented in hardware packages other than Arduino AVR Boards.
This code reproduces what happens in a slightly simplified manner. Part of the simplification I've made is to only deal with Serial rather than Serial1-3 as actually happens but the other Serial interfaces are handled exactly the same.
int main(void)
{
init();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (Serial.available()) {
serialEvent();
}
}
return 0;
}
GolamMostafa:
Question: Assume that the user is not doing any data processing in the serialEvent() SUR, that means that the user has not explicitly declared the serialEvent() SUR by the statement void serialEvent(){ //codes }; does the MCU still make a jump from ISR to the empty serialEvent() SUR?
Well, as I said, there is no call of serialEvent() from an ISR.
However, it is interesting to consider what happens when serialEvent() is not in use. This is not entirely clear to me because we have this strange construct:
if (serialEventRun) serialEventRun();
and:
if (Serial0_available && serialEvent && Serial0_available()) serialEvent();
serialEventRun() and serialEvent() are given the weak attribute in HardwareSerial.h/HardwareSerial.cpp. serialEventRun() is defined in HardwareSerial.cpp but serialEvent() is not defined anywhere in the core.
What I do know is that if you compile this sketch for Uno with Arduino IDE 1.8.5/Arduino AVR Boards 1.6.21:
void setup() {}
void loop() {}
The size is:
Sketch uses 444 bytes (1%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.
If you compile this sketch:
void setup() {}
void loop() {}
void serialEventRun() {}
The size is:
Sketch uses 434 bytes (1%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.
If you do a loop speed test you will also find the latter is something like 0.5 us faster. So there is overhead for this serialEventRun() call even when, as with 99.99% of Arduino users, you never use the serialEvent() feature. It's not a lot of overhead but I take any code that runs on every single loop seriously. Well at least there is a simple way to avoid the overhead.