Burn new bootloader in Arduino Mega 2560 R3

Hi !

For one of my project I need a little speed boost, so I've decided to tweek my Mega by changing the clock crystal.

That was the easy part...

Now I've to upload a modified bootloader with the new clock speed.

I made a copy of the "hardware\arduino\avr\bootloaders\stk500v2" folder and modified the Makefile accordingly.

First question: Did I choose the right BL for my board ?

If yes, how am I supposed to compile it to obtain an .hex file ?

After it seems that I've to link a UNO to my Mega and use Arduino IDE to burn the new BL.

Regards

What is the frequency of the new crystal?

22.1184Mhz

Now that I see my "custom board" on my IDE, how do I compile my new BL ?

BR

OK,

Here is what I've done until now:

  1. Wired the UNO and the MEGA

UNO: MEGA 2560:
10 => RESET
11 => 51
12 => 50
13 => 52
5V => 5V
GND => GND

  1. Program Uno as ArduinoISP
  • Select UNO board
  • Select UNO port
  • Select programmer "AVRISP mkII"
  • Open: File > Examples > ArduinoISP > ArduinoISP
  • Upload Sketch

=> OK

  1. Programing the MEGA with ORIGINAL BL
  • Select Original MEGA 2560 board
  • Select programmer "Arduino as ISP"
  • Tools > Burn Bootloader

=> OK, still recognized, can still upload sketch, etc...

  1. Programing the MEGA with MOFIFIED BL
  • Select custom MEGA 2560 board
  • Select programmer "Arduino as ISP"
  • Tools > Burn Bootloader

=> ERROR:

avrdude: can't open input file C:\Users...\Arduino\hardware\my_boards\avr/bootloaders/stk500v2/stk500boot_v2_mega2560_22Mhz.hex: No such file or directory

Something going wrong as there is no .hex file :frowning:

I join the files

Thanks for support.

my_boards.zip (76 KB)

Alkerion:
=> ERROR:

avrdude: can't open input file C:\Users...\Arduino\hardware\my_boards\avr/bootloaders/stk500v2/stk500boot_v2_mega2560_22Mhz.hex: No such file or directory

Something going wrong as there is no .hex file :frowning:

That's because you must create the .hex file by compiling the bootloader.

So how do you do that?:

Since you're using Windows, the process is a bit more difficult, since some of the required tools are not installed with a stock Windows installation, as they are with Linux or macOS. The easiest way to get them is via Git Bash, which you get by installing Git for Windows: Git - Downloading Package

Open the folder my_boards\avr\bootloaders\stk500v2 in Windows Explorer

Right click > Git Bash Here

In order to compile the bootloader, you need to use the avr-gcc compiler. Luckily, that comes with the Arduino IDE so you only need to add its folder to the system path. You do that by running a command from the Git Bash prompt that looks something like this:

PATH="${PATH}:/c/Program Files (x86)/Arduino/hardware/tools/avr/bin"

The above command is for an installation of the Arduino IDE at C:\Program Files (x86)\Arduino. If your Arduino IDE is installed somewhere else, you'll need to adjust it accordingly. Note that paths work a little differently in Git Bash. The drive letter is specified like /c/ instead of C:\ and you can't use \ as a path separator.

Now run this command from the Git Bash prompt to remove the previous compilation files from the folder:

make clean

Now it's time to compile the bootloader. I see you have modified the mega2560 in the makefile. So you only need to run this command from the Git Bash prompt:

make mega2560

Now wait for the compilation to finish successfully. The compiled bootloader will be my_boards\avr\bootloaders\stk500v2\stk500boot_v2_mega2560.hex. You can rename the file as you like.

If you're feeling very cocky with your bootloader compiling skills now and want to go for extra credit by upgrading to the superior Optiboot bootloader (which only requires a 1 kB boot section instead of the 8 kB boot section of the STK500V2 bootloader, freeing up 7 kB of program memory), you can find the source code here:

