Serial differenciation...

Hello,

i try to understand how the serial interface(usb) is actualy making a difference between the things i send and receive during my program runtime, and the data that are sent/received during code upload

what if the data i send correspond to some binary sequence that will trigger the code upload functions in the arduino firmware ?

i just dont get it

thx

No differentiation is needed.

The bootloader ALWAYS gets control first. It waits a short time to see if any code upload is going to take place. During this time, the bootloader uses the serial interface.

Once the bootloader is done, it hands over control (meaning: jumps to) your program. Your program can now take control of the serial interface. There is no "binary sequence that will trigger the code upload functions in the arduino firmware."

Two caveats:

  1. The sequence "!!!" (three exclamation marks without the double quotes and without anything intervening) in the uploaded code means something special to the bootloader, so do NOT have this sequence in your program.

  2. If your code jumps to the bootloader, or if a reset occurs, the bootloader will of course get control again

It all comes down to timing. When you upload a new sketch from the IDE it opens the serial port. This toggles the DTE handshaking signal of RS232. The Arduino has the DTE signal hardwired into the reset circuit. (This is why your Arduino resets when you open the Serial Monitor. You may not have noticed it before, but it does.) Then a "helper" utility that is distributed as part of the IDE called "avrdude" kicks in and communicates with the Arduino (hopefully the bootloader on the Arduino).

When the Arduino is reset it starts executing the first bit of code in it's memory, the bootloader. If the bootloader then sees the right message come in over the RX line (I actually don't know this message, but I don't need to because I'm not into modifying bootloaders or avrdude) it will respond and (I would imagine) perform a quick handshake with avrdude to load a new sketch. If it doesn't see that message after a set period of time (I'm not quite sure what it is, but I'm sure applying google-fu could answer it) the bootloader times out and then hands execution to the previously loaded sketch which never releases execution control (barring a reset command or intentionally mucking with the execution pointer with assembler code). Because the bootloader doesn't (normally) execute again you can't accidentally force it to load data into sketch space. I suppose one could be extremely unlucky and have an incoming serial stream on digital pin 0 when during a manual reset with the reset button (or after a power glitch) that the bootloader would mistake as a command and normal handshaking between the bootloader and avrdude to overwrite the sketch space, but IMHO that would be extremely rare, bordering on nearly impossible. (But I could be wrong...)

Edit: Or what vaj4088 said... :wink:

Two caveats:

  1. The sequence "!!!" (three exclamation marks without the double quotes and without anything intervening) in the uploaded code means something special to the bootloader, so do NOT have this sequence in your program.

Only to the Mega bootloader.

vaj4088:
Two caveats:

  1. The sequence "!!!" (three exclamation marks without the double quotes and without anything intervening) in the uploaded code means something special to the bootloader, so do NOT have this sequence in your program.

well that's an issue..that i was expecting, hence my question
one is supposed to be able to send any kind of data through serial interface
i guess i'll have to use Serial1 instead

thanks for the infos

well that's an issue..that i was expecting, hence my question
one is supposed to be able to send any kind of data through serial interface
i guess i'll have to use Serial1 instead

You're not paying attention. The "!!!" sequence is ONLY an issue if you have a Mega bootloader AND it's an issue ONLY when the bootloader is receiving the data. The bootloader will NOT be running when your code is running AND you can't upload code on Serial1

PaulS:

well that's an issue..that i was expecting, hence my question
one is supposed to be able to send any kind of data through serial interface
i guess i'll have to use Serial1 instead

You're not paying attention. The "!!!" sequence is ONLY an issue if you have a Mega bootloader AND it's an issue ONLY when the bootloader is receiving the data. The bootloader will NOT be running when your code is running AND you can't upload code on Serial1

ok, that's right sorry about the mistake, indeed it concerns the bootloader...

i was talking about Serial1 for communication, not for code upload :slight_smile:

Sembazuru:
...The Arduino has the DTE signal hardwired into the reset circuit....

so what would be usefull is that when i run my C++ code, i engage the reset on the arduino
(and then wait a while that the bootloader finishes)

so what is the chain of states do i need to put the DTE(DTR?) line ?

high, low, high/low, low/high ??

[edit : ok i got his in C++ (i use termios.h for communication)

/* sets the baud rate to 0 => (hangup) => DTR=LOW => DTE(computer) not present => arduino reset */
cfsetospeed(&oldtio, B0);
cfsetispeed(&oldtio, B0);
tcsetattr(fd,TCSANOW,&oldtio);
sleep(1);

it is actualy engaging the arduino reset :slight_smile: yeay
]