I want to read the application area of flash memory


Forgive my poor English skills.

I want to read the application area of flash memory

I know I can read flash memory via pgm_read_byte()

What I want to know is the starting and ending addresses

In more detail,

Write programs to work through the Arduino sketch

And uploads to Arduino mega 2560 via Arduino sketch

At this time, I know that the uploaded program is stored in the application area of the flash memory

So I want to read only the part of the program that I have written in flash memory

Where should I read flash memory from?

In addition, I want to know how to read the bootloader area of flash memory

Please help me

The sketch starts at 0. The bootloader is put at the end of the flash memory.

There are much easier ways of programming the Mega2560 than the one you have chosen.

Somehow, you would have to have the Mega2560 program stored in "application memory" for your idea to work. Put it somewhere else, like a file on a uSD card, FRAM memory, etc.

"So I want to read only the part of the program that I have written in flash memory

In addition, I want to know how to read the bootloader area of flash memory"

You know what is written there based on files you uploaded.

If you look at the .hex files you can see the 'raw data' which will be the same as what you read back.

For example, here are the first couple and last couple of lines of
stk500boot_v2_mega2560.hex, which I believe is the bootloader for the Mega2560:


The first 10 says there are 16 bytes on the line,
the next 4 characters are the starting address of the line (E000, E010, .. FCF0, FD00)
the next 16 bytes are the data itself,
and the last 4 are a checksum used to make the data didn't get corrupted before it was transferred into the part.
If you read back the flash, you would get

How is that helpful to you? I don't know.

Same for a sketch.
If you use Sketch:Export compiled library, and then look at the hex file, you will see this:


for this sketch compiled for an Uno:

  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO 
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino model, check
  the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products
  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second

Is that helpful? I don't really think so.

Thank you for the reply

I have more questions to answer and I will write again

You can see the address value and the data value in the .hex file.

Could you just read the data in .hex directly from the Arduino program?

That is, ask if you can read the .hex file through the source code

(Is it possible to use only the Arduino Mega 2560 without using additional modules like SD card?)

Why? What use could this be?

Yes a program can read its own code but you should never modify code while it's running. The architecture on the chip explicitly prevents that.

And if you're reading the code of the program you're running, why would you do that? If there's some constant data that you need, like a string or the value of pi, then just refer to those constants in the code. The compiler does all that memory mapping work for you.

I also would like to read and display what is in flash memory.
The reason is that I'm trying to write a new bootloader into my ATmega328P and I continue to get a verification error when I insert a modified .hex file into the "images.cpp". (I'm using the Standalone AVR ISP Shield from Adafruit. I'm using an existing UNO (call it chip#1) ). I've assembled the shield, downloaded the sketch and run it on a spare UNO in a ZIF socket - call it chip#2 and it writes the bootloader just perfectly, sets fuses and verifies. When I cut and paste my hex file into the images.cpp file and try to burn (a somewhat larger) hex file, i get the verification error same place every time.

So I'd like to see what is getting put onto the chip.

What is an "images.cpp" file?
Why are you hijacking a 1.5 year old thread?