Placing memory constants at a fixed location

First of all hi everyone, I have been reading the forum for a long time but never asked anything but I seem to not find the answer right now.

Also this is for Arduino AVR

I have been searching if I can fix a PROGMEM variable in a certain memory locations.

I know I can do

const uint8_t var1 PROGMEM = 80;

const uint8_t var2[] PROGMEM ={1,2,3,4,5,6,7,8,9,10};

As far as my understanding this variables will be located in some part of the flash memory by the compiler, I could find the location at the hex file and searching for the text string and some trial an error but that is not useful for me because every time I compile I will need to change this because the memory location could change. So I have seen in other compilers something like

#pragma location=0x3F00

__no_init const uint8_t ReservedArea[0xFF];

This will store reserved area of the size 256 in the location 0x3F00 and will finish in 0x3F00+0xFF

So in the hex file you can you will see a section file with 0xFF bytes from 0x3F00 to 0x3FFF, and you can use it with pointers with in you program or name several variables and call the pointers.

After this is done you can modify the .hex file and add the data you need

I know I can store then in EEPROM but the EEPROM can be erased more easily I also know that I can add the preserve EEPROM fuse.

Is there a way I can do this I know I think I can just add the variables in the hex file and after that with pointers access them or macros in the avr/pgmspae.h maybe would work but I would like the compiler to know there is some information in that place so it would not add any thing there or alt least return me an error.

Why can't you address the constants by name?

that's an interesting approach, but i believe constants might be located together, next to initialized data and you would just need to figure out the location in the hex file to manually edit them (don't forget about updating the checksum)

data in PROGMEM can't be accessed directly. it needs to be copied from flash using functions similar to pgm_read_byte_near()

What exactly are you trying to accomplish? Do you want to take the compiled hex file and modify it before loading onto the arduino, instead of just recompiling the sketch when the data changes?

I can access by name using the progmem attribute but I can not place them in the desired location. But I can not access by name if I just hardcode them in the hex file

Yes, but every time I compile the hex file would be updated and maybe the constant location will change and require some extra time

So what? What problem are you trying to solve?

Adding an independent serial number on every device I program

What exactly are you trying to accomplish?
It is a like a little serial number, I will have a program in the PC this program will read the hex file and will be able to program this hex file on the Arduino, but I need to have tracking of the number of devices in the wild that is why the serial number, so the PC program shall be able to read the hex file and modify it in the desired location to add the serial number and then flash it this will be done automatically every time I hit the program button it just needs the initial hex file, the address (maybe also length) at which the hex file is going to be modified and the starting serial number. If I input at the start address 0x1F00 and the starting serial number is 1000, the program will generate a new hex file using the input hex file and adding 1000 to the address 0x1F00 (in this case It uses 2 bytes) also calculate the correct checksum of the hex file, after I hit program, and everything is successful it will automatically generate the new hex file with the serial number 1001 and put it in the location 0x1F00 I can just hit program again and this new hex file will be programed in the Arduino with the serial number 1001 and so on. So, in this way if I go to any of this devices, I will be able to read it serial number and see when it was programmed and have a tracking if this has had some issue before. Ideally this same PC software will also print a label with the serial number so the user (who programs the Arduinos) can put the label on it and both serial numbers shall match. To be clear I am not asking about the PC software just I way I can put this variable in a specific location on the Arduino
I hope this describes the process I want to do

Do you want to take the compiled hex file and modify it before loading onto the arduino, instead of just recompiling the sketch when the data changes? Yes

That's what the EEPROM is for! However you still have to explicitly program it differently for each device, which is a pain.

Or you could just add one of these I2C GUID chips to the project perhaps?
Seen it used in the Nanode for a hardware unique MAC address, but you can just treat is a 48bit GUID on a chip, the manufacturer has done the work for you.

As you mention it is a pain to program the EEPROM for each device wich I can also increase in my SW and it is also easier by SW to modify the EEPROM, also if the device is reflashed with a new SW, if it is in EEPROM we do not know for sure if the EEPROM was erased or not that is why I prefer to have it in flash.

This I2C GUID chips I have seen them but it increases cost that the major drawback

Modifying the EEPROM is no more difficult than modifying the FLASH. You can prepare a HEX file for the EEPROM contents, and program it with avrdude, exactly as you can for the FLASH. It is also trivial to provide a Serial interface function to accept EEPROM data, and write the EEPROM. It would be equally trivial to only allow that function to write to a fully erased EEPROM, so that one written the EEPROM contents could not be modified again by the same mechanism. Trying to put FLASH data at a specific address is about the worst approach to take, and unnecessarily complicated.


Modify boards.txt to reduce the available flash memory for the sketch, then place your serial number within the unusable area.

Another idea, put the code for the serial number in its own header and cpp file, the modify the hex file from that and rerun the linker before uploading.

@RayLivingston and @jremington I also used to think that putting this kind of data in EEROM was the best but the automotive industry (at least the company I work for) put this on flash and it is not just serial numbers, Pointer calibration, ADC offsets, external watchdog configuration , etc ...
That is why i think it is better to put it on flash.

Thansk that seems really usefull I will block the last 256 bytes of data and read them with the <avr/pgmspace.h>: Program Space Utilities I actually try it doing it and it worked