Arduino Forum

Using Arduino => Installation & Troubleshooting => Topic started by: Alkerion on Apr 16, 2019, 09:31 am

Title: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 16, 2019, 09:31 am
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



Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: pert on Apr 16, 2019, 09:37 am
What is the frequency of the new crystal?
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 16, 2019, 11:54 am
22.1184Mhz

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

BR
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 16, 2019, 05:22 pm
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


2) Program Uno as ArduinoISP

- Select UNO board
- Select UNO port
- Select programmer "AVRISP mkII"
- Open: File > Examples > ArduinoISP > ArduinoISP
- Upload Sketch

=> OK


3) 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...


4) 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 :-(

I join the files

Thanks for support.

Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: pert on Apr 17, 2019, 04:02 am
=> 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 :-(
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: https://git-scm.com/download/win (https://git-scm.com/download/win)

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:
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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:
https://github.com/Optiboot/optiboot (https://github.com/Optiboot/optiboot)
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:
Code: [Select]
make atmega2560 AVR_FREQ=22114800L
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: pert on Apr 17, 2019, 04:06 am
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.
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 17, 2019, 11:40 am
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 (http://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega2560)


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
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: pert on Apr 17, 2019, 12:06 pm
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:
Code: [Select]
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.

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?

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.
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 17, 2019, 09:44 pm
HI !

Git installed successfully.

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

http://blog.zakkemble.net/avr-gcc-builds/

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.
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 17, 2019, 10:45 pm
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


Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 18, 2019, 12:00 am
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

Code: [Select]

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.


Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Juraj on Apr 18, 2019, 07:13 am
you try to compile the original Atmel bootloader
why not use Optiboot?
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 18, 2019, 09:38 am
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.
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Juraj on Apr 18, 2019, 09:57 am
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
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: pert on Apr 18, 2019, 10:12 am
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.
https://github.com/MCUdude/MegaCore#supported-clock-frequencies (https://github.com/MCUdude/MegaCore#supported-clock-frequencies)

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.
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Juraj on Apr 18, 2019, 11:09 am
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?
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: pert on Apr 18, 2019, 11:21 am
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.
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Juraj on Apr 18, 2019, 12:27 pm
I use my 2560 with customized Optiboot and custom hardware definition (https://github.com/jandrassy/my_boards/blob/master/README.MD), because of different fuse settings and baud rate
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 19, 2019, 09:06 pm
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 (https://youtu.be/fcOJrAZpxJc)

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
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 19, 2019, 09:21 pm
The crystal used :

The 22.11Mhz crystal (https://fr.aliexpress.com/item/30-pi-ces-5032-2-p-22-1184-M-22-1184-MHZ-5-3-2-MM/32887668969.html?spm=a2g0w.search0204.3.7.64461e0cbnyW8J&transAbTest=ae803_5&ws_ab_test=searchweb0_0%2Csearchweb201602_5_10065_10068_319_10892_317_10696_10084_453_454_10083_10618_10304_10307_10820_10821_537_10302_536_10843_10059_10884_10887_321_322_10103%2Csearchweb201603_16%2CppcSwitch_0&algo_pvid=564c1fc9-e2bb-46b5-9acb-8d065d401501&algo_expid=564c1fc9-e2bb-46b5-9acb-8d065d401501-1)

The crystal installed:
(https://i.imgur.com/gEw7mXq.png)
(I know it's not a very professional soldering, but it works, so I won't try to improve)

Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: pert on Apr 19, 2019, 09:59 pm
You're welcome. I'm glad to see it's working now. Enjoy!
Per
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: westfw on Apr 19, 2019, 10:32 pm
Quote
Here is a speed comparison
Code: [Select]
 sx1 = (int)(cos(angle1) * radius1 + centerx1);
  sx2 = (int)(cos(angle2) * radius2 + centerx2);
  sx3 = (int)(cos(angle3) * radius3 + centerx3);
  sx4 = (int)(cos(angle4) * radius4 + centerx4);
  y1  = (int)(sin(angle1) * radius1 + centery1);
  y2  = (int)(sin(angle2) * radius2 + centery2);
  y3  = (int)(sin(angle3) * radius3 + centery3);
  y4 = (int)(sin(angle4) * radius4 + centery4);

Not exactly the sort of code that is likely to run fast on an 8bit CPU...

OTOH, the program used isn't well-optimized for the SAMD51, either (should use sinf() to better utilize the single precision floating point hardware!), and the code contains a LIMIT of the FPS that it will generate, so it's not clear that the M4 isn't even faster than it looks in that video...
Title: Re: Burn new bootloader in Arduino Mega 2560 R3
Post by: Alkerion on Apr 19, 2019, 11:01 pm
HI !

I'll try with sinf().

Regarding the FPS limit I've disabled it of course.