The same process as above applies to compiling optiboot. The only difference is that the target is named atmega2560 instead of mega2560 and you can specify the CPU frequency from the command line, rather than modifying the makefile:

make atmega2560 AVR_FREQ=22114800L

I should add that a few changes to boards.txt will also be necessary if you want to use the optiboot bootloader. If you end up needing help with that, let me know.

Hi Pert,

Thanks a lot for your detailed reply, I really appreciate.

I'll read it carefully and try to understand all the new things I've to learn.

I've also read that overclocking an Arduino impact commands like millis() & micros(), and that you also have to take care of the fuses values.

I found that link:FuseCalc

It also seems that it's having an impact on the serial speed, the 16U2 that is used on all my boards seems to be a quite poor quality chip.

Some use different types of oscillator, and even add capacitors, etc...

All that is new to me, I need some times to digest and test all that.

As an alternative I'm also considering to select a more powerful board for my project.

Regards

I don't have any experience with overclocking, but the default fuse settings of the Mega 2560 look OK. If you're having troubles you could try changing to the full swing oscillator option.

As for timing accuracy, that is certainly an issue with that clock speed.

I'm not sure about the serial speed issue. There is a different baud error at each baud rate and clock frequency. The Optiboot makefile actually runs a check of the upload baud rate versus CPU frequency and tells you what the error is. It says this:

BAUD RATE CHECK: Desired: 115200, Real: 115181, UBRRL = 23, Difference=0.0%

So it looks very good for an upload speed of 115200 with your clock speed.

Alkerion:
the 16U2 that is used on all my boards seems to be a quite poor quality chip.

What's the problem you're having with the ATmega16U2?

Alkerion:
As an alternative I'm also considering to select a more powerful board for my project.

There certainly are some modern microcontrollers available that have pretty impressive performance! The good old AVR boards are plenty good enough for a lot of projects, but there certainly are applications where another board is better suited.

HI !

Git installed successfully.

Sadly there is no make file in my AVR bin dir, I've installed AVRDude from here:

Make clean -> OK

Make mega2560:-> Error

-------- begin --------
avr-gcc (GCC) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Compiling: stk500boot.c
avr-gcc -c -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=16000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -fno-jump-tables -Wall -Wstrict-prototypes -Wa,-adhlns=stk500boot.lst -std=gnu99 -D_MEGA_BOARD_ -MD -MP -MF .dep/stk500boot.o.d stk500boot.c -o stk500boot.o
avr-gcc: error: device-specs/specs-atmega2560: No such file or directory
make: *** [Makefile:539: stk500boot.o] Error 1

Not found a solution until now to get it to work.

OK,

I've created a folder "device-specs" on my bootloader folder and copied the 2560 spec found in this folder:
C:\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\5.4.0\device-specs

Now I've this error:

Compiling: stk500boot.c
avr-gcc -c -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=22114800UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -fno-jump-tables -Wall -Wstrict-prototypes -Wa,-adhlns=stk500boot.lst -std=gnu99 -D_MEGA_BOARD_ -MD -MP -MF .dep/stk500boot.o.d stk500boot.c -o stk500boot.o
avr-gcc: error: CreateProcess: No such file or directory
make: *** [Makefile:539: stk500boot.o] Error 1

That's totally crazy how complicated it is to just build a bootloader....

I'm moving forward.

  1. Rename folder
    C:\Users(username)\AppData\Roaming\Arduino15

  2. Installed again Arduino IDE 1.8.9

  3. Installed AVR in another dir
    C:\Program Files\avr-gcc-8.3.0-x64-mingw

  4. updated the path accordingly

  5. It's now compiling the BL

Compiling: stk500boot.c
avr-gcc -c -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=22114800UL  -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -fno-jump-tables -Wall -Wstrict-prototypes -Wa,-adhlns=stk500boot.lst  -std=gnu99 -D_MEGA_BOARD_ -MD -MP -MF .dep/stk500boot.o.d stk500boot.c -o stk500boot.o

Linking: stk500boot.elf
avr-gcc -mmcu=atmega2560 -I. -gdwarf-2 -DF_CPU=22114800UL  -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -fno-jump-tables -Wall -Wstrict-prototypes -Wa,-adhlns=stk500boot.o  -std=gnu99 -D_MEGA_BOARD_ -MD -MP -MF .dep/stk500boot.elf.d stk500boot.o --output stk500boot.elf -Wl,-Map=stk500boot.map,--cref    -lm -Wl,--section-start=.text=3E000

Creating load file for Flash: stk500boot.hex
avr-objcopy -O ihex -R .eeprom stk500boot.elf stk500boot.hex

Creating load file for EEPROM: stk500boot.eep
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O ihex stk500boot.elf stk500boot.eep
C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-objcopy.exe: --change-section-lma .eeprom=0x00000000 never used

Creating Extended Listing: stk500boot.lss
avr-objdump -h -S stk500boot.elf > stk500boot.lss

Creating Symbol Table: stk500boot.sym
avr-nm -n stk500boot.elf > stk500boot.sym

Size after:
AVR Memory Usage
----------------
Device: atmega2560

Program:    5926 bytes (2.3% Full)
(.text + .data + .bootloader)

Data:         18 bytes (0.2% Full)
(.data + .bss + .noinit)

The only issue I see now, is that the .hex file produced is 17Kb and the original is 21Kb.

No idea why it is smaller.

stk500boot_v2_mega2560_22Mhz.zip (6.44 KB)

you try to compile the original Atmel bootloader
why not use Optiboot?

Hi,

I've read that the 2560 is using a stk500v2 compatible bootloader instead of Optiboot because optiboot only supports upto 64kb addresses.

And I'm also new to BL, I've still a lot to learn and understand.

Alkerion:
Hi,

I've read that the 2560 is using a stk500v2 compatible bootloader instead of Optiboot because optiboot only supports upto 64kb addresses.

And I'm also new to BL, I've still a lot to learn and understand.

Optiboot 8 works with 2560. I tested a sketch over 128 kB large.
install MegaCore boards package. it has many versions of Optiboot already build.

only I am not sure about this, because I don't know why Per didn't recommend this. And I know he knows MegaCore posibilities

Juraj:
only I am not sure about this, because I don't know why Per didn't recommend this. And I know he knows MegaCore posibilities

Because Alkerion is using a 22.1148 MHz clock and MegaCore doesn't have compiled bootloaders for that clock speed.

Alkerion:
The only issue I see now, is that the .hex file produced is 17Kb and the original is 21Kb.

No idea why it is smaller.

The original bootloader was compiled a long time ago with a different compiler version. Note that the .hex file size is not the size it will occupy on the microcontroller. For that, use the avr-size tool, which is part of avr-gcc.

pert:
Because Alkerion is using a 22.1148 MHz clock and MegaCore doesn't have compiled bootloaders for that clock speed.
[

yes but it is simple to compile it for a new frequency. or not?

Sure it's simple to compile for a new frequency. I already provided instructions for doing that. However, MegaCore doesn't really have any relevance for that project. MegaCore does come with the Optiboot source, so you could get it that way, but MegaCore is a much larger download than just grabbing the contents of the Optiboot repository.

I use my 2560 with customized Optiboot and custom hardware definition, because of different fuse settings and baud rate

OK, it works ! ;D

Bootloader uploaded without any issue, and uploaded code without problem.

Will have to do further tests to determine if there is an impact on micros() and millis() or not.

Here is a speed comparison between :

Left: Adafruit Grand Central M4 120Mhz
Center: Arduino Mega 2560 R3 22.11Mhz
Right: Arduino Mega 2560 R3 stock 16Mhz

The video

That is ridiculous !

BIG Thanks to all of you who helped me to successfully overclock my Mega

I've attached the files for those of you who want to OC their Mega to 22.11Mhz

my_boards.zip (219 KB)

The crystal used :

The 22.11Mhz crystal

The crystal installed:


(I know it's not a very professional soldering, but it works, so I won't try to improve)