Very special case with UART and Bootloader

First off all: Hi @ all and let me say that i appreciate this great community:D

I have a very special Problem. I hope someone can help me.

I have an Arduino Mega 2560 and an Raspberry Pi. I can programm the Arduino trough the Raspberry from the Arduino IDE when it is powerd over USB.
I can prgramm the Arduino trough the GPOIs UART from the Raspberry and the UART 0 from the Arduino with an Manual Reset of the Arduino. But i want to programm the Arduino over the USB Port(UART 0) and over UART1.
The Reason is i want to programm the Arduino over the Raspberry pi that is connected trough the GPIOs to the UART 1 and also programm the Arduino trough another PC with the USB port (UART0).

My idea is to change the Bootloader to look at UART0 first and then UART1. Is this possible or has someone else an better idea?

Another idea is to let both PC and raspberry to UART0 but i think this is an problem with the UART Ports or is that possible?

Kind Regards Markus

Sorry for my english :slight_smile:

I seem to remember that it's difficult to compile Arduino's stock bootloader for the Mega 2560 due to changes since the time when it was written many years ago. Majekw has added support for the ATmega2560 to the excellent Optiboot bootloader. That repository is here:

That repository is mostly set up only for the purposes of submitting a pull request for adding the ATmega2560 and some other awesome improvements to Optiboot:

You might find it easier to use this copy of Majekw's version of Optiboot:

since it is in active use. You'll note it contains optiboot compiled for uploads to UART1 as well as UART0. So you can look into the Optiboot source code for where the UART macro is used and adapt that code to your needs.

OK thank you for your reply. I take a quick look into the Optiboot code, seems to be very hard to change it like i want it, that it looks first at UART0 and then at UART1.

Do you think some else that is familiar with the Optiboot Code can do that?

Do you think it is possible to connect both devices at UART0? Or is this a bad idea?

It is pretty fun to play around with bootloaders and setting up to compile them is really not as hard as it might seem at first. I noticed that there are instructions only for macOS and Linux at MCUdude/optiboot_flash but I can say from experience it's easy enough to do on Windows also if that's your OS. I remember westfw recently did some work to make it even more simple on Windows by just running a batch file to automatically set things up.

The inner workings of bootloaders are still somewhat of a mystery to me, despite having spent quite a bit of time mucking around in the source code of a couple of them (not optiboot though).

Luckily we have some forum members a lot more knowledgeable than myself and I'm sure they'll be along shortly.

Majekw has added support for the ATmega2560 to the excellent Optiboot bootloader.

That allows addressing beyond 64 kibi?

Well, this sketch works as expected:

If you want to try it out, the easiest way is via MegaCore:

Well, this sketch works as expected:

According to Chrome the entire INO is only 187 kibi; a bit short of 256.

Sketch uses 176458 bytes (69%) of program storage space. Maximum is 253952 bytes.
Global variables use 184 bytes (2%) of dynamic memory, leaving 8008 bytes for local variables. Maximum is 8192 bytes.

Yup. A bit short of 256. But, more than 128 (64 kibi words).

Yeah, I noticed that too but I guess they just figured it was over 128 kB so good enough to serve as the test? Certainly there was plenty more left to the story but I guess that just ends up making the upload take longer.

so good enough to serve as the test?

At least four boundaries. Smaller than the maximum (covered). One byte smaller than the maximum (should work). Exact size of the maximum (should work). One byte larger than the maximum (should fail gracefully).

Tests also should be included that fall on page boundaries (one page less and one page more).

A test of zero bytes would be good to include.

A test of exactly the full 256 kibi would be a good idea.

A test larger than the full 256 kibi would be good.

So, nope, not good enough.

But complaining is childish. It's open source. If I really want those tests included I can add them myself.

The Reason is i want to programm the Arduino over the Raspberry pi that is connected trough the GPIOs to the UART 1 and also programm the Arduino trough another PC with the USB port (UART0).

I'm a little concerned about electrical (power) problems. I suggest you make two diagrams for the two scenarios. (Actual schematics would be good.)

I made a Schematic what i want.
I want to Program the Arduino over UART1 and over USB(UART0).

The alternative scheme is to put both Raspberry Pi and USB(is anyway UART0) to UART0 (RX0 and TX0) but i think that should be a Problem with the electrical?

Is it safe to connect the USB and 12V to the Mega at the same time? It may be sort of dealt with in the sense of an occasional connection mishap but it is questionable for normal use.

I expect the breadboard has some sort of level shift going on (Fritzing) to convert the 3.3V to 5V. Perhaps a link would help to see what it is.

From experience, I know that when the Raspberry Pi has no power it will pull down on its hardware UART pins. If those pins are connected to the AVR UART0 then the USB will not work when the power is off to the Pi.

Anyway, that seems to be one of the problems you are trying to solve.

After a little thinking, this is my thoughts on how I would try to get both the Pi and a PC to work on UART0.

As long as the PC is not trying to use the serial lines I think the Raspberry can control them and as long as the Raspberry is powered off or has its port closed then I think the PC can use the serial.

Majekw has added support for the ATmega2560 to the excellent Optiboot bootloader.

That allows addressing beyond 64 kibi?

Yes, but it cheats a bit; essentially using an out-of-band command to set the RAMPZ (flash bank) register, so the bootloader (actually the bootloader protocol is the limiting factor) doesn't actually understand addresses beyond 128k, it just puts the bytes in the correct place anyway. This makes me nervous enough that I haven't accepted that change back into the core Optiboot repository. (Although I'm probably being silly; there are programmers that work entirely on side-effects like this...)
It shouldn't be too difficult to add "either/or" serial port capability to Optiboot; by the time you get to the 2560, even the minimum bootloader size is WAY oversized for the code...

Thank you for your reply.
I´ve try your schematic. Your are right this works. (I tested it a little bit different)
But for other reasons it would be nice to have a working bootloader that can write the flash trough UART0 and UART1. Is there anybody who can make a Bootloader that can do this. I think i am not the only one who needs a Bootloader like this.
I´ve tried it on myself but i can´t even compile the existing one. :frowning:

Kind Regards

Did you try to compile it on the Raspberry Pi?

commands look something like:

sudo apt-get install git gcc-avr binutils-avr gdb-avr avr-libc avrdude
git clone
cd optiboot_flash

It builds on one of my Ubuntu 17.10 PC's, and the Raspberry works the same from what I have seen (it is not on the bench at the moment).