Write permanent data in flash memory at run-time on Arduino Mega 2560.
To write permanent data in flash memory at run-time on Arduino Mega 2560, we need to burn a particular bootloader: Optiboot Flash.
Optiboot is an easy to install upgrade to the Arduino bootloader within Arduino boards. It provides the following features:
- Allows larger sketches. Optiboot is only 512 bytes, freeing 1.5k of extra code space compared to older bootloaders.
- Makes your sketches upload faster. Optiboot operates at higher baud rates and has streamlined programming.
- Adaboot performance improvements. Optiboot implements "fastboot" that starts sketches immediate after power-on.
- Compatible with ATmega8, ATmega168, and ATmega328p Arduinos and derivatives including Lilypad, Pro, Nano, and many derivatives.
- Works with MANY additional Atmel AVR chips - almost anything that supports bootloads or "flash self-programming." This includes chips from ATtiny 8pin chips through the 100pin ATmega2560 used on Arduino Mega.
- Supports alternate serial ports, CPU frequencies and baud rates.
There are some major repositories of "generic" versions of cores for various targets, including:
- MegaCore by MCUdude which supports large AVRs like ATmega128, ATmega640, ATmega1280, ATmega1281, ATmega2560 and ATmega2561.
Optiboot Flash is part of MegaCore and provides key features:
- Small in size (<512B when EEPROM upload support is disabled)
- Supports baudrates upto 1Mbit
- Supports write to flash within application
- Compatible with a most AVR microcontrollers
- EEPROM upload support
We burn the optiboot_flash
bootloader with an easy process using:
- Arduino IDE
- USB AVR programmer which emulates an STK500 on a virtual serial port
- MegaCore, an Arduino core for ATmega64, ATmega128, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, AT90CAN32, AT90CAN64 and AT90CAN128, all running Optiboot flash.
- Pololu USB AVR Programmer v2.1 or equivalent
-
Pololu USB AVR Programmer v2 Software and Drivers for Windows (.msi)
-
Arduino IDE for Windows (release 1.8.13 at this time)
-
MegaCore: an Arduino core for ATmega2560 running Optiboot flash
In ./extras folder you will find software that we used the time I wrote this how-to:
- Pololu USB AVR Programmer Drivers 2.1.1.0 for Windows
- MegaCore source code in
MegaCore-master.zip
(get the latest)
First, install AVR programmer drivers: Pololu USB AVR Programmer v2 Software and Drivers for Windows
- Connect you PC to the AVR programmer with an USB cable and power the Mega 2560 with a 9v adapter:
-
Open Arduino IDE and load
Examples/01.Basics/Blink
sketch -
Upload sketch to Mega via AVR programmer:
-
Check that the Mega on-board LED is blinking: we are ready to continue
We follow instructions given by MCUdude in there.
-
Open the Arduino IDE.
-
Open the File > Preferences menu item.
-
Enter the following URL in Additional Boards Manager URLs:
https://mcudude.github.io/MegaCore/package_MCUdude_MegaCore_index.json
-
Open the Tools > Board > Boards Manager... menu item.
-
Wait for the platform indexes to finish downloading.
-
Scroll down until you see the MegaCore entry and click on it.
-
Click Install.
-
After installation is complete close the Boards Manager window.
We follow instructions given by MCUdude in there.
-
Install the files as directed, usually (for Arduino 1.0+) in a subdirectory of your personal sketch's
../hardware/
directory:-
Download MegaCore source code: MegaCore-master.zip from https://github.com/MCUdude/MegaCore
-
Extract the
optiboot_flash
folder fromMegaCore-master.zip
in:C:\Program Files (x86)\Arduino\hardware\arduino\avr\bootloaders
-
-
Connect a device programmer to the ISP connector of the target board, we've done that when we tested the AVR programmer:
-
Running the Arduino IDE, select the
Tools/Board
of the target chip, and theTools/Programmer
of your programmer (Atmel STK500 development board
), and if necessary theTools/Serial
port of the programmer: -
Proceed : select
Tools/Burn Bootloader
avrdude: Version 6.3-20190619 Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/ Copyright (c) 2007-2014 Joerg Wunsch System wide configuration file is "C:\Users\jm_de\AppData\Local\Arduino15\packages\MegaCore\hardware\avr\2.0.3/avrdude.conf" Using Port : COM5 Using Programmer : stk500 AVR Part : ATmega2560 Chip Erase delay : 9000 us PAGEL : PD7 BS2 : PA0 RESET disposition : dedicated RETRY pulse : SCK serial program mode : yes parallel program mode : yes Timeout : 200 StabDelay : 100 CmdexeDelay : 25 SyncLoops : 32 ByteDelay : 0 PollIndex : 3 PollValue : 0x53 Memory Detail : Block Poll Page Polled Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- eeprom 65 10 8 0 no 4096 8 0 9000 9000 0x00 0x00 flash 65 10 256 0 yes 262144 256 1024 4500 4500 0x00 0x00 lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00 hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00 efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00 lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00 calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00 signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00 Programmer Type : STK500V2 Description : Atmel STK500 Programmer Model: STK500 Hardware Version: 15 Firmware Version Master : 2.10 Topcard : Unknown Vtarget : 5.1 V SCK period : 8.7 us Varef : 0.0 V Oscillator : Off avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.00s avrdude: Device signature = 0x1e9801 (probably m2560) avrdude: erasing chip avrdude: reading input file "0x3f" avrdude: writing lock (1 bytes): Writing | ################################################## | 100% 0.00s avrdude: 1 bytes of lock written avrdude: verifying lock memory against 0x3f: avrdude: load data lock data from input file 0x3f: avrdude: input file 0x3f contains 1 bytes avrdude: reading on-chip lock data: Reading | ################################################## | 100% 0.00s avrdude: verifying ... avrdude: 1 bytes of lock verified avrdude: reading input file "0xfd" avrdude: writing efuse (1 bytes): Writing | ################################################## | 100% 0.00s avrdude: 1 bytes of efuse written avrdude: verifying efuse memory against 0xfd: avrdude: load data efuse data from input file 0xfd: avrdude: input file 0xfd contains 1 bytes avrdude: reading on-chip efuse data: Reading | ################################################## | 100% 0.00s avrdude: verifying ... avrdude: 1 bytes of efuse verified avrdude: reading input file "0xd6" avrdude: writing hfuse (1 bytes): Writing | ################################################## | 100% 0.01s avrdude: 1 bytes of hfuse written avrdude: verifying hfuse memory against 0xd6: avrdude: load data hfuse data from input file 0xd6: avrdude: input file 0xd6 contains 1 bytes avrdude: reading on-chip hfuse data: Reading | ################################################## | 100% 0.00s avrdude: verifying ... avrdude: 1 bytes of hfuse verified avrdude: reading input file "0xf7" avrdude: writing lfuse (1 bytes): Writing | ################################################## | 100% 0.01s avrdude: 1 bytes of lfuse written avrdude: verifying lfuse memory against 0xf7: avrdude: load data lfuse data from input file 0xf7: avrdude: input file 0xf7 contains 1 bytes avrdude: reading on-chip lfuse data: Reading | ################################################## | 100% 0.00s avrdude: verifying ... avrdude: 1 bytes of lfuse verified avrdude done. Thank you.
If it's OK, just after burning the optiboot_flash
bootloader, the on-board LED is blinking with a double fast flash, see the video on YouTube:
We use the SerialReadWrite sketch provided by MegaCore / Optiboot_flasher library which is installed, so:
-
Open Arduino IDE and load
Examples
/Examples for ATmega2560
/Optiboot flash read/write
/SerialReadWrite
sketch -
Open
Tools/Serial Monitor
and upload the sketch -
Play with this sketch to write data in flash, power off/on the board and read back from flash to verifiy it's working.
- ./extras
- MCUdude / optiboot_flash
- MCUdude / MegaCore
- optiboot wiki
- Pololu USB AVR Programmer v2.1
- Pololu software
- ELEGOO MEGA 2560 R3 Board Blue
Jean-Michel (Jim) FAURE (September 9th, 2020)
- company: FAURE SYSTEMS®
- mail: dev at faure dot systems
- github: fauresystems
- web: Faure●Systems