Change ATMEGA2560 Bootloader to run at 8Mhz ?

I have been doing a fair bit of work recently with LoRa long range communication for lost model locators and High Altitude Balloon trackers in particular.

I want to provide a fairly capable and easy build portable receiver that can run from a single Lithium Ion battery. The receiver battery to be charged via the USB supply and accessed on a PC via a the same USB connection. I understand how to make this part work.

An ATMEGA2560 base running at 3.3v, seems ideal and I have one running OK with one of my LoRa shield PCBs and a 3.3V\5V switchable ATMEGA2560 base set to 3.3V.

Whilst my ATMEGA2560 bases do run at 16Mhz on 3.3V, this is out of spec.

Can the ATMEGA2560 Bootloader code (and fuse bits) be modified so that when the CPU starts it runs initially at an in spec speed then immediatly switches over to an external clock and divison so that it then runs at 8Mhz ?

Some background here;

http://www.loratracker.uk/

srnet:
Can the ATMEGA2560 Bootloader code (and fuse bits) be modified so that when the CPU starts it runs initially at an in spec speed then immediatly switches over to an external clock and divison so that it then runs at 8Mhz ?

I don't understand what you mean by starting at an in spec speed. 8MHz is an in spec speed so why would you need to change?

You should be able to use an 8MHz external clock with no modifications to the 16MHz fuse settings or bootloader. You only need to change 2 lines in boards.txt. Using the Mega 2560 entry for an example:
Change:

mega.build.f_cpu=16000000L

to:

mega.build.f_cpu=8000000L

Change:

mega.menu.cpu.atmega2560.upload.speed=115200

to:

mega.menu.cpu.atmega2560.upload.speed=57600

I think his issue that his board has a 16mhz crystal on it?

Set clock div 8 fuse... create new boards.txt entry that specifies an upload speed 1/8th normal and 8mhz clock, and then in setup(), change the clock divider.

If your mega2560 board uses a crystal you can replace (instead of the rice-grain sized resonator that most boards now use :frowning: ) you can just pop that off and put on an 8mhz one - in which case you'd want to set 8mhz f_cpu in board def, and half the normal upload speed.

pert:
I don't understand what you mean by starting at an in spec speed. 8MHz is an in spec speed so why would you need to change?

I should have mentioned it, the ATMEGA2560 boards come with 16Mhz crystals and its not that practical to change them.

The idea was to change the fuse bits so the CPU will startup at less than 16Mhz, then in the first few instructions of the bootloader, set the CKSEL3 and CLKPR registers to give a 8Mhz CPU clock from the external 16Mhz crystal.

Section 10.13.2 of the data sheet sugegsts this is possible for circumstances where "the selected clock source has a higher frequency than the maximum frequency of the device at the present operating conditions"

But if this is possible in the Arduino environment, I do not know, or where to find the editable source of the bootloader.

Nick Gammon has programs for uploading the bootloader. Use the LilyPad bootloader to get an Atmega 328 to run at 8MHz using its internal clock.

EDIT ... apologies I missed the fact that you are using an Atmega2560 :frowning:

...R

srnet:
I should have mentioned it, the ATMEGA2560 boards come with 16Mhz crystals and its not that practical to change them.

That makes perfect sense. I actually realized that was probably the situation after I posted.

So it sounds like you still don't need to need to modify the bootloader. Using Arduino AVR Boards Mega 2560 boards.txt entry for an example again.

change:

mega.menu.cpu.atmega2560.upload.speed=115200

to:

mega.menu.cpu.atmega2560.upload.speed=14400

change:

mega.bootloader.low_fuses=0xFF

to:

mega.bootloader.low_fuses=0x7F

That will start at 2MHz by setting CKDIV8.

Change:

mega.build.f_cpu=16000000L

to:

mega.build.f_cpu=8000000L

You will need to do Tools > Burn Bootloader with the new settings to set the new low_fuses value.

Then you just need to set the prescaler in setup(). I'm guessing you want to set it to 0x1 to divide the 16MHz clock to 8MHz. Maybe someone can help with the specifics on that code.

Depending on the frequency and size of your program uploads, the excessively slow upload speed of that technique might make it worthwhile to rebuild the bootloader for 2MHz clock instead of using the upload speed division hack. The bootloader source for the Mega 2560 is at {Arduino IDE installation folder}/hardware/arduino/avr/bootloaders/stk500v2. You shouldn't need to make any changes to the source, only build it with F_CPU set to 2000000. After that you can upload at 115200.

pert:
Depending on the frequency and size of your program uploads, the excessively slow upload speed of that technique might make it worthwhile to rebuild the bootloader for 2MHz clock instead of using the upload speed division hack. The bootloader source for the Mega 2560 is at {Arduino IDE installation folder}/hardware/arduino/avr/bootloaders/stk500v2. You shouldn't need to make any changes to the source, only build it with F_CPU set to 2000000. After that you can upload at 115200.

That is what I was thinking, rebuild the bootloader.

Then it could be made available as a HEX file.

Accepted that in most cases the ATMEGA2560 will run at 16Mhz and 3.3V, but I would not want to design a PCB (to plug into a standard base) that relied on that.