PROGMEM: real FLASH memory address access

I have been using PROGMEM basically to store/read constant values at FLASH instead of RAM.

Now, I'd like to directly access the flash addresses to, at least, read or "dump" the flash memory content.

Using 4 unchar variables called N01_S1, N01_S2, N01_S3, and N01_S4, I defined different values for each one (0x51, 0x52, 0x53, 0x54).

Using a loop from 0 to 255, I printed the content of the Flash for the first 256 positions by using pgm_read_byte_near(i), where i varies from 0 to 255. I figured out that the positions 228-231 have the values 0x51 to 0x54.

Is this kind of address allocation random or it is precisely defined at Arduino environment? If positive, where?

Thanks

Show your code?

The flash memory contents will be whatever is in the .hex file uploaded to the processor, if you want to see what it is.

. I figured out that the positions 228-231

How did you figure that?

I am using a different computer to send this message. I'll try to upload the original code later.

But.. I just figured out (experimentally) that PROGMEM only works at compilation time. When we try to use the statement below anywhere in setup() or loop()

PROGMEM prog_uchar N01_S5[] = { 0x55 };

it does not work and you cannot access the content of N01_S5.

Therefore, PROGMEM seems to me designed to deal with constant values only. I expected to extend its use, but I believe it is not possible.

Any comments?

Therefore, PROGMEM seems to me designed to deal with constant values only.

The only place you can write to PROGMEM is from code running in the boot area, which is why the bootloader works so well.

I started 2 related topics today, but after feedbacks and some research, I realized that for one of my projects, the internal flash memory of ATmega1280 (128KB) is enough for me and the number of writes cycles will not be an issue for at least 5 years.

Some details:

  • No Arduino bootloader. Actually, I never used it on my projects. I develop using Arduino IDE but only upload the object code without the bootloader. Therefore, I have the full 128KB available for my design (ATmega1280 case).

  • No external flash device for this project.

  • EEPROM is not an option: I am already fully using it for other purpose.

In short, I want to use the internal flash memory of Atmega without using PROGMEM. I do not care to access byte-by-byte instead of page mode, but I need to read and write in running time.

Thank you for any, any idea or suggestion :slight_smile:

Program memory is intended to hold programs, hence its name. It doesn't have an indefinite life for writing.

Why do you need to write to it? Do you not have enough RAM? Some processors (like the 1284) have a lot more RAM (16 Kb). Or you can attach an SD card and write to that.

Only the Boot Loader Section is allowed to write into the Application Program Section of FLASH at run time. You will have to put some code in the BLS that you can then call to write data into the APS with the SPM (Store Program Memory) instruction.

Thanks johnwasser, it seems to be the direction.

However, I never coded in such level...
Guys, any idea of how to get the steps/examples in this area?

Or an SPI-accessed Ramtron FRAM, access it at serial SRAM speeds, holds data like an EEPROM, no battery backup needed.
Available at mouser.

ionito:
Therefore, PROGMEM seems to me designed to deal with constant values only. I expected to extend its use, but I believe it is not possible.

Any comments?

Not "seems to be", but "is".

PROGMEM stands for Program Memory. It cannot be modified during run-time, unless the code is run out of the boot part of PROGMEM. This is why the bootloader can write to it, and you cannot.

ionito:
any idea of how to get the steps/examples in this area?

Well, the bootloader writes into Application Program Section. The source code for the bootloaders are included in Arduino 1.0.

The problem is that I'm removing the bootloader when I make the final deployment of the solution. I cannot include the bootloader due to other issues.

Any other form of coding without using the bootloader?

You wanted an example of how code in the BLS could write into the APS. The bootloaders do that. You can use the source code as an example.

Also you would have to write whole pages at once, as its Flash, not EEPROM, which is inconvient for general storage.

ionito:
The problem is that I'm removing the bootloader when I make the final deployment of the solution.

John's suggestion isn't to use the Arduino bootloader. Instead it is to look at how it writes to PROGMEM. You'll need to write your own bootloader in this case.

ionito:
Any other form of coding without using the bootloader?

To write to PROGMEM? No. Only the bootloader sector can write to PROGMEM.

johnwasser:
Only the Boot Loader Section is allowed to write into the Application Program Section of FLASH at run time.

Example code in boot.h (the low-level stuff) and see how it is called in optiboot.c.

I've merged the topics, it seems we were discussing almost exactly the same thing in two threads.