I want to read the application area of flash memory

Hello

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:

:10E000000D9489F10D94B2F10D94B2F10D94B2F129
:10E010000D94B2F10D94B2F10D94B2F10D94B2F1F0
:
:
:10FCF000FECF92BD81BDF89A992780B50895262F31
:10FD0000F999FECF1FBA92BD81BD20BD0FB6F89400

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
000D9489F10D94B2F10D94B2F10D94B2
000D94B2F10D94B2F10D94B2F10D94B2
:
:
00FECF92BD81BDF89A992780B5089526
00F999FECF1FBA92BD81BD20BD0FB6F8

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:

:100000000C945C000C946E000C946E000C946E00CA
:100010000C946E000C946E000C946E000C946E00A8
:100020000C946E000C946E000C946E000C946E0098
:100030000C946E000C946E000C946E000C946E0088
:100040000C9415010C946E000C946E000C946E00D0
:100050000C946E000C946E000C946E000C946E0068
:100060000C946E000C946E00000000002400270029
:100070002A0000000000250028002B0004040404CE
:100080000404040402020202020203030303030342
:10009000010204081020408001020408102001021F
:1000A00004081020000000080002010000030407FB
:1000B000000000000000000011241FBECFEFD8E0B8
:1000C000DEBFCDBF21E0A0E0B1E001C01D92A930AC
:1000D000B207E1F70E945F010C94CE010C9400007E
:1000E000E1EBF0E02491EDE9F0E09491E9E8F0E053
:1000F000E491EE2309F43BC0222339F1233091F03F
:1001000038F42130A9F0223001F524B52F7D12C03A
:10011000273091F02830A1F02430B9F420918000EC
:100120002F7D03C0209180002F77209380000DC089
:1001300024B52F7724BD09C02091B0002F7703C0CC
:100140002091B0002F7D2093B000F0E0EE0FFF1F54
:10015000EE58FF4FA591B4912FB7F894EC9181110F
:1001600003C090959E2301C09E2B9C932FBF0895A2
:100170003FB7F8948091050190910601A091070185
:10018000B091080126B5A89B05C02F3F19F0019634
:10019000A11DB11D3FBFBA2FA92F982F8827820F0D
:1001A000911DA11DB11DBC01CD0142E0660F771F5D
:1001B000881F991F4A95D1F708958F929F92AF9209
:1001C000BF92CF92DF92EF92FF920E94B8004B0154
:1001D0005C0188EEC82E83E0D82EE12CF12C0E9421
:1001E000B800DC01CB0188199909AA09BB09883E2E
:1001F0009340A105B10558F021E0C21AD108E108E9
:10020000F10888EE880E83E0981EA11CB11CC11471
:10021000D104E104F10419F7FF90EF90DF90CF9043
:10022000BF90AF909F908F9008951F920F920FB63E
:100230000F9211242F933F938F939F93AF93BF936C
:100240008091010190910201A0910301B0910401FC
:100250003091000123E0230F2D3720F40196A11DDA
:10026000B11D05C026E8230F0296A11DB11D2093E4
:1002700000018093010190930201A0930301B093C8
:1002800004018091050190910601A0910701B091B0
:1002900008010196A11DB11D8093050190930601EF
:1002A000A0930701B0930801BF91AF919F918F91E7
:1002B0003F912F910F900FBE0F901F90189578943B
:1002C00084B5826084BD84B5816084BD85B582605B
:1002D00085BD85B5816085BD80916E00816080930C
:1002E0006E00109281008091810082608093810075
:1002F0008091810081608093810080918000816085
:10030000809380008091B10084608093B1008091DF
:10031000B00081608093B00080917A008460809307
:100320007A0080917A00826080937A0080917A00CE
:10033000816080937A0080917A00806880937A004F
:100340001092C100EDE9F0E02491E9E8F0E0849139
:10035000882399F090E0880F991FFC01E859FF4F1E
:10036000A591B491FC01EE58FF4F859194918FB700
:10037000F894EC91E22BEC938FBFC0E0D0E081E0E9
:100380000E9470000E94DD0080E00E9470000E94C8
:10039000DD002097A1F30E940000F1CFF894FFCF79
:00000001FF

for this sketch compiled for an Uno:
Blink.ino.standard.hex

/*
  Blink
  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.
  pinMode(LED_BUILTIN, 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.

Hi,
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